2 * Copyright (c) 2010-2024 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 localHandler) {
62 bridgeHandler = localHandler;
63 localHandler.setDiscoveryService(this);
64 api = localHandler.getBridgeAPI();
69 public @Nullable ThingHandler getThingHandler() {
74 protected void startBackgroundDiscovery() {
78 public synchronized void discoverNow() {
79 ScheduledFuture<?> localDiscoveryJob = discoveryJob;
80 if (localDiscoveryJob != null) {
81 localDiscoveryJob.cancel(true);
83 discoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH_INTERVAL_MINUTES, TimeUnit.MINUTES);
87 protected synchronized void startScan() {
88 logger.debug("Start scan for Bond devices.");
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);
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);
108 } catch (BondException ignored) {
109 logger.debug("Error getting devices for discovery: {}", ignored.getMessage());
111 removeOlderResults(getTimestampOfLastScan());
116 protected void stopBackgroundDiscovery() {
118 ScheduledFuture<?> discoveryJob = this.discoveryJob;
119 if (discoveryJob != null) {
120 discoveryJob.cancel(true);
121 this.discoveryJob = null;