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.tr064.internal;
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.*;
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.List;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDDeviceType;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.BridgeHandler;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.openhab.core.util.UIDUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link Tr064DiscoveryService} discovers sub devices of a root device.
42 * @author Jan N. Klug - Initial contribution
45 public class Tr064DiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
46 private static final int SEARCH_TIME = 5;
47 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SUBDEVICE);
49 private final Logger logger = LoggerFactory.getLogger(Tr064DiscoveryService.class);
50 private @Nullable Tr064RootHandler bridgeHandler;
52 public Tr064DiscoveryService() {
57 public void setThingHandler(ThingHandler thingHandler) {
58 if (thingHandler instanceof Tr064RootHandler) {
59 this.bridgeHandler = (Tr064RootHandler) thingHandler;
64 public @Nullable ThingHandler getThingHandler() {
69 public void deactivate() {
70 BridgeHandler bridgeHandler = this.bridgeHandler;
71 if (bridgeHandler == null) {
72 logger.warn("Bridgehandler not found, could not cleanup discovery results.");
75 removeOlderResults(new Date().getTime(), bridgeHandler.getThing().getUID());
79 public Set<ThingTypeUID> getSupportedThingTypes() {
80 return SUPPORTED_THING_TYPES;
84 public void startScan() {
85 Tr064RootHandler bridgeHandler = this.bridgeHandler;
86 if (bridgeHandler == null) {
87 logger.warn("Could not start discovery, bridge handler not set");
90 List<SCPDDeviceType> devices = bridgeHandler.getAllSubDevices();
91 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
92 devices.forEach(device -> {
93 logger.trace("Trying to add {} to discovery results on {}", device, bridgeUID);
94 String udn = device.getUDN();
96 ThingTypeUID thingTypeUID;
97 if ("urn:dslforum-org:device:LANDevice:1".equals(device.getDeviceType())) {
98 thingTypeUID = THING_TYPE_SUBDEVICE_LAN;
100 thingTypeUID = THING_TYPE_SUBDEVICE;
102 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, UIDUtils.encode(udn));
104 Map<String, Object> properties = new HashMap<>(2);
105 properties.put("uuid", udn);
106 properties.put("deviceType", device.getDeviceType());
108 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(device.getFriendlyName())
109 .withBridge(bridgeHandler.getThing().getUID()).withProperties(properties)
110 .withRepresentationProperty("uuid").build();
111 thingDiscovered(result);
117 protected synchronized void stopScan() {
119 removeOlderResults(getTimestampOfLastScan());