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.freeathomesystem.internal;
15 import java.time.Instant;
16 import java.util.HashMap;
17 import java.util.List;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.freeathomesystem.internal.datamodel.FreeAtHomeDeviceDescription;
26 import org.openhab.binding.freeathomesystem.internal.handler.FreeAtHomeBridgeHandler;
27 import org.openhab.binding.freeathomesystem.internal.util.FreeAtHomeHttpCommunicationException;
28 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link FreeAtHomeSystemDiscoveryService} is responsible for performing discovery of things
39 * @author Andras Uhrin - Initial contribution
42 public class FreeAtHomeSystemDiscoveryService extends AbstractThingHandlerDiscoveryService<FreeAtHomeBridgeHandler> {
44 private final Logger logger = LoggerFactory.getLogger(FreeAtHomeSystemDiscoveryService.class);
45 private @Nullable ScheduledFuture<?> backgroundDiscoveryJob = null;
47 private static final long BACKGROUND_DISCOVERY_DELAY = 1L;
48 private boolean isScanTerminated;
50 Runnable runnable = new Runnable() {
53 ThingUID bridgeUID = thingHandler.getThing().getUID();
55 List<String> deviceList;
58 deviceList = thingHandler.getDeviceDeviceList();
60 for (int i = 0; (i < deviceList.size()) && !isScanTerminated; i++) {
61 FreeAtHomeDeviceDescription device = thingHandler.getFreeatHomeDeviceDescription(deviceList.get(i));
63 ThingUID uid = new ThingUID(FreeAtHomeSystemBindingConstants.DEVICE_TYPE_UID, bridgeUID,
65 Map<String, Object> properties = new HashMap<>(1);
66 properties.put("deviceId", device.deviceId);
67 properties.put("interface", device.interfaceType);
69 String deviceLabel = device.deviceLabel;
71 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(uid).withLabel(deviceLabel)
72 .withRepresentationProperty("deviceId").withBridge(bridgeUID).withProperties(properties)
75 thingDiscovered(discoveryResult);
77 logger.debug("Thing discovered - DeviceId: {} - Device label: {}", device.getDeviceId(),
78 device.getDeviceLabel());
82 } catch (FreeAtHomeHttpCommunicationException e) {
83 logger.debug("Communication error in device discovery with the bridge: {}",
84 thingHandler.getThing().getLabel());
85 } catch (RuntimeException e) {
86 logger.debug("Scanning interrupted");
91 public FreeAtHomeSystemDiscoveryService(int timeout) {
92 super(FreeAtHomeBridgeHandler.class, FreeAtHomeSystemBindingConstants.SUPPORTED_THING_TYPES_UIDS, timeout,
96 public FreeAtHomeSystemDiscoveryService() {
101 public Set<ThingTypeUID> getSupportedThingTypes() {
102 return Set.of(FreeAtHomeSystemBindingConstants.BRIDGE_TYPE_UID);
106 protected void startScan() {
107 if (backgroundDiscoveryJob == null) {
108 this.removeOlderResults(Instant.now().toEpochMilli());
110 isScanTerminated = false;
111 backgroundDiscoveryJob = scheduler.schedule(runnable, BACKGROUND_DISCOVERY_DELAY, TimeUnit.SECONDS);
116 protected synchronized void stopScan() {
119 isScanTerminated = true;
121 ScheduledFuture<?> localDiscoveryJob = backgroundDiscoveryJob;
123 if (localDiscoveryJob != null) {
124 localDiscoveryJob.cancel(true);
127 backgroundDiscoveryJob = null;
129 removeOlderResults(Instant.now().toEpochMilli());
133 public void deactivate() {
134 removeOlderResults(Instant.now().toEpochMilli());