2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.dlinksmarthome.internal;
15 import static org.openhab.binding.dlinksmarthome.internal.DLinkSmartHomeBindingConstants.*;
16 import static org.openhab.binding.dlinksmarthome.internal.motionsensor.DLinkMotionSensorConfig.IP_ADDRESS;
18 import java.net.Inet4Address;
19 import java.util.HashMap;
23 import javax.jmdns.ServiceInfo;
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;
35 * The {@link DLinkSmartHomeDiscoveryParticipant} is responsible for discovering devices through UPnP.
37 * @author Mike Major - Initial contribution
41 public class DLinkSmartHomeDiscoveryParticipant implements MDNSDiscoveryParticipant {
43 private static final String SERVICE_TYPE = "_dhnap._tcp.local.";
44 private final Logger logger = LoggerFactory.getLogger(getClass());
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return SUPPORTED_THING_TYPES_UIDS;
52 public String getServiceType() {
57 public DiscoveryResult createResult(final ServiceInfo serviceInfo) {
58 final ThingUID thingUID = getThingUID(serviceInfo);
60 if (thingUID == null) {
64 final ThingTypeUID thingTypeUID = getThingType(serviceInfo);
66 if (THING_TYPE_DCHS150.equals(thingTypeUID)) {
67 return createMotionSensor(thingUID, thingTypeUID, serviceInfo);
74 public ThingUID getThingUID(final ServiceInfo serviceInfo) {
75 final ThingTypeUID thingTypeUID = getThingType(serviceInfo);
77 if (thingTypeUID != null) {
78 final String mac = serviceInfo.getPropertyString("mac").replace(":", "").toLowerCase();
79 return new ThingUID(thingTypeUID, mac);
85 private ThingTypeUID getThingType(final ServiceInfo serviceInfo) {
86 final String model = serviceInfo.getPropertyString("model_number");
90 } else if ("DCH-S150".equals(model)) {
91 return THING_TYPE_DCHS150;
93 logger.debug("D-Link HNAP Type: {}", model);
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) {
104 final String host = addresses[0].getHostAddress();
105 final String mac = serviceInfo.getPropertyString("mac");
107 final Map<String, Object> properties = new HashMap<>();
108 properties.put(IP_ADDRESS, host);
110 logger.debug("DCH-S150 found: {}", host);
112 return DiscoveryResultBuilder.create(thingUID).withThingType(thingType).withProperties(properties)
113 .withLabel("Motion Sensor (" + mac + ")").build();