]> git.basschouten.com Git - openhab-addons.git/blob
4f3ace5dc5a295b4f8ea13424538b67cc4db3ccf
[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.km200.internal.discovery;
14
15 import static org.openhab.binding.km200.internal.KM200BindingConstants.THING_TYPE_KMDEVICE;
16
17 import java.net.InetAddress;
18 import java.util.Random;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
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;
34
35 /**
36  * The {@link KM200GatewayDiscoveryParticipant} class discovers gateways and adds the results to the inbox.
37  *
38  * @author Markus Eckhardt - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(configurationPid = "binding.km200")
42 public class KM200GatewayDiscoveryParticipant implements MDNSDiscoveryParticipant {
43
44     private static final Set<ThingTypeUID> SUPPORTED_ALL_THING_TYPES_UIDS = KM200GatewayHandler.SUPPORTED_THING_TYPES_UIDS;
45     private static final String IP4_ADDRESS = "ip4Address";
46
47     private final Logger logger = LoggerFactory.getLogger(KM200GatewayDiscoveryParticipant.class);
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51         return SUPPORTED_ALL_THING_TYPES_UIDS;
52     }
53
54     @Override
55     public @Nullable DiscoveryResult createResult(ServiceInfo info) {
56         ThingUID uid = getThingUID(info);
57         logger.debug("MDNS info: {}, uid: {}", info, uid);
58         if (uid != null) {
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();
64         }
65         return null;
66     }
67
68     @Override
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");
76                     if (null != devId) {
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());
83                         }
84                         return new ThingUID(typeUID, devId);
85                     } else {
86                         logger.debug("No uuid property found");
87                     }
88                 }
89             }
90         }
91         return null;
92     }
93
94     @Override
95     public String getServiceType() {
96         return "_http._tcp.local.";
97     }
98
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;
106             }
107         }
108         return null;
109     }
110 }