]> git.basschouten.com Git - openhab-addons.git/blob
fb40cdadc16de3916761e28c0420039b14d47bdc
[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.hue.internal.discovery;
14
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_SERIAL_NUMBER;
17
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.Dictionary;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Set;
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.HueBindingConstants;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
34 import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
37 import org.osgi.service.cm.Configuration;
38 import org.osgi.service.cm.ConfigurationAdmin;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link HueBridgeDiscoveryParticipant} is responsible for discovering new and
47  * removed hue bridges. It uses the central {@link UpnpDiscoveryService}.
48  *
49  * @author Kai Kreuzer - Initial contribution
50  * @author Thomas Höfer - Added representation
51  */
52 @NonNullByDefault
53 @Component(service = UpnpDiscoveryParticipant.class)
54 public class HueBridgeDiscoveryParticipant implements UpnpDiscoveryParticipant {
55
56     private final Logger logger = LoggerFactory.getLogger(HueBridgeDiscoveryParticipant.class);
57
58     // Hue bridges have maxAge 100 seconds, so set the default grace period to half of that
59     private long removalGracePeriodSeconds = 50;
60
61     private final ConfigurationAdmin configAdmin;
62
63     @Activate
64     public HueBridgeDiscoveryParticipant(final @Reference ConfigurationAdmin configAdmin) {
65         this.configAdmin = configAdmin;
66     }
67
68     @Override
69     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
70         return Collections.singleton(THING_TYPE_BRIDGE);
71     }
72
73     @Override
74     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
75         ThingUID uid = getThingUID(device);
76         if (uid != null) {
77             Map<String, Object> properties = new HashMap<>();
78             properties.put(HOST, device.getDetails().getBaseURL().getHost());
79             properties.put(PORT, device.getDetails().getBaseURL().getPort());
80             properties.put(PROTOCOL, device.getDetails().getBaseURL().getProtocol());
81             String serialNumber = device.getDetails().getSerialNumber();
82             DiscoveryResult result;
83             if (serialNumber != null && !serialNumber.isBlank()) {
84                 properties.put(PROPERTY_SERIAL_NUMBER, serialNumber.toLowerCase());
85
86                 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
87                         .withLabel(device.getDetails().getFriendlyName())
88                         .withRepresentationProperty(PROPERTY_SERIAL_NUMBER).build();
89             } else {
90                 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
91                         .withLabel(device.getDetails().getFriendlyName()).build();
92             }
93             return result;
94         } else {
95             return null;
96         }
97     }
98
99     @Override
100     public @Nullable ThingUID getThingUID(RemoteDevice device) {
101         DeviceDetails details = device.getDetails();
102         if (details != null) {
103             ModelDetails modelDetails = details.getModelDetails();
104             String serialNumber = details.getSerialNumber();
105             if (modelDetails != null && serialNumber != null && !serialNumber.isBlank()) {
106                 String modelName = modelDetails.getModelName();
107                 if (modelName != null) {
108                     if (modelName.startsWith("Philips hue bridge")) {
109                         return new ThingUID(THING_TYPE_BRIDGE, serialNumber.toLowerCase());
110                     }
111                 }
112             }
113         }
114         return null;
115     }
116
117     @Override
118     public long getRemovalGracePeriodSeconds(RemoteDevice device) {
119         try {
120             Configuration conf = configAdmin.getConfiguration("binding.hue");
121             Dictionary<String, @Nullable Object> properties = conf.getProperties();
122             if (properties != null) {
123                 Object property = properties.get(HueBindingConstants.REMOVAL_GRACE_PERIOD);
124                 if (property != null) {
125                     removalGracePeriodSeconds = Long.parseLong(property.toString());
126                 }
127             }
128         } catch (IOException | IllegalStateException | NumberFormatException e) {
129             // fall through to pre-initialised (default) value
130         }
131         logger.trace("getRemovalGracePeriodSeconds={}", removalGracePeriodSeconds);
132         return removalGracePeriodSeconds;
133     }
134 }