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.tradfri.internal.discovery;
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16 import static org.openhab.core.thing.Thing.*;
18 import java.util.HashMap;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import javax.jmdns.ServiceInfo;
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;
38 * This class identifies Tradfri gateways by their mDNS service information.
40 * @author Kai Kreuzer - Initial contribution
42 @Component(service = MDNSDiscoveryParticipant.class)
44 public class TradfriDiscoveryParticipant implements MDNSDiscoveryParticipant {
46 private final Logger logger = LoggerFactory.getLogger(TradfriDiscoveryParticipant.class);
48 private static final String SERVICE_TYPE = "_coap._udp.local.";
51 * RegEx patter to match the gateway name announced by mDNS
53 * gw:001122334455, gw-001122334455, gw:00-11-22-33-44-55, gw-001122334455ServiceName
56 private static final Pattern GATEWAY_NAME_REGEX_PATTERN = Pattern.compile("(gw[:-]{1}([a-f0-9]{2}[-]?){6}){1}");
59 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
60 return SUPPORTED_BRIDGE_TYPES_UIDS;
64 public String getServiceType() {
69 public @Nullable ThingUID getThingUID(@Nullable ServiceInfo service) {
70 if (service != null) {
71 Matcher m = GATEWAY_NAME_REGEX_PATTERN.matcher(service.getName());
73 return new ThingUID(GATEWAY_TYPE_UID, m.group(1).replaceAll("[^A-Za-z0-9_]", ""));
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);
95 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
96 .withLabel("@text/discovery.gateway.label").withRepresentationProperty(PROPERTY_SERIAL_NUMBER)
99 logger.debug("Discovered Tradfri gateway doesn't have an IP address: {}", service);