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