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.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 GOOGLETV(s).
37 * @author Ben Rosenblum - initial contribution
40 @Component(service = MDNSDiscoveryParticipant.class, immediate = true, configurationPid = "discovery.googletv")
41 public class GoogleTVDiscoveryParticipant implements MDNSDiscoveryParticipant {
43 private final Logger logger = LoggerFactory.getLogger(GoogleTVDiscoveryParticipant.class);
44 private static final String GOOGLETV_MDNS_SERVICE_TYPE = "_androidtvremote2._tcp.local.";
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return SUPPORTED_THING_TYPES;
52 public String getServiceType() {
53 return GOOGLETV_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 macAddress = service.getPropertyString("bt");
68 if (logger.isDebugEnabled()) {
69 String nice = service.getNiceTextString();
70 String qualifiedName = service.getQualifiedName();
71 logger.debug("GoogleTV mDNS discovery notified of GoogleTV mDNS service: {}", nice);
72 logger.trace("GoogleTV mDNS service qualifiedName: {}", qualifiedName);
73 logger.trace("GoogleTV mDNS service ipAddresses: {} ({})", ipAddresses, ipAddresses.length);
74 logger.trace("GoogleTV mDNS service selected ipAddress: {}", ipAddress);
75 logger.trace("GoogleTV mDNS service property macAddress: {}", macAddress);
78 final ThingUID uid = getThingUID(service);
80 final String id = uid.getId();
81 final String label = service.getName() + " (" + id + ")";
82 final Map<String, Object> properties = Map.of(PROPERTY_IP_ADDRESS, ipAddress);
84 return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
94 public @Nullable ThingUID getThingUID(@Nullable ServiceInfo service) {
95 if ((service == null) || !service.hasData() || (service.getPropertyString("bt") == null)) {
99 return new ThingUID(THING_TYPE_GOOGLETV, service.getPropertyString("bt").replace(":", ""));