]> git.basschouten.com Git - openhab-addons.git/blob
e87dc0890c3f93fc4b0f10b7c0e811bbd9c87a87
[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.dlinksmarthome.internal;
14
15 import static org.openhab.binding.dlinksmarthome.internal.DLinkSmartHomeBindingConstants.*;
16 import static org.openhab.binding.dlinksmarthome.internal.motionsensor.DLinkMotionSensorConfig.IP_ADDRESS;
17
18 import java.net.Inet4Address;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22
23 import javax.jmdns.ServiceInfo;
24
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link DLinkSmartHomeDiscoveryParticipant} is responsible for discovering devices through UPnP.
36  *
37  * @author Mike Major - Initial contribution
38  *
39  */
40 @Component
41 public class DLinkSmartHomeDiscoveryParticipant implements MDNSDiscoveryParticipant {
42
43     private static final String SERVICE_TYPE = "_dhnap._tcp.local.";
44     private final Logger logger = LoggerFactory.getLogger(getClass());
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return SUPPORTED_THING_TYPES_UIDS;
49     }
50
51     @Override
52     public String getServiceType() {
53         return SERVICE_TYPE;
54     }
55
56     @Override
57     public DiscoveryResult createResult(final ServiceInfo serviceInfo) {
58         final ThingUID thingUID = getThingUID(serviceInfo);
59
60         if (thingUID == null) {
61             return null;
62         }
63
64         final ThingTypeUID thingTypeUID = getThingType(serviceInfo);
65
66         if (THING_TYPE_DCHS150.equals(thingTypeUID)) {
67             return createMotionSensor(thingUID, thingTypeUID, serviceInfo);
68         } else {
69             return null;
70         }
71     }
72
73     @Override
74     public ThingUID getThingUID(final ServiceInfo serviceInfo) {
75         final ThingTypeUID thingTypeUID = getThingType(serviceInfo);
76
77         if (thingTypeUID != null) {
78             final String mac = serviceInfo.getPropertyString("mac").replace(":", "").toLowerCase();
79             return new ThingUID(thingTypeUID, mac);
80         } else {
81             return null;
82         }
83     }
84
85     private ThingTypeUID getThingType(final ServiceInfo serviceInfo) {
86         final String model = serviceInfo.getPropertyString("model_number");
87
88         if (model == null) {
89             return null;
90         } else if (model.equals("DCH-S150")) {
91             return THING_TYPE_DCHS150;
92         } else {
93             logger.debug("D-Link HNAP Type: {}", model);
94             return null;
95         }
96     }
97
98     private DiscoveryResult createMotionSensor(final ThingUID thingUID, final ThingTypeUID thingType,
99             final ServiceInfo serviceInfo) {
100         final Inet4Address[] addresses = serviceInfo.getInet4Addresses();
101         if (addresses.length != 1) {
102             return null;
103         }
104         final String host = addresses[0].getHostAddress();
105         final String mac = serviceInfo.getPropertyString("mac");
106
107         final Map<String, Object> properties = new HashMap<>();
108         properties.put(IP_ADDRESS, host);
109
110         logger.debug("DCH-S150 found: {}", host);
111
112         return DiscoveryResultBuilder.create(thingUID).withThingType(thingType).withProperties(properties)
113                 .withLabel("Motion Sensor (" + mac + ")").build();
114     }
115 }