]> git.basschouten.com Git - openhab-addons.git/blob
7084f1602829d2ac45a6b883a9ffcc0bd3e77445
[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.tr064.internal;
14
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
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;
38
39 /**
40  * The {@link Tr064DiscoveryService} discovers sub devices of a root device.
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 @NonNullByDefault
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);
48
49     private final Logger logger = LoggerFactory.getLogger(Tr064DiscoveryService.class);
50     private @Nullable Tr064RootHandler bridgeHandler;
51
52     public Tr064DiscoveryService() {
53         super(SEARCH_TIME);
54     }
55
56     @Override
57     public void setThingHandler(ThingHandler thingHandler) {
58         if (thingHandler instanceof Tr064RootHandler) {
59             this.bridgeHandler = (Tr064RootHandler) thingHandler;
60         }
61     }
62
63     @Override
64     public @Nullable ThingHandler getThingHandler() {
65         return bridgeHandler;
66     }
67
68     @Override
69     public void deactivate() {
70         BridgeHandler bridgeHandler = this.bridgeHandler;
71         if (bridgeHandler == null) {
72             logger.warn("Bridgehandler not found, could not cleanup discovery results.");
73             return;
74         }
75         removeOlderResults(new Date().getTime(), bridgeHandler.getThing().getUID());
76     }
77
78     @Override
79     public Set<ThingTypeUID> getSupportedThingTypes() {
80         return SUPPORTED_THING_TYPES;
81     }
82
83     @Override
84     public void startScan() {
85         Tr064RootHandler bridgeHandler = this.bridgeHandler;
86         if (bridgeHandler == null) {
87             logger.warn("Could not start discovery, bridge handler not set");
88             return;
89         }
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();
95             if (udn != null) {
96                 ThingTypeUID thingTypeUID;
97                 if ("urn:dslforum-org:device:LANDevice:1".equals(device.getDeviceType())) {
98                     thingTypeUID = THING_TYPE_SUBDEVICE_LAN;
99                 } else {
100                     thingTypeUID = THING_TYPE_SUBDEVICE;
101                 }
102                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, UIDUtils.encode(udn));
103
104                 Map<String, Object> properties = new HashMap<>(2);
105                 properties.put("uuid", udn);
106                 properties.put("deviceType", device.getDeviceType());
107
108                 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(device.getFriendlyName())
109                         .withBridge(bridgeHandler.getThing().getUID()).withProperties(properties)
110                         .withRepresentationProperty("uuid").build();
111                 thingDiscovered(result);
112             }
113         });
114     }
115
116     @Override
117     protected synchronized void stopScan() {
118         super.stopScan();
119         removeOlderResults(getTimestampOfLastScan());
120     }
121 }