]> git.basschouten.com Git - openhab-addons.git/blob
27f70c21f83e2f8fce2f7900526077a76b289a7c
[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.net.URL;
18 import java.util.Dictionary;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 import java.util.regex.PatternSyntaxException;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.jupnp.model.meta.DeviceDetails;
28 import org.jupnp.model.meta.ModelDetails;
29 import org.jupnp.model.meta.RemoteDevice;
30 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
35 import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.osgi.service.component.ComponentContext;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Modified;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * The {@link HueBridgeUPNPDiscoveryParticipant} is responsible for discovering new and removed Hue Bridges. It uses the
48  * central {@link UpnpDiscoveryService}.
49  *
50  * The discovery through UPnP was replaced by mDNS discovery for recent bridges (V2).
51  * For old bridges (V1), the UPnP discovery is still required (as mDNS is not implemented).
52  * This class allows discovering only old bridges using UPnP.
53  *
54  * @author Laurent Garnier - Initial contribution
55  */
56 @Component(configurationPid = "discovery.hue")
57 @NonNullByDefault
58 public class HueBridgeUPNPDiscoveryParticipant implements UpnpDiscoveryParticipant {
59
60     private static final String EXPECTED_MODEL_NAME_PREFIX = "Philips hue bridge";
61
62     private final Logger logger = LoggerFactory.getLogger(HueBridgeUPNPDiscoveryParticipant.class);
63
64     private long removalGracePeriod = 50L;
65
66     private boolean isAutoDiscoveryEnabled = true;
67
68     @Activate
69     protected void activate(ComponentContext componentContext) {
70         activateOrModifyService(componentContext);
71     }
72
73     @Modified
74     protected void modified(ComponentContext componentContext) {
75         activateOrModifyService(componentContext);
76     }
77
78     private void activateOrModifyService(ComponentContext componentContext) {
79         Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
80         String autoDiscoveryPropertyValue = (String) properties
81                 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
82         if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
83             isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
84         }
85         String removalGracePeriodPropertyValue = (String) properties.get(REMOVAL_GRACE_PERIOD);
86         if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
87             try {
88                 removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
89             } catch (NumberFormatException e) {
90                 logger.warn("Configuration property '{}' has invalid value: {}", REMOVAL_GRACE_PERIOD,
91                         removalGracePeriodPropertyValue);
92             }
93         }
94     }
95
96     @Override
97     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
98         return HueBridgeHandler.SUPPORTED_THING_TYPES;
99     }
100
101     @Override
102     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
103         if (!isAutoDiscoveryEnabled) {
104             return null;
105         }
106         DeviceDetails details = device.getDetails();
107         ThingUID uid = getThingUID(device);
108         if (details == null || uid == null) {
109             return null;
110         }
111         URL baseUrl = details.getBaseURL();
112         String serialNumber = details.getSerialNumber();
113         if (baseUrl == null || serialNumber == null || serialNumber.isBlank()) {
114             return null;
115         }
116         String label = String.format(DISCOVERY_LABEL_PATTERN, baseUrl.getHost());
117         String modelName = EXPECTED_MODEL_NAME_PREFIX;
118         ModelDetails modelDetails = details.getModelDetails();
119         if (modelDetails != null && modelDetails.getModelName() != null && modelDetails.getModelNumber() != null) {
120             modelName = String.format("%s (%s)", modelDetails.getModelName(), modelDetails.getModelNumber());
121         }
122         return DiscoveryResultBuilder.create(uid) //
123                 .withProperties(Map.of( //
124                         HOST, baseUrl.getHost(), //
125                         PORT, baseUrl.getPort(), //
126                         PROTOCOL, baseUrl.getProtocol(), //
127                         Thing.PROPERTY_MODEL_ID, modelName, //
128                         Thing.PROPERTY_SERIAL_NUMBER, serialNumber.toLowerCase())) //
129                 .withLabel(label) //
130                 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER) //
131                 .build();
132     }
133
134     @Override
135     public @Nullable ThingUID getThingUID(RemoteDevice device) {
136         DeviceDetails details = device.getDetails();
137         if (details == null) {
138             return null;
139         }
140         String serialNumber = details.getSerialNumber();
141         ModelDetails modelDetails = details.getModelDetails();
142         if (serialNumber == null || serialNumber.isBlank() || modelDetails == null) {
143             return null;
144         }
145         String modelName = modelDetails.getModelName();
146         // Model name has the format "Philips hue bridge <year>" with <year> being 2012
147         // for a hue bridge V1 or 2015 for a hue bridge V2.
148         if (modelName == null || !modelName.startsWith(EXPECTED_MODEL_NAME_PREFIX)) {
149             return null;
150         }
151         try {
152             Pattern pattern = Pattern.compile("\\d{4}");
153             Matcher matcher = pattern.matcher(modelName);
154             int year = Integer.parseInt(matcher.find() ? matcher.group() : "9999");
155             // The bridge is ignored if year is greater or equal to 2015
156             if (year >= 2015) {
157                 return null;
158             }
159         } catch (PatternSyntaxException | NumberFormatException e) {
160             // No int value found, this bridge is ignored
161             return null;
162         }
163         return new ThingUID(THING_TYPE_BRIDGE, serialNumber.toLowerCase());
164     }
165
166     @Override
167     public long getRemovalGracePeriodSeconds(RemoteDevice device) {
168         return removalGracePeriod;
169     }
170 }