Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Added connectionTime to MqttConnection

Rich Turnerdfcd040763c9
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
index 0bc4ec405..f74bbe65a 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -33,9 +33,9 @@ import io.netty.handler.codec.mqtt.MqttQoS;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.persistence.PersistenceEvent;
+import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
-import org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.model.Container;
@@ -77,6 +77,7 @@ public class MqttBrokerService extends RouteBuilder implements ContainerService,
protected ClientEventService clientEventService;
protected MessageBrokerService messageBrokerService;
protected ScheduledExecutorService executorService;
+ protected TimerService timerService;
protected final Map<String, MqttConnection> clientIdConnectionMap = new HashMap<>();
protected List<MQTTHandler> customHandlers = new ArrayList<>();
@@ -100,6 +101,7 @@ public class MqttBrokerService extends RouteBuilder implements ContainerService,
ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
executorService = container.getExecutorService();
+ timerService = container.getService(TimerService.class);
if (!identityService.isKeycloakEnabled()) {
LOG.warning("MQTT connections are not supported when not using Keycloak identity provider");
@@ -118,7 +120,7 @@ public class MqttBrokerService extends RouteBuilder implements ContainerService,
properties.setProperty(BrokerConstants.HOST_PROPERTY_NAME, host);
properties.setProperty(BrokerConstants.PORT_PROPERTY_NAME, String.valueOf(port));
properties.setProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, String.valueOf(true));
- List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService));
+ List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService, timerService));
// Load custom handlers
this.customHandlers = stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
index fd6191ebb..e2eebe066 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
@@ -31,11 +31,13 @@ public class MqttConnection {
protected final boolean cleanSession;
protected Supplier<String> tokenSupplier;
protected ManagerKeycloakIdentityProvider identityProvider;
+ protected long connectionTime;
- public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password, boolean cleanSession) {
+ public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password, boolean cleanSession, long connectionTime) {
this.cleanSession = cleanSession;
this.clientId = clientId;
this.identityProvider = identityProvider;
+ this.connectionTime = connectionTime;
setCredentials(realm, username, password);
}
@@ -63,6 +65,11 @@ public class MqttConnection {
return tokenSupplier.get();
}
+ public String getUserId() {
+ AuthContext authContext = getAuthContext();
+ return authContext != null ? authContext.getUserId() : null;
+ }
+
public AuthContext getAuthContext() {
AuthContext authContext;
@@ -114,7 +121,8 @@ public class MqttConnection {
@Override
public String toString() {
return this.getClass().getSimpleName() + "{" +
- "realm='" + realm + '\'' +
+ "connectionTime=" + connectionTime +
+ ", realm='" + realm + '\'' +
", username='" + username + '\'' +
", clientId='" + clientId + '\'' +
'}';
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
index 4da32052b..6eaff6dbf 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
@@ -24,6 +24,7 @@ import io.moquette.broker.subscriptions.Topic;
import io.moquette.interception.AbstractInterceptHandler;
import io.moquette.interception.messages.*;
import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.timer.TimerService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.model.syslog.SyslogCategory;
@@ -37,13 +38,16 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
protected final MqttBrokerService brokerService;
protected final MessageBrokerService messageBrokerService;
protected final ManagerKeycloakIdentityProvider identityProvider;
+ protected final TimerService timerService;
public ORInterceptHandler(MqttBrokerService brokerService,
ManagerKeycloakIdentityProvider identityProvider,
- MessageBrokerService messageBrokerService) {
+ MessageBrokerService messageBrokerService,
+ TimerService timerService) {
this.brokerService = brokerService;
this.identityProvider = identityProvider;
this.messageBrokerService = messageBrokerService;
+ this.timerService = timerService;
}
@Override
@@ -75,7 +79,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
username = realmAndUsername[1];
}
- MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password, msg.isCleanSession());
+ MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password, msg.isCleanSession(), timerService.getCurrentTimeMillis());
brokerService.addConnection(connection.getClientId(), connection);
// Notify all custom handlers