]> git.basschouten.com Git - openhab-addons.git/blob
154527045d759ea0dfe6a914e40cd783167c98ec
[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.tradfri.internal.discovery;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16 import static org.openhab.core.thing.Thing.*;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import javax.jmdns.ServiceInfo;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Component;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * This class identifies Tradfri gateways by their mDNS service information.
39  *
40  * @author Kai Kreuzer - Initial contribution
41  */
42 @Component(service = MDNSDiscoveryParticipant.class)
43 @NonNullByDefault
44 public class TradfriDiscoveryParticipant implements MDNSDiscoveryParticipant {
45
46     private final Logger logger = LoggerFactory.getLogger(TradfriDiscoveryParticipant.class);
47
48     private static final String SERVICE_TYPE = "_coap._udp.local.";
49
50     /**
51      * RegEx patter to match the gateway name announced by mDNS
52      * Possible values:
53      * gw:001122334455, gw-001122334455, gw:00-11-22-33-44-55, gw-001122334455ServiceName
54      *
55      */
56     private static final Pattern GATEWAY_NAME_REGEX_PATTERN = Pattern.compile("(gw[:-]{1}([a-f0-9]{2}[-]?){6}){1}");
57
58     @Override
59     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
60         return SUPPORTED_BRIDGE_TYPES_UIDS;
61     }
62
63     @Override
64     public String getServiceType() {
65         return SERVICE_TYPE;
66     }
67
68     @Override
69     public @Nullable ThingUID getThingUID(@Nullable ServiceInfo service) {
70         if (service != null) {
71             Matcher m = GATEWAY_NAME_REGEX_PATTERN.matcher(service.getName());
72             if (m.find()) {
73                 return new ThingUID(GATEWAY_TYPE_UID, m.group(1).replaceAll("[^A-Za-z0-9_]", ""));
74             }
75         }
76         return null;
77     }
78
79     @Override
80     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
81         ThingUID thingUID = getThingUID(service);
82         if (thingUID != null) {
83             if (service.getHostAddresses() != null && service.getHostAddresses().length > 0
84                     && !service.getHostAddresses()[0].isEmpty()) {
85                 logger.debug("Discovered Tradfri gateway: {}", service);
86                 Map<String, Object> properties = new HashMap<>(4);
87                 properties.put(PROPERTY_VENDOR, "IKEA of Sweden");
88                 properties.put(GATEWAY_CONFIG_HOST, service.getHostAddresses()[0]);
89                 properties.put(GATEWAY_CONFIG_PORT, service.getPort());
90                 properties.put(PROPERTY_SERIAL_NUMBER, service.getName());
91                 String fwVersion = service.getPropertyString("version");
92                 if (fwVersion != null) {
93                     properties.put(PROPERTY_FIRMWARE_VERSION, fwVersion);
94                 }
95                 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
96                         .withLabel("@text/discovery.gateway.label").withRepresentationProperty(PROPERTY_SERIAL_NUMBER)
97                         .build();
98             } else {
99                 logger.debug("Discovered Tradfri gateway doesn't have an IP address: {}", service);
100             }
101         }
102         return null;
103     }
104 }