2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hue.internal.discovery;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_SERIAL_NUMBER;
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.Dictionary;
21 import java.util.HashMap;
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;
46 * The {@link HueBridgeDiscoveryParticipant} is responsible for discovering new and
47 * removed hue bridges. It uses the central {@link UpnpDiscoveryService}.
49 * @author Kai Kreuzer - Initial contribution
50 * @author Thomas Höfer - Added representation
53 @Component(service = UpnpDiscoveryParticipant.class)
54 public class HueBridgeDiscoveryParticipant implements UpnpDiscoveryParticipant {
56 private final Logger logger = LoggerFactory.getLogger(HueBridgeDiscoveryParticipant.class);
58 // Hue bridges have maxAge 100 seconds, so set the default grace period to half of that
59 private long removalGracePeriodSeconds = 50;
61 private final ConfigurationAdmin configAdmin;
64 public HueBridgeDiscoveryParticipant(final @Reference ConfigurationAdmin configAdmin) {
65 this.configAdmin = configAdmin;
69 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
70 return Collections.singleton(THING_TYPE_BRIDGE);
74 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
75 ThingUID uid = getThingUID(device);
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());
86 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
87 .withLabel(device.getDetails().getFriendlyName())
88 .withRepresentationProperty(PROPERTY_SERIAL_NUMBER).build();
90 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
91 .withLabel(device.getDetails().getFriendlyName()).build();
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());
118 public long getRemovalGracePeriodSeconds(RemoteDevice device) {
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());
128 } catch (IOException | IllegalStateException | NumberFormatException e) {
129 // fall through to pre-initialised (default) value
131 logger.trace("getRemovalGracePeriodSeconds={}", removalGracePeriodSeconds);
132 return removalGracePeriodSeconds;