-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mqtt] Enable discovery timeout reset #6704
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,13 +12,17 @@ | |||||
*/ | ||||||
package org.openhab.binding.mqtt.discovery; | ||||||
|
||||||
import java.util.Date; | ||||||
import java.util.Set; | ||||||
import java.util.concurrent.ScheduledFuture; | ||||||
import java.util.concurrent.TimeUnit; | ||||||
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||||||
import org.eclipse.jdt.annotation.Nullable; | ||||||
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService; | ||||||
import org.eclipse.smarthome.core.thing.ThingTypeUID; | ||||||
|
||||||
import java.util.Date; | ||||||
import java.util.Set; | ||||||
import org.slf4j.Logger; | ||||||
import org.slf4j.LoggerFactory; | ||||||
|
||||||
/** | ||||||
* Base MQTT discovery class. Responsible for connecting to the {@link MQTTTopicDiscoveryService}. | ||||||
|
@@ -36,25 +40,60 @@ | |||||
*/ | ||||||
@NonNullByDefault | ||||||
public abstract class AbstractMQTTDiscovery extends AbstractDiscoveryService implements MQTTTopicDiscoveryParticipant { | ||||||
private final Logger logger = LoggerFactory.getLogger(AbstractMQTTDiscovery.class); | ||||||
|
||||||
protected final String subscribeTopic; | ||||||
|
||||||
private int timeout; | ||||||
|
||||||
private @Nullable ScheduledFuture<?> scheduledStop; | ||||||
|
||||||
public AbstractMQTTDiscovery(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, | ||||||
boolean backgroundDiscoveryEnabledByDefault, String baseTopic) { | ||||||
super(supportedThingTypes, timeout, backgroundDiscoveryEnabledByDefault); | ||||||
super(supportedThingTypes, 0, backgroundDiscoveryEnabledByDefault); | ||||||
this.subscribeTopic = baseTopic; | ||||||
this.timeout = timeout; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Return the topic discovery service. | ||||||
*/ | ||||||
protected abstract MQTTTopicDiscoveryService getDiscoveryService(); | ||||||
|
||||||
private synchronized void stopTimeout() { | ||||||
if (scheduledStop != null) { | ||||||
scheduledStop.cancel(false); | ||||||
scheduledStop = null; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
protected synchronized void resetTimeout() { | ||||||
stopTimeout(); | ||||||
|
||||||
// schedule an automatic call of stopScan when timeout is reached | ||||||
if (timeout > 0) { | ||||||
Runnable runnable = new Runnable() { | ||||||
@Override | ||||||
public void run() { | ||||||
try { | ||||||
stopScan(); | ||||||
} catch (Exception e) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really necessary to catch Exception here? What exception do you expect`? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above: the code is basically taken from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eclipse smarthome contains a lot of over engineered, pre Java-8 code that is only slowly refactored and converted into modern Java 11 code. I suggest not taking it as example code. |
||||||
logger.debug("Exception occurred during execution: {}", e.getMessage(), e); | ||||||
} | ||||||
} | ||||||
}; | ||||||
|
||||||
scheduledStop = scheduler.schedule(runnable, timeout, TimeUnit.SECONDS); | ||||||
} | ||||||
} | ||||||
|
||||||
@Override | ||||||
protected void startScan() { | ||||||
if (isBackgroundDiscoveryEnabled()) { | ||||||
super.stopScan(); | ||||||
return; | ||||||
} | ||||||
resetTimeout(); | ||||||
getDiscoveryService().subscribe(this, subscribeTopic); | ||||||
} | ||||||
|
||||||
|
@@ -64,10 +103,17 @@ protected synchronized void stopScan() { | |||||
super.stopScan(); | ||||||
return; | ||||||
} | ||||||
stopTimeout(); | ||||||
getDiscoveryService().unsubscribe(this); | ||||||
super.stopScan(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public synchronized void abortScan() { | ||||||
stopTimeout(); | ||||||
super.abortScan(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
protected void startBackgroundDiscovery() { | ||||||
// Remove results that are restored after a restart | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point?
This is a synchronized method. If it would not be, then there is a lot more to take care of....
Actually, most of the code is taken from
org.eclipse.smarthome.config.discovery.AbstractDiscoveryService
. If the code is not good, then we should refactor that class and build the resetTimeout method into that.