Skip to content

Commit

Permalink
discovery: Implement background scan
Browse files Browse the repository at this point in the history
Implement background discovery
  • Loading branch information
Sonic-Amiga committed May 29, 2020
1 parent ec41a86 commit 64bbc95
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
Expand All @@ -28,12 +30,34 @@ public class IRobotDiscoveryService extends AbstractDiscoveryService {

private final static Logger logger = LoggerFactory.getLogger(IRobotDiscoveryService.class);
private final Runnable scanner;
private ScheduledFuture<?> backgroundFuture;

public IRobotDiscoveryService() {
super(Collections.singleton(IRobotBindingConstants.THING_TYPE_ROOMBA), 30, true);
scanner = createScanner();
}

@Override
protected void startBackgroundDiscovery() {
logger.trace("Starting background discovery");
stopBackgroundScan();
backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
}

@Override
protected void stopBackgroundDiscovery() {
logger.trace("Stopping background discovery");
stopBackgroundScan();
super.stopBackgroundDiscovery();
}

private void stopBackgroundScan() {
if (backgroundFuture != null && !backgroundFuture.isDone()) {
backgroundFuture.cancel(true);
backgroundFuture = null;
}
}

@Override
protected void startScan() {
logger.trace("startScan");
Expand Down

0 comments on commit 64bbc95

Please sign in to comment.