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