]> git.basschouten.com Git - openhab-addons.git/blob
6d3ce30c6ef70d6fe0963adcfef468a455e3ae36
[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.bondhome.internal.discovery;
14
15 import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
16
17 import java.util.List;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.bondhome.internal.BondException;
24 import org.openhab.binding.bondhome.internal.api.BondDevice;
25 import org.openhab.binding.bondhome.internal.api.BondHttpApi;
26 import org.openhab.binding.bondhome.internal.handler.BondBridgeHandler;
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.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * This class does discovery of discoverable things
38  *
39  * @author Sara Geleskie Damiano - Initial contribution
40  */
41 @NonNullByDefault
42 public class BondDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
43     private static final long REFRESH_INTERVAL_MINUTES = 60;
44     private final Logger logger = LoggerFactory.getLogger(BondDiscoveryService.class);
45     private @Nullable ScheduledFuture<?> discoveryJob;
46     private @Nullable BondBridgeHandler bridgeHandler;
47     private @Nullable BondHttpApi api;
48
49     public BondDiscoveryService() {
50         super(SUPPORTED_THING_TYPES, 10);
51         this.discoveryJob = null;
52     }
53
54     @Override
55     public void deactivate() {
56         super.deactivate();
57     }
58
59     @Override
60     public void setThingHandler(@Nullable ThingHandler handler) {
61         if (handler instanceof BondBridgeHandler) {
62             @Nullable
63             BondBridgeHandler localHandler = (BondBridgeHandler) handler;
64             bridgeHandler = localHandler;
65             localHandler.setDiscoveryService(this);
66             api = localHandler.getBridgeAPI();
67         }
68     }
69
70     @Override
71     public @Nullable ThingHandler getThingHandler() {
72         return bridgeHandler;
73     }
74
75     @Override
76     protected void startBackgroundDiscovery() {
77         discoverNow();
78     }
79
80     public synchronized void discoverNow() {
81         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
82         if (localDiscoveryJob != null) {
83             localDiscoveryJob.cancel(true);
84         }
85         discoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH_INTERVAL_MINUTES, TimeUnit.MINUTES);
86     }
87
88     @Override
89     protected synchronized void startScan() {
90         logger.debug("Start scan for Bond devices.");
91         try {
92             final ThingUID bridgeUid = bridgeHandler.getThing().getUID();
93             api = bridgeHandler.getBridgeAPI();
94             List<String> deviceList = api.getDevices();
95             if (deviceList != null) {
96                 for (final String deviceId : deviceList) {
97                     BondDevice thisDevice = api.getDevice(deviceId);
98                     String deviceName;
99                     if (thisDevice.type != null && (deviceName = thisDevice.name) != null) {
100                         final ThingUID deviceUid = new ThingUID(thisDevice.type.getThingTypeUID(), bridgeUid, deviceId);
101                         final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(deviceUid)
102                                 .withBridge(bridgeUid).withLabel(thisDevice.name)
103                                 .withProperty(CONFIG_DEVICE_ID, deviceId)
104                                 .withProperty(PROPERTIES_DEVICE_NAME, deviceName)
105                                 .withRepresentationProperty(CONFIG_DEVICE_ID).build();
106                         thingDiscovered(discoveryResult);
107                     }
108                 }
109             }
110         } catch (BondException ignored) {
111             logger.warn("Error getting devices for discovery: {}", ignored.getMessage());
112         } finally {
113             removeOlderResults(getTimestampOfLastScan());
114         }
115     }
116
117     @Override
118     protected void stopBackgroundDiscovery() {
119         stopScan();
120         ScheduledFuture<?> discoveryJob = this.discoveryJob;
121         if (discoveryJob != null) {
122             discoveryJob.cancel(true);
123             this.discoveryJob = null;
124         }
125     }
126 }