]> git.basschouten.com Git - openhab-addons.git/blob
83fabceb0fad5a7ee3574a7585fbfdcc88465ffb
[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.tapocontrol.internal.api;
14
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
16
17 import java.util.Dictionary;
18 import java.util.Set;
19
20 import javax.jmdns.ServiceInfo;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.ComponentContext;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Modified;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Handler class for TAPO Smart Home thing discovery over mDNS
38  *
39  * @author Christian Wild - Initial contribution
40  */
41 @Component(configurationPid = "discovery.tapocontrol")
42 @NonNullByDefault
43 public class TapoMDNS implements MDNSDiscoveryParticipant {
44     private final Logger logger = LoggerFactory.getLogger(TapoMDNS.class);
45     private boolean isAutoDiscoveryEnabled = true;
46
47     @Activate
48     protected void activate(ComponentContext componentContext) {
49         activateOrModifyService(componentContext);
50     }
51
52     @Modified
53     protected void modified(ComponentContext componentContext) {
54         activateOrModifyService(componentContext);
55     }
56
57     private void activateOrModifyService(ComponentContext componentContext) {
58         Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
59         String autoDiscoveryPropertyValue = (String) properties
60                 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
61         if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
62             isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
63         }
64     }
65
66     @Override
67     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
68         return SUPPORTED_THING_TYPES_UIDS;
69     }
70
71     @Override
72     public String getServiceType() {
73         return "NULL";
74     }
75
76     @Override
77     public @Nullable ThingUID getThingUID(ServiceInfo service) {
78         ThingTypeUID thingTypeUID = getThingType(service);
79         if (thingTypeUID != null) {
80             String id = service.getPropertyString(PROPERTY_FAMILY); // device id
81             return new ThingUID(thingTypeUID, id);
82         }
83         return null;
84     }
85
86     private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
87         String model = service.getPropertyString(PROPERTY_FAMILY); // model
88         logger.debug("found Type: {}", model);
89         if (model == null) {
90             return null;
91         }
92         return L510_THING_TYPE;
93     }
94
95     @Override
96     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
97         if (isAutoDiscoveryEnabled) {
98             ThingUID uid = getThingUID(service);
99             if (uid != null) {
100                 String host = service.getHostAddresses()[0];
101                 int port = service.getPort();
102                 logger.debug("device Found: {} {}", host, port);
103             }
104         }
105         return null;
106     }
107 }