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