2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.internal.action;
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;
28 * This is the automation engine action handler service for the publishMQTT action.
30 * @author David Graeff - Initial contribution
32 @ThingActionsScope(name = "mqtt")
34 public class MQTTActions implements ThingActions {
35 private final Logger logger = LoggerFactory.getLogger(MQTTActions.class);
36 private @Nullable AbstractBrokerHandler handler;
39 public void setThingHandler(@Nullable ThingHandler handler) {
40 this.handler = (AbstractBrokerHandler) handler;
44 public @Nullable ThingHandler getThingHandler() {
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);
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) {
61 logger.debug("skipping MQTT publishing to topic '{}' due to null value.", topic);
64 publishMQTT(topic, value.getBytes(), retain);
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);
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!");
84 MqttBrokerConnection connection = brokerHandler.getConnection();
85 if (connection == null) {
86 logger.warn("MQTT Action service ThingHandler connection is null!");
90 logger.debug("skipping MQTT publishing of value '{}' as topic is null.", value);
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);
102 public static void publishMQTT(ThingActions actions, @Nullable String topic, @Nullable String value) {
103 publishMQTT(actions, topic, value, null);
106 public static void publishMQTT(ThingActions actions, @Nullable String topic, @Nullable String value,
107 @Nullable Boolean retain) {
108 ((MQTTActions) actions).publishMQTT(topic, value, retain);
111 public static void publishMQTT(ThingActions actions, @Nullable String topic, byte[] value) {
112 publishMQTT(actions, topic, value, null);
115 public static void publishMQTT(ThingActions actions, @Nullable String topic, byte[] value,
116 @Nullable Boolean retain) {
117 ((MQTTActions) actions).publishMQTT(topic, value, retain);