2 * Copyright (c) 2010-2024 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.internal.handler;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Optional;
20 import java.util.concurrent.CompletableFuture;
21 import java.util.stream.Collectors;
23 import javax.measure.Unit;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.mqtt.generic.AbstractMQTTThingHandler;
28 import org.openhab.binding.mqtt.generic.ChannelConfig;
29 import org.openhab.binding.mqtt.generic.ChannelState;
30 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
31 import org.openhab.binding.mqtt.generic.MqttChannelStateDescriptionProvider;
32 import org.openhab.binding.mqtt.generic.internal.MqttBindingConstants;
33 import org.openhab.binding.mqtt.generic.utils.FutureCollector;
34 import org.openhab.binding.mqtt.generic.values.Value;
35 import org.openhab.binding.mqtt.generic.values.ValueFactory;
36 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
37 import org.openhab.core.thing.Channel;
38 import org.openhab.core.thing.ChannelUID;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.binding.ThingHandlerCallback;
43 import org.openhab.core.thing.binding.builder.ChannelBuilder;
44 import org.openhab.core.thing.binding.builder.ThingBuilder;
45 import org.openhab.core.thing.type.ChannelTypeUID;
46 import org.openhab.core.types.StateDescription;
47 import org.openhab.core.types.util.UnitUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * This handler manages manual created Things with manually added channels to link to MQTT topics.
54 * @author David Graeff - Initial contribution
57 public class GenericMQTTThingHandler extends AbstractMQTTThingHandler implements ChannelStateUpdateListener {
58 private final Logger logger = LoggerFactory.getLogger(GenericMQTTThingHandler.class);
59 final Map<ChannelUID, ChannelState> channelStateByChannelUID = new HashMap<>();
60 protected final MqttChannelStateDescriptionProvider stateDescProvider;
63 * Creates a new Thing handler for generic MQTT channels.
65 * @param thing The thing of this handler
66 * @param stateDescProvider A channel state provider
67 * @param subscribeTimeout The subscribe timeout
69 public GenericMQTTThingHandler(Thing thing, MqttChannelStateDescriptionProvider stateDescProvider,
70 int subscribeTimeout) {
71 super(thing, subscribeTimeout);
72 this.stateDescProvider = stateDescProvider;
76 public @Nullable ChannelState getChannelState(ChannelUID channelUID) {
77 return channelStateByChannelUID.get(channelUID);
81 * Subscribe on all channel static topics on all {@link ChannelState}s.
82 * If subscribing on all channels worked, the thing is put ONLINE, else OFFLINE.
84 * @param connection A started broker connection
87 protected CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection) {
88 // availability topics are also started asynchronously, so no problem here
89 clearAllAvailabilityTopics();
90 initializeAvailabilityTopicsFromConfig();
91 return channelStateByChannelUID.values().stream().map(c -> c.start(connection, scheduler, 0))
92 .collect(FutureCollector.allOf()).thenRun(() -> calculateAndUpdateThingStatus(false));
96 protected void stop() {
97 channelStateByChannelUID.values().forEach(c -> c.getCache().resetState());
102 public void dispose() {
103 // Remove all state descriptions of this handler
104 channelStateByChannelUID.forEach((uid, state) -> stateDescProvider.remove(uid));
106 // there is a design flaw, we can't clean up our stuff because it is needed by the super-class on disposal for
108 channelStateByChannelUID.clear();
112 public CompletableFuture<Void> unsubscribeAll() {
113 return CompletableFuture.allOf(
114 channelStateByChannelUID.values().stream().map(ChannelState::stop).toArray(CompletableFuture[]::new));
118 * For every Thing channel there exists a corresponding {@link ChannelState}. It consists of the MQTT state
119 * and MQTT command topic, the ChannelUID and a value state.
121 * @param channelConfig The channel configuration that contains MQTT state and command topic and multiple other
123 * @param channelUID The channel UID
124 * @param valueState The channel value state
127 protected ChannelState createChannelState(ChannelConfig channelConfig, ChannelUID channelUID, Value valueState) {
128 return new ChannelState(channelConfig, channelUID, valueState, this);
132 public void initialize() {
133 initializeAvailabilityTopicsFromConfig();
135 ThingHandlerCallback callback = getCallback();
136 if (callback == null) {
137 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Framework failure: callback must not be null");
141 ThingBuilder thingBuilder = editThing();
142 boolean modified = false;
144 List<ChannelUID> configErrors = new ArrayList<>();
145 for (Channel channel : thing.getChannels()) {
146 final ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
147 if (channelTypeUID == null) {
148 logger.warn("Channel {} has no type", channel.getLabel());
151 final ChannelConfig channelConfig = channel.getConfiguration().as(ChannelConfig.class);
154 .equals(new ChannelTypeUID(MqttBindingConstants.BINDING_ID, MqttBindingConstants.NUMBER))) {
155 Unit<?> unit = UnitUtils.parseUnit(channelConfig.unit);
156 String dimension = unit == null ? null : UnitUtils.getDimensionName(unit);
157 String expectedItemType = dimension == null ? "Number" : "Number:" + dimension; // unknown dimension ->
159 String actualItemType = channel.getAcceptedItemType();
160 if (!expectedItemType.equals(actualItemType)) {
161 ChannelBuilder channelBuilder = callback.createChannelBuilder(channel.getUID(), channelTypeUID)
162 .withAcceptedItemType(expectedItemType).withConfiguration(channel.getConfiguration());
163 String label = channel.getLabel();
165 channelBuilder.withLabel(label);
167 String description = channel.getDescription();
168 if (description != null) {
169 channelBuilder.withDescription(description);
171 thingBuilder.withoutChannel(channel.getUID());
172 thingBuilder.withChannel(channelBuilder.build());
178 Value value = ValueFactory.createValueState(channelConfig, channelTypeUID.getId());
179 ChannelState channelState = createChannelState(channelConfig, channel.getUID(), value);
180 channelStateByChannelUID.put(channel.getUID(), channelState);
181 StateDescription description = value.createStateDescription(channelConfig.commandTopic.isBlank())
182 .build().toStateDescription();
183 if (description != null) {
184 stateDescProvider.setDescription(channel.getUID(), description);
186 } catch (IllegalArgumentException e) {
187 logger.warn("Configuration error for channel '{}'", channel.getUID(), e);
188 configErrors.add(channel.getUID());
193 updateThing(thingBuilder.build());
196 // If some channels could not start up, put the entire thing offline and display the channels
197 // in question to the user.
198 if (!configErrors.isEmpty()) {
199 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Remove and recreate: "
200 + configErrors.stream().map(ChannelUID::getAsString).collect(Collectors.joining(",")));
207 protected void updateThingStatus(boolean messageReceived, Optional<Boolean> availibilityTopicsSeen) {
208 if (availibilityTopicsSeen.orElse(true)) {
209 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
211 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE);
215 private void initializeAvailabilityTopicsFromConfig() {
216 GenericThingConfiguration config = getConfigAs(GenericThingConfiguration.class);
218 String availabilityTopic = config.availabilityTopic;
220 if (availabilityTopic != null) {
221 addAvailabilityTopic(availabilityTopic, config.payloadAvailable, config.payloadNotAvailable,
222 config.transformationPattern);
224 clearAllAvailabilityTopics();