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.androiddebugbridge.internal.discovery;
15 import static org.openhab.binding.androiddebugbridge.internal.AndroidDebugBridgeBindingConstants.*;
17 import java.util.Dictionary;
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.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;
39 * The {@link FireTVStickMDNSDiscoveryParticipant} is responsible for discovering new and removed Fire TV Stick. It uses
40 * the central {@link MDNSDiscoveryService}.
42 * @author Christoph Weitkamp - Initial contribution
44 @Component(service = MDNSDiscoveryParticipant.class, configurationPid = "discovery.androiddebugbridge")
46 public class FireTVStickMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
48 private static final String SERVICE_TYPE = "_amzn-wplay._tcp.local.";
49 private static final String MDNS_PROPERTY_NAME = "n";
50 private static final String MDNS_PROPERTY_MAC_ADDRESS = "c";
52 private boolean isAutoDiscoveryEnabled = true;
55 protected void activate(ComponentContext componentContext) {
56 activateOrModifyService(componentContext);
60 protected void modified(ComponentContext componentContext) {
61 activateOrModifyService(componentContext);
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);
74 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
75 return SUPPORTED_THING_TYPES;
79 public String getServiceType() {
84 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
85 if (isAutoDiscoveryEnabled) {
86 ThingUID uid = getThingUID(service);
88 String ip = service.getHostAddresses()[0];
89 String macAddress = service.getPropertyString(MDNS_PROPERTY_MAC_ADDRESS);
90 String friendlyName = String.format("%s (%s)", service.getPropertyString(MDNS_PROPERTY_NAME), ip);
91 return DiscoveryResultBuilder.create(uid) //
92 .withProperties(Map.of( //
94 Thing.PROPERTY_MAC_ADDRESS, macAddress.toLowerCase())) //
95 .withLabel(friendlyName) //
96 .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS) //
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());