]> git.basschouten.com Git - openhab-addons.git/blob
d867778549ac131aafef6038172a841ba8a761c0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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         AbstractBrokerHandler brokerHandler = handler;
61         if (brokerHandler == null) {
62             logger.warn("MQTT Action service ThingHandler is null!");
63             return;
64         }
65         MqttBrokerConnection connection = brokerHandler.getConnection();
66         if (connection == null) {
67             logger.warn("MQTT Action service ThingHandler connection is null!");
68             return;
69         }
70         if (value == null) {
71             logger.debug("skipping MQTT publishing to topic '{}' due to null value.", topic);
72             return;
73         }
74         if (topic == null) {
75             logger.debug("skipping MQTT publishing of value '{}' as topic is null.", value);
76             return;
77         }
78
79         connection.publish(topic, value.getBytes(), connection.getQos(), retain != null && retain.booleanValue())
80                 .thenRun(() -> {
81                     logger.debug("MQTT publish to {} performed", topic);
82                 }).exceptionally(e -> {
83                     logger.warn("MQTT publish to {} failed!", topic);
84                     return null;
85                 });
86     }
87
88     public static void publishMQTT(ThingActions actions, @Nullable String topic, @Nullable String value) {
89         publishMQTT(actions, topic, value, null);
90     }
91
92     public static void publishMQTT(ThingActions actions, @Nullable String topic, @Nullable String value,
93             @Nullable Boolean retain) {
94         ((MQTTActions) actions).publishMQTT(topic, value, retain);
95     }
96 }