]> git.basschouten.com Git - openhab-addons.git/blob
b019cfc8f00555c3f33ff413375f2bf1c0cf5068
[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.vizio.internal.discovery;
14
15 import static org.openhab.binding.vizio.internal.VizioBindingConstants.*;
16
17 import java.net.InetAddress;
18 import java.util.Set;
19
20 import javax.jmdns.ServiceInfo;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link VizioDiscoveryParticipant} is responsible processing the
36  * results of searches for mDNS services of type _viziocast._tcp.local.
37  *
38  * @author Michael Lobstein - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(configurationPid = "discovery.vizio")
42 public class VizioDiscoveryParticipant implements MDNSDiscoveryParticipant {
43     private final Logger logger = LoggerFactory.getLogger(VizioDiscoveryParticipant.class);
44
45     @Override
46     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47         return SUPPORTED_THING_TYPES_UIDS;
48     }
49
50     @Override
51     public String getServiceType() {
52         return "_viziocast._tcp.local.";
53     }
54
55     @Override
56     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
57         DiscoveryResult result = null;
58
59         ThingUID thingUid = getThingUID(service);
60         if (thingUid != null) {
61             InetAddress ip = getIpAddress(service);
62             if (ip == null) {
63                 return null;
64             }
65             String inetAddress = ip.toString().substring(1); // trim leading slash
66             String label = service.getName();
67             int port = service.getPort();
68
69             result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withRepresentationProperty(PROPERTY_UUID)
70                     .withProperty(PROPERTY_UUID, thingUid.getId())
71                     .withProperty(Thing.PROPERTY_MODEL_ID, service.getPropertyString("mdl"))
72                     .withProperty(PROPERTY_HOST_NAME, inetAddress).withProperty(PROPERTY_PORT, port).build();
73             logger.debug("Created {} for Vizio TV at {}, name: '{}'", result, inetAddress, label);
74         }
75         return result;
76     }
77
78     /**
79      * @see org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant#getThingUID(javax.jmdns.ServiceInfo)
80      */
81     @Override
82     public @Nullable ThingUID getThingUID(ServiceInfo service) {
83         if (service.getType() != null && service.getType().equals(getServiceType())) {
84             String uidName = getUIDName(service);
85             return uidName != null ? new ThingUID(THING_TYPE_VIZIO_TV, uidName) : null;
86         }
87         return null;
88     }
89
90     /**
91      * Gets the UID name from the mdns record txt info (mac address), fall back with IP address
92      *
93      * @param service the mdns service
94      * @return the UID name
95      */
96     private @Nullable String getUIDName(ServiceInfo service) {
97         String uid = service.getPropertyString("eth");
98
99         if (uid == null || uid.endsWith("000") || uid.length() < 12) {
100             uid = service.getPropertyString("wifi");
101         }
102
103         if (uid == null || uid.endsWith("000") || uid.length() < 12) {
104             InetAddress ip = getIpAddress(service);
105             if (ip == null) {
106                 return null;
107             } else {
108                 uid = ip.toString();
109             }
110         }
111         return uid.replaceAll("[^A-Za-z0-9_]", "_");
112     }
113
114     /**
115      * {@link InetAddress} gets the IP address of the device in v4 or v6 format.
116      *
117      * @param ServiceInfo service
118      * @return InetAddress the IP address
119      *
120      */
121     private @Nullable InetAddress getIpAddress(ServiceInfo service) {
122         InetAddress address = null;
123         for (InetAddress addr : service.getInet4Addresses()) {
124             return addr;
125         }
126         // Fall back for Inet6addresses
127         for (InetAddress addr : service.getInet6Addresses()) {
128             return addr;
129         }
130         return address;
131     }
132 }