]> git.basschouten.com Git - openhab-addons.git/blob
bcaf26a06102f5578c5a3e6d6e164a2794e647eb
[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.deconz.internal.discovery;
14
15 import static org.openhab.binding.deconz.internal.BindingConstants.*;
16
17 import java.net.URL;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.TreeMap;
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.RemoteDevice;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33
34 /**
35  * Discover deCONZ software instances. They announce themselves as HUE bridges,
36  * and their REST API is compatible to HUE bridges. But they also provide a websocket
37  * real-time channel for sensors.
38  *
39  * We check for the manufacturer string of "dresden elektronik".
40  *
41  * @author David Graeff - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = UpnpDiscoveryParticipant.class)
45 public class BridgeDiscoveryParticipant implements UpnpDiscoveryParticipant {
46
47     @Override
48     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49         return Collections.singleton(BRIDGE_TYPE);
50     }
51
52     @Override
53     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
54         ThingUID uid = getThingUID(device);
55         if (uid == null) {
56             return null;
57         }
58         URL descriptorURL = device.getIdentity().getDescriptorURL();
59         String UDN = device.getIdentity().getUdn().getIdentifierString();
60
61         // Friendly name is like "name (host)"
62         String name = device.getDetails().getFriendlyName();
63         // Cut out the pure name
64         if (name.indexOf('(') - 1 > 0) {
65             name = name.substring(0, name.indexOf('(') - 1);
66         }
67         // Add host+port
68         String host = descriptorURL.getHost();
69         int port = descriptorURL.getPort();
70         name = name + " (" + host + ":" + String.valueOf(port) + ")";
71
72         Map<String, Object> properties = new TreeMap<>();
73
74         properties.put(CONFIG_HOST, host);
75         properties.put(CONFIG_HTTP_PORT, port);
76         properties.put(PROPERTY_UDN, UDN);
77
78         return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(name)
79                 .withRepresentationProperty(PROPERTY_UDN).build();
80     }
81
82     @Override
83     public @Nullable ThingUID getThingUID(RemoteDevice device) {
84         DeviceDetails details = device.getDetails();
85         if (details != null && details.getManufacturerDetails() != null
86                 && "dresden elektronik".equals(details.getManufacturerDetails().getManufacturer())) {
87             return new ThingUID(BRIDGE_TYPE, details.getSerialNumber());
88         }
89         return null;
90     }
91 }