]> git.basschouten.com Git - openhab-addons.git/commitdiff
[velux] Enabled background discovery (#9477)
authorAndrew Fiddian-Green <software@whitebear.ch>
Mon, 28 Dec 2020 01:55:20 +0000 (01:55 +0000)
committerGitHub <noreply@github.com>
Mon, 28 Dec 2020 01:55:20 +0000 (17:55 -0800)
* [velux] enable background discovery
* [velux] spotless
* [velux] adopt reviewer feedback

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxBridgeFinder.java
bundles/org.openhab.binding.velux/src/main/java/org/openhab/binding/velux/internal/discovery/VeluxDiscoveryService.java

index 1c3511307f9bccae37eeb8411bf13fc4f6e09d4a..2233d84aa8a92270ab41ecec9c4c0795c68893f5 100644 (file)
@@ -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;
 
index 4eadd57ae1cdaa0207233dfded8c9ba3a0416e27..fc1761b0edb48abc0c9422193a3b02e1fc0b01ca 100644 (file)
@@ -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<VeluxBridgeHandler> 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);
+        }
+    }
 }