2 * Copyright (c) 2010-2020 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.homie.internal.handler;
15 import java.util.List;
16 import java.util.concurrent.CompletableFuture;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.function.Consumer;
19 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.mqtt.generic.AbstractMQTTThingHandler;
24 import org.openhab.binding.mqtt.generic.ChannelState;
25 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
26 import org.openhab.binding.mqtt.generic.tools.DelayedBatchProcessing;
27 import org.openhab.binding.mqtt.homie.generic.internal.MqttBindingConstants;
28 import org.openhab.binding.mqtt.homie.internal.homie300.Device;
29 import org.openhab.binding.mqtt.homie.internal.homie300.DeviceAttributes;
30 import org.openhab.binding.mqtt.homie.internal.homie300.DeviceAttributes.ReadyState;
31 import org.openhab.binding.mqtt.homie.internal.homie300.DeviceCallback;
32 import org.openhab.binding.mqtt.homie.internal.homie300.HandlerConfiguration;
33 import org.openhab.binding.mqtt.homie.internal.homie300.Node;
34 import org.openhab.binding.mqtt.homie.internal.homie300.Property;
35 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
36 import org.openhab.core.thing.Channel;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingStatusDetail;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Handles MQTT topics that follow the Homie MQTT convention. The convention specifies a MQTT topic layout
46 * and defines Devices, Nodes and Properties, corresponding to Things, Channel Groups and Channels respectively.
48 * @author David Graeff - Initial contribution
51 public class HomieThingHandler extends AbstractMQTTThingHandler implements DeviceCallback, Consumer<List<Object>> {
52 private final Logger logger = LoggerFactory.getLogger(HomieThingHandler.class);
53 protected Device device;
54 protected final MqttChannelTypeProvider channelTypeProvider;
55 /** The timeout per attribute field subscription */
56 protected final int attributeReceiveTimeout;
57 protected final int subscribeTimeout;
58 protected final int deviceTimeout;
59 protected HandlerConfiguration config = new HandlerConfiguration();
60 protected DelayedBatchProcessing<Object> delayedProcessing;
61 private @Nullable ScheduledFuture<?> heartBeatTimer;
64 * Create a new thing handler for homie discovered things. A channel type provider and a topic value receive timeout
67 * @param thing The thing of this handler
68 * @param channelTypeProvider A channel type provider
69 * @param deviceTimeout Timeout for the entire device subscription. In milliseconds.
70 * @param subscribeTimeout Timeout for an entire attribute class subscription and receive. In milliseconds.
71 * Even a slow remote device will publish a full node or property within 100ms.
72 * @param attributeReceiveTimeout The timeout per attribute field subscription. In milliseconds.
73 * One attribute subscription and receiving should not take longer than 50ms.
75 public HomieThingHandler(Thing thing, MqttChannelTypeProvider channelTypeProvider, int deviceTimeout,
76 int subscribeTimeout, int attributeReceiveTimeout) {
77 super(thing, deviceTimeout);
78 this.channelTypeProvider = channelTypeProvider;
79 this.deviceTimeout = deviceTimeout;
80 this.subscribeTimeout = subscribeTimeout;
81 this.attributeReceiveTimeout = attributeReceiveTimeout;
82 this.delayedProcessing = new DelayedBatchProcessing<>(subscribeTimeout, this, scheduler);
83 this.device = new Device(this.thing.getUID(), this, new DeviceAttributes());
87 * Overwrite the {@link Device} and {@link DelayedBatchProcessing} object.
88 * Those are set in the constructor already, but require to be replaced for tests.
90 * @param device The device object
91 * @param delayedProcessing The delayed processing object
93 protected void setInternalObjects(Device device, DelayedBatchProcessing<Object> delayedProcessing) {
95 this.delayedProcessing = delayedProcessing;
99 public void initialize() {
100 logger.debug("About to initialize Homie device {}", device.attributes.name);
101 config = getConfigAs(HandlerConfiguration.class);
102 if (config.deviceid.isEmpty()) {
103 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Object ID unknown");
106 device.initialize(config.basetopic, config.deviceid, thing.getChannels());
111 public void handleRemoval() {
113 if (config.removetopics) {
114 this.removeRetainedTopics();
116 super.handleRemoval();
120 protected CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection) {
121 logger.debug("About to start Homie device {}", device.attributes.name);
122 if (connection.getQos() != 1) {
123 // QoS 1 is required.
125 "Homie devices require QoS 1 but Qos 0/2 is configured. Using override. Please check the configuration");
126 connection.setQos(1);
128 return device.subscribe(connection, scheduler, attributeReceiveTimeout).thenCompose((Void v) -> {
129 return device.startChannels(connection, scheduler, attributeReceiveTimeout, this);
131 logger.debug("Homie device {} fully attached (start)", device.attributes.name);
136 protected void stop() {
137 logger.debug("About to stop Homie device {}", device.attributes.name);
138 final ScheduledFuture<?> heartBeatTimer = this.heartBeatTimer;
139 if (heartBeatTimer != null) {
140 heartBeatTimer.cancel(false);
141 this.heartBeatTimer = null;
143 delayedProcessing.join();
149 public CompletableFuture<Void> unsubscribeAll() {
150 // already unsubscribed everything by calling stop()
151 return CompletableFuture.allOf();
155 public @Nullable ChannelState getChannelState(ChannelUID channelUID) {
156 Property property = device.getProperty(channelUID);
157 return property != null ? property.getChannelState() : null;
161 public void readyStateChanged(ReadyState state) {
164 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_ERROR);
167 updateStatus(ThingStatus.OFFLINE);
170 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING);
173 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "Device did not send heartbeat in time");
176 updateStatus(ThingStatus.ONLINE);
179 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.DUTY_CYCLE);
182 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "Device did not publish a ready state");
190 public void nodeRemoved(Node node) {
191 channelTypeProvider.removeChannelGroupType(node.channelGroupTypeUID);
192 delayedProcessing.accept(node);
196 public void propertyRemoved(Property property) {
197 channelTypeProvider.removeChannelType(property.channelTypeUID);
198 delayedProcessing.accept(property);
202 public void nodeAddedOrChanged(Node node) {
203 channelTypeProvider.setChannelGroupType(node.channelGroupTypeUID, node.type());
204 delayedProcessing.accept(node);
208 public void propertyAddedOrChanged(Property property) {
209 channelTypeProvider.setChannelType(property.channelTypeUID, property.getType());
210 delayedProcessing.accept(property);
214 * Callback of {@link DelayedBatchProcessing}.
215 * Add all newly discovered nodes and properties to the Thing and start subscribe to each channel state topic.
218 public void accept(@Nullable List<Object> t) {
219 if (!device.isInitialized()) {
222 List<Channel> channels = device.nodes().stream().flatMap(n -> n.properties.stream()).map(Property::getChannel)
223 .collect(Collectors.toList());
224 updateThing(editThing().withChannels(channels).build());
225 updateProperty(MqttBindingConstants.HOMIE_PROPERTY_VERSION, device.attributes.homie);
226 final MqttBrokerConnection connection = this.connection;
227 if (connection != null) {
228 device.startChannels(connection, scheduler, attributeReceiveTimeout, this).thenRun(() -> {
229 logger.debug("Homie device {} fully attached (accept)", device.attributes.name);
235 * Removes all retained topics related to the device
237 private void removeRetainedTopics() {
238 MqttBrokerConnection connection = this.connection;
239 if (connection == null) {
240 logger.warn("couldn't remove retained topics for {} because connection is null", thing.getUID());
243 device.getRetainedTopics().stream().map(d -> {
244 return String.format("%s/%s", config.basetopic, d);
245 }).collect(Collectors.toList()).forEach(t -> connection.publish(t, new byte[0], 1, true));
249 protected void updateThingStatus(boolean messageReceived, boolean availabilityTopicsSeen) {