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.elerotransmitterstick.internal.discovery;
15 import static org.openhab.binding.elerotransmitterstick.internal.EleroTransmitterStickBindingConstants.*;
17 import java.util.ArrayList;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.openhab.binding.elerotransmitterstick.internal.handler.EleroTransmitterStickHandler;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.ThingUID;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link EleroChannelDiscoveryService} is responsible for discovery of elero channels from an Elero Transmitter
34 * @author Volker Bier - Initial contribution
36 public class EleroChannelDiscoveryService extends AbstractDiscoveryService {
37 private static final int DISCOVER_TIMEOUT_SECONDS = 30;
38 private final Logger logger = LoggerFactory.getLogger(EleroChannelDiscoveryService.class);
40 private EleroTransmitterStickHandler bridge;
41 private ScheduledFuture<?> sensorDiscoveryJob;
44 * Creates the discovery service for the given handler and converter.
46 public EleroChannelDiscoveryService(EleroTransmitterStickHandler stickHandler) {
47 super(Set.of(THING_TYPE_ELERO_CHANNEL), DISCOVER_TIMEOUT_SECONDS, true);
49 bridge = stickHandler;
53 protected void startScan() {
58 protected synchronized void stopScan() {
60 removeOlderResults(getTimestampOfLastScan());
64 protected void startBackgroundDiscovery() {
65 logger.debug("Start Elero Channel background discovery");
66 if (sensorDiscoveryJob == null || sensorDiscoveryJob.isCancelled()) {
67 sensorDiscoveryJob = scheduler.scheduleWithFixedDelay(() -> {
69 }, 0, 2, TimeUnit.SECONDS);
74 protected void stopBackgroundDiscovery() {
75 logger.debug("Stop Elero Channel background discovery");
76 if (sensorDiscoveryJob != null && !sensorDiscoveryJob.isCancelled()) {
77 sensorDiscoveryJob.cancel(true);
78 sensorDiscoveryJob = null;
82 private void discoverSensors() {
83 if (bridge.getStick() == null) {
84 logger.debug("Stick not opened, scanning skipped.");
88 ArrayList<Integer> channelIds = bridge.getStick().getKnownIds();
89 if (channelIds.isEmpty()) {
90 logger.debug("Could not obtain known channels from the stick, scanning skipped.");
94 for (Integer id : channelIds) {
95 ThingUID sensorThing = new ThingUID(THING_TYPE_ELERO_CHANNEL, bridge.getThing().getUID(),
98 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(sensorThing).withLabel("Channel " + id)
99 .withRepresentationProperty("id").withBridge(bridge.getThing().getUID())
100 .withProperty(PROPERTY_CHANNEL_ID, id).build();
101 thingDiscovered(discoveryResult);