]> git.basschouten.com Git - openhab-addons.git/blob
5b83320b1de9e344d25d42ff6e3f71fec1701e9d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.mqtt.discovery;
14
15 import java.util.Date;
16 import java.util.Set;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.config.discovery.AbstractDiscoveryService;
23 import org.openhab.core.thing.ThingTypeUID;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Base MQTT discovery class. Responsible for connecting to the {@link MQTTTopicDiscoveryService}.
29  *
30  * Implement MQTT discovery services on top of this. You still need to reference
31  * the MQTTTopicDiscoveryService like in:
32  *
33  * <pre>
34  * &#64;NonNullByDefault({})
35  * &#64;Reference
36  * protected MQTTTopicDiscoveryService mqttTopicDiscovery;
37  * </pre>
38  *
39  * @author David Graeff - Initial contribution
40  */
41 @NonNullByDefault
42 public abstract class AbstractMQTTDiscovery extends AbstractDiscoveryService implements MQTTTopicDiscoveryParticipant {
43     private final Logger logger = LoggerFactory.getLogger(AbstractMQTTDiscovery.class);
44
45     protected final String subscribeTopic;
46
47     private int timeout;
48
49     private @Nullable ScheduledFuture<?> scheduledStop;
50
51     public AbstractMQTTDiscovery(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout,
52             boolean backgroundDiscoveryEnabledByDefault, String baseTopic) {
53         super(supportedThingTypes, 0, backgroundDiscoveryEnabledByDefault);
54         this.subscribeTopic = baseTopic;
55         this.timeout = timeout;
56     }
57
58     /**
59      * Return the topic discovery service.
60      */
61     protected abstract MQTTTopicDiscoveryService getDiscoveryService();
62
63     private synchronized void stopTimeout() {
64         if (scheduledStop != null) {
65             scheduledStop.cancel(false);
66             scheduledStop = null;
67         }
68     }
69
70     protected synchronized void resetTimeout() {
71         stopTimeout();
72
73         // schedule an automatic call of stopScan when timeout is reached
74         if (timeout > 0) {
75             Runnable runnable = new Runnable() {
76                 @Override
77                 public void run() {
78                     try {
79                         stopScan();
80                     } catch (Exception e) {
81                         logger.debug("Exception occurred during execution: {}", e.getMessage(), e);
82                     }
83                 }
84             };
85
86             scheduledStop = scheduler.schedule(runnable, timeout, TimeUnit.SECONDS);
87         }
88     }
89
90     @Override
91     protected void startScan() {
92         if (isBackgroundDiscoveryEnabled()) {
93             super.stopScan();
94             return;
95         }
96         resetTimeout();
97         getDiscoveryService().subscribe(this, subscribeTopic);
98     }
99
100     @Override
101     protected synchronized void stopScan() {
102         if (isBackgroundDiscoveryEnabled()) {
103             super.stopScan();
104             return;
105         }
106         stopTimeout();
107         getDiscoveryService().unsubscribe(this);
108         super.stopScan();
109     }
110
111     @Override
112     public synchronized void abortScan() {
113         stopTimeout();
114         super.abortScan();
115     }
116
117     @Override
118     protected void startBackgroundDiscovery() {
119         // Remove results that are restored after a restart
120         removeOlderResults(new Date().getTime());
121         getDiscoveryService().subscribe(this, subscribeTopic);
122     }
123
124     @Override
125     protected void stopBackgroundDiscovery() {
126         getDiscoveryService().unsubscribe(this);
127     }
128 }