]> git.basschouten.com Git - openhab-addons.git/blob
2776e85feac90e91c563bfbdc7de6ee1724a35d8
[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.homie.internal.discovery;
14
15 import java.nio.charset.StandardCharsets;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mqtt.discovery.AbstractMQTTDiscovery;
23 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
24 import org.openhab.binding.mqtt.homie.generic.internal.MqttBindingConstants;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link Homie300Discovery} is responsible for discovering device nodes that follow the
37  * Homie 3.x convention (https://github.com/homieiot/convention).
38  *
39  * @author David Graeff - Initial contribution
40  */
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.mqtthomie")
42 @NonNullByDefault
43 public class Homie300Discovery extends AbstractMQTTDiscovery {
44     private final Logger logger = LoggerFactory.getLogger(Homie300Discovery.class);
45
46     protected final MQTTTopicDiscoveryService discoveryService;
47
48     @Activate
49     public Homie300Discovery(@Reference MQTTTopicDiscoveryService discoveryService) {
50         super(Collections.singleton(MqttBindingConstants.HOMIE300_MQTT_THING), 3, true, "+/+/$homie");
51         this.discoveryService = discoveryService;
52     }
53
54     @Override
55     protected MQTTTopicDiscoveryService getDiscoveryService() {
56         return discoveryService;
57     }
58
59     /**
60      * @param topic A topic like "homie/mydevice/$homie"
61      * @return Returns the "mydevice" part of the example
62      */
63     public static @Nullable String extractDeviceID(String topic) {
64         String[] strings = topic.split("/");
65         if (strings.length > 2) {
66             return strings[1];
67         }
68         return null;
69     }
70
71     /**
72      * Returns true if the version is something like "3.x" or "4.x".
73      */
74     public static boolean checkVersion(byte[] payload) {
75         return payload.length > 0 && (payload[0] == '3' || payload[0] == '4');
76     }
77
78     @Override
79     public void receivedMessage(ThingUID connectionBridge, MqttBrokerConnection connection, String topic,
80             byte[] payload) {
81         resetTimeout();
82
83         if (!checkVersion(payload)) {
84             logger.trace("Found homie device. But version {} is out of range.",
85                     new String(payload, StandardCharsets.UTF_8));
86             return;
87         }
88         final String deviceID = extractDeviceID(topic);
89         if (deviceID == null) {
90             logger.trace("Found homie device. But deviceID {} is invalid.", deviceID);
91             return;
92         }
93
94         publishDevice(connectionBridge, connection, deviceID, topic, deviceID);
95     }
96
97     void publishDevice(ThingUID connectionBridge, MqttBrokerConnection connection, String deviceID, String topic,
98             String name) {
99         Map<String, Object> properties = new HashMap<>();
100         properties.put("deviceid", deviceID);
101         properties.put("basetopic", topic.substring(0, topic.indexOf("/")));
102
103         thingDiscovered(DiscoveryResultBuilder
104                 .create(new ThingUID(MqttBindingConstants.HOMIE300_MQTT_THING, connectionBridge, deviceID))
105                 .withBridge(connectionBridge).withProperties(properties).withRepresentationProperty("deviceid")
106                 .withLabel(name).build());
107     }
108
109     @Override
110     public void topicVanished(ThingUID connectionBridge, MqttBrokerConnection connection, String topic) {
111         String deviceID = extractDeviceID(topic);
112         if (deviceID == null) {
113             return;
114         }
115         thingRemoved(new ThingUID(MqttBindingConstants.HOMIE300_MQTT_THING, connectionBridge, deviceID));
116     }
117 }