2 * Copyright (c) 2010-2024 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.Date;
18 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDDeviceType;
24 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.util.UIDUtils;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.ServiceScope;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link Tr064DiscoveryService} discovers sub devices of a root device.
38 * @author Jan N. Klug - Initial contribution
40 @Component(scope = ServiceScope.PROTOTYPE, service = Tr064DiscoveryService.class)
42 public class Tr064DiscoveryService extends AbstractThingHandlerDiscoveryService<Tr064RootHandler> {
43 private static final int SEARCH_TIME = 5;
44 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SUBDEVICE);
46 private final Logger logger = LoggerFactory.getLogger(Tr064DiscoveryService.class);
48 public Tr064DiscoveryService() {
49 super(Tr064RootHandler.class, SEARCH_TIME);
53 public void dispose() {
55 removeOlderResults(new Date().getTime(), thingHandler.getThing().getUID());
59 public Set<ThingTypeUID> getSupportedThingTypes() {
60 return SUPPORTED_THING_TYPES;
64 public void startScan() {
65 List<SCPDDeviceType> devices = thingHandler.getAllSubDevices();
66 ThingUID bridgeUID = thingHandler.getThing().getUID();
67 devices.forEach(device -> {
68 logger.trace("Trying to add {} to discovery results on {}", device, bridgeUID);
69 String udn = device.getUDN();
71 ThingTypeUID thingTypeUID;
72 if ("urn:dslforum-org:device:LANDevice:1".equals(device.getDeviceType())) {
73 thingTypeUID = THING_TYPE_SUBDEVICE_LAN;
75 thingTypeUID = THING_TYPE_SUBDEVICE;
77 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, UIDUtils.encode(udn));
79 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID) //
80 .withLabel(device.getFriendlyName()) //
81 .withBridge(bridgeUID) //
82 .withProperties(Map.of("uuid", udn, "deviceType", device.getDeviceType())) //
83 .withRepresentationProperty("uuid") //
85 thingDiscovered(result);
91 protected synchronized void stopScan() {
93 removeOlderResults(getTimestampOfLastScan());