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.util.HashMap;
22 import javax.jmdns.ServiceInfo;
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;
34 * The {@link DLinkSmartHomeDiscoveryParticipant} is responsible for discovering devices through UPnP.
36 * @author Mike Major - Initial contribution
40 public class DLinkSmartHomeDiscoveryParticipant implements MDNSDiscoveryParticipant {
42 private static final String SERVICE_TYPE = "_dhnap._tcp.local.";
43 private final Logger logger = LoggerFactory.getLogger(getClass());
46 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47 return SUPPORTED_THING_TYPES_UIDS;
51 public String getServiceType() {
56 public DiscoveryResult createResult(final ServiceInfo serviceInfo) {
57 final ThingUID thingUID = getThingUID(serviceInfo);
59 if (thingUID == null) {
63 final ThingTypeUID thingTypeUID = getThingType(serviceInfo);
65 if (THING_TYPE_DCHS150.equals(thingTypeUID)) {
66 return createMotionSensor(thingUID, thingTypeUID, serviceInfo);
73 public ThingUID getThingUID(final ServiceInfo serviceInfo) {
74 final ThingTypeUID thingTypeUID = getThingType(serviceInfo);
76 if (thingTypeUID != null) {
77 final String mac = serviceInfo.getPropertyString("mac").replace(":", "").toLowerCase();
78 return new ThingUID(thingTypeUID, mac);
84 private ThingTypeUID getThingType(final ServiceInfo serviceInfo) {
85 final String model = serviceInfo.getPropertyString("model_number");
89 } else if (model.equals("DCH-S150")) {
90 return THING_TYPE_DCHS150;
92 logger.debug("D-Link HNAP Type: {}", model);
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");
102 final Map<String, Object> properties = new HashMap<>();
103 properties.put(IP_ADDRESS, host);
105 logger.debug("DCH-S150 found: {}", host);
107 return DiscoveryResultBuilder.create(thingUID).withThingType(thingType).withProperties(properties)
108 .withLabel("Motion Sensor (" + mac + ")").build();