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.elroconnects.internal.discovery;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
17 import java.time.Instant;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants;
25 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceType;
26 import org.openhab.binding.elroconnects.internal.devices.ElroConnectsDevice;
27 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link ElroConnectsDiscoveryService} discovers devices connected to the ELRO Connects K1 Controller.
40 * @author Mark Herwege - Initial contribution
43 public class ElroConnectsDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
45 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDiscoveryService.class);
47 private @Nullable ElroConnectsBridgeHandler bridgeHandler;
49 private static final int TIMEOUT_S = 5;
50 private static final int REFRESH_INTERVAL_S = 60;
52 private @Nullable ScheduledFuture<?> discoveryJob;
54 public ElroConnectsDiscoveryService() {
55 super(ElroConnectsBindingConstants.SUPPORTED_DEVICE_TYPES_UIDS, TIMEOUT_S);
56 logger.debug("Discovery service started");
60 protected void startScan() {
64 private void discoverDevices() {
65 logger.debug("Starting device discovery scan");
66 ElroConnectsBridgeHandler bridge = bridgeHandler;
68 Map<Integer, ElroConnectsDevice> devices = bridge.getDevices();
69 ThingUID bridgeUID = bridge.getThing().getUID();
70 devices.entrySet().forEach(e -> {
71 String deviceId = e.getKey().toString();
72 String deviceName = e.getValue().getDeviceName();
73 String deviceType = e.getValue().getDeviceType();
74 if (!deviceType.isEmpty()) {
75 ElroDeviceType type = TYPE_MAP.get(deviceType);
77 ThingTypeUID thingTypeUID = THING_TYPE_MAP.get(type);
78 if (thingTypeUID != null) {
79 thingDiscovered(DiscoveryResultBuilder
80 .create(new ThingUID(thingTypeUID, bridgeUID, deviceId)).withLabel(deviceName)
81 .withBridge(bridgeUID).withProperty(CONFIG_DEVICE_ID, deviceId)
82 .withRepresentationProperty(CONFIG_DEVICE_ID).build());
91 protected synchronized void stopScan() {
93 removeOlderResults(getTimestampOfLastScan());
97 public void startBackgroundDiscovery() {
98 logger.debug("Start device background discovery");
99 ScheduledFuture<?> job = discoveryJob;
100 if (job == null || job.isCancelled()) {
101 discoveryJob = scheduler.scheduleWithFixedDelay(this::discoverDevices, 0, REFRESH_INTERVAL_S,
107 protected void stopBackgroundDiscovery() {
108 logger.debug("Stop device background discovery");
109 ScheduledFuture<?> job = discoveryJob;
110 if (job != null && !job.isCancelled()) {
117 public void deactivate() {
118 removeOlderResults(Instant.now().toEpochMilli());
123 public void setThingHandler(@Nullable ThingHandler handler) {
124 if (handler instanceof ElroConnectsBridgeHandler bridgeHandler) {
125 this.bridgeHandler = bridgeHandler;
126 bridgeHandler.setDiscoveryService(this);
131 public @Nullable ThingHandler getThingHandler() {
132 return bridgeHandler;