2 * Copyright (c) 2010-2022 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.*;
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.elroconnects.internal.ElroConnectsBindingConstants;
24 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceType;
25 import org.openhab.binding.elroconnects.internal.devices.ElroConnectsDevice;
26 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingTypeUID;
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 * The {@link ElroConnectsDiscoveryService} discovers devices connected to the ELRO Connects K1 Controller.
39 * @author Mark Herwege - Initial contribution
42 public class ElroConnectsDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
44 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDiscoveryService.class);
46 private @Nullable ElroConnectsBridgeHandler bridgeHandler;
48 private static final int TIMEOUT_SECONDS = 5;
49 private static final int REFRESH_INTERVAL_SECONDS = 60;
51 private @Nullable ScheduledFuture<?> discoveryJob;
53 public ElroConnectsDiscoveryService() {
54 super(ElroConnectsBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT_SECONDS);
55 logger.debug("Bridge discovery service started");
59 protected void startScan() {
63 private void discoverDevices() {
64 logger.debug("Starting device discovery scan");
65 ElroConnectsBridgeHandler bridge = bridgeHandler;
67 Map<Integer, ElroConnectsDevice> devices = bridge.getDevices();
68 ThingUID bridgeUID = bridge.getThing().getUID();
69 devices.entrySet().forEach(e -> {
70 String deviceId = e.getKey().toString();
71 String deviceName = e.getValue().getDeviceName();
72 String deviceType = e.getValue().getDeviceType();
73 if (!deviceType.isEmpty()) {
74 ElroDeviceType type = TYPE_MAP.get(deviceType);
76 ThingTypeUID thingTypeUID = THING_TYPE_MAP.get(type);
77 if (thingTypeUID != null) {
78 thingDiscovered(DiscoveryResultBuilder
79 .create(new ThingUID(thingTypeUID, bridgeUID, deviceId)).withLabel(deviceName)
80 .withBridge(bridgeUID).withProperty(CONFIG_DEVICE_ID, deviceId)
81 .withRepresentationProperty(CONFIG_DEVICE_ID).build());
90 protected synchronized void stopScan() {
92 removeOlderResults(getTimestampOfLastScan());
96 protected void startBackgroundDiscovery() {
97 logger.debug("Start device background discovery");
98 ScheduledFuture<?> job = discoveryJob;
99 if (job == null || job.isCancelled()) {
100 discoveryJob = scheduler.scheduleWithFixedDelay(this::discoverDevices, 0, REFRESH_INTERVAL_SECONDS,
106 protected void stopBackgroundDiscovery() {
107 logger.debug("Stop device background discovery");
108 ScheduledFuture<?> job = discoveryJob;
116 public void deactivate() {
121 public void setThingHandler(@Nullable ThingHandler handler) {
122 if (handler instanceof ElroConnectsBridgeHandler) {
123 bridgeHandler = (ElroConnectsBridgeHandler) handler;
128 public @Nullable ThingHandler getThingHandler() {
129 return bridgeHandler;