]> git.basschouten.com Git - openhab-addons.git/blob
9fdf28ce02d1dc47ee8225aba11849d0e522d687
[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.tivo.internal.discovery;
14
15 import static org.openhab.binding.tivo.internal.TiVoBindingConstants.*;
16
17 import java.net.InetAddress;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
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.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The Class TiVoDiscoveryParticipant.
37  * *
38  *
39  * @author Jayson Kubilis (DigitalBytes) - Initial contribution
40  * @author Andrew Black (AndyXMB) - minor updates.
41  * @author Michael Lobstein - Updated for OH3
42  */
43 @NonNullByDefault
44 @Component(configurationPid = "discovery.tivo")
45 public class TiVoDiscoveryParticipant implements MDNSDiscoveryParticipant {
46     private final Logger logger = LoggerFactory.getLogger(TiVoDiscoveryParticipant.class);
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50         return Set.of(THING_TYPE_TIVO);
51     }
52
53     @Override
54     public String getServiceType() {
55         return "_tivo-remote._tcp.local.";
56     }
57
58     @Override
59     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
60         DiscoveryResult result = null;
61
62         ThingUID uid = getThingUID(service);
63         if (uid != null) {
64             Map<String, Object> properties = new HashMap<>(2);
65             // remove the domain from the name
66             InetAddress ip = getIpAddress(service);
67             if (ip == null) {
68                 return null;
69             }
70             String inetAddress = ip.toString().substring(1); // trim leading slash
71             String label = service.getName();
72             int port = service.getPort();
73
74             properties.put(CONFIG_HOST, inetAddress);
75             properties.put(CONFIG_PORT, port);
76
77             result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("Tivo: " + label)
78                     .withProperty(CONFIG_HOST, inetAddress).withRepresentationProperty(CONFIG_HOST).build();
79             logger.debug("Created {} for TiVo host '{}' name '{}'", result, inetAddress, label);
80         }
81         return result;
82     }
83
84     /**
85      * @see org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant#getThingUID(javax.jmdns.ServiceInfo)
86      */
87     @Override
88     public @Nullable ThingUID getThingUID(ServiceInfo service) {
89         if (service.getType() != null) {
90             if (service.getType().equals(getServiceType())) {
91                 String uidName = getUIDName(service);
92                 return new ThingUID(THING_TYPE_TIVO, uidName);
93             }
94         }
95         return null;
96     }
97
98     /**
99      * Gets the UID name, replacing any non AlphaNumeric characters with underscores.
100      *
101      * @param service the service
102      * @return the UID name
103      */
104     private String getUIDName(ServiceInfo service) {
105         return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
106     }
107
108     /**
109      * {@link InetAddress} gets the IP address of the device in v4 or v6 format.
110      *
111      * @param ServiceInfo service
112      * @return InetAddress the IP address
113      *
114      */
115     private @Nullable InetAddress getIpAddress(ServiceInfo service) {
116         InetAddress address = null;
117         for (InetAddress addr : service.getInet4Addresses()) {
118             return addr;
119         }
120         // Fall back for Inet6addresses
121         for (InetAddress addr : service.getInet6Addresses()) {
122             return addr;
123         }
124         return address;
125     }
126 }