Skip to content

armgit / core

Public Imported from GitHub
0

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

Added options to subscribe on a MQTT topic for one Attribute or just the value of the Attribute

Michael Rademaker7455ba7f2e7c
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java
index b5859230f..882b54533 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java
@@ -48,6 +48,7 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
+import static org.openremote.manager.mqtt.MqttBrokerService.ASSET_ATTRIBUTE_VALUE_TOPIC;
import static org.openremote.manager.mqtt.MqttBrokerService.TOPIC_SEPARATOR;
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
@@ -128,14 +129,22 @@ public class EventInterceptHandler extends AbstractInterceptHandler {
String[] topicParts = interceptSubscribeMessage.getTopicFilter().split(TOPIC_SEPARATOR);
String assetId = topicParts[1];
AttributeRef attributeRef = null;
- if (topicParts.length == 3) { //attribute specific
+ boolean isValueSubscription = false;
+ if (topicParts.length > 2) { //attribute specific
attributeRef = new AttributeRef(assetId, topicParts[2]);
}
+ if (topicParts.length == 4 && topicParts[3].equals(ASSET_ATTRIBUTE_VALUE_TOPIC)) {
+ isValueSubscription = true;
+ }
String subscriptionId;
if (attributeRef == null) {
subscriptionId = connection.assetSubscriptions.remove(assetId);
} else {
- subscriptionId = connection.assetAttributeSubscriptions.remove(attributeRef);
+ if (isValueSubscription) {
+ subscriptionId = connection.assetAttributeValueSubscriptions.remove(attributeRef);
+ } else {
+ subscriptionId = connection.assetAttributeSubscriptions.remove(attributeRef);
+ }
}
if (subscriptionId != null) { //renew subscription
RenewEventSubscriptions renewEventSubscriptions = new RenewEventSubscriptions(new String[]{subscriptionId});
@@ -153,7 +162,11 @@ public class EventInterceptHandler extends AbstractInterceptHandler {
connection.assetSubscriptions.put(assetId, subscription.getSubscriptionId());
} else {
attributeAssetFilter.setAttributeNames(attributeRef.getAttributeName());
- connection.assetAttributeSubscriptions.put(attributeRef, subscription.getSubscriptionId());
+ if (isValueSubscription) {
+ connection.assetAttributeValueSubscriptions.put(attributeRef, subscription.getSubscriptionId());
+ } else {
+ connection.assetAttributeSubscriptions.put(attributeRef, subscription.getSubscriptionId());
+ }
}
Map<String, Object> headers = prepareHeaders(connection);
@@ -171,8 +184,13 @@ public class EventInterceptHandler extends AbstractInterceptHandler {
String[] topicParts = interceptUnsubscribeMessage.getTopicFilter().split(TOPIC_SEPARATOR);
String assetId = topicParts[1];
String subscriptionId;
- if (topicParts.length == 3) { //attribute specific
- subscriptionId = connection.assetAttributeSubscriptions.remove(new AttributeRef(assetId, topicParts[2]));
+
+ if (topicParts.length > 2) { //attribute specific
+ if (topicParts.length == 4 && topicParts[3].equals(ASSET_ATTRIBUTE_VALUE_TOPIC)) {
+ subscriptionId = connection.assetAttributeValueSubscriptions.remove(new AttributeRef(assetId, topicParts[2]));
+ } else {
+ subscriptionId = connection.assetAttributeSubscriptions.remove(new AttributeRef(assetId, topicParts[2]));
+ }
} else {
subscriptionId = connection.assetSubscriptions.remove(assetId);
}
@@ -191,9 +209,13 @@ public class EventInterceptHandler extends AbstractInterceptHandler {
String[] topicParts = msg.getTopicName().split(TOPIC_SEPARATOR);
String assetId = topicParts[1];
AttributeRef attributeRef = null;
- if (topicParts.length == 3) { //attribute specific
+ boolean isValueSubscription = false;
+ if (topicParts.length > 2) { //attribute specific
attributeRef = new AttributeRef(assetId, topicParts[2]);
}
+ if (topicParts.length == 4 && topicParts[3].equals(ASSET_ATTRIBUTE_VALUE_TOPIC)) {
+ isValueSubscription = true;
+ }
if (attributeRef == null) {
if (connection.assetSubscriptions.containsKey(assetId)) {
String payloadContent = msg.getPayload().toString(Charset.defaultCharset());
@@ -204,11 +226,19 @@ public class EventInterceptHandler extends AbstractInterceptHandler {
});
}
} else {
- if (connection.assetAttributeSubscriptions.containsKey(attributeRef)) {
+ if (isValueSubscription && connection.assetAttributeValueSubscriptions.containsKey(attributeRef)) {
String payloadContent = msg.getPayload().toString(Charset.defaultCharset());
Map<String, Object> headers = prepareHeaders(connection);
AttributeEvent attributeEvent = new AttributeEvent(assetId, attributeRef.getAttributeName(), Values.create(payloadContent));
messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
+ } else if (connection.assetAttributeSubscriptions.containsKey(attributeRef)) {
+ String payloadContent = msg.getPayload().toString(Charset.defaultCharset());
+ String attributeName = attributeRef.getAttributeName();
+ Values.parse(payloadContent).flatMap(Values::getObject).ifPresent(objectValue -> {
+ Map<String, Object> headers = prepareHeaders(connection);
+ AttributeEvent attributeEvent = new AttributeEvent(assetId, attributeName, objectValue.get(objectValue.keys()[0]).orElse(null));
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
+ });
}
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java
index f64b1247e..b3fda1ff0 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java
@@ -26,8 +26,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
-import static org.openremote.manager.mqtt.MqttBrokerService.ASSETS_TOPIC;
-import static org.openremote.manager.mqtt.MqttBrokerService.TOPIC_SEPARATOR;
+import static org.openremote.manager.mqtt.MqttBrokerService.*;
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {
@@ -78,6 +77,11 @@ public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {
return false;
}
+ if(topic.getTokens().size() == 4 && topic.getTokens().stream().noneMatch(token -> token.toString().equals(ASSET_ATTRIBUTE_VALUE_TOPIC))) {
+ LOG.info("Topic for raw values should end with '" + ASSET_ATTRIBUTE_VALUE_TOPIC + "'");
+ return false;
+ }
+
AccessToken accessToken = null;
try {
accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));
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 024f9bf50..072b9ce82 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -61,6 +61,7 @@ public class MqttBrokerService implements ContainerService {
public static final String ASSETS_TOPIC = "assets";
public static final String TOPIC_SEPARATOR = "/";
+ public static final String ASSET_ATTRIBUTE_VALUE_TOPIC = "value";
protected ManagerKeycloakIdentityProvider identityProvider;
protected ClientEventService clientEventService;
@@ -115,9 +116,10 @@ public class MqttBrokerService implements ContainerService {
.forEach(event -> {
MqttConnection mqttConnection = mqttConnectionMap.get(sessionKey);
if (mqttConnection != null) {
- if (mqttConnection.assetSubscriptions.containsKey(event.getEntityId())) {
+ if (mqttConnection.assetSubscriptions.containsKey(event.getEntityId()) || mqttConnection.assetAttributeSubscriptions.containsKey(event.getAttributeRef())) {
sendAttributeEvent(sessionKey, event);
- } else if (mqttConnection.assetAttributeSubscriptions.containsKey(event.getAttributeRef())) {
+ }
+ if (mqttConnection.assetAttributeValueSubscriptions.containsKey(event.getAttributeRef())) {
sendAttributeValue(sessionKey, event);
}
}
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 8041ff2a3..c48cf9306 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
@@ -15,6 +15,7 @@ public class MqttConnection {
protected final byte[] password;
protected final Map<String, String> assetSubscriptions;
protected final Map<AttributeRef, String> assetAttributeSubscriptions;
+ protected final Map<AttributeRef, String> assetAttributeValueSubscriptions;
protected String accessToken;
protected int subscriptionId;
@@ -30,6 +31,7 @@ public class MqttConnection {
this.password = password;
this.assetSubscriptions = new HashMap<>();
this.assetAttributeSubscriptions = new HashMap<>();
+ assetAttributeValueSubscriptions = new HashMap<>();
this.subscriptionId = 0;
}
diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovy
index e78d76457..fa8778fd5 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovy
@@ -302,13 +302,68 @@ class MqttTest extends Specification implements ManagerContainerTrait {
attributeEvent = new AttributeEvent(managerDemoSetup.apartment1HallwayId, "motionSensor", Values.create(30))
assetProcessingService.sendAttributeEvent(attributeEvent)
+ then: "A publish event message should be sent"
+ conditions.eventually {
+ assert mqttBrokerServiceAttributeEventCalls == 4
+ }
+
+ when: "Another asset attribute changed without any subscriptions on that attribute"
+ attributeEvent = new AttributeEvent(managerDemoSetup.apartment1HallwayId, "presenceDetected", Values.create(true))
+ assetProcessingService.sendAttributeEvent(attributeEvent)
+
+ then: "No publish event message should be sent"
+ conditions.eventually {
+ assert mqttBrokerServiceAttributeEventCalls == 4
+ }
+
+ when: "a mqtt client unsubscribes to an asset attribute"
+ topic = "assets/" + managerDemoSetup.apartment1HallwayId + "/motionSensor"
+ remainingLength = 4 + topic.size()
+
+ client
+ // UNSUBSCRIBE
+ .write(0xA2) // MQTT Control Packet type(10) with QoS level 1
+ .write(remainingLength.byteValue()) // Remaining Length
+ .write(0x00, 0x10) // MessageId
+
+ // Payload
+ .write(0x00, topic.size().byteValue()) // Topic Length
+ .write(topic) // Topic
+ .flush()
+
+
+ then: "No subscription should exist"
+ conditions.eventually {
+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeSubscriptions.size() == 0
+ }
+
+ when: "a mqtt client subscribes to an asset attribute value"
+ topic = "assets/" + managerDemoSetup.apartment1HallwayId + "/motionSensor/value"
+ remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
+
+ client
+ // SUBSCRIBE
+ .write(0x82) // MQTT Control Packet type(8) with QoS level 1
+ .write(remainingLength.byteValue()) // Remaining Length
+ .write(0x00, 0x10) // MessageId
+
+ // Payload
+ .write(0x00, topic.size().byteValue()) // Topic Length
+ .write(topic) // Topic
+ .write(0x01) // QoS level 1
+ .flush()
+
+ and: "that attribute changed"
+ attributeEvent = new AttributeEvent(managerDemoSetup.apartment1HallwayId, "motionSensor", Values.create(40))
+ assetProcessingService.sendAttributeEvent(attributeEvent)
+
then: "A publish value message should be sent"
conditions.eventually {
assert mqttBrokerServiceAttributeValueCalls == 1
}
when: "Another asset attribute changed without any subscriptions on that attribute"
- attributeEvent = new AttributeEvent(managerDemoSetup.apartment1HallwayId, "presenceDetected", Values.create(true))
+ attributeEvent = new AttributeEvent(managerDemoSetup.apartment1HallwayId, "presenceDetected", Values.create(false))
assetProcessingService.sendAttributeEvent(attributeEvent)
then: "No publish value message should be sent"
@@ -316,6 +371,27 @@ class MqttTest extends Specification implements ManagerContainerTrait {
assert mqttBrokerServiceAttributeValueCalls == 1
}
+ when: "a mqtt client unsubscribes to an asset attribute value"
+ topic = "assets/" + managerDemoSetup.apartment1HallwayId + "/motionSensor/value"
+ remainingLength = 4 + topic.size()
+
+ client
+ // UNSUBSCRIBE
+ .write(0xA2) // MQTT Control Packet type(10) with QoS level 1
+ .write(remainingLength.byteValue()) // Remaining Length
+ .write(0x00, 0x10) // MessageId
+
+ // Payload
+ .write(0x00, topic.size().byteValue()) // Topic Length
+ .write(topic) // Topic
+ .flush()
+
+
+ then: "No subscription should exist"
+ conditions.eventually {
+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeValueSubscriptions.size() == 0
+ }
+
cleanup: "the server should be stopped"
stopContainer(container)
}