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
Added initial implementation of MQTT Client Agent
Rich Turnerc73b16217026
diff --git a/agent/build.gradle b/agent/build.gradleindex 490b9d7cc..815311dcb 100644--- a/agent/build.gradle+++ b/agent/build.gradle@@ -29,6 +29,8 @@ dependencies {compile("org.openremote:or-zwave:$zwaveVersion") {exclude group: "org.bidib.com.neuronrobotics"}++ compile "com.hivemq:hivemq-mqtt-client:$hiveMQClientVersion"}jar {diff --git a/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.javaindex 0e7c7dda2..ed6307dd6 100644--- a/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.java@@ -3,6 +3,7 @@ package org.openremote.agent.protocol.artnet;import io.netty.channel.ChannelHandler;import org.openremote.agent.protocol.io.AbstractIOClientProtocol;import org.openremote.agent.protocol.io.AbstractNettyIOClient;+import org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol;import org.openremote.agent.protocol.udp.UDPIOClient;import org.openremote.model.asset.AssetTreeNode;import org.openremote.model.asset.agent.DefaultAgentLink;@@ -18,7 +19,7 @@ import java.util.concurrent.Future;import java.util.function.Consumer;import java.util.function.Supplier;-public class ArtnetProtocol extends AbstractIOClientProtocol<ArtnetProtocol, ArtnetAgent, ArtnetPacket, UDPIOClient<ArtnetPacket>, DefaultAgentLink> implements ProtocolAssetImport {+public class ArtnetProtocol extends AbstractNettyIOClientProtocol<ArtnetProtocol, ArtnetAgent, ArtnetPacket, UDPIOClient<ArtnetPacket>, DefaultAgentLink> implements ProtocolAssetImport {public static final String PROTOCOL_DISPLAY_NAME = "Artnet";protected final Map<AttributeRef, Consumer<ArtnetPacket>> protocolMessageConsumers = new HashMap<>();diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.javaindex fcf8cb694..8e83b44ab 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java@@ -172,8 +172,6 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtocoW client = doCreateIoClient();ProtocolIOClient<V, W> protocolIoClient = new ProtocolIOClient<>(client, this::onConnectionStatusChanged, this::onMessageReceived);this.client = protocolIoClient;- Supplier<ChannelHandler[]> encoderDecoderProvider = getEncoderDecoderProvider();- client.setEncoderDecoderProvider(encoderDecoderProvider);return protocolIoClient;}@@ -185,13 +183,10 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtoco}/**- * Should return an instance of {@link IOClient} for the linked {@link Agent}; the configuration of- * encoders/decoders is handled by the separate call to {@link #getEncoderDecoderProvider}+ * Should return an instance of {@link IOClient} for the linked {@link Agent}.*/protected abstract W doCreateIoClient() throws Exception;- protected abstract Supplier<ChannelHandler[]> getEncoderDecoderProvider();-/*** Called when the {@link IOClient} receives a message from the server*/diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.javaindex 2030aa99d..eb44d2dee 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java@@ -72,7 +72,7 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;* <b>NOTE: Care must be taken when working with Netty {@link ByteBuf} as Netty uses reference counting to manage their* lifecycle. Refer to the Netty documentation for more information.</b>*/-public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implements IOClient<T> {+public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implements NettyIOClient<T> {/*** This is intended to be used at the end of a decoder chain where the previous decoder outputs a {@link ByteBuf};diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClientProtocol.javanew file mode 100644index 000000000..911969138--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClientProtocol.java@@ -0,0 +1,48 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.io;++import io.netty.channel.ChannelHandler;+import org.openremote.model.asset.agent.AgentLink;+import org.openremote.model.asset.agent.Protocol;++import java.util.function.Supplier;++/**+ * This is an abstract {@link Protocol} for protocols that require an {@link NettyIOClient}.+ */+public abstract class AbstractNettyIOClientProtocol<T extends AbstractIOClientProtocol<T, U, V, W, X>, U extends IOAgent<U, T, X>, V, W extends NettyIOClient<V>, X extends AgentLink<?>> extends AbstractIOClientProtocol<T, U, V, W, X> {+ protected AbstractNettyIOClientProtocol(U agent) {+ super(agent);+ }++ @Override+ protected ProtocolIOClient<V, W> createIoClient() throws Exception {+ ProtocolIOClient<V, W> protocolIOClient = super.createIoClient();+ Supplier<ChannelHandler[]> encoderDecoderProvider = getEncoderDecoderProvider();+ protocolIOClient.ioClient.setEncoderDecoderProvider(encoderDecoderProvider);+ return client;+ }++ /**+ * Gets the Netty {@link ChannelHandler}s for the IO client+ */+ protected abstract Supplier<ChannelHandler[]> getEncoderDecoderProvider();+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.javaindex 30c6d08c9..226005be4 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.java@@ -19,15 +19,12 @@*/package org.openremote.agent.protocol.io;-import io.netty.channel.ChannelHandler;import org.openremote.model.asset.agent.ConnectionStatus;import java.util.function.Consumer;-import java.util.function.Supplier;/**- * Represents an IO client that communicates with a server; this is heavily tied to netty and uses the concept of- * {@link io.netty.channel.ChannelHandler}s for encoding/decoding messages of type <T>.+ * Represents an IO client that communicates with a server** @param <T> Defines the message type that the instance will encode/decode*/@@ -87,10 +84,4 @@ public interface IOClient<T> {* Should return a URI that uniquely identifies this client instance*/String getClientUri();-- /**- * Allows appropriate encoders and decoders to be added to the message pipeline; if an {@link IOClient} doesn't- * support this then an {@link UnsupportedOperationException} will be thrown, consult the {@link IOClient}'s documentation.- */- void setEncoderDecoderProvider(Supplier<ChannelHandler[]> encoderDecoderProvider) throws UnsupportedOperationException;}diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/NettyIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/NettyIOClient.javanew file mode 100644index 000000000..c432fb412--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/io/NettyIOClient.java@@ -0,0 +1,39 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.io;++import io.netty.channel.ChannelHandler;++import java.util.function.Supplier;++/**+ * This is for IO clients that use Netty and are based on {@link io.netty.channel.ChannelHandler}s for+ * encoding/decoding messages of type <T>.+ *+ * @param <T> Defines the message type that the instance will encode/decode+ */+public interface NettyIOClient<T> extends IOClient<T> {++ /**+ * Allows appropriate encoders and decoders to be added to the message pipeline; if an {@link IOClient} doesn't+ * support this then an {@link UnsupportedOperationException} will be thrown, consult the {@link IOClient}'s documentation.+ */+ void setEncoderDecoderProvider(Supplier<ChannelHandler[]> encoderDecoderProvider) throws UnsupportedOperationException;+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/ProtocolIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/ProtocolIOClient.javaindex eecc9a8d0..eeeb705e8 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/ProtocolIOClient.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/ProtocolIOClient.java@@ -35,17 +35,13 @@ public class ProtocolIOClient<W, X extends IOClient<W>> {}public void connect() {- ioClient.addConnectionStatusConsumer(status -> {- if (connectionStatusConsumer != null) {- connectionStatusConsumer.accept(status);- }- });+ if (connectionStatusConsumer != null) {+ ioClient.addConnectionStatusConsumer(status -> connectionStatusConsumer.accept(status));+ }- ioClient.addMessageConsumer(msg -> {- if (messageConsumer != null) {- messageConsumer.accept(msg);- }- });+ if (messageConsumer != null) {+ ioClient.addMessageConsumer(msg -> messageConsumer.accept(msg));+ }AbstractIOClientProtocol.LOG.info("Connecting IO client");ioClient.connect();diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTTClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTTClientProtocol.javanew file mode 100644index 000000000..cb6a4aba4--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTTClientProtocol.java@@ -0,0 +1,31 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.mqtt;++import org.openremote.agent.protocol.io.AbstractIOClientProtocol;+import org.openremote.agent.protocol.io.IOAgent;+import org.openremote.model.asset.agent.AgentLink;++public abstract class AbstractMQTTClientProtocol<T extends AbstractMQTTClientProtocol<T, U, V, W, X>, U extends IOAgent<U, T, X>, V, W extends AbstractMQTT_IOClient<V>, X extends AgentLink<?>> extends AbstractIOClientProtocol<T,U,MQTTMessage<V>,W,X> {++ protected AbstractMQTTClientProtocol(U agent) {+ super(agent);+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.javanew file mode 100644index 000000000..c27024e3f--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java@@ -0,0 +1,335 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.mqtt;++import com.hivemq.client.mqtt.MqttClient;+import com.hivemq.client.mqtt.MqttClientState;+import com.hivemq.client.mqtt.MqttWebSocketConfig;+import com.hivemq.client.mqtt.MqttWebSocketConfigBuilder;+import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient;+import com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder;+import com.hivemq.client.mqtt.mqtt3.message.connect.Mqtt3ConnectBuilder;+import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck;+import org.openremote.agent.protocol.io.IOClient;+import org.openremote.container.util.UniqueIdentifierGenerator;+import org.openremote.model.asset.agent.ConnectionStatus;+import org.openremote.model.auth.UsernamePassword;+import org.openremote.model.syslog.SyslogCategory;++import java.net.URI;+import java.util.HashMap;+import java.util.HashSet;+import java.util.Map;+import java.util.Set;+import java.util.concurrent.CompletableFuture;+import java.util.function.Consumer;+import java.util.logging.Level;+import java.util.logging.Logger;++import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;++public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S>> {++ protected String clientId;+ protected String host;+ protected int port;+ protected boolean secure;+ protected UsernamePassword usernamePassword;+ protected URI websocketURI;+ protected Mqtt3AsyncClient client;+ protected final Set<Consumer<ConnectionStatus>> connectionStatusConsumers = new HashSet<>();+ protected final Map<String, Set<Consumer<MQTTMessage<S>>>> topicConsumerMap = new HashMap<>();+ public static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractMQTT_IOClient.class);++ protected AbstractMQTT_IOClient(String host, int port, boolean secure, UsernamePassword usernamePassword, URI websocketURI) {+ this(UniqueIdentifierGenerator.generateId(), host, port, secure, usernamePassword, websocketURI);+ }++ @SuppressWarnings("unchecked")+ protected AbstractMQTT_IOClient(String clientId, String host, int port, boolean secure, UsernamePassword usernamePassword, URI websocketURI) {+ this.clientId = clientId;+ this.host = host;+ this.port = port;+ this.secure = secure;+ this.usernamePassword = usernamePassword;+ this.websocketURI = websocketURI;++ Mqtt3ClientBuilder builder = MqttClient.builder()+ .useMqttVersion3()+ .identifier(clientId);++ if (secure) {+ builder = builder.sslWithDefaultConfig();+ }++ if (websocketURI != null) {+ builder = builder+ .serverHost(websocketURI.getHost())+ .serverPort(websocketURI.getPort());++ MqttWebSocketConfigBuilder webSocketConfigBuilder = MqttWebSocketConfig.builder()+ .serverPath(websocketURI.getPath())+ .queryString(websocketURI.getQuery());+ builder = builder.webSocketConfig(webSocketConfigBuilder.build());+ } else {+ builder = builder+ .serverHost(host)+ .serverPort(port);+ }++ try {+ client = builder.buildAsync();+ } catch (Exception e) {+ LOG.log(Level.WARNING, "Invalid MQTT client config for client '" + getClientUri() + "'", e);+ client = null;+ }+ }++ @Override+ public void sendMessage(MQTTMessage<S> message) {+ if (client == null) {+ LOG.info("Cannot send message as client is invalid '" + getClientUri() + "'");+ return;+ }++ client.publishWith()+ .topic(message.topic)+ .payload(messageToBytes(message.payload))+ .send()+ .whenComplete((publish, throwable) -> {+ if (throwable != null) {+ // Failure+ LOG.log(Level.INFO, "Failed to publish to MQTT broker '" + getClientUri() + "'", throwable);+ } else {+ // Success+ LOG.finer("Published message to MQTT broker '" + getClientUri() + "'");+ }+ });+ }++ @Override+ public void addMessageConsumer(Consumer<MQTTMessage<S>> messageConsumer) {+ addMessageConsumer("#", messageConsumer);+ }++ public void addMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {+ if (client == null) {+ return;+ }++ Set<Consumer<MQTTMessage<S>>> consumers = topicConsumerMap.computeIfAbsent(topic, (t) -> new HashSet<>());+ boolean initialise = consumers.isEmpty();+ consumers.add(messageConsumer);++ if (initialise) {+ addSubscription(+ topic,+ message -> {+ if (!topicConsumerMap.containsKey(topic)) {+ return;+ }++ consumers.forEach(consumer -> {+ try {+ consumer.accept(message);+ } catch (Exception e) {+ LOG.log(Level.WARNING, "Message consumer threw an exception", e);+ }+ });+ },+ () -> consumers.remove(messageConsumer));+ }+ }++ @Override+ public void removeMessageConsumer(Consumer<MQTTMessage<S>> messageConsumer) {+ removeMessageConsumer("#", messageConsumer);+ }++ public void removeMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {+ topicConsumerMap.computeIfPresent(topic, (t, consumers) -> {+ if (consumers.remove(messageConsumer) && consumers.isEmpty()) {+ removeSubscription(topic);+ return null;+ }+ return consumers;+ });+ }++ @Override+ public void removeAllMessageConsumers() {+ Set<String> topics = new HashSet<>(topicConsumerMap.keySet());+ topicConsumerMap.clear();+ topics.forEach(this::removeSubscription);+ }++ protected void addSubscription(String topic, Consumer<MQTTMessage<S>> onPublish, Runnable onFailure) {+ client.subscribeWith()+ .topicFilter(topic)+ .callback(publish -> {+ try {+ String topicStr = publish.getTopic().toString();+ S payload = messageFromBytes(publish.getPayloadAsBytes());+ MQTTMessage<S> message = new MQTTMessage<>(topicStr, payload);+ onPublish.accept(message);+ } catch (Exception e) {+ LOG.log(Level.WARNING, "Failed to process published message on client '" + getClientUri() + "'", e);+ }+ })+ .send()+ .whenComplete((subAck, throwable) -> {+ if (throwable != null) {+ LOG.log(Level.WARNING, "Failed to subscribe to topic '" + topic + "' on client '" + getClientUri() + "'", throwable);+ onFailure.run();+ } else {+ LOG.fine("Subscribed to topic '" + topic + "' on client '" + getClientUri() + "'");+ }+ });+ }++ protected void removeSubscription(String topic) {+ if (client != null) {+ client.unsubscribeWith()+ .topicFilter(topic)+ .send()+ .whenComplete((subAck, throwable) -> {+ if (throwable != null) {+ LOG.log(Level.WARNING, "Failed to unsubscribe to topic '" + topic + "' on client '" + getClientUri() + "'", throwable);+ } else {+ LOG.fine("Unsubscribed from topic '" + topic + "' on client '" + getClientUri() + "'");+ }+ });+ }+ }++ @Override+ public void addConnectionStatusConsumer(Consumer<ConnectionStatus> connectionStatusConsumer) {+ connectionStatusConsumers.add(connectionStatusConsumer);+ }++ @Override+ public void removeConnectionStatusConsumer(Consumer<ConnectionStatus> connectionStatusConsumer) {+ connectionStatusConsumers.remove(connectionStatusConsumer);+ }++ @Override+ public void removeAllConnectionStatusConsumers() {+ connectionStatusConsumers.clear();+ }++ @Override+ public ConnectionStatus getConnectionStatus() {+ if (client != null) {+ MqttClientState state = client.getState();+ switch (state) {++ case DISCONNECTED:+ return ConnectionStatus.DISCONNECTED;+ case CONNECTING:+ case CONNECTING_RECONNECT:+ return ConnectionStatus.CONNECTING;+ case CONNECTED:+ return ConnectionStatus.CONNECTED;+ case DISCONNECTED_RECONNECT:+ return ConnectionStatus.WAITING;+ }+ }++ return ConnectionStatus.ERROR;+ }++ @Override+ public void connect() {+ if (client == null) {+ LOG.info("Cannot connect as client is invalid '" + getClientUri() + "'");+ return;+ }++ if (getConnectionStatus() != ConnectionStatus.DISCONNECTED) {+ LOG.info("Client must be disconnected before calling connect '" + getClientUri() + "'");+ return;+ }++ Mqtt3ConnectBuilder.Send<CompletableFuture<Mqtt3ConnAck>> completableFutureSend = client.connectWith();++ if (usernamePassword != null) {+ completableFutureSend = completableFutureSend.simpleAuth()+ .username(usernamePassword.getUsername())+ .password(usernamePassword.getPassword().getBytes())+ .applySimpleAuth();+ }++ completableFutureSend.send()+ .whenComplete((connAck, throwable) -> {+ if (throwable != null) {+ // Failure+ LOG.log(Level.WARNING, "Failed to connect to MQTT broker '" + getClientUri() + "'", throwable);+ } else {+ // Connected+ LOG.log(Level.INFO, "Connected to MQTT broker '" + getClientUri() + "'");+ connectionStatusConsumers.forEach(consumer -> {+ try {+ consumer.accept(ConnectionStatus.CONNECTED);+ } catch (Exception e) {+ LOG.log(Level.WARNING, "Connection status consumer threw an exception", e);+ }+ });+ }+ });+ }++ @Override+ public void disconnect() {+ if (client == null) {+ LOG.info("Cannot disconnect as client is invalid '" + getClientUri() + "'");+ return;+ }++ client.disconnect()+ .whenComplete((status, throwable) -> {+ if (throwable != null) {+ // Failure+ LOG.log(Level.WARNING, "Failed to disconnect from MQTT broker '" + getClientUri() + "'", throwable);+ } else {+ // Disconnected+ LOG.log(Level.INFO, "Disconnected from MQTT broker '" + getClientUri() + "'");+ connectionStatusConsumers.forEach(consumer -> {+ try {+ consumer.accept(ConnectionStatus.DISCONNECTED);+ } catch (Exception e) {+ LOG.log(Level.WARNING, "Connection status consumer threw an exception", e);+ }+ });+ }+ });+ }++ @Override+ public String getClientUri() {+ if (websocketURI != null) {+ return "mqtt_" + websocketURI + "?clientId=" + clientId;+ }+ return "mqtt" + (secure ? "s://" : "://" ) + host + ":" + port + "/?clientId=" + clientId;+ }++ public abstract byte[] messageToBytes(S message);++ public abstract S messageFromBytes(byte[] bytes);+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.javanew file mode 100644index 000000000..4c0094d2e--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java@@ -0,0 +1,106 @@+/*+ * Copyright 2020, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.mqtt;++import org.openremote.agent.protocol.io.IOAgent;+import org.openremote.model.asset.agent.AgentDescriptor;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.util.ModelIgnore;+import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.ValueType;++import javax.persistence.Entity;+import java.util.Optional;++@Entity+public class MQTTAgent extends IOAgent<MQTTAgent, MQTTProtocol, MQTTAgentLink> {++ public static final AttributeDescriptor<String> MQTT_HOST = HOST.withOptional(false);+ public static final AttributeDescriptor<Integer> MQTT_PORT = PORT.withOptional(false);+ public static final AttributeDescriptor<String> CLIENT_ID = new AttributeDescriptor<>("clientId", ValueType.TEXT);+ public static final AttributeDescriptor<Boolean> SECURE_MODE = new AttributeDescriptor<>("secureMode", ValueType.BOOLEAN);+ public static final AttributeDescriptor<Boolean> WEBSOCKET_MODE = new AttributeDescriptor<>("websocketMode", ValueType.BOOLEAN);+ public static final AttributeDescriptor<String> WEBSOCKET_PATH = new AttributeDescriptor<>("websocketPath", ValueType.TEXT);+ public static final AttributeDescriptor<String> WEBSOCKET_QUERY = new AttributeDescriptor<>("websocketQuery", ValueType.TEXT);++ public static final AgentDescriptor<MQTTAgent, MQTTProtocol, MQTTAgentLink> DESCRIPTOR = new AgentDescriptor<>(+ MQTTAgent.class, MQTTProtocol.class, MQTTAgentLink.class+ );++ /**+ * For use by hydrators (i.e. JPA/Jackson)+ */+ protected MQTTAgent() {+ }++ public MQTTAgent(String name) {+ super(name);+ }++ @Override+ public MQTTProtocol getProtocolInstance() {+ return new MQTTProtocol(this);+ }++ public Optional<String> getClientId() {+ return getAttributes().getValue(CLIENT_ID);+ }++ public MQTTAgent setClientId(String clientId) {+ getAttributes().addOrReplace(new Attribute<>(CLIENT_ID, clientId));+ return this;+ }++ public Optional<Boolean> isSecureMode() {+ return getAttributes().getValue(SECURE_MODE);+ }++ public MQTTAgent setSecureMode(boolean secureMode) {+ getAttributes().addOrReplace(new Attribute<>(SECURE_MODE, secureMode));+ return this;+ }++ public Optional<Boolean> isWebsocketMode() {+ return getAttributes().getValue(WEBSOCKET_MODE);+ }++ public MQTTAgent setWebsocketMode(boolean websocketMode) {+ getAttributes().addOrReplace(new Attribute<>(WEBSOCKET_MODE, websocketMode));+ return this;+ }++ public Optional<String> getWebsocketPath() {+ return getAttributes().getValue(WEBSOCKET_PATH);+ }++ public MQTTAgent setWebsocketPath(String websocketPath) {+ getAttributes().addOrReplace(new Attribute<>(WEBSOCKET_PATH, websocketPath));+ return this;+ }++ public Optional<String> getWebsocketQuery() {+ return getAttributes().getValue(WEBSOCKET_QUERY);+ }++ public MQTTAgent setWebsocketQuery(String websocketQuery) {+ getAttributes().addOrReplace(new Attribute<>(WEBSOCKET_QUERY, websocketQuery));+ return this;+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgentLink.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgentLink.javanew file mode 100644index 000000000..e84ccfeed--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgentLink.java@@ -0,0 +1,61 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.mqtt;++import com.fasterxml.jackson.databind.annotation.JsonSerialize;+import org.openremote.model.asset.agent.AgentLink;++import java.util.Optional;++public class MQTTAgentLink extends AgentLink<MQTTAgentLink> {++ protected String subscriptionTopic;+ protected String publishTopic;++ @JsonSerialize+ protected String getType() {+ return getClass().getSimpleName();+ }++ // For Hydrators+ protected MQTTAgentLink() {}++ protected MQTTAgentLink(String id) {+ super(id);+ }++ public Optional<String> getSubscriptionTopic() {+ return Optional.ofNullable(subscriptionTopic);+ }++ public MQTTAgentLink setSubscriptionTopic(String subscriptionTopic) {+ this.subscriptionTopic = subscriptionTopic;+ return this;+ }++ public Optional<String> getPublishTopic() {+ return Optional.ofNullable(publishTopic);+ }++ public MQTTAgentLink setPublishTopic(String publishTopic) {+ this.publishTopic = publishTopic;+ return this;+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTMessage.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTMessage.javanew file mode 100644index 000000000..eddc114fc--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTMessage.java@@ -0,0 +1,31 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.mqtt;++public class MQTTMessage<T> {++ protected String topic;+ protected T payload;++ public MQTTMessage(String topic, T payload) {+ this.topic = topic;+ this.payload = payload;+ }+}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.javanew file mode 100644index 000000000..070b9d859--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java@@ -0,0 +1,130 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.mqtt;++import org.apache.http.client.utils.URIBuilder;+import org.openremote.agent.protocol.io.ProtocolIOClient;+import org.openremote.container.util.UniqueIdentifierGenerator;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.attribute.AttributeState;+import org.openremote.model.protocol.ProtocolUtil;+import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.ValueUtil;++import java.net.URI;+import java.util.HashMap;+import java.util.Map;+import java.util.Optional;+import java.util.function.Consumer;+import java.util.logging.Logger;++import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;++public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTAgent, String, MQTT_IOClient, MQTTAgentLink> {++ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, MQTTProtocol.class);+ public static final String PROTOCOL_DISPLAY_NAME = "MQTT Client";+ protected final Map<AttributeRef, Consumer<MQTTMessage<String>>> protocolMessageConsumers = new HashMap<>();++ protected MQTTProtocol(MQTTAgent agent) {+ super(agent);+ }++ @Override+ protected void doLinkAttribute(String assetId, Attribute<?> attribute, MQTTAgentLink agentLink) throws RuntimeException {+ agentLink.getSubscriptionTopic().ifPresent(topic -> {+ Consumer<MQTTMessage<String>> messageConsumer = msg -> updateLinkedAttribute(+ new AttributeState(assetId, attribute.getName(), msg.payload)+ );+ client.ioClient.addMessageConsumer(topic, messageConsumer);+ protocolMessageConsumers.put(new AttributeRef(assetId, attribute.getName()), messageConsumer);+ });+ }++ @Override+ protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, MQTTAgentLink agentLink) {+ agentLink.getSubscriptionTopic().ifPresent(topic -> {+ AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());+ Consumer<MQTTMessage<String>> messageConsumer = protocolMessageConsumers.remove(attributeRef);+ if (messageConsumer != null) {+ client.ioClient.removeMessageConsumer(topic, messageConsumer);+ }+ });+ }++ @Override+ protected ProtocolIOClient<MQTTMessage<String>, MQTT_IOClient> createIoClient() throws Exception {+ MQTT_IOClient client = doCreateIoClient();+ ProtocolIOClient<MQTTMessage<String>, MQTT_IOClient> protocolIoClient = new ProtocolIOClient<>(client, this::onConnectionStatusChanged, null);+ this.client = protocolIoClient;+ return protocolIoClient;+ }+++ @Override+ protected MQTT_IOClient doCreateIoClient() throws Exception {+ String host = agent.getHost().orElse(null);+ int port = agent.getPort().orElseGet(() -> {+ if (agent.isSecureMode().orElse(false)) {+ return agent.isWebsocketMode().orElse(false) ? 443 : 8883;+ } else {+ return agent.isWebsocketMode().orElse(false) ? 80 : 1883;+ }+ });++ URI websocketURI = null;++ if (agent.isWebsocketMode().orElse(false)) {+ URIBuilder builder = new URIBuilder()+ .setHost(host)+ .setPort(port);+ agent.getWebsocketPath().ifPresent(builder::setPath);+ agent.getWebsocketQuery().map(query -> query.startsWith("?") ? query.substring(1) : query).ifPresent(builder::setCustomQuery);+ websocketURI = builder.build();+ }++ return new MQTT_IOClient(agent.getClientId().orElseGet(UniqueIdentifierGenerator::generateId), host, port, agent.isSecureMode().orElse(false), agent.getUsernamePassword().orElse(null), websocketURI);+ }++ @Override+ protected void onMessageReceived(MQTTMessage<String> message) {+ // This isn't used instead messages are targeted by topic+ }++ @Override+ protected MQTTMessage<String> createWriteMessage(Attribute<?> attribute, MQTTAgentLink agentLink, AttributeEvent event, Object processedValue) {+ Optional<String> topic = agentLink.getPublishTopic();++ if (!topic.isPresent()) {+ LOG.fine(prefixLogMessage("Publish topic is not set in agent link so cannot publish message"));+ return null;+ }++ String valueStr = ValueUtil.convert(processedValue, String.class);+ return new MQTTMessage<>(topic.get(), valueStr);+ }++ @Override+ public String getProtocolName() {+ return "MQTT Client";+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTT_IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTT_IOClient.javanew file mode 100644index 000000000..65264ff8a--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTT_IOClient.java@@ -0,0 +1,46 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.agent.protocol.mqtt;++import org.openremote.model.auth.UsernamePassword;++import java.net.URI;+import java.nio.charset.StandardCharsets;++public class MQTT_IOClient extends AbstractMQTT_IOClient<String> {++ protected MQTT_IOClient(String host, int port, boolean secure, UsernamePassword usernamePassword, URI websocketURI) {+ super(host, port, secure, usernamePassword, websocketURI);+ }++ protected MQTT_IOClient(String clientId, String host, int port, boolean secure, UsernamePassword usernamePassword, URI websocketURI) {+ super(clientId, host, port, secure, usernamePassword, websocketURI);+ }++ @Override+ public byte[] messageToBytes(String message) {+ return message != null ? message.getBytes(StandardCharsets.UTF_8) : new byte[0];+ }++ @Override+ public String messageFromBytes(byte[] bytes) {+ return new String(bytes);+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialProtocol.javaindex 54d4ab898..e6270bd60 100644--- a/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialProtocol.java@@ -19,7 +19,7 @@*/package org.openremote.agent.protocol.serial;-import org.openremote.agent.protocol.io.AbstractIOClientProtocol;+import org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol;import org.openremote.agent.protocol.io.IOAgent;import org.openremote.agent.protocol.io.IOClient;import org.openremote.model.asset.agent.AgentLink;@@ -28,7 +28,7 @@ import org.openremote.model.asset.agent.AgentLink;* This is an abstract TCP client protocol for communicating with TCP servers; concrete implementations must provide* an {@link IOClient <T> for handling over the wire communication}.*/-public abstract class AbstractSerialProtocol<T extends AbstractIOClientProtocol<T, U, W, X, V>, U extends IOAgent<U, T, V>, V extends AgentLink<?>, W, X extends SerialIOClient<W>> extends AbstractIOClientProtocol<T, U, W, X, V> {+public abstract class AbstractSerialProtocol<T extends AbstractNettyIOClientProtocol<T, U, W, X, V>, U extends IOAgent<U, T, V>, V extends AgentLink<?>, W, X extends SerialIOClient<W>> extends AbstractNettyIOClientProtocol<T, U, W, X, V> {protected AbstractSerialProtocol(U agent) {super(agent);diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTCPClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTCPClientProtocol.javaindex b40b890d5..655e60f9f 100644--- a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTCPClientProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTCPClientProtocol.java@@ -19,7 +19,7 @@*/package org.openremote.agent.protocol.tcp;-import org.openremote.agent.protocol.io.AbstractIOClientProtocol;+import org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol;import org.openremote.agent.protocol.io.IOAgent;import org.openremote.agent.protocol.io.IOClient;import org.openremote.model.asset.agent.Agent;@@ -29,7 +29,7 @@ import org.openremote.model.asset.agent.AgentLink;* This is an abstract TCP client protocol for communicating with TCP servers; concrete implementations must provide* an {@link IOClient <T> for handling over the wire communication}.*/-public abstract class AbstractTCPClientProtocol<T extends AbstractIOClientProtocol<T, U, W, X, V>, U extends IOAgent<U, T, V>, V extends AgentLink<?>, W, X extends TCPIOClient<W>> extends AbstractIOClientProtocol<T, U, W, X, V> {+public abstract class AbstractTCPClientProtocol<T extends AbstractNettyIOClientProtocol<T, U, W, X, V>, U extends IOAgent<U, T, V>, V extends AgentLink<?>, W, X extends TCPIOClient<W>> extends AbstractNettyIOClientProtocol<T, U, W, X, V> {protected AbstractTCPClientProtocol(U agent) {super(agent);diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUDPProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUDPProtocol.javaindex 964471257..39736da82 100644--- a/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUDPProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUDPProtocol.java@@ -19,7 +19,7 @@*/package org.openremote.agent.protocol.udp;-import org.openremote.agent.protocol.io.AbstractIOClientProtocol;+import org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol;import org.openremote.agent.protocol.io.IOAgent;import org.openremote.model.asset.agent.AgentLink;@@ -27,7 +27,7 @@ import org.openremote.model.asset.agent.AgentLink;* This is an abstract UDP client protocol for communicating with UDP servers; concrete implementations must implement* {@link #getEncoderDecoderProvider} to provide encoders/decoders for messages of type <T>.*/-public abstract class AbstractUDPProtocol<T extends AbstractIOClientProtocol<T, U, W, X, V>, U extends IOAgent<U, T, V>, V extends AgentLink<?>, W, X extends UDPIOClient<W>> extends AbstractIOClientProtocol<T, U, W, X, V> {+public abstract class AbstractUDPProtocol<T extends AbstractNettyIOClientProtocol<T, U, W, X, V>, U extends IOAgent<U, T, V>, V extends AgentLink<?>, W, X extends UDPIOClient<W>> extends AbstractNettyIOClientProtocol<T, U, W, X, V> {protected AbstractUDPProtocol(U agent) {super(agent);diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.javaindex 029d0a1fd..679263129 100644--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.java@@ -24,7 +24,7 @@ import org.apache.http.HttpHeaders;import org.jboss.resteasy.client.jaxrs.ResteasyClient;import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;import org.jboss.resteasy.util.BasicAuthHelper;-import org.openremote.agent.protocol.io.AbstractIOClientProtocol;+import org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol;import org.openremote.container.web.WebTargetBuilder;import org.openremote.model.Container;import org.openremote.model.asset.agent.ConnectionStatus;@@ -65,10 +65,10 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;* <h2>Protocol Specifics</h2>* When the websocket connection is established it is possible to subscribe to events by specifying the* {@link WebsocketAgent#CONNECT_SUBSCRIPTIONS} on the {@link WebsocketAgent} or- * {@link WebsocketAgentLink#getConnectSubscriptions()} on linked {@link Attribute}s; a+ * {@link WebsocketAgentLink#getWebsocketSubscriptions()} on linked {@link Attribute}s; a* subscription can be a message sent over the websocket or a HTTP REST API call.*/-public class WebsocketAgentProtocol extends AbstractIOClientProtocol<WebsocketAgentProtocol, WebsocketAgent, String, WebsocketIOClient<String>, WebsocketAgentLink> {+public class WebsocketAgentProtocol extends AbstractNettyIOClientProtocol<WebsocketAgentProtocol, WebsocketAgent, String, WebsocketIOClient<String>, WebsocketAgentLink> {public static final String PROTOCOL_DISPLAY_NAME = "Websocket Client";private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketAgentProtocol.class);diff --git a/container/src/main/java/org/openremote/container/security/keycloak/SimpleKeycloakServletExtension.java b/container/src/main/java/org/openremote/container/security/keycloak/SimpleKeycloakServletExtension.javaindex 831dc895c..a9684dfff 100644--- a/container/src/main/java/org/openremote/container/security/keycloak/SimpleKeycloakServletExtension.java+++ b/container/src/main/java/org/openremote/container/security/keycloak/SimpleKeycloakServletExtension.java@@ -47,16 +47,17 @@ public class SimpleKeycloakServletExtension implements ServletExtension {protected static Logger log = Logger.getLogger(KeycloakServletExtension.class);final protected KeycloakConfigResolver configResolver;+ final protected AdapterDeploymentContext deploymentContext;public SimpleKeycloakServletExtension(KeycloakConfigResolver configResolver) {this.configResolver = configResolver;+ deploymentContext = new AdapterDeploymentContext(configResolver);}@Override@SuppressWarnings("UseSpecificCatch")public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {- AdapterDeploymentContext deploymentContext = new AdapterDeploymentContext(configResolver);servletContext.setAttribute(AdapterDeploymentContext.class.getName(), deploymentContext);UndertowUserSessionManagement userSessionManagement = new UndertowUserSessionManagement();final NodesRegistrationManagement nodesRegistrationManagement = new NodesRegistrationManagement();diff --git a/gradle.properties b/gradle.propertiesindex 05c171ff2..2db1a634c 100644--- a/gradle.properties+++ b/gradle.properties@@ -35,7 +35,7 @@ uuidGeneratorVersion = 3.1.3jSerialCommVersion = 2.7.0saxonHEVersion = 9.8.0-3jmxtermVersion = 1.0.0-nettyVersion = 4.1.56.Final+nettyVersion = 4.1.69.FinalflywaydbVersion = 6.4.3ical4jVersion = 2.1.5jafamaVersion = 2.3.1@@ -53,3 +53,4 @@ hibernateTypesVersion = 2.10.0reflectionsVersion=0.9.12swaggerVersion=2.1.9jsonSchemaVersion=4.18.0+hiveMQClientVersion=1.2.2diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.javaindex 8f326c472..d3341ce98 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java@@ -112,7 +112,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());- if(!connection.realm.equals(topicTokens.get(0))) {+ if (!connection.realm.equals(topicTokens.get(0))) {LOG.warning("Topic should start with the realm of the connection: " + connection);return false;}diff --git a/test/src/main/java/org/openremote/test/protocol/velbus/MockVelbusClient.java b/test/src/main/java/org/openremote/test/protocol/velbus/MockVelbusClient.javaindex f0705e742..7833874bd 100644--- a/test/src/main/java/org/openremote/test/protocol/velbus/MockVelbusClient.java+++ b/test/src/main/java/org/openremote/test/protocol/velbus/MockVelbusClient.java@@ -21,6 +21,7 @@ package org.openremote.test.protocol.velbus;import io.netty.channel.ChannelHandler;import org.openremote.agent.protocol.io.IOClient;+import org.openremote.agent.protocol.io.NettyIOClient;import org.openremote.agent.protocol.velbus.VelbusPacket;import org.openremote.model.asset.agent.ConnectionStatus;@@ -30,7 +31,7 @@ import java.util.Map;import java.util.function.Consumer;import java.util.function.Supplier;-public class MockVelbusClient implements IOClient<VelbusPacket> {+public class MockVelbusClient implements NettyIOClient<VelbusPacket> {protected final List<Consumer<VelbusPacket>> messageConsumers = new ArrayList<>();protected final List<Consumer<ConnectionStatus>> statusConsumers = new ArrayList<>();protected List<String> sentMessages = new ArrayList<>();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.groovynew file mode 100644index 000000000..2fa3f085b--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy@@ -0,0 +1,157 @@+/*+ * Copyright 2019, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.test.protocol.mqtt++import io.moquette.BrokerConstants+import org.openremote.agent.protocol.mqtt.MQTTAgent+import org.openremote.agent.protocol.mqtt.MQTTAgentLink+import org.openremote.agent.protocol.mqtt.MQTTProtocol+import org.openremote.agent.protocol.simulator.SimulatorProtocol+import org.openremote.agent.protocol.websocket.WebsocketAgentLink+import org.openremote.agent.protocol.websocket.WebsocketHTTPSubscription+import org.openremote.agent.protocol.websocket.WebsocketSubscription+import org.openremote.agent.protocol.websocket.WebsocketSubscriptionImpl+import org.openremote.container.util.UniqueIdentifierGenerator+import org.openremote.manager.agent.AgentService+import org.openremote.manager.asset.AssetProcessingService+import org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.event.ClientEventService+import org.openremote.manager.setup.SetupService+import org.openremote.model.Constants+import org.openremote.model.asset.ReadAttributeEvent+import org.openremote.model.asset.agent.Agent+import org.openremote.model.asset.agent.ConnectionStatus+import org.openremote.model.asset.agent.Protocol+import org.openremote.model.asset.impl.ThingAsset+import org.openremote.model.attribute.Attribute+import org.openremote.model.attribute.AttributeEvent+import org.openremote.model.attribute.MetaItem+import org.openremote.model.auth.UsernamePassword+import org.openremote.model.event.TriggeredEventSubscription+import org.openremote.model.event.shared.SharedEvent+import org.openremote.model.query.filter.StringPredicate+import org.openremote.model.util.ValueUtil+import org.openremote.model.value.JsonPathFilter+import org.openremote.model.value.RegexValueFilter+import org.openremote.model.value.SubStringValueFilter+import org.openremote.model.value.ValueFilter+import org.openremote.test.ManagerContainerTrait+import org.openremote.test.setup.KeycloakTestSetup+import org.openremote.test.setup.ManagerTestSetup+import spock.lang.Specification+import spock.util.concurrent.PollingConditions++import javax.ws.rs.core.MediaType++import static org.openremote.container.util.MapAccess.getInteger+import static org.openremote.container.util.MapAccess.getString+import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_HOST+import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_PORT+import static org.openremote.model.value.MetaItemType.AGENT_LINK+import static org.openremote.model.value.ValueType.NUMBER++class MQTTClientProtocolTest extends Specification implements ManagerContainerTrait {++ @SuppressWarnings("GroovyAccessibility")+ def "Check websocket client protocol and linked attribute deployment"() {++ given: "expected conditions"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)++ and: "the container starts"+ def container = startContainer(defaultConfig(), defaultServices())+ def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)+ def agentService = container.getService(AgentService.class)+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def clientEventService = container.getService(ClientEventService.class)+ def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST)+ def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT)++ when: "an MQTT client agent is created to connect to this tests manager"+ def clientId = UniqueIdentifierGenerator.generateId()+ def agent = new MQTTAgent("Test agent")+ .setRealm(Constants.MASTER_REALM)+ .setClientId(clientId)+ .setHost(mqttHost)+ .setPort(mqttPort)+ .setUsernamePassword(new UsernamePassword(keycloakTestSetup.tenantBuilding.realm + ":" + KeycloakTestSetup.serviceUserId, KeycloakTestSetup.serviceUserSecret))++ 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 asset = new ThingAsset("Test Asset")+ .setParent(agent)+ .addOrReplaceAttributes(+ // write attribute value+ new Attribute<>("readWriteTargetTemp", NUMBER)+ .addMeta(+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)+ .setSubscriptionTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/${managerTestSetup.apartment1LivingroomId}/targetTemperature")+ .setPublishTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/${managerTestSetup.apartment1LivingroomId}/targetTemperature")+ .setWriteValue("${Protocol.DYNAMIC_VALUE_PLACEHOLDER}")+ ))+ )++ and: "the asset is merged into the asset service"+ asset = assetStorageService.merge(asset)++ then: "the linked attributes should be correctly linked"+ conditions.eventually {+ assert !(agentService.getProtocolInstance(agent.id) as MQTTProtocol).protocolMessageConsumers.isEmpty()+ }++ when: "the source attribute are updated"+ ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(new AttributeEvent(managerTestSetup.apartment1LivingroomId, "targetTemperature", 99d))++ then: "the values should be stored in the database"+ conditions.eventually {+ def livingRoom = assetStorageService.find(managerTestSetup.apartment1LivingroomId)+ assert livingRoom != null+ assert livingRoom.getAttribute("targetTemperature", Double.class).flatMap{it.value}.orElse(0d) == 99d+ }++ then: "the linked attributes should also have the updated values of the subscribed attributes"+ conditions.eventually {+ asset = assetStorageService.find(asset.getId(), true)+ assert asset.getAttribute("readWriteTargetTemp").get().getValue().orElse(null) == 99d+ }++ when: "a linked attribute value is updated"+ def attributeEvent = new AttributeEvent(asset.id,+ "readWriteTargetTemp",+ 19.5)+ assetProcessingService.sendAttributeEvent(attributeEvent)++ then: "the linked targetTemperature attribute should contain this written value (it should have been written to the target temp attribute and then read back again)"+ conditions.eventually {+ asset = assetStorageService.find(asset.getId(), true)+ assert asset.getAttribute("readWriteTargetTemp", Double.class).flatMap{it.getValue()}.orElse(null) == 19.5d+ }+ }+}