[velux] Enabled background discovery (#9477)

* [velux] enable background discovery
* [velux] spotless
* [velux] adopt reviewer feedback

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green 2020-12-28 01:55:20 +00:00 committed by GitHub
parent 8cbd358b84
commit 27a02cac5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

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

View 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);
}
}
}