Skip to content

Commit

Permalink
[velux] Enabled background discovery (openhab#9477)
Browse files Browse the repository at this point in the history
* [velux] enable background discovery
* [velux] spotless
* [velux] adopt reviewer feedback

Signed-off-by: Andrew Fiddian-Green <[email protected]>
  • Loading branch information
andrewfg authored Dec 28, 2020
1 parent 6da1bbf commit dbdee8c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public Set<String> call() throws Exception {

// 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -303,4 +309,21 @@ private void discoverBridges() {
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);
}
}
}

0 comments on commit dbdee8c

Please sign in to comment.