]> git.basschouten.com Git - openhab-addons.git/blob
b04653f37d329ffb2a9dfb565e3a194607a3b74b
[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.enigma2.internal.discovery;
14
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Stream;
21
22 import javax.jmdns.ServiceInfo;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.enigma2.internal.Enigma2BindingConstants;
27 import org.openhab.binding.enigma2.internal.Enigma2HttpClient;
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 Enigma2DiscoveryParticipant} is responsible processing the
39  * results of searches for mDNS services of type _http._tcp.local. and finding a webinterface
40  *
41  * @author Guido Dolfen - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = MDNSDiscoveryParticipant.class)
45 public class Enigma2DiscoveryParticipant implements MDNSDiscoveryParticipant {
46
47     private final Logger logger = LoggerFactory.getLogger(Enigma2DiscoveryParticipant.class);
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51         return Enigma2BindingConstants.SUPPORTED_THING_TYPES_UIDS;
52     }
53
54     @Override
55     public @Nullable DiscoveryResult createResult(ServiceInfo info) {
56         logger.debug("ServiceInfo {}", info);
57         String ipAddress = getIPAddress(info);
58         if (ipAddress != null && isEnigma2Device(ipAddress)) {
59             logger.debug("Enigma2 device discovered: IP-Adress={}, name={}", ipAddress, info.getName());
60             ThingUID uid = getThingUID(info);
61             if (uid != null) {
62                 Map<String, Object> properties = new HashMap<>();
63                 properties.put(Enigma2BindingConstants.CONFIG_HOST, ipAddress);
64                 properties.put(Enigma2BindingConstants.CONFIG_REFRESH, 5);
65                 properties.put(Enigma2BindingConstants.CONFIG_TIMEOUT, 5);
66                 return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(info.getName()).build();
67             }
68         }
69         return null;
70     }
71
72     @Override
73     public @Nullable ThingUID getThingUID(ServiceInfo info) {
74         logger.debug("ServiceInfo {}", info);
75         String ipAddress = getIPAddress(info);
76         if (ipAddress != null) {
77             return new ThingUID(Enigma2BindingConstants.THING_TYPE_DEVICE, ipAddress.replace(".", "_"));
78         }
79         return null;
80     }
81
82     @Override
83     public String getServiceType() {
84         return "_http._tcp.local.";
85     }
86
87     private boolean isEnigma2Device(String ipAddress) {
88         try {
89             return getEnigma2HttpClient().get("http://" + ipAddress + "/web/about").contains("e2enigmaversion");
90         } catch (IOException ignore) {
91             return false;
92         }
93     }
94
95     private @Nullable String getIPAddress(ServiceInfo info) {
96         InetAddress[] addresses = info.getInet4Addresses();
97         if (addresses.length > 1) {
98             logger.debug("Enigma2 device {} reports multiple addresses - using the first one! {}", info.getName(),
99                     addresses);
100         }
101         return Stream.of(addresses).findFirst().map(InetAddress::getHostAddress).orElse(null);
102     }
103
104     /**
105      * Getter for Test-Injection
106      * 
107      * @return HttpGet.
108      */
109     Enigma2HttpClient getEnigma2HttpClient() {
110         return new Enigma2HttpClient(5);
111     }
112 }