]> git.basschouten.com Git - openhab-addons.git/blob
e5bb815e4aa1480ae42f3c865b735ca564be1b30
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
14 package org.openhab.binding.mqtt.espmilighthub.internal.discovery;
15
16 import static org.openhab.binding.mqtt.MqttBindingConstants.BINDING_ID;
17 import static org.openhab.binding.mqtt.espmilighthub.internal.EspMilightHubBindingConstants.*;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.mqtt.discovery.AbstractMQTTDiscovery;
24 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
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.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33
34 /**
35  * The {@link EspMilightHubDiscoveryService} is responsible for finding globes
36  * and setting them up for the handlers.
37  *
38  * @author Matthew Skinner - Initial contribution
39  */
40
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.mqttespmilighthub")
42 @NonNullByDefault
43 public class EspMilightHubDiscoveryService extends AbstractMQTTDiscovery {
44     protected final MQTTTopicDiscoveryService discoveryService;
45
46     @Activate
47     public EspMilightHubDiscoveryService(@Reference MQTTTopicDiscoveryService discoveryService) {
48         super(SUPPORTED_THING_TYPES, 3, true, STATES_BASE_TOPIC + "#");
49         this.discoveryService = discoveryService;
50     }
51
52     @Override
53     protected MQTTTopicDiscoveryService getDiscoveryService() {
54         return discoveryService;
55     }
56
57     @Override
58     public void receivedMessage(ThingUID connectionBridge, MqttBrokerConnection connection, String topic,
59             byte[] payload) {
60         resetTimeout();
61         if (topic.startsWith(STATES_BASE_TOPIC)) {
62             String cutTopic = topic.replace(STATES_BASE_TOPIC, "");
63             int index = cutTopic.indexOf("/");
64             if (index != -1) // -1 means "not found"
65             {
66                 String remoteCode = (cutTopic.substring(0, index)); // Store the remote code for use later
67                 cutTopic = topic.replace(STATES_BASE_TOPIC + remoteCode + "/", "");
68                 index = cutTopic.indexOf("/");
69                 if (index != -1) {
70                     String globeType = (cutTopic.substring(0, index));
71                     String remoteGroupID = (cutTopic.substring(index + 1, index + 2));
72                     // openHAB's framework has better code for handling groups then the firmware does
73                     if (!"0".equals(remoteGroupID)) {// Users can manually add group 0 things if they wish
74                         publishDevice(connectionBridge, connection, topic, remoteCode, globeType, remoteGroupID);
75                     }
76                 }
77             }
78         }
79     }
80
81     void publishDevice(ThingUID connectionBridge, MqttBrokerConnection connection, String topic, String remoteCode,
82             String globeType, String remoteGroupID) {
83         Map<String, Object> properties = new HashMap<>();
84         properties.put("deviceid", remoteCode + remoteGroupID);
85         properties.put("basetopic", STATES_BASE_TOPIC + remoteCode + "/" + globeType + "/" + remoteGroupID);
86         thingDiscovered(DiscoveryResultBuilder
87                 .create(new ThingUID(new ThingTypeUID(BINDING_ID, globeType), connectionBridge,
88                         remoteCode + remoteGroupID))
89                 .withProperties(properties).withRepresentationProperty("deviceid").withBridge(connectionBridge)
90                 .withLabel("Milight " + globeType).build());
91     }
92
93     @Override
94     public void topicVanished(ThingUID connectionBridge, MqttBrokerConnection connection, String topic) {
95     }
96 }