]> git.basschouten.com Git - openhab-addons.git/blob
78d4aabaa6020e6e05b1cbe2649fad8419d3dcfe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.tr064.internal;
14
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.*;
16
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
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;
34
35 /**
36  * The {@link Tr064DiscoveryService} discovers sub devices of a root device.
37  *
38  * @author Jan N. Klug - Initial contribution
39  */
40 @Component(scope = ServiceScope.PROTOTYPE, service = Tr064DiscoveryService.class)
41 @NonNullByDefault
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);
45
46     private final Logger logger = LoggerFactory.getLogger(Tr064DiscoveryService.class);
47
48     public Tr064DiscoveryService() {
49         super(Tr064RootHandler.class, SEARCH_TIME);
50     }
51
52     @Override
53     public void dispose() {
54         super.dispose();
55         removeOlderResults(new Date().getTime(), thingHandler.getThing().getUID());
56     }
57
58     @Override
59     public Set<ThingTypeUID> getSupportedThingTypes() {
60         return SUPPORTED_THING_TYPES;
61     }
62
63     @Override
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();
70             if (udn != null) {
71                 ThingTypeUID thingTypeUID;
72                 if ("urn:dslforum-org:device:LANDevice:1".equals(device.getDeviceType())) {
73                     thingTypeUID = THING_TYPE_SUBDEVICE_LAN;
74                 } else {
75                     thingTypeUID = THING_TYPE_SUBDEVICE;
76                 }
77                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, UIDUtils.encode(udn));
78
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") //
84                         .build();
85                 thingDiscovered(result);
86             }
87         });
88     }
89
90     @Override
91     protected synchronized void stopScan() {
92         super.stopScan();
93         removeOlderResults(getTimestampOfLastScan());
94     }
95 }