100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Add writeattribute MQTT topic to allow specifying value and timestamp (#1735)
Rich Turner27d920ba121c
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.javaindex 38796f16c..54b91372d 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java@@ -19,6 +19,7 @@*/package org.openremote.manager.mqtt;+import com.fasterxml.jackson.databind.node.ObjectNode;import com.google.common.cache.Cache;import com.google.common.cache.CacheBuilder;import io.netty.buffer.ByteBuf;@@ -64,6 +65,7 @@ public class DefaultMQTTHandler extends MQTTHandler {public static final String ATTRIBUTE_TOPIC = "attribute";public static final String ATTRIBUTE_VALUE_TOPIC = "attributevalue";public static final String ATTRIBUTE_VALUE_WRITE_TOPIC = "writeattributevalue";+ public static final String ATTRIBUTE_WRITE_TOPIC = "writeattribute";private static final Logger LOG = SyslogCategory.getLogger(API, DefaultMQTTHandler.class);final protected Map<String, Map<String, Consumer<? extends Event>>> sessionSubscriptionConsumers = new HashMap<>();// An authorisation cache for publishing@@ -114,7 +116,7 @@ public class DefaultMQTTHandler extends MQTTHandler {@Overridepublic boolean topicMatches(Topic topic) {- return isAttributeTopic(topic) || isAssetTopic(topic) || isAttributeValueWriteTopic(topic);+ return isAttributeTopic(topic) || isAssetTopic(topic) || isAttributeValueWriteTopic(topic) || isAttributeWriteTopic(topic);}@Override@@ -231,13 +233,13 @@ public class DefaultMQTTHandler extends MQTTHandler {AuthContext authContext = getAuthContextFromSecurityContext(securityContext);if (authContext == null) {- LOG.finer("Anonymous publish not supported: topic=" + topic + ", connection=" + mqttBrokerService.connectionToString(connection));+ LOG.finer("Anonymous publish not supported: topic=" + topic + ", connection=" + connectionToString(connection));return false;}- if (isAttributeValueWriteTopic(topic)) {+ if (isAttributeValueWriteTopic(topic) || isAttributeWriteTopic(topic)) {if (topic.getTokens().size() != 5 || !Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {- LOG.finer("Publish attribute value topic should be {realm}/{clientId}/writeattributevalue/{attributeName}/{assetId}: topic=" + topic + ", connection=" + mqttBrokerService.connectionToString(connection));+ LOG.finer("Invalid publish topic: topic=" + topic + ", connection=" + connectionToString(connection));return false;}} else {@@ -254,7 +256,7 @@ public class DefaultMQTTHandler extends MQTTHandler {// We don't know the value at this point so just use a null value for authorization (value type will be handled// when the event is processed)- if (!clientEventService.authorizeEventWrite(topicRealm(topic), authContext, buildAttributeEvent(topic.getTokens(), null))) {+ if (!clientEventService.authorizeEventWrite(topicRealm(topic), authContext, buildAttributeEvent(topic.getTokens(), null, null))) {LOG.fine("Publish was not authorised for this user and topic: topic=" + topic + ", subject=" + authContext);return false;}@@ -329,7 +331,8 @@ public class DefaultMQTTHandler extends MQTTHandler {@Overridepublic Set<String> getPublishListenerTopics() {return Set.of(- TOKEN_SINGLE_LEVEL_WILDCARD + "/" + TOKEN_SINGLE_LEVEL_WILDCARD + "/" + ATTRIBUTE_VALUE_WRITE_TOPIC + "/" + TOKEN_MULTI_LEVEL_WILDCARD+ TOKEN_SINGLE_LEVEL_WILDCARD + "/" + TOKEN_SINGLE_LEVEL_WILDCARD + "/" + ATTRIBUTE_VALUE_WRITE_TOPIC + "/" + TOKEN_MULTI_LEVEL_WILDCARD,+ TOKEN_SINGLE_LEVEL_WILDCARD + "/" + TOKEN_SINGLE_LEVEL_WILDCARD + "/" + ATTRIBUTE_WRITE_TOPIC + "/" + TOKEN_MULTI_LEVEL_WILDCARD);}@@ -337,23 +340,38 @@ public class DefaultMQTTHandler extends MQTTHandler {public void onPublish(RemotingConnection connection, Topic topic, ByteBuf body) {List<String> topicTokens = topic.getTokens();String payloadContent = body.toString(StandardCharsets.UTF_8);- Object value = ValueUtil.parse(payloadContent).orElse(null);- AttributeEvent attributeEvent = buildAttributeEvent(topicTokens, value);-- // Set timestamp as early as possible if not set- if (attributeEvent.getTimestamp() <= 0) {- attributeEvent.setTimestamp(timerService.getCurrentTimeMillis());+ AttributeEvent attributeEvent;++ if (isAttributeWriteTopic(topic)) {+ attributeEvent = ValueUtil.parse(payloadContent, ObjectNode.class).map(valueWithTimestamp -> {+ if (valueWithTimestamp.has("value") && valueWithTimestamp.has("timestamp")) {+ Object value = valueWithTimestamp.get("value");+ long timestamp = valueWithTimestamp.get("timestamp").asLong();+ if (timestamp > 0L) {+ return buildAttributeEvent(topicTokens, value, timestamp);+ }+ }+ return null;+ }).orElse(null);+ if (attributeEvent == null) {+ LOG.info(() -> "Invalid publish to write attribute topic '" + topic + "': " + connectionToString(connection));+ }+ } else {+ Object value = ValueUtil.parse(payloadContent).orElse(null);+ attributeEvent = buildAttributeEvent(topicTokens, value, timerService.getCurrentTimeMillis());}- // This is called by a single ActiveMQ client thread (the session) and async offloaded to the container executor,- // once the container executor has no free threads the caller will execute (i.e. the client thread) which will- // effectively limit rate of publish consumption eventually filling the attribute queue in the broker and- // preventing additional attribute events from being added to the queue. This gives us a consistent failure mode- // and natural rate limiting.- messageBrokerService.getFluentProducerTemplate()- .withBody(attributeEvent)- .to(ATTRIBUTE_EVENT_PROCESSOR)- .asyncSend();+ if (attributeEvent != null) {+ // This is called by a single ActiveMQ client thread (the session) and async offloaded to the container executor,+ // once the container executor has no free threads the caller will execute (i.e. the client thread) which will+ // effectively limit rate of publish consumption eventually filling the attribute queue in the broker and+ // preventing additional attribute events from being added to the queue. This gives us a consistent failure mode+ // and natural rate limiting.+ messageBrokerService.getFluentProducerTemplate()+ .withBody(attributeEvent)+ .to(ATTRIBUTE_EVENT_PROCESSOR)+ .asyncSend();+ }}@Override@@ -369,10 +387,10 @@ public class DefaultMQTTHandler extends MQTTHandler {}}- protected static AttributeEvent buildAttributeEvent(List<String> topicTokens, Object value) {+ protected static AttributeEvent buildAttributeEvent(List<String> topicTokens, Object value, Long timestamp) {String attributeName = topicTokens.get(3);String assetId = topicTokens.get(4);- return new AttributeEvent(assetId, attributeName, value).setSource(DefaultMQTTHandler.class.getSimpleName());+ return new AttributeEvent(assetId, attributeName, value, timestamp).setSource(DefaultMQTTHandler.class.getSimpleName());}protected static AssetFilter<?> buildAssetFilter(Topic topic) {@@ -518,6 +536,10 @@ public class DefaultMQTTHandler extends MQTTHandler {return ATTRIBUTE_VALUE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));}+ protected static boolean isAttributeWriteTopic(Topic topic) {+ return ATTRIBUTE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));+ }+protected static boolean isAssetTopic(Topic topic) {return ASSET_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));}diff --git a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.javaindex 4912acecc..faee961cf 100644--- a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java+++ b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java@@ -391,9 +391,7 @@ public class ManagerTestSetup extends ManagerSetup {.addMeta(new MetaItem<>(ACCESS_RESTRICTED_READ, true)).addMeta(new MetaItem<>(ACCESS_RESTRICTED_WRITE, true)));- addDemoApartmentRoomMotionSensor(apartment1Hallway, true, () ->- new SimulatorAgentLink(apartment1ServiceAgentId)- );+ addDemoApartmentRoomMotionSensor(apartment1Hallway, false, null);apartment1Hallway = assetStorageService.merge(apartment1Hallway);apartment1HallwayId = apartment1Hallway.getId();diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 5d8db174a..17a8a7c3e 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -8,7 +8,7 @@ import io.netty.channel.socket.SocketChannelimport org.openremote.agent.protocol.mqtt.MQTTLastWillimport org.openremote.agent.protocol.mqtt.MQTTMessageimport org.openremote.agent.protocol.mqtt.MQTT_IOClient-import org.openremote.agent.protocol.simulator.SimulatorProtocol+import org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.model.util.UniqueIdentifierGeneratorimport org.openremote.manager.agent.AgentServiceimport org.openremote.manager.asset.AssetStorageService@@ -56,6 +56,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)def mqttBrokerService = container.getService(MQTTBrokerService.class)def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)def clientEventService = container.getService(ClientEventService.class)def defaultMQTTHandler = mqttBrokerService.getCustomHandlers().find {it instanceof DefaultMQTTHandler} as DefaultMQTTHandlerdef agentService = container.getService(AgentService.class)@@ -164,7 +165,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "An attribute event occurs for a subscribed attribute"def attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 50)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "A publish event message should be sent"conditions.eventually {@@ -178,7 +179,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "Another asset attribute changed the client is subscribed on"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", true)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "A second publish event message should be sent"conditions.eventually {@@ -251,7 +252,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "another asset attribute changed without any subscriptions"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", false)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "No publish event message should be sent"new PollingConditions(initialDelay: 1, delay: 1, timeout: 10).eventually {@@ -273,7 +274,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "that attribute changes"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 30)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "a publish event message should be sent to the client"conditions.eventually {@@ -287,7 +288,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "another asset attribute changed without any subscriptions on that attribute"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", true)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "no publish event message should be sent"new PollingConditions(initialDelay: 1, delay: 1, timeout: 10).eventually {@@ -319,7 +320,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "a child asset of the subscription attribute event occurs"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 40)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "a publish event message should be sent"conditions.eventually {@@ -349,7 +350,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "a subscribed attribute changes"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 50)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "A publish value message should be sent to both subscriptions"conditions.eventually {@@ -366,7 +367,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "a subscribed attribute changes"attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", false)- ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "only the wildcard subscription should have received a publish"conditions.eventually {@@ -534,11 +535,12 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 1000d}- receivedEvents.clear()- when: "the new client tries to publish again"- topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/${managerTestSetup.apartment1HallwayId}".toString()- payload = "170"+ when: "the new client tries to publish again with a specified timestamp"+ def specifiedTimestamp = (receivedEvents.get(0) as AttributeEvent).timestamp + 5+ receivedEvents.clear()+ topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC/motionSensor/${managerTestSetup.apartment1HallwayId}".toString()+ payload = "{\"value\": 170, \"timestamp\": ${specifiedTimestamp}}".toString()newClient.sendMessage(new MQTTMessage<String>(topic, payload))then: "The value of the attribute should be updated and the first client should have received the event"@@ -549,6 +551,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayIdassert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 170d+ assert (receivedEvents.get(0) as AttributeEvent).timestamp == specifiedTimestamp}receivedEvents.clear()