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.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(configurationPid = "binding.km200")
42 public class KM200GatewayDiscoveryParticipant implements MDNSDiscoveryParticipant {
44 private static final Set<ThingTypeUID> SUPPORTED_ALL_THING_TYPES_UIDS = KM200GatewayHandler.SUPPORTED_THING_TYPES_UIDS;
45 private static final String IP4_ADDRESS = "ip4Address";
47 private final Logger logger = LoggerFactory.getLogger(KM200GatewayDiscoveryParticipant.class);
50 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51 return SUPPORTED_ALL_THING_TYPES_UIDS;
55 public @Nullable DiscoveryResult createResult(ServiceInfo info) {
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 return DiscoveryResultBuilder.create(uid).withProperty(IP4_ADDRESS, addrs[0].getHostAddress())
62 .withProperty("deviceId", uid.getId()).withRepresentationProperty(IP4_ADDRESS)
63 .withLabel("KM50/100/200 Gateway (" + addrs[0].getHostAddress() + ")").build();
69 public @Nullable ThingUID getThingUID(ServiceInfo info) {
70 ThingTypeUID typeUID = getThingTypeUID(info);
71 if (typeUID != null) {
72 logger.debug("getType: {}", info.getType());
73 if (info.getType() != null) {
74 if (info.getType().equalsIgnoreCase(getServiceType())) {
75 String devId = info.getPropertyString("uuid");
77 logger.info("Discovered a KMXXX gateway with name: '{}' id: '{}'", info.getName(), devId);
78 if (devId.isEmpty()) {
79 /* If something is wrong then we are generating a random UUID */
80 logger.debug("Error in automatic device-id detection. Using random value");
81 Random rnd = new Random();
82 devId = String.valueOf(rnd.nextLong());
84 return new ThingUID(typeUID, devId);
86 logger.debug("No uuid property found");
95 public String getServiceType() {
96 return "_http._tcp.local.";
99 private @Nullable ThingTypeUID getThingTypeUID(ServiceInfo info) {
100 InetAddress[] addrs = info.getInetAddresses();
101 if (addrs.length > 0) {
102 String hardwareID = info.getPropertyString("hwversion");
103 logger.debug("hardwareID: {}", hardwareID);
104 if (hardwareID != null && hardwareID.contains("iCom_Low")) {
105 return THING_TYPE_KMDEVICE;