]> git.basschouten.com Git - openhab-addons.git/blob
9d273a2ff4a3b6173f50f34fd9beea639f3009fd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.elerotransmitterstick.internal.discovery;
14
15 import static org.openhab.binding.elerotransmitterstick.internal.EleroTransmitterStickBindingConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
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;
29
30 /**
31  * The {@link EleroChannelDiscoveryService} is responsible for discovery of elero channels from an Elero Transmitter
32  * Stick.
33  *
34  * @author Volker Bier - Initial contribution
35  */
36 public class EleroChannelDiscoveryService extends AbstractDiscoveryService {
37     private static final int DISCOVER_TIMEOUT_SECONDS = 30;
38     private final Logger logger = LoggerFactory.getLogger(EleroChannelDiscoveryService.class);
39
40     private EleroTransmitterStickHandler bridge;
41     private ScheduledFuture<?> sensorDiscoveryJob;
42
43     /**
44      * Creates the discovery service for the given handler and converter.
45      */
46     public EleroChannelDiscoveryService(EleroTransmitterStickHandler stickHandler) {
47         super(Collections.singleton(THING_TYPE_ELERO_CHANNEL), DISCOVER_TIMEOUT_SECONDS, true);
48
49         bridge = stickHandler;
50     }
51
52     @Override
53     protected void startScan() {
54         discoverSensors();
55     }
56
57     @Override
58     protected synchronized void stopScan() {
59         super.stopScan();
60         removeOlderResults(getTimestampOfLastScan());
61     }
62
63     @Override
64     protected void startBackgroundDiscovery() {
65         logger.debug("Start Elero Channel background discovery");
66         if (sensorDiscoveryJob == null || sensorDiscoveryJob.isCancelled()) {
67             sensorDiscoveryJob = scheduler.scheduleWithFixedDelay(() -> {
68                 discoverSensors();
69             }, 0, 2, TimeUnit.SECONDS);
70         }
71     }
72
73     @Override
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;
79         }
80     }
81
82     private void discoverSensors() {
83         if (bridge.getStick() == null) {
84             logger.debug("Stick not opened, scanning skipped.");
85             return;
86         }
87
88         ArrayList<Integer> channelIds = bridge.getStick().getKnownIds();
89         if (channelIds.isEmpty()) {
90             logger.debug("Could not obtain known channels from the stick, scanning skipped.");
91             return;
92         }
93
94         for (Integer id : channelIds) {
95             ThingUID sensorThing = new ThingUID(THING_TYPE_ELERO_CHANNEL, bridge.getThing().getUID(),
96                     String.valueOf(id));
97
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);
102         }
103     }
104 }