From 27a02cac5a01c16bdd40f4217d204074f8651cd7 Mon Sep 17 00:00:00 2001 From: Andrew Fiddian-Green Date: Mon, 28 Dec 2020 01:55:20 +0000 Subject: [PATCH] [velux] Enabled background discovery (#9477) * [velux] enable background discovery * [velux] spotless * [velux] adopt reviewer feedback Signed-off-by: Andrew Fiddian-Green --- .../internal/discovery/VeluxBridgeFinder.java | 1 - .../discovery/VeluxDiscoveryService.java | 23 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxBridgeFinder.java b/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxBridgeFinder.java index 1c3511307..2233d84aa 100644 --- a/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxBridgeFinder.java +++ b/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxBridgeFinder.java @@ -105,7 +105,6 @@ public class VeluxBridgeFinder implements Closeable { // create a multicast listener socket try (MulticastSocket rcvSocket = new MulticastSocket(MDNS_PORT)) { - final byte[] rcvBytes = new byte[BUFFER_SIZE]; final long finishTime = System.currentTimeMillis() + SEARCH_DURATION_MSECS; diff --git a/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxDiscoveryService.java b/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxDiscoveryService.java index 4eadd57ae..fc1761b0e 100644 --- a/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxDiscoveryService.java +++ b/bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxDiscoveryService.java @@ -16,8 +16,11 @@ import static org.openhab.binding.velux.internal.VeluxBindingConstants.*; import java.util.HashSet; 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.openhab.binding.velux.internal.VeluxBindingConstants; import org.openhab.binding.velux.internal.VeluxBindingProperties; import org.openhab.binding.velux.internal.config.VeluxBridgeConfiguration; @@ -60,6 +63,9 @@ public class VeluxDiscoveryService extends AbstractDiscoveryService implements R private Localization localization = Localization.UNKNOWN; private final Set bridgeHandlers = new HashSet<>(); + @Nullable + private ScheduledFuture backgroundTask = null; + // Private private void updateLocalization() { @@ -303,4 +309,21 @@ public class VeluxDiscoveryService extends AbstractDiscoveryService implements R thingDiscovered(result); } } + + @Override + protected void startBackgroundDiscovery() { + logger.trace("startBackgroundDiscovery() called."); + if (backgroundTask == null || backgroundTask.isCancelled()) { + this.backgroundTask = scheduler.scheduleWithFixedDelay(this::startScan, 10, 600, TimeUnit.SECONDS); + } + } + + @Override + protected void stopBackgroundDiscovery() { + logger.trace("stopBackgroundDiscovery() called."); + ScheduledFuture task = this.backgroundTask; + if (task != null) { + task.cancel(true); + } + } }