]> git.basschouten.com Git - openhab-addons.git/blob
720ff892898d7306173c26dc77450444bfba4490
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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(immediate = true, configurationPid = "binding.km200")
42 public class KM200GatewayDiscoveryParticipant implements MDNSDiscoveryParticipant {
43
44     private final Logger logger = LoggerFactory.getLogger(KM200GatewayDiscoveryParticipant.class);
45
46     public static final Set<ThingTypeUID> SUPPORTED_ALL_THING_TYPES_UIDS = KM200GatewayHandler.SUPPORTED_THING_TYPES_UIDS;
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50         return SUPPORTED_ALL_THING_TYPES_UIDS;
51     }
52
53     @Override
54     public @Nullable DiscoveryResult createResult(ServiceInfo info) {
55         DiscoveryResult discoveryResult = null;
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             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();
64
65             return discoveryResult;
66         }
67         return null;
68     }
69
70     @Override
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");
78                     if (null != devId) {
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());
85                         }
86                         ThingUID thinguid = new ThingUID(typeUID, devId);
87                         return thinguid;
88                     } else {
89                         logger.debug("No uuid property found");
90                     }
91                 }
92             }
93         }
94         return null;
95     }
96
97     @Override
98     public String getServiceType() {
99         return "_http._tcp.local.";
100     }
101
102     private @Nullable ThingTypeUID getThingTypeUID(ServiceInfo info) {
103         InetAddress[] addrs = info.getInetAddresses();
104         if (addrs.length > 0) {
105             String hardwareID;
106             hardwareID = info.getPropertyString("hwversion");
107             logger.debug("hardwareID: {}", hardwareID);
108             if (hardwareID != null && hardwareID.contains("iCom_Low")) {
109                 return THING_TYPE_KMDEVICE;
110             }
111         }
112         return null;
113     }
114 }