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
MQTT agent message match filter bug fix (#1917)
rainerhitz7e0405f6a2de
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.javaindex a0bd9334d..885d2d6c2 100644--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java@@ -22,6 +22,7 @@ package org.openremote.agent.protocol.mqtt;import com.hivemq.client.mqtt.datatypes.MqttQos;import org.apache.http.client.utils.URIBuilder;import org.openremote.model.Container;+import org.openremote.model.protocol.ProtocolUtil;import org.openremote.model.security.KeyStoreService;import org.openremote.model.util.UniqueIdentifierGenerator;import org.openremote.model.attribute.Attribute;@@ -53,9 +54,16 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTA@Overrideprotected void doLinkAttribute(String assetId, Attribute<?> attribute, MQTTAgentLink agentLink) throws RuntimeException {agentLink.getSubscriptionTopic().ifPresent(topic -> {- Consumer<MQTTMessage<String>> messageConsumer = msg -> updateLinkedAttribute(- new AttributeRef(assetId, attribute.getName()), msg.payload+ Consumer<String> genericConsumer = ProtocolUtil.createGenericAttributeMessageConsumer(+ assetId, attribute, agentLink, timerService::getCurrentTimeMillis, this::updateLinkedAttribute);+ Consumer<MQTTMessage<String>> messageConsumer = msg -> {+ if (genericConsumer != null) {+ genericConsumer.accept(msg.payload);+ } else {+ updateLinkedAttribute(new AttributeRef(assetId, attribute.getName()), msg.payload);+ }+ };client.addMessageConsumer(topic, agentLink.getQos().map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(null), messageConsumer);protocolMessageConsumers.put(new AttributeRef(assetId, attribute.getName()), messageConsumer);});diff --git a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovyindex 59a2e8eb0..3f6c01952 100644--- a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy@@ -51,7 +51,10 @@ import org.openremote.model.attribute.Attributeimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.attribute.MetaItemimport org.openremote.model.auth.UsernamePassword+import org.openremote.model.query.filter.NumberPredicateimport org.openremote.model.util.UniqueIdentifierGenerator+import org.openremote.model.value.JsonPathFilter+import org.openremote.model.value.ValueFilterimport org.openremote.setup.integration.KeycloakTestSetupimport org.openremote.setup.integration.ManagerTestSetupimport org.openremote.test.ManagerContainerTrait@@ -72,7 +75,10 @@ import java.security.cert.CertificateFactoryimport java.security.cert.X509Certificateimport static org.openremote.manager.mqtt.MQTTBrokerService.getConnectionIDString+import static org.openremote.model.Constants.MASTER_REALMimport static org.openremote.model.value.MetaItemType.AGENT_LINK+import static org.openremote.model.value.ValueType.BOOLEAN+import static org.openremote.model.value.ValueType.JSONimport static org.openremote.model.value.ValueType.NUMBERclass MQTTClientProtocolTest extends Specification implements ManagerContainerTrait {@@ -194,6 +200,129 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr}}+ def "Check MQTT client protocol message match filter support"() {++ given: "expected conditions"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)++ when: "the container starts"+ def container = startContainer(defaultConfig(), defaultServices())+ def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)+ def brokerService = container.getService(MQTTBrokerService.class)+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def mqttHost = brokerService.host+ def mqttPort = brokerService.port+ def mqttAgentClientId = UniqueIdentifierGenerator.generateId()++ and: "a test asset is created"+ def testThing = new ThingAsset("Test thing")+ .setRealm(keycloakTestSetup.realmBuilding.name)+ .addOrReplaceAttributes(+ new Attribute<Object>("test", JSON)+ )++ and: "the test asset is added to the asset service"+ testThing = assetStorageService.merge(testThing)++ and: "a MQTT agent is created"+ def agent = new MQTTAgent("Test agent")+ .setRealm(MASTER_REALM)+ .setClientId(mqttAgentClientId)+ .setHost(mqttHost)+ .setPort(mqttPort)+ .setUsernamePassword(new UsernamePassword(keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser.username, keycloakTestSetup.serviceUser.secret))++ and: "the agent is added to the asset service"+ agent = assetStorageService.merge(agent)++ then: "the protocol should authenticate and the agent status should become CONNECTED"+ conditions.eventually {+ agent = assetStorageService.find(agent.id, Agent.class)+ assert agent.getAgentStatus().orElse(null) == ConnectionStatus.CONNECTED+ }++ when: "an asset is created with attributes linked to the agent"+ def subscriptionTopic = "${keycloakTestSetup.realmBuilding.name}/$mqttAgentClientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/test/${testThing.id}"+ def asset = new ThingAsset("Test Asset")+ .setParent(agent)+ .addOrReplaceAttributes(+ new Attribute<>("temperature", NUMBER)+ .addMeta(+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)+ .setSubscriptionTopic(subscriptionTopic)+ .setValueFilters([new JsonPathFilter('$.temperature', true, false)] as ValueFilter[])+ .setMessageMatchFilters([new JsonPathFilter('$.port', true, false)] as ValueFilter[])+ .setMessageMatchPredicate(new NumberPredicate(85))+ )),+ new Attribute<>("switchStatus", BOOLEAN)+ .addMeta(+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)+ .setSubscriptionTopic(subscriptionTopic)+ .setValueFilters([new JsonPathFilter('$.switch_status', true, false)] as ValueFilter[])+ .setMessageMatchFilters([new JsonPathFilter('$.port', true, false)] as ValueFilter[])+ .setMessageMatchPredicate(new NumberPredicate(85))+ .setValueConverter([+ SWITCH_ON : true,+ SWITCH_OFF: false+ ])+ ))+ )++ and: "the asset is merged into the asset service"+ asset = assetStorageService.merge(asset)++ and: "a message which should pass the message match filter is published"+ def json = [+ port: 85,+ temperature: 19.5,+ switch_status: "switch_on"+ ]+ def attributeEvent = new AttributeEvent(testThing.id, "test", json)+ Thread.sleep(200)+ assetProcessingService.sendAttributeEvent(attributeEvent)++ then: "asset attribute values should be updated"+ conditions.eventually {+ asset = assetStorageService.find(asset.id, true)+ assert asset.getAttribute("temperature").flatMap { it.value }.map { it == 19.5 }.orElse(false)+ assert asset.getAttribute("switchStatus").flatMap { it.value }.map { it == true }.orElse(false)+ }++ when: "a message which should not pass the message match filter is published"+ json = [+ port: 1,+ temperature: 20.5,+ switch_status: "switch_off"+ ]+ attributeEvent = new AttributeEvent(testThing.id, "test", json)+ assetProcessingService.sendAttributeEvent(attributeEvent)++ then: "asset attribute values should not be updated"+ def start = System.currentTimeMillis()+ while (System.currentTimeMillis() - start < 2000) {+ asset = assetStorageService.find(asset.id, true)+ assert asset.getAttribute("temperature").flatMap { it.value }.map { it == 19.5 }.orElse(false)+ assert asset.getAttribute("switchStatus").flatMap { it.value }.map { it == true }.orElse(false)+ Thread.sleep((100))+ }++ when: "a message which should pass the message match filter is published"+ json = [+ port: 85,+ temperature: 21.5,+ switch_status: "switch_off"+ ]+ attributeEvent = new AttributeEvent(testThing.id, "test", json)+ assetProcessingService.sendAttributeEvent(attributeEvent)++ then: "asset attribute values should be updated"+ conditions.eventually {+ asset = assetStorageService.find(asset.id, true)+ assert asset.getAttribute("temperature").flatMap { it.value }.map { it == 21.5 }.orElse(false)+ assert asset.getAttribute("switchStatus").flatMap { it.value }.map { it == false }.orElse(false)+ }+ }/*** This test is not fully done right now, as a non-internet-requiring test would require mTLS functionality within the