2 * Copyright (c) 2010-2024 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.androidtv.internal.discovery;
15 import static org.openhab.binding.androidtv.internal.AndroidTVBindingConstants.*;
17 import java.net.InetAddress;
21 import javax.jmdns.ServiceInfo;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
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 * Implementation of {@link MDNSDiscoveryParticipant} that will discover SHIELDTV(s).
37 * @author Ben Rosenblum - initial contribution
40 @Component(service = MDNSDiscoveryParticipant.class, immediate = true, configurationPid = "discovery.shieldtv")
41 public class ShieldTVDiscoveryParticipant implements MDNSDiscoveryParticipant {
43 private final Logger logger = LoggerFactory.getLogger(ShieldTVDiscoveryParticipant.class);
44 private static final String SHIELDTV_MDNS_SERVICE_TYPE = "_nv_shield_remote._tcp.local.";
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return SUPPORTED_THING_TYPES;
52 public String getServiceType() {
53 return SHIELDTV_MDNS_SERVICE_TYPE;
57 public @Nullable DiscoveryResult createResult(@Nullable ServiceInfo service) {
58 if (service == null || !service.hasData()) {
62 InetAddress[] ipAddresses = service.getInet4Addresses();
64 if (ipAddresses.length > 0) {
65 String ipAddress = ipAddresses[0].getHostAddress();
66 String serverId = service.getPropertyString("SERVER");
67 String serverCapability = service.getPropertyString("SERVER_CAPABILITY");
69 if (logger.isDebugEnabled()) {
70 String nice = service.getNiceTextString();
71 String qualifiedName = service.getQualifiedName();
72 logger.debug("ShieldTV mDNS discovery notified of ShieldTV mDNS service: {}", nice);
73 logger.trace("ShieldTV mDNS service qualifiedName: {}", qualifiedName);
74 logger.trace("ShieldTV mDNS service ipAddresses: {} ({})", ipAddresses, ipAddresses.length);
75 logger.trace("ShieldTV mDNS service selected ipAddress: {}", ipAddress);
76 logger.trace("ShieldTV mDNS service property SERVER: {}", serverId);
77 logger.trace("ShieldTV mDNS service property SERVER_CAPABILITY: {}", serverCapability);
80 final ThingUID uid = getThingUID(service);
82 final String id = uid.getId();
83 final String label = service.getName() + " (" + id + ")";
84 final Map<String, Object> properties = Map.of(PARAMETER_IP_ADDRESS, ipAddress);
86 return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
96 public @Nullable ThingUID getThingUID(@Nullable ServiceInfo service) {
97 if (service == null || !service.hasData() || (service.getPropertyString("SERVER") == null)) {
101 return new ThingUID(THING_TYPE_SHIELDTV, service.getPropertyString("SERVER").substring(8));