]> git.basschouten.com Git - openhab-addons.git/blob
4f3a13a9d53359c4dbc91f4cf55576d9a67e1b1a
[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, immediate = true)
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             properties.put(PROPERTY_SERIAL_NUMBER, device.getDetails().getSerialNumber());
61
62             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
63                     .withLabel(device.getDetails().getFriendlyName()).withRepresentationProperty(PROPERTY_SERIAL_NUMBER)
64                     .build();
65             return result;
66         } else {
67             return null;
68         }
69     }
70
71     @Override
72     public @Nullable ThingUID getThingUID(RemoteDevice device) {
73         DeviceDetails details = device.getDetails();
74         if (details != null) {
75             ModelDetails modelDetails = details.getModelDetails();
76             if (modelDetails != null) {
77                 String modelName = modelDetails.getModelName();
78                 if (modelName != null) {
79                     if (modelName.startsWith("Philips hue bridge")) {
80                         return new ThingUID(THING_TYPE_BRIDGE, details.getSerialNumber());
81                     }
82                 }
83             }
84         }
85         return null;
86     }
87 }