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.enigma2.internal.discovery;
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.util.HashMap;
20 import java.util.stream.Stream;
22 import javax.jmdns.ServiceInfo;
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;
38 * The {@link Enigma2DiscoveryParticipant} is responsible processing the
39 * results of searches for mDNS services of type _http._tcp.local. and finding a webinterface
41 * @author Guido Dolfen - Initial contribution
44 @Component(service = MDNSDiscoveryParticipant.class)
45 public class Enigma2DiscoveryParticipant implements MDNSDiscoveryParticipant {
47 private final Logger logger = LoggerFactory.getLogger(Enigma2DiscoveryParticipant.class);
50 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51 return Enigma2BindingConstants.SUPPORTED_THING_TYPES_UIDS;
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);
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();
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(".", "_"));
83 public String getServiceType() {
84 return "_http._tcp.local.";
87 private boolean isEnigma2Device(String ipAddress) {
89 return getEnigma2HttpClient().get("http://" + ipAddress + "/web/about").contains("e2enigmaversion");
90 } catch (IOException ignore) {
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(),
101 return Stream.of(addresses).findFirst().map(InetAddress::getHostAddress).orElse(null);
105 * Getter for Test-Injection
109 Enigma2HttpClient getEnigma2HttpClient() {
110 return new Enigma2HttpClient(5);