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