]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt] publishMQTT Action accepts a bytearray payload (#12170)
authorjimtng <2554958+jimtng@users.noreply.github.com>
Mon, 31 Jan 2022 23:51:46 +0000 (09:51 +1000)
committerGitHub <noreply@github.com>
Mon, 31 Jan 2022 23:51:46 +0000 (00:51 +0100)
* [mqtt] MQTT Action publishMQTT accept byte array

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
bundles/org.openhab.binding.mqtt.generic/README.md
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/internal/action/MQTTActions.java

index 788069084c7123909c5384275e2cd1878527ed44..eb679a04badf9d09f542d812c44f4a4299fd4eb1 100644 (file)
@@ -220,6 +220,7 @@ Once this action instance is retrieved, you can invoke the `publishMQTT(String t
 ```
 mqttActions.publishMQTT("mytopic","myvalue", true)
 ```
+Alternatively, `publishMQTT(String topic, byte[] value, Boolean retained)` can publish a byte array data.
 
 The retained argument is optional and if not supplied defaults to `false`.
 
index d867778549ac131aafef6038172a841ba8a761c0..dad6decf02ef24ed5090b3166c6e33bfc09c66d7 100644 (file)
@@ -57,6 +57,25 @@ public class MQTTActions implements ThingActions {
             @ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable final String topic,
             @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") @Nullable final String value,
             @ActionInput(name = "retain", label = "@text/actionInputRetainlabel", description = "@text/actionInputRetainDesc") @Nullable final Boolean retain) {
+        if (value == null) {
+            logger.debug("skipping MQTT publishing to topic '{}' due to null value.", topic);
+            return;
+        }
+        publishMQTT(topic, value.getBytes(), retain);
+    }
+
+    @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
+    public void publishMQTT(
+            @ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable final String topic,
+            @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") final byte[] value) {
+        publishMQTT(topic, value, null);
+    }
+
+    @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
+    public void publishMQTT(
+            @ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable final String topic,
+            @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") final byte[] value,
+            @ActionInput(name = "retain", label = "@text/actionInputRetainlabel", description = "@text/actionInputRetainDesc") @Nullable final Boolean retain) {
         AbstractBrokerHandler brokerHandler = handler;
         if (brokerHandler == null) {
             logger.warn("MQTT Action service ThingHandler is null!");
@@ -67,22 +86,17 @@ public class MQTTActions implements ThingActions {
             logger.warn("MQTT Action service ThingHandler connection is null!");
             return;
         }
-        if (value == null) {
-            logger.debug("skipping MQTT publishing to topic '{}' due to null value.", topic);
-            return;
-        }
         if (topic == null) {
             logger.debug("skipping MQTT publishing of value '{}' as topic is null.", value);
             return;
         }
 
-        connection.publish(topic, value.getBytes(), connection.getQos(), retain != null && retain.booleanValue())
-                .thenRun(() -> {
-                    logger.debug("MQTT publish to {} performed", topic);
-                }).exceptionally(e -> {
-                    logger.warn("MQTT publish to {} failed!", topic);
-                    return null;
-                });
+        connection.publish(topic, value, connection.getQos(), retain != null && retain.booleanValue()).thenRun(() -> {
+            logger.debug("MQTT publish to {} performed", topic);
+        }).exceptionally(e -> {
+            logger.warn("MQTT publish to {} failed!", topic);
+            return null;
+        });
     }
 
     public static void publishMQTT(ThingActions actions, @Nullable String topic, @Nullable String value) {
@@ -93,4 +107,13 @@ public class MQTTActions implements ThingActions {
             @Nullable Boolean retain) {
         ((MQTTActions) actions).publishMQTT(topic, value, retain);
     }
+
+    public static void publishMQTT(ThingActions actions, @Nullable String topic, byte[] value) {
+        publishMQTT(actions, topic, value, null);
+    }
+
+    public static void publishMQTT(ThingActions actions, @Nullable String topic, byte[] value,
+            @Nullable Boolean retain) {
+        ((MQTTActions) actions).publishMQTT(topic, value, retain);
+    }
 }