Skip to content

Commit

Permalink
trap polling Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
NickWaterton committed Dec 26, 2021
1 parent 5a32841 commit 2798ea3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -435,10 +437,27 @@ public void initialize() {
upnpService.getRegistry().addListener(this);

checkAndCreateServices();
}

logger.debug("{}: Start refresh task, interval={}", host, configuration.getRefreshInterval());
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, configuration.getRefreshInterval(),
TimeUnit.MILLISECONDS);
public void startPolling() {
try {
if (pollingJob == null || pollingJob.isCancelled() || pollingJob.isDone()) {
if (pollingJob != null && pollingJob.isDone()) {
pollingJob.get();
}
logger.debug("{}: Start refresh task, interval={}", host, configuration.getRefreshInterval());
pollingJob = scheduler.scheduleWithFixedDelay(this::poll, 0, configuration.getRefreshInterval(),
TimeUnit.MILLISECONDS);
}
} catch (CancellationException | InterruptedException | ExecutionException e) {
if (logger.isTraceEnabled()) {
logger.trace("{}: Polling Job Exception: ", host, e);
} else {
logger.debug("{}: Polling Job Exception: {}", host, e.getMessage());
}
pollingJob = null;
startPolling();
}
}

@Override
Expand Down Expand Up @@ -497,11 +516,19 @@ private boolean isDuplicateChannel(String channel) {
}

private void poll() {
// Skip channels if service is not connected/started
services.stream().filter(service -> service.checkConnection())
.forEach(service -> service.getSupportedChannelNames(true).stream()
.filter(channel -> isLinked(channel) && !isDuplicateChannel(channel))
.forEach(channel -> service.handleCommand(channel, RefreshType.REFRESH)));
try {
// Skip channels if service is not connected/started
services.stream().filter(service -> service.checkConnection())
.forEach(service -> service.getSupportedChannelNames(true).stream()
.filter(channel -> isLinked(channel) && !isDuplicateChannel(channel))
.forEach(channel -> service.handleCommand(channel, RefreshType.REFRESH)));
} catch (Exception e) {
if (logger.isTraceEnabled()) {
logger.trace("{}: Polling Job threw exception: ", host, e);
} else {
logger.debug("{}: Polling Job threw exception: {}", host, e.getMessage());
}
}
}

public synchronized void valueReceived(String variable, State value) {
Expand Down Expand Up @@ -559,6 +586,7 @@ private void checkAndCreateServices() {
putOffline();
}
logger.debug("{}: TV is {}online", host, isOnline ? "" : "NOT ");
startPolling();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public void powerUpdated(boolean on, boolean artMode) {
String powerState = fetchPowerState();
if (checkConnection() && "off".equals(powerState)) {
// retry if we are connected, but get "off' for powerState
logger.warn("Rechecking, received powerState '{}' but websocket is still connected", powerState);
logger.warn("{}: Rechecking, received powerState '{}' but websocket is still connected", host, powerState);
remoteController.getArtmodeStatus();
// powerState = fetchPowerState();
}
Expand Down

0 comments on commit 2798ea3

Please sign in to comment.