]> git.basschouten.com Git - openhab-addons.git/blob
8cd56bce2795fb1f5558386c26e135e6e8c587c9
[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.draytonwiser.internal.discovery;
14
15 import static org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.*;
16
17 import java.net.InetAddress;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
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  * The {@link DraytonWiserMDNSDiscoveryParticipant} is responsible for discovering Drayton Wiser Heat Hubs. It uses the
39  * central MDNS Discovery Service.
40  *
41  * @author Andrew Schofield - Initial contribution
42  *
43  */
44 @NonNullByDefault
45 @Component(service = MDNSDiscoveryParticipant.class, configurationPid = "mdnsdiscovery.draytonwiser")
46 public class DraytonWiserMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
47
48     private final Logger logger = LoggerFactory.getLogger(DraytonWiserMDNSDiscoveryParticipant.class);
49     private final Pattern findIllegalChars = Pattern.compile("[^A-Za-z0-9_-]");
50
51     @Override
52     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53         return Collections.singleton(THING_TYPE_BRIDGE);
54     }
55
56     @Override
57     public String getServiceType() {
58         return "_http._tcp.local.";
59     }
60
61     @Override
62     public @Nullable DiscoveryResult createResult(final ServiceInfo service) {
63         if (service.getApplication().contains("http")) {
64             final ThingUID uid = getThingUID(service);
65
66             if (uid != null) {
67                 logger.debug("Discovered Heat Hub '{}' with uid: {}", service.getName(), uid);
68                 final Map<String, Object> properties = new HashMap<>(2);
69                 final InetAddress[] addresses = service.getInetAddresses();
70
71                 if (addresses.length > 0 && addresses[0] != null) {
72                     properties.put(PROP_ADDRESS, addresses[0].getHostAddress());
73                     properties.put(REFRESH_INTERVAL, DEFAULT_REFRESH_SECONDS);
74
75                     return DiscoveryResultBuilder.create(uid).withProperties(properties)
76                             .withRepresentationProperty(PROP_ADDRESS).withLabel("Heat Hub - " + service.getName())
77                             .build();
78                 }
79
80             }
81         }
82         return null;
83     }
84
85     @Override
86     public @Nullable ThingUID getThingUID(final ServiceInfo service) {
87         if (service.getType() != null && service.getType().equals(getServiceType())
88                 && service.getName().contains("WiserHeat")) {
89             logger.trace("Discovered a Drayton Wiser Heat Hub thing with name '{}'", service.getName());
90             return new ThingUID(THING_TYPE_BRIDGE, findIllegalChars.matcher(service.getName()).replaceAll(""));
91         }
92         return null;
93     }
94 }