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.vizio.internal.discovery;
15 import static org.openhab.binding.vizio.internal.VizioBindingConstants.*;
17 import java.net.InetAddress;
20 import javax.jmdns.ServiceInfo;
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;
35 * The {@link VizioDiscoveryParticipant} is responsible processing the
36 * results of searches for mDNS services of type _viziocast._tcp.local.
38 * @author Michael Lobstein - Initial contribution
41 @Component(configurationPid = "discovery.vizio")
42 public class VizioDiscoveryParticipant implements MDNSDiscoveryParticipant {
43 private final Logger logger = LoggerFactory.getLogger(VizioDiscoveryParticipant.class);
46 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47 return SUPPORTED_THING_TYPES_UIDS;
51 public String getServiceType() {
52 return "_viziocast._tcp.local.";
56 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
57 DiscoveryResult result = null;
59 ThingUID thingUid = getThingUID(service);
60 if (thingUid != null) {
61 InetAddress ip = getIpAddress(service);
65 String inetAddress = ip.toString().substring(1); // trim leading slash
66 String label = service.getName();
67 int port = service.getPort();
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);
79 * @see org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant#getThingUID(javax.jmdns.ServiceInfo)
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;
91 * Gets the UID name from the mdns record txt info (mac address), fall back with IP address
93 * @param service the mdns service
94 * @return the UID name
96 private @Nullable String getUIDName(ServiceInfo service) {
97 String uid = service.getPropertyString("eth");
99 if (uid == null || uid.endsWith("000") || uid.length() < 12) {
100 uid = service.getPropertyString("wifi");
103 if (uid == null || uid.endsWith("000") || uid.length() < 12) {
104 InetAddress ip = getIpAddress(service);
111 return uid.replaceAll("[^A-Za-z0-9_]", "_");
115 * {@link InetAddress} gets the IP address of the device in v4 or v6 format.
117 * @param ServiceInfo service
118 * @return InetAddress the IP address
121 private @Nullable InetAddress getIpAddress(ServiceInfo service) {
122 InetAddress address = null;
123 for (InetAddress addr : service.getInet4Addresses()) {
126 // Fall back for Inet6addresses
127 for (InetAddress addr : service.getInet6Addresses()) {