2 * Copyright (c) 2010-2020 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.km200.internal.discovery;
15 import static org.openhab.binding.km200.internal.KM200BindingConstants.THING_TYPE_KMDEVICE;
17 import java.net.InetAddress;
18 import java.util.Random;
21 import javax.jmdns.ServiceInfo;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.km200.internal.handler.KM200GatewayHandler;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link KM200GatewayDiscoveryParticipant} class discovers gateways and adds the results to the inbox.
38 * @author Markus Eckhardt - Initial contribution
41 @Component(immediate = true, configurationPid = "binding.km200")
42 public class KM200GatewayDiscoveryParticipant implements MDNSDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(KM200GatewayDiscoveryParticipant.class);
46 public static final Set<ThingTypeUID> SUPPORTED_ALL_THING_TYPES_UIDS = KM200GatewayHandler.SUPPORTED_THING_TYPES_UIDS;
49 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50 return SUPPORTED_ALL_THING_TYPES_UIDS;
54 public @Nullable DiscoveryResult createResult(ServiceInfo info) {
55 DiscoveryResult discoveryResult = null;
56 ThingUID uid = getThingUID(info);
57 logger.debug("MDNS info: {}, uid: {}", info, uid);
59 InetAddress[] addrs = info.getInetAddresses();
60 logger.debug("ip: {} id:{}", addrs[0].getHostAddress(), uid.getId());
61 discoveryResult = DiscoveryResultBuilder.create(uid).withProperty("ip4Address", addrs[0].getHostAddress())
62 .withProperty("deviceId", uid.getId()).withRepresentationProperty(addrs[0].getHostAddress())
63 .withLabel("KM50/100/200 Gateway (" + addrs[0].getHostAddress() + ")").build();
65 return discoveryResult;
71 public @Nullable ThingUID getThingUID(ServiceInfo info) {
72 ThingTypeUID typeUID = getThingTypeUID(info);
73 if (typeUID != null) {
74 logger.debug("getType: {}", info.getType());
75 if (info.getType() != null) {
76 if (info.getType().equalsIgnoreCase(getServiceType())) {
77 String devId = info.getPropertyString("uuid");
79 logger.info("Discovered a KMXXX gateway with name: '{}' id: '{}'", info.getName(), devId);
80 if (devId.isEmpty()) {
81 /* If something is wrong then we are generating a random UUID */
82 logger.debug("Error in automatic device-id detection. Using random value");
83 Random rnd = new Random();
84 devId = String.valueOf(rnd.nextLong());
86 ThingUID thinguid = new ThingUID(typeUID, devId);
89 logger.debug("No uuid property found");
98 public String getServiceType() {
99 return "_http._tcp.local.";
102 private @Nullable ThingTypeUID getThingTypeUID(ServiceInfo info) {
103 InetAddress[] addrs = info.getInetAddresses();
104 if (addrs.length > 0) {
106 hardwareID = info.getPropertyString("hwversion");
107 logger.debug("hardwareID: {}", hardwareID);
108 if (hardwareID != null && hardwareID.contains("iCom_Low")) {
109 return THING_TYPE_KMDEVICE;