]> git.basschouten.com Git - openhab-addons.git/blob
c22716c6c4c5e9558e3a2a0216230a0471a3f6d8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * This is the automation engine action handler service for the publishMQTT action.
29  *
30  * @author David Graeff - Initial contribution
31  */
32 @ThingActionsScope(name = "mqtt")
33 @NonNullByDefault
34 public class MQTTActions implements ThingActions {
35     private final Logger logger = LoggerFactory.getLogger(MQTTActions.class);
36     private @Nullable AbstractBrokerHandler handler;
37
38     @Override
39     public void setThingHandler(@Nullable ThingHandler handler) {
40         this.handler = (AbstractBrokerHandler) handler;
41     }
42
43     @Override
44     public @Nullable ThingHandler getThingHandler() {
45         return handler;
46     }
47
48     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
49     public void publishMQTT(
50             @ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable String topic,
51             @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") @Nullable String value) {
52         publishMQTT(topic, value, null);
53     }
54
55     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
56     public void publishMQTT(
57             @ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable final String topic,
58             @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") @Nullable final String value,
59             @ActionInput(name = "retain", label = "@text/actionInputRetainlabel", description = "@text/actionInputRetainDesc") @Nullable final Boolean retain) {
60         if (value == null) {
61             logger.debug("skipping MQTT publishing to topic '{}' due to null value.", topic);
62             return;
63         }
64         publishMQTT(topic, value.getBytes(), retain);
65     }
66
67     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
68     public void publishMQTT(
69             @ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable final String topic,
70             @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") final byte[] value) {
71         publishMQTT(topic, value, null);
72     }
73
74     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
75     public void publishMQTT(
76             @ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable final String topic,
77             @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") final byte[] value,
78             @ActionInput(name = "retain", label = "@text/actionInputRetainlabel", description = "@text/actionInputRetainDesc") @Nullable final Boolean retain) {
79         AbstractBrokerHandler brokerHandler = handler;
80         if (brokerHandler == null) {
81             logger.warn("MQTT Action service ThingHandler is null!");
82             return;
83         }
84         MqttBrokerConnection connection = brokerHandler.getConnection();
85         if (connection == null) {
86             logger.warn("MQTT Action service ThingHandler connection is null!");
87             return;
88         }
89         if (topic == null) {
90             logger.debug("skipping MQTT publishing of value '{}' as topic is null.", value);
91             return;
92         }
93
94         connection.publish(topic, value, connection.getQos(), retain != null && retain.booleanValue()).thenRun(() -> {
95             logger.debug("MQTT publish to {} performed", topic);
96         }).exceptionally(e -> {
97             logger.warn("MQTT publish to {} failed!", topic);
98             return null;
99         });
100     }
101
102     public static void publishMQTT(ThingActions actions, @Nullable String topic, @Nullable String value) {
103         publishMQTT(actions, topic, value, null);
104     }
105
106     public static void publishMQTT(ThingActions actions, @Nullable String topic, @Nullable String value,
107             @Nullable Boolean retain) {
108         ((MQTTActions) actions).publishMQTT(topic, value, retain);
109     }
110
111     public static void publishMQTT(ThingActions actions, @Nullable String topic, byte[] value) {
112         publishMQTT(actions, topic, value, null);
113     }
114
115     public static void publishMQTT(ThingActions actions, @Nullable String topic, byte[] value,
116             @Nullable Boolean retain) {
117         ((MQTTActions) actions).publishMQTT(topic, value, retain);
118     }
119 }