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.draytonwiser.internal.discovery;
15 import static org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.*;
17 import java.net.InetAddress;
18 import java.util.HashMap;
21 import java.util.regex.Pattern;
23 import javax.jmdns.ServiceInfo;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link DraytonWiserMDNSDiscoveryParticipant} is responsible for discovering Drayton Wiser Heat Hubs. It uses the
38 * central MDNS Discovery Service.
40 * @author Andrew Schofield - Initial contribution
44 @Component(service = MDNSDiscoveryParticipant.class, configurationPid = "mdnsdiscovery.draytonwiser")
45 public class DraytonWiserMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
47 private final Logger logger = LoggerFactory.getLogger(DraytonWiserMDNSDiscoveryParticipant.class);
48 private final Pattern findIllegalChars = Pattern.compile("[^A-Za-z0-9_-]");
51 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52 return Set.of(THING_TYPE_BRIDGE);
56 public String getServiceType() {
57 return "_http._tcp.local.";
61 public @Nullable DiscoveryResult createResult(final ServiceInfo service) {
62 if (service.getApplication().contains("http")) {
63 final ThingUID uid = getThingUID(service);
66 logger.debug("Discovered Heat Hub '{}' with uid: {}", service.getName(), uid);
67 final Map<String, Object> properties = new HashMap<>(2);
68 final InetAddress[] addresses = service.getInetAddresses();
70 if (addresses.length > 0 && addresses[0] != null) {
71 properties.put(PROP_ADDRESS, addresses[0].getHostAddress());
72 properties.put(REFRESH_INTERVAL, DEFAULT_REFRESH_SECONDS);
74 return DiscoveryResultBuilder.create(uid).withProperties(properties)
75 .withRepresentationProperty(PROP_ADDRESS).withLabel("Heat Hub - " + service.getName())
84 public @Nullable ThingUID getThingUID(final ServiceInfo service) {
85 if (service.getType() != null && service.getType().equals(getServiceType())
86 && service.getName().contains("WiserHeat")) {
87 logger.trace("Discovered a Drayton Wiser Heat Hub thing with name '{}'", service.getName());
88 return new ThingUID(THING_TYPE_BRIDGE, findIllegalChars.matcher(service.getName()).replaceAll(""));