]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt] Support birth and shutdown message (#12152)
authorjimtng <2554958+jimtng@users.noreply.github.com>
Thu, 3 Feb 2022 11:42:54 +0000 (21:42 +1000)
committerGitHub <noreply@github.com>
Thu, 3 Feb 2022 11:42:54 +0000 (12:42 +0100)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
bundles/org.openhab.binding.mqtt/README.md
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/handler/BrokerHandler.java
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/handler/BrokerHandlerConfig.java
bundles/org.openhab.binding.mqtt/src/main/resources/OH-INF/thing/thing-types.xml

index 89544c24cce46958ac729325ad49f4c6129c32a3..b41323a04c9ea9c6e27be82803c11c99aa1183c3 100644 (file)
@@ -38,10 +38,22 @@ Reconnect parameters are:
 
 An MQTT last will and testament can be configured:
 
-* __lwtMessage__: An optional last will and testament message. Defaults to empty. 
-* __lwtTopic__: The last will topic. Defaults to empty and therefore disables the last will. 
-* __lwtQos__: The optional qos of the last will. Defaults to 0. 
-* __lwtRetain__: Retain last will message. Defaults to false.
+* __lwtMessage__: An optional last will and testament message. Defaults to empty.
+* __lwtTopic__: The last will topic. Defaults to empty and therefore disables the last will.
+* __lwtQos__: The optional qos of the last will. Defaults to 0.
+* __lwtRetain__: Retain last will message. Defaults to true.
+
+An MQTT message can be published upon a successful connection to the MQTT broker with these parameters:
+
+* __birthMessage__: An optional message to be published once the bridge established a connection to the MQTT broker. Defaults to empty.
+* __birthTopic__: The birth topic. Defaults to empty and therefore no birth message will be published.
+* __birthRetain__: Retain the birth message. Defaults to true.
+
+An MQTT message can be published just before disconnecting from the broker with these parameters:
+
+* __shutdownMessage__: An optional message to be published before the bridge disconnects from the MQTT broker. Defaults to empty.
+* __shutdownTopic__: The shutdown topic. Defaults to empty and therefore no shutdown message will be published.
+* __shutdownRetain__: Retain the shutdown message. Defaults to true.
 
 For more security, the following optional parameters can be altered:
 
index c5eb7345c86a2ea5a006d51e0da4db492afd5692..d7cf35686a639a65f9e2b8c8479b2cc7df441a90 100644 (file)
@@ -13,6 +13,7 @@
 package org.openhab.binding.mqtt.handler;
 
 import java.security.NoSuchAlgorithmException;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
@@ -44,6 +45,7 @@ import org.slf4j.LoggerFactory;
  * connection to the {@link MqttService}.
  *
  * @author David Graeff - Initial contribution
+ * @author Jimmy Tanagra - Add birth and shutdown message
  */
 @NonNullByDefault
 public class BrokerHandler extends AbstractBrokerHandler implements PinnedCallback {
@@ -60,12 +62,16 @@ public class BrokerHandler extends AbstractBrokerHandler implements PinnedCallba
         // Store generated client ID if none was set by the user
         final MqttBrokerConnection connection = this.connection;
         String clientID = config.clientID;
-        if (connection != null && state == MqttConnectionState.CONNECTED && (clientID == null || clientID.isBlank())) {
-            clientID = connection.getClientId();
-            config.clientID = clientID;
-            Configuration editConfig = editConfiguration();
-            editConfig.put("clientid", clientID);
-            updateConfiguration(editConfig);
+        if (connection != null && state == MqttConnectionState.CONNECTED) {
+            if (clientID == null || clientID.isBlank()) {
+                clientID = connection.getClientId();
+                config.clientID = clientID;
+                Configuration editConfig = editConfiguration();
+                editConfig.put("clientid", clientID);
+                updateConfiguration(editConfig);
+            } else {
+                publish(config.birthTopic, config.birthMessage, config.birthRetain);
+            }
         }
     }
 
@@ -114,6 +120,8 @@ public class BrokerHandler extends AbstractBrokerHandler implements PinnedCallba
     public void dispose() {
         try {
             if (connection != null) {
+                publish(config.shutdownTopic, config.shutdownMessage, config.shutdownRetain).get(1000,
+                        TimeUnit.MILLISECONDS);
                 connection.stop().get(1000, TimeUnit.MILLISECONDS);
             } else {
                 logger.warn("Trying to dispose handler {} but connection is already null. Most likely this is a bug.",
@@ -236,4 +244,15 @@ public class BrokerHandler extends AbstractBrokerHandler implements PinnedCallba
 
         super.initialize();
     }
+
+    /**
+     * Calls the @NonNull MqttBrokerConnection::publish() with @Nullable topic and message
+     */
+    private CompletableFuture<Boolean> publish(@Nullable String topic, @Nullable String message, boolean retain) {
+        if (topic == null || connection == null) {
+            return CompletableFuture.completedFuture(true);
+        }
+        String nonNullMessage = message != null ? message : "";
+        return connection.publish(topic, nonNullMessage.getBytes(), connection.getQos(), retain);
+    }
 }
index 5c8a2727bc932b2894b3078ddf1582c58cbcd2f7..170078fa1a66fb6fe016fc7535eff9c2543c3b08 100644 (file)
@@ -36,4 +36,14 @@ public class BrokerHandlerConfig extends MqttBrokerConnectionConfig {
     public String publickey = "";
 
     public boolean enableDiscovery = true;
+
+    // Birth message parameters
+    public @Nullable String birthTopic;
+    public @Nullable String birthMessage;
+    public Boolean birthRetain = true;
+
+    // Shutdown message parameters
+    public @Nullable String shutdownTopic;
+    public @Nullable String shutdownMessage;
+    public Boolean shutdownRetain = true;
 }
index 9c7a16ef3df794372ef7eb578514db65abdbb335..0f9f5a68aa53c726edf55c227cc7d493cba34b12 100644 (file)
                                <advanced>true</advanced>
                        </parameter>
 
+                       <parameter name="birthMessage" type="text">
+                               <label>Birth Message</label>
+                               <description>The message to send to the broker when a connection is established.</description>
+                               <advanced>true</advanced>
+                       </parameter>
+
+                       <parameter name="birthTopic" type="text">
+                               <label>Birth Topic</label>
+                               <description>Defaults to empty and therefore disables the birth message.</description>
+                               <advanced>true</advanced>
+                       </parameter>
+
+                       <parameter name="birthRetain" type="boolean">
+                               <label>Birth Message Retain</label>
+                               <description>True if the birth message should be retained (defaults to true)</description>
+                               <default>true</default>
+                               <advanced>true</advanced>
+                       </parameter>
+
+                       <parameter name="shutdownMessage" type="text">
+                               <label>Shutdown Message</label>
+                               <description>The message to send to the broker before the connection terminates.</description>
+                               <advanced>true</advanced>
+                       </parameter>
+
+                       <parameter name="shutdownTopic" type="text">
+                               <label>Shutdown Topic</label>
+                               <description>Defaults to empty and therefore disables the shutdown message.</description>
+                               <advanced>true</advanced>
+                       </parameter>
+
+                       <parameter name="shutdownRetain" type="boolean">
+                               <label>Shutdown Message Retain</label>
+                               <description>True if the shutdown message should be retained (defaults to true)</description>
+                               <default>false</default>
+                               <advanced>true</advanced>
+                       </parameter>
+
                        <parameter name="username" type="text">
                                <label>Username</label>
                                <description>The MQTT username</description>