]> git.basschouten.com Git - openhab-addons.git/blob
f48c3f6a3824639e397994d1ed22d4bf9da74ee7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.THING_TYPE_SUBDEVICE;
16 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.THING_TYPE_SUBDEVICE_LAN;
17
18 import java.util.*;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDDeviceType;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.binding.BridgeHandler;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerService;
31 import org.openhab.core.util.UIDUtils;
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 @NonNullByDefault
41 public class Tr064DiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
42     private static final int SEARCH_TIME = 5;
43     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SUBDEVICE);
44
45     private final Logger logger = LoggerFactory.getLogger(Tr064DiscoveryService.class);
46     private @Nullable Tr064RootHandler bridgeHandler;
47
48     public Tr064DiscoveryService() {
49         super(SEARCH_TIME);
50     }
51
52     @Override
53     public void setThingHandler(ThingHandler thingHandler) {
54         if (thingHandler instanceof Tr064RootHandler) {
55             this.bridgeHandler = (Tr064RootHandler) thingHandler;
56         }
57     }
58
59     @Override
60     public @Nullable ThingHandler getThingHandler() {
61         return bridgeHandler;
62     }
63
64     @Override
65     public void deactivate() {
66         BridgeHandler bridgeHandler = this.bridgeHandler;
67         if (bridgeHandler == null) {
68             logger.warn("Bridgehandler not found, could not cleanup discovery results.");
69             return;
70         }
71         removeOlderResults(new Date().getTime(), bridgeHandler.getThing().getUID());
72     }
73
74     @Override
75     public Set<ThingTypeUID> getSupportedThingTypes() {
76         return SUPPORTED_THING_TYPES;
77     }
78
79     @Override
80     public void startScan() {
81         Tr064RootHandler bridgeHandler = this.bridgeHandler;
82         if (bridgeHandler == null) {
83             logger.warn("Could not start discovery, bridge handler not set");
84             return;
85         }
86         List<SCPDDeviceType> devices = bridgeHandler.getAllSubDevices();
87         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
88         devices.forEach(device -> {
89             logger.trace("Trying to add {} to discovery results on {}", device, bridgeUID);
90             String udn = device.getUDN();
91             if (udn != null) {
92                 ThingTypeUID thingTypeUID;
93                 if ("urn:dslforum-org:device:LANDevice:1".equals(device.getDeviceType())) {
94                     thingTypeUID = THING_TYPE_SUBDEVICE_LAN;
95                 } else {
96                     thingTypeUID = THING_TYPE_SUBDEVICE;
97                 }
98                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, UIDUtils.encode(udn));
99
100                 Map<String, Object> properties = new HashMap<>(2);
101                 properties.put("uuid", udn);
102                 properties.put("deviceType", device.getDeviceType());
103
104                 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(device.getFriendlyName())
105                         .withBridge(bridgeHandler.getThing().getUID()).withProperties(properties)
106                         .withRepresentationProperty("uuid").build();
107                 thingDiscovered(result);
108             }
109         });
110     }
111
112     @Override
113     protected synchronized void stopScan() {
114         super.stopScan();
115         removeOlderResults(getTimestampOfLastScan());
116     }
117 }