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