]> git.basschouten.com Git - openhab-addons.git/blob
4df029f12f58284eaecdbf5c765af8436853828a
[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 static final String CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD = "removalGracePeriod";
58
59     private final Logger logger = LoggerFactory.getLogger(HueBridgeMDNSDiscoveryParticipant.class);
60
61     private long removalGracePeriod = 0L;
62
63     private boolean isAutoDiscoveryEnabled = true;
64
65     @Activate
66     protected void activate(ComponentContext componentContext) {
67         activateOrModifyService(componentContext);
68     }
69
70     @Modified
71     protected void modified(ComponentContext componentContext) {
72         activateOrModifyService(componentContext);
73     }
74
75     private void activateOrModifyService(ComponentContext componentContext) {
76         Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
77         String autoDiscoveryPropertyValue = (String) properties
78                 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
79         if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
80             isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
81         }
82         String removalGracePeriodPropertyValue = (String) properties.get(CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD);
83         if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
84             try {
85                 removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
86             } catch (NumberFormatException e) {
87                 logger.warn("Configuration property '{}' has invalid value: {}", CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD,
88                         removalGracePeriodPropertyValue);
89             }
90         }
91     }
92
93     @Override
94     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
95         return HueBridgeHandler.SUPPORTED_THING_TYPES;
96     }
97
98     @Override
99     public String getServiceType() {
100         return SERVICE_TYPE;
101     }
102
103     @Override
104     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
105         if (isAutoDiscoveryEnabled) {
106             ThingUID uid = getThingUID(service);
107             if (uid != null) {
108                 String host = service.getHostAddresses()[0];
109                 String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
110                 String friendlyName = String.format("%s (%s)", service.getName(), host);
111                 return DiscoveryResultBuilder.create(uid) //
112                         .withProperties(Map.of( //
113                                 HOST, host, //
114                                 Thing.PROPERTY_MODEL_ID, service.getPropertyString(MDNS_PROPERTY_MODEL_ID), //
115                                 Thing.PROPERTY_SERIAL_NUMBER, id.toLowerCase())) //
116                         .withLabel(friendlyName) //
117                         .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER) //
118                         .withTTL(120L) //
119                         .build();
120             }
121         }
122         return null;
123     }
124
125     @Override
126     public @Nullable ThingUID getThingUID(ServiceInfo service) {
127         String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
128         if (id != null && !id.isBlank()) {
129             return new ThingUID(THING_TYPE_BRIDGE, id.toLowerCase());
130         }
131         return null;
132     }
133
134     @Override
135     public long getRemovalGracePeriodSeconds(ServiceInfo service) {
136         return removalGracePeriod;
137     }
138 }