]> git.basschouten.com Git - openhab-addons.git/blob
1c2e162a8fe424497112ee589c6c429a0ae6b677
[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 package org.openhab.binding.hue.internal.discovery;
14
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16
17 import java.util.Dictionary;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
30 import org.openhab.core.config.discovery.mdns.internal.MDNSDiscoveryService;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.ComponentContext;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Modified;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link HueBridgeMDNSDiscoveryParticipant} is responsible for discovering new and removed Hue Bridges. It uses the
43  * central {@link MDNSDiscoveryService}.
44  *
45  * @author Kai Kreuzer - Initial contribution
46  * @author Thomas Höfer - Added representation
47  * @author Christoph Weitkamp - Change discovery protocol to mDNS
48  */
49 @Component(configurationPid = "discovery.hue")
50 @NonNullByDefault
51 public class HueBridgeMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
52
53     private static final String SERVICE_TYPE = "_hue._tcp.local.";
54     private static final String MDNS_PROPERTY_BRIDGE_ID = "bridgeid";
55     private static final String MDNS_PROPERTY_MODEL_ID = "modelid";
56
57     private final Logger logger = LoggerFactory.getLogger(HueBridgeMDNSDiscoveryParticipant.class);
58
59     private long removalGracePeriod = 0L;
60
61     private boolean isAutoDiscoveryEnabled = true;
62
63     @Activate
64     protected void activate(ComponentContext componentContext) {
65         activateOrModifyService(componentContext);
66     }
67
68     @Modified
69     protected void modified(ComponentContext componentContext) {
70         activateOrModifyService(componentContext);
71     }
72
73     private void activateOrModifyService(ComponentContext componentContext) {
74         Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
75         String autoDiscoveryPropertyValue = (String) properties
76                 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
77         if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
78             isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
79         }
80         String removalGracePeriodPropertyValue = (String) properties.get(REMOVAL_GRACE_PERIOD);
81         if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
82             try {
83                 removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
84             } catch (NumberFormatException e) {
85                 logger.warn("Configuration property '{}' has invalid value: {}", REMOVAL_GRACE_PERIOD,
86                         removalGracePeriodPropertyValue);
87             }
88         }
89     }
90
91     @Override
92     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
93         return HueBridgeHandler.SUPPORTED_THING_TYPES;
94     }
95
96     @Override
97     public String getServiceType() {
98         return SERVICE_TYPE;
99     }
100
101     @Override
102     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
103         if (isAutoDiscoveryEnabled) {
104             ThingUID uid = getThingUID(service);
105             if (uid != null) {
106                 String host = service.getHostAddresses()[0];
107                 String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
108                 String friendlyName = String.format(DISCOVERY_LABEL_PATTERN, host);
109                 return DiscoveryResultBuilder.create(uid) //
110                         .withProperties(Map.of( //
111                                 HOST, host, //
112                                 Thing.PROPERTY_MODEL_ID, service.getPropertyString(MDNS_PROPERTY_MODEL_ID), //
113                                 Thing.PROPERTY_SERIAL_NUMBER, id.toLowerCase())) //
114                         .withLabel(friendlyName) //
115                         .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER) //
116                         .withTTL(120L) //
117                         .build();
118             }
119         }
120         return null;
121     }
122
123     @Override
124     public @Nullable ThingUID getThingUID(ServiceInfo service) {
125         String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
126         if (id != null && !id.isBlank()) {
127             return new ThingUID(THING_TYPE_BRIDGE, id.toLowerCase());
128         }
129         return null;
130     }
131
132     @Override
133     public long getRemovalGracePeriodSeconds(ServiceInfo service) {
134         return removalGracePeriod;
135     }
136 }