]> git.basschouten.com Git - openhab-addons.git/blob
64551f0cbf85442dfc806e21027134c1fad1a1cb
[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 localHandler) {
62             bridgeHandler = localHandler;
63             localHandler.setDiscoveryService(this);
64             api = localHandler.getBridgeAPI();
65         }
66     }
67
68     @Override
69     public @Nullable ThingHandler getThingHandler() {
70         return bridgeHandler;
71     }
72
73     @Override
74     protected void startBackgroundDiscovery() {
75         discoverNow();
76     }
77
78     public synchronized void discoverNow() {
79         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
80         if (localDiscoveryJob != null) {
81             localDiscoveryJob.cancel(true);
82         }
83         discoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH_INTERVAL_MINUTES, TimeUnit.MINUTES);
84     }
85
86     @Override
87     protected synchronized void startScan() {
88         logger.debug("Start scan for Bond devices.");
89         try {
90             final ThingUID bridgeUid = bridgeHandler.getThing().getUID();
91             api = bridgeHandler.getBridgeAPI();
92             List<String> deviceList = api.getDevices();
93             if (deviceList != null) {
94                 for (final String deviceId : deviceList) {
95                     BondDevice thisDevice = api.getDevice(deviceId);
96                     String deviceName;
97                     if (thisDevice.type != null && (deviceName = thisDevice.name) != null) {
98                         final ThingUID deviceUid = new ThingUID(thisDevice.type.getThingTypeUID(), bridgeUid, deviceId);
99                         final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(deviceUid)
100                                 .withBridge(bridgeUid).withLabel(thisDevice.name)
101                                 .withProperty(CONFIG_DEVICE_ID, deviceId)
102                                 .withProperty(PROPERTIES_DEVICE_NAME, deviceName)
103                                 .withRepresentationProperty(CONFIG_DEVICE_ID).build();
104                         thingDiscovered(discoveryResult);
105                     }
106                 }
107             }
108         } catch (BondException ignored) {
109             logger.debug("Error getting devices for discovery: {}", ignored.getMessage());
110         } finally {
111             removeOlderResults(getTimestampOfLastScan());
112         }
113     }
114
115     @Override
116     protected void stopBackgroundDiscovery() {
117         stopScan();
118         ScheduledFuture<?> discoveryJob = this.discoveryJob;
119         if (discoveryJob != null) {
120             discoveryJob.cancel(true);
121             this.discoveryJob = null;
122         }
123     }
124 }