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