]> git.basschouten.com Git - openhab-addons.git/blob
ebdc19245fbb14b54abf81b3eefaaeeae8538a4b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.jupnp.model.meta.DeviceDetails;
26 import org.jupnp.model.meta.ModelDetails;
27 import org.jupnp.model.meta.RemoteDevice;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
31 import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.annotations.Component;
35
36 /**
37  * The {@link HueBridgeDiscoveryParticipant} is responsible for discovering new and
38  * removed hue bridges. It uses the central {@link UpnpDiscoveryService}.
39  *
40  * @author Kai Kreuzer - Initial contribution
41  * @author Thomas Höfer - Added representation
42  */
43 @NonNullByDefault
44 @Component(service = UpnpDiscoveryParticipant.class)
45 public class HueBridgeDiscoveryParticipant implements UpnpDiscoveryParticipant {
46
47     @Override
48     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49         return Collections.singleton(THING_TYPE_BRIDGE);
50     }
51
52     @Override
53     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
54         ThingUID uid = getThingUID(device);
55         if (uid != null) {
56             Map<String, Object> properties = new HashMap<>();
57             properties.put(HOST, device.getDetails().getBaseURL().getHost());
58             properties.put(PORT, device.getDetails().getBaseURL().getPort());
59             properties.put(PROTOCOL, device.getDetails().getBaseURL().getProtocol());
60             String serialNumber = device.getDetails().getSerialNumber();
61             DiscoveryResult result;
62             if (serialNumber != null && !serialNumber.isBlank()) {
63                 properties.put(PROPERTY_SERIAL_NUMBER, serialNumber.toLowerCase());
64
65                 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
66                         .withLabel(device.getDetails().getFriendlyName())
67                         .withRepresentationProperty(PROPERTY_SERIAL_NUMBER).build();
68             } else {
69                 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
70                         .withLabel(device.getDetails().getFriendlyName()).build();
71             }
72             return result;
73         } else {
74             return null;
75         }
76     }
77
78     @Override
79     public @Nullable ThingUID getThingUID(RemoteDevice device) {
80         DeviceDetails details = device.getDetails();
81         if (details != null) {
82             ModelDetails modelDetails = details.getModelDetails();
83             String serialNumber = details.getSerialNumber();
84             if (modelDetails != null && serialNumber != null && !serialNumber.isBlank()) {
85                 String modelName = modelDetails.getModelName();
86                 if (modelName != null) {
87                     if (modelName.startsWith("Philips hue bridge")) {
88                         return new ThingUID(THING_TYPE_BRIDGE, serialNumber.toLowerCase());
89                     }
90                 }
91             }
92         }
93         return null;
94     }
95 }