2 * Copyright (c) 2010-2021 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.generic;
15 import java.nio.charset.StandardCharsets;
16 import java.util.ArrayList;
17 import java.util.IllegalFormatException;
18 import java.util.List;
19 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.mqtt.generic.values.TextValue;
27 import org.openhab.binding.mqtt.generic.values.Value;
28 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
29 import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.TypeParser;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This object consists of an {@link Value}, which is updated on the respective MQTT topic change.
39 * Updates to the value are propagated via the {@link ChannelStateUpdateListener}.
41 * @author David Graeff - Initial contribution
44 public class ChannelState implements MqttMessageSubscriber {
45 private final Logger logger = LoggerFactory.getLogger(ChannelState.class);
47 // Immutable channel configuration
48 protected final boolean readOnly;
49 protected final ChannelUID channelUID;
50 protected final ChannelConfig config;
53 protected final Value cachedValue;
56 private @Nullable MqttBrokerConnection connection;
57 protected final List<ChannelStateTransformation> transformationsIn = new ArrayList<>();
58 protected final List<ChannelStateTransformation> transformationsOut = new ArrayList<>();
59 private @Nullable ChannelStateUpdateListener channelStateUpdateListener;
60 protected boolean hasSubscribed = false;
61 private @Nullable ScheduledFuture<?> scheduledFuture;
62 private CompletableFuture<@Nullable Void> future = CompletableFuture.completedFuture(null);
63 private final Object futureLock = new Object();
66 * Creates a new channel state.
68 * @param config The channel configuration
69 * @param channelUID The channelUID is used for the {@link ChannelStateUpdateListener} to notify about value changes
70 * @param cachedValue MQTT only notifies us once about a value, during the subscribe. The channel state therefore
71 * needs a cache for the current value.
72 * @param channelStateUpdateListener A channel state update listener
74 public ChannelState(ChannelConfig config, ChannelUID channelUID, Value cachedValue,
75 @Nullable ChannelStateUpdateListener channelStateUpdateListener) {
77 this.channelStateUpdateListener = channelStateUpdateListener;
78 this.channelUID = channelUID;
79 this.cachedValue = cachedValue;
80 this.readOnly = config.commandTopic.isBlank();
83 public boolean isReadOnly() {
88 * Add a transformation that is applied for each received MQTT topic value.
89 * The transformations are executed in order.
91 * @param transformation A transformation
93 public void addTransformation(ChannelStateTransformation transformation) {
94 transformationsIn.add(transformation);
98 * Add a transformation that is applied for each value to be published.
99 * The transformations are executed in order.
101 * @param transformation A transformation
103 public void addTransformationOut(ChannelStateTransformation transformation) {
104 transformationsOut.add(transformation);
108 * Clear transformations
110 public void clearTransformations() {
111 transformationsIn.clear();
112 transformationsOut.clear();
116 * Returns the cached value state object of this message subscriber.
118 * MQTT only notifies us once about a value, during the subscribe.
119 * The channel state therefore needs a cache for the current value.
120 * If MQTT has not yet published a value, the cache might still be in UNDEF state.
123 public Value getCache() {
128 * Return the channelUID
130 public ChannelUID channelUID() {
135 * Incoming message from the MqttBrokerConnection
137 * @param topic The topic. Is the same as the field stateTopic.
138 * @param payload The byte payload. Must be UTF8 encoded text or binary data.
141 public void processMessage(String topic, byte[] payload) {
142 final ChannelStateUpdateListener channelStateUpdateListener = this.channelStateUpdateListener;
143 if (channelStateUpdateListener == null) {
144 logger.warn("MQTT message received for topic {}, but MessageSubscriber object hasn't been started!", topic);
148 if (cachedValue.isBinary()) {
149 cachedValue.update(payload);
150 channelStateUpdateListener.updateChannelState(channelUID, cachedValue.getChannelState());
155 // String value: Apply transformations
156 String strValue = new String(payload, StandardCharsets.UTF_8);
157 for (ChannelStateTransformation t : transformationsIn) {
158 String transformedValue = t.processValue(strValue);
159 if (transformedValue != null) {
160 strValue = transformedValue;
162 logger.debug("Transformation '{}' returned null on '{}', discarding message", strValue, t.serviceName);
168 // Is trigger?: Special handling
169 if (config.trigger) {
170 channelStateUpdateListener.triggerChannel(channelUID, strValue);
175 Command command = TypeParser.parseCommand(cachedValue.getSupportedCommandTypes(), strValue);
176 if (command == null) {
177 logger.warn("Incoming payload '{}' not supported by type '{}'", strValue,
178 cachedValue.getClass().getSimpleName());
183 Command postOnlyCommand = cachedValue.isPostOnly(command);
184 if (postOnlyCommand != null) {
185 channelStateUpdateListener.postChannelCommand(channelUID, postOnlyCommand);
190 // Map the string to a command, update the cached value and post the command to the framework
192 cachedValue.update(command);
193 } catch (IllegalArgumentException | IllegalStateException e) {
194 logger.warn("Command '{}' not supported by type '{}': {}", strValue, cachedValue.getClass().getSimpleName(),
200 if (config.postCommand) {
201 channelStateUpdateListener.postChannelCommand(channelUID, (Command) cachedValue.getChannelState());
203 channelStateUpdateListener.updateChannelState(channelUID, cachedValue.getChannelState());
209 * Returns the state topic. Might be an empty string if this is a stateless channel (TRIGGER kind channel).
211 public String getStateTopic() {
212 return config.stateTopic;
216 * Return the command topic. Might be an empty string, if this is a read-only channel.
218 public String getCommandTopic() {
219 return config.commandTopic;
223 * Returns the channelType ID which also happens to be an item-type
225 public String getItemType() {
226 return cachedValue.getItemType();
230 * Returns true if this is a stateful channel.
232 public boolean isStateful() {
233 return config.retained;
237 * Removes the subscription to the state topic and resets the channelStateUpdateListener.
239 * @return A future that completes with true if unsubscribing from the state topic succeeded.
240 * It completes with false if no connection is established and completes exceptionally otherwise.
242 public CompletableFuture<@Nullable Void> stop() {
243 final MqttBrokerConnection connection = this.connection;
244 if (connection != null && !config.stateTopic.isBlank()) {
245 return connection.unsubscribe(config.stateTopic, this).thenRun(this::internalStop);
248 return CompletableFuture.completedFuture(null);
252 private void internalStop() {
253 logger.debug("Unsubscribed channel {} form topic: {}", this.channelUID, config.stateTopic);
254 this.connection = null;
255 this.channelStateUpdateListener = null;
256 hasSubscribed = false;
257 cachedValue.resetState();
260 private void receivedOrTimeout() {
261 final ScheduledFuture<?> scheduledFuture = this.scheduledFuture;
262 if (scheduledFuture != null) { // Cancel timeout
263 scheduledFuture.cancel(false);
264 this.scheduledFuture = null;
266 future.complete(null);
269 private @Nullable Void subscribeFail(Throwable e) {
270 final ScheduledFuture<?> scheduledFuture = this.scheduledFuture;
271 if (scheduledFuture != null) { // Cancel timeout
272 scheduledFuture.cancel(false);
273 this.scheduledFuture = null;
275 future.completeExceptionally(e);
280 * Subscribes to the state topic on the given connection and informs about updates on the given listener.
282 * @param connection A broker connection
283 * @param scheduler A scheduler to realize the timeout
284 * @param timeout A timeout in milliseconds. Can be 0 to disable the timeout and let the future return earlier.
285 * @return A future that completes with true if the subscribing worked, with false if the stateTopic is not set
286 * and exceptionally otherwise.
288 public CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
290 synchronized (futureLock) {
291 // if the connection is still the same, the subscription is still present, otherwise we need to renew
292 if ((hasSubscribed || !future.isDone()) && connection.equals(this.connection)) {
295 hasSubscribed = false;
297 this.connection = connection;
299 if (config.stateTopic.isBlank()) {
300 return CompletableFuture.completedFuture(null);
303 this.future = new CompletableFuture<>();
305 connection.subscribe(config.stateTopic, this).thenRun(() -> {
306 hasSubscribed = true;
307 logger.debug("Subscribed channel {} to topic: {}", this.channelUID, config.stateTopic);
308 if (timeout > 0 && !future.isDone()) {
309 this.scheduledFuture = scheduler.schedule(this::receivedOrTimeout, timeout, TimeUnit.MILLISECONDS);
313 }).exceptionally(this::subscribeFail);
318 * Return true if this channel has subscribed to its MQTT topics.
319 * You need to call {@link #start(MqttBrokerConnection, ScheduledExecutorService, int)} and
320 * have a stateTopic set, to subscribe this channel.
322 public boolean hasSubscribed() {
323 return this.hasSubscribed;
327 * Publishes a value on MQTT. A command topic needs to be set in the configuration.
329 * @param command The command to send
330 * @return A future that completes with true if the publishing worked and false if it is a readonly topic
331 * and exceptionally otherwise.
333 public CompletableFuture<Boolean> publishValue(Command command) {
334 cachedValue.update(command);
336 Value mqttCommandValue = cachedValue;
338 final MqttBrokerConnection connection = this.connection;
340 if (connection == null) {
341 CompletableFuture<Boolean> f = new CompletableFuture<>();
342 f.completeExceptionally(new IllegalStateException(
343 "The connection object has not been set. start() should have been called!"));
349 "You have tried to publish {} to the mqtt topic '{}' that was marked read-only. You can't 'set' anything on a sensor state topic for example.",
350 mqttCommandValue, config.commandTopic);
351 return CompletableFuture.completedFuture(false);
354 // Outgoing transformations
355 for (ChannelStateTransformation t : transformationsOut) {
356 String commandString = mqttCommandValue.getMQTTpublishValue(null);
357 String transformedValue = t.processValue(commandString);
358 if (transformedValue != null) {
359 Value textValue = new TextValue();
360 textValue.update(new StringType(transformedValue));
361 mqttCommandValue = textValue;
363 logger.debug("Transformation '{}' returned null on '{}', discarding message", mqttCommandValue,
365 return CompletableFuture.completedFuture(false);
369 String commandString;
371 // Formatter: Applied before the channel state value is published to the MQTT broker.
372 if (config.formatBeforePublish.length() > 0) {
374 commandString = mqttCommandValue.getMQTTpublishValue(config.formatBeforePublish);
375 } catch (IllegalFormatException e) {
376 logger.debug("Format pattern incorrect for {}", channelUID, e);
377 commandString = mqttCommandValue.getMQTTpublishValue(null);
380 commandString = mqttCommandValue.getMQTTpublishValue(null);
383 int qos = (config.qos != null) ? config.qos : connection.getQos();
385 return connection.publish(config.commandTopic, commandString.getBytes(), qos, config.retained);
389 * @return The channelStateUpdateListener
391 public @Nullable ChannelStateUpdateListener getChannelStateUpdateListener() {
392 return channelStateUpdateListener;
396 * @param channelStateUpdateListener The channelStateUpdateListener to set
398 public void setChannelStateUpdateListener(ChannelStateUpdateListener channelStateUpdateListener) {
399 this.channelStateUpdateListener = channelStateUpdateListener;
402 public @Nullable MqttBrokerConnection getConnection() {
407 * This is for tests only to inject a broker connection. Use
408 * {@link #start(MqttBrokerConnection, ScheduledExecutorService, int)} instead.
410 * @param connection MQTT Broker connection
412 public void setConnection(MqttBrokerConnection connection) {
413 this.connection = connection;