]> git.basschouten.com Git - openhab-addons.git/blob
96c3852ce0687d70be57e25961db2091fd865ace
[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.androidtv.internal.discovery;
14
15 import static org.openhab.binding.androidtv.internal.AndroidTVBindingConstants.*;
16
17 import java.net.InetAddress;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
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;
33
34 /**
35  * Implementation of {@link MDNSDiscoveryParticipant} that will discover GOOGLETV(s).
36  * 
37  * @author Ben Rosenblum - initial contribution
38  */
39 @NonNullByDefault
40 @Component(service = MDNSDiscoveryParticipant.class, immediate = true, configurationPid = "discovery.googletv")
41 public class GoogleTVDiscoveryParticipant implements MDNSDiscoveryParticipant {
42
43     private final Logger logger = LoggerFactory.getLogger(GoogleTVDiscoveryParticipant.class);
44     private static final String GOOGLETV_MDNS_SERVICE_TYPE = "_androidtvremote2._tcp.local.";
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return SUPPORTED_THING_TYPES;
49     }
50
51     @Override
52     public String getServiceType() {
53         return GOOGLETV_MDNS_SERVICE_TYPE;
54     }
55
56     @Override
57     public @Nullable DiscoveryResult createResult(@Nullable ServiceInfo service) {
58         if ((service == null) || !service.hasData()) {
59             return null;
60         }
61
62         InetAddress[] ipAddresses = service.getInet4Addresses();
63
64         if (ipAddresses.length > 0) {
65             String ipAddress = ipAddresses[0].getHostAddress();
66             String macAddress = service.getPropertyString("bt");
67
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);
76             }
77
78             final ThingUID uid = getThingUID(service);
79             if (uid != null) {
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);
83
84                 return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
85             } else {
86                 return null;
87             }
88         } else {
89             return null;
90         }
91     }
92
93     @Override
94     public @Nullable ThingUID getThingUID(@Nullable ServiceInfo service) {
95         if ((service == null) || !service.hasData() || (service.getPropertyString("bt") == null)) {
96             return null;
97         }
98
99         return new ThingUID(THING_TYPE_GOOGLETV, service.getPropertyString("bt").replace(":", ""));
100     }
101 }