2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bondhome.internal.discovery;
15 import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
17 import java.util.List;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
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;
37 * This class does discovery of discoverable things
39 * @author Sara Geleskie Damiano - Initial contribution
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;
49 public BondDiscoveryService() {
50 super(SUPPORTED_THING_TYPES, 10);
51 this.discoveryJob = null;
55 public void deactivate() {
60 public void setThingHandler(@Nullable ThingHandler handler) {
61 if (handler instanceof BondBridgeHandler) {
63 BondBridgeHandler localHandler = (BondBridgeHandler) handler;
64 bridgeHandler = localHandler;
65 localHandler.setDiscoveryService(this);
66 api = localHandler.getBridgeAPI();
71 public @Nullable ThingHandler getThingHandler() {
76 protected void startBackgroundDiscovery() {
80 public synchronized void discoverNow() {
81 ScheduledFuture<?> localDiscoveryJob = discoveryJob;
82 if (localDiscoveryJob != null) {
83 localDiscoveryJob.cancel(true);
85 discoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH_INTERVAL_MINUTES, TimeUnit.MINUTES);
89 protected synchronized void startScan() {
90 logger.debug("Start scan for Bond devices.");
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);
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);
110 } catch (BondException ignored) {
111 logger.warn("Error getting devices for discovery: {}", ignored.getMessage());
113 removeOlderResults(getTimestampOfLastScan());
118 protected void stopBackgroundDiscovery() {
120 ScheduledFuture<?> discoveryJob = this.discoveryJob;
121 if (discoveryJob != null) {
122 discoveryJob.cancel(true);
123 this.discoveryJob = null;