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.mqtt.discovery;
15 import java.util.Date;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.atomic.AtomicBoolean;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Base MQTT discovery class. Responsible for connecting to the {@link MQTTTopicDiscoveryService}.
31 * Implement MQTT discovery services on top of this. You still need to reference
32 * the MQTTTopicDiscoveryService like in:
35 * @NonNullByDefault({})
37 * protected MQTTTopicDiscoveryService mqttTopicDiscovery;
40 * @author David Graeff - Initial contribution
43 public abstract class AbstractMQTTDiscovery extends AbstractDiscoveryService implements MQTTTopicDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(AbstractMQTTDiscovery.class);
46 protected final String subscribeTopic;
50 private @Nullable ScheduledFuture<?> scheduledStop;
52 private AtomicBoolean isSubscribed;
54 public AbstractMQTTDiscovery(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout,
55 boolean backgroundDiscoveryEnabledByDefault, String baseTopic) {
56 super(supportedThingTypes, 0, backgroundDiscoveryEnabledByDefault);
57 this.subscribeTopic = baseTopic;
58 this.timeout = timeout;
59 isSubscribed = new AtomicBoolean(false);
63 * Only subscribe if we were not already subscribed
65 private void subscribe() {
66 if (!isSubscribed.getAndSet(true)) {
67 getDiscoveryService().subscribe(this, subscribeTopic);
72 * Only unsubscribe if we were already subscribed
74 private void unSubscribe() {
75 if (isSubscribed.getAndSet(false)) {
76 getDiscoveryService().unsubscribe(this);
81 * Return the topic discovery service.
83 protected abstract MQTTTopicDiscoveryService getDiscoveryService();
85 private synchronized void stopTimeout() {
86 if (scheduledStop != null) {
87 scheduledStop.cancel(false);
92 protected synchronized void resetTimeout() {
95 // schedule an automatic call of stopScan when timeout is reached
97 Runnable runnable = new Runnable() {
102 } catch (Exception e) {
103 logger.debug("Exception occurred during execution: {}", e.getMessage(), e);
108 scheduledStop = scheduler.schedule(runnable, timeout, TimeUnit.SECONDS);
113 protected void startScan() {
114 if (isBackgroundDiscoveryEnabled()) {
123 protected synchronized void stopScan() {
124 if (isBackgroundDiscoveryEnabled()) {
134 public synchronized void abortScan() {
140 protected void startBackgroundDiscovery() {
141 // Remove results that are restored after a restart
142 removeOlderResults(new Date().getTime());
147 protected void stopBackgroundDiscovery() {