]> git.basschouten.com Git - openhab-addons.git/blob
55923016066b796b31ed7401476e6b54dec7b976
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.androiddebugbridge.internal.discovery;
14
15 import static org.openhab.binding.androiddebugbridge.internal.AndroidDebugBridgeBindingConstants.*;
16
17 import java.util.Dictionary;
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.DiscoveryService;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.config.discovery.mdns.internal.MDNSDiscoveryService;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.ComponentContext;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Modified;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link AndroidTVMDNSDiscoveryParticipant} is responsible for discovering new and removed Android TV devices. It
42  * uses
43  * the central {@link MDNSDiscoveryService}.
44  *
45  * @author Miguel Álvarez - Initial contribution
46  */
47 @Component(service = MDNSDiscoveryParticipant.class, configurationPid = "discovery.androiddebugbridge")
48 @NonNullByDefault
49 public class AndroidTVMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
50
51     private static final String SERVICE_TYPE = "_androidtvremote2._tcp.local.";
52     private static final String MDNS_PROPERTY_MAC_ADDRESS = "bt";
53     private final Logger logger = LoggerFactory.getLogger(AndroidTVMDNSDiscoveryParticipant.class);
54
55     private boolean isAutoDiscoveryEnabled = true;
56
57     @Activate
58     protected void activate(ComponentContext componentContext) {
59         activateOrModifyService(componentContext);
60     }
61
62     @Modified
63     protected void modified(ComponentContext componentContext) {
64         activateOrModifyService(componentContext);
65     }
66
67     private void activateOrModifyService(ComponentContext componentContext) {
68         Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
69         String autoDiscoveryPropertyValue = (String) properties
70                 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
71         if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
72             isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
73         }
74     }
75
76     @Override
77     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
78         return SUPPORTED_THING_TYPES;
79     }
80
81     @Override
82     public String getServiceType() {
83         return SERVICE_TYPE;
84     }
85
86     @Override
87     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
88         if (isAutoDiscoveryEnabled) {
89             ThingUID uid = getThingUID(service);
90             if (uid != null) {
91                 String ip = service.getHostAddresses()[0];
92                 String macAddress = service.getPropertyString(MDNS_PROPERTY_MAC_ADDRESS);
93                 String friendlyName = String.format("%s (%s)", service.getName(), ip);
94                 return DiscoveryResultBuilder.create(uid) //
95                         .withProperties(Map.of( //
96                                 PARAMETER_IP, ip, //
97                                 Thing.PROPERTY_MAC_ADDRESS, macAddress.toLowerCase())) //
98                         .withLabel(friendlyName) //
99                         .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS) //
100                         .build();
101             }
102         }
103         return null;
104     }
105
106     @Override
107     public @Nullable ThingUID getThingUID(ServiceInfo service) {
108         String macAddress = service.getPropertyString(MDNS_PROPERTY_MAC_ADDRESS);
109         if (macAddress != null && !macAddress.isBlank()) {
110             return new ThingUID(THING_TYPE_ANDROID_DEVICE, macAddress.replaceAll(":", "").toLowerCase());
111         }
112         return null;
113     }
114 }