import static org.openhab.core.thing.Thing.*;
import java.io.IOException;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
private final Map<String, SensorStatusListener> sensorStatusListeners = new ConcurrentHashMap<>();
private final Map<String, GroupStatusListener> groupStatusListeners = new ConcurrentHashMap<>();
+ private List<Scene> lastScenes = new CopyOnWriteArrayList<>();
+ private Instant lastScenesRetrieval = Instant.MIN;
+
final ReentrantLock pollingLock = new ReentrantLock();
abstract class PollingRunnable implements Runnable {
protected void doConnectedRun() throws IOException, ApiException {
updateLights();
updateGroups();
+ if (lastScenesRetrieval.isBefore(Instant.now().minusSeconds(SCENE_POLLING_INTERVAL))) {
+ updateScenes();
+ lastScenesRetrieval = Instant.now();
+ }
}
private void updateLights() throws IOException, ApiException {
}
});
}
- };
- private final Runnable scenePollingRunnable = new PollingRunnable() {
- @Override
- protected void doConnectedRun() throws IOException, ApiException {
- List<Scene> scenes = hueBridge.getScenes();
- logger.trace("Scenes detected: {}", scenes);
+ private void updateScenes() throws IOException, ApiException {
+ lastScenes = hueBridge.getScenes();
+ logger.trace("Scenes detected: {}", lastScenes);
- setBridgeSceneChannelStateOptions(scenes, lastGroupStates);
- notifyGroupSceneUpdate(scenes);
+ setBridgeSceneChannelStateOptions(lastScenes, lastGroupStates);
+ notifyGroupSceneUpdate(lastScenes);
}
private void setBridgeSceneChannelStateOptions(List<Scene> scenes, Map<String, FullGroup> groups) {
private @Nullable Future<?> initJob;
private @Nullable ScheduledFuture<?> lightPollingJob;
private @Nullable ScheduledFuture<?> sensorPollingJob;
- private @Nullable ScheduledFuture<?> scenePollingJob;
private @NonNullByDefault({}) HueBridge hueBridge = null;
private @NonNullByDefault({}) HueBridgeConfig hueBridgeConfig = null;
sensorPollingJob = null;
}
- private void startScenePolling() {
- ScheduledFuture<?> job = scenePollingJob;
- if (job == null || job.isCancelled()) {
- // Delay the first execution to give a chance to have all group things registered
- scenePollingJob = scheduler.scheduleWithFixedDelay(scenePollingRunnable, 5, SCENE_POLLING_INTERVAL,
- TimeUnit.SECONDS);
- }
- }
-
- private void stopScenePolling() {
- ScheduledFuture<?> job = scenePollingJob;
- if (job != null) {
- job.cancel(true);
- }
- scenePollingJob = null;
- }
-
@Override
public void dispose() {
logger.debug("Disposing Hue Bridge handler ...");
}
stopLightPolling();
stopSensorPolling();
- stopScenePolling();
if (hueBridge != null) {
hueBridge = null;
}
private synchronized void onUpdate() {
startLightPolling();
startSensorPolling();
- startScenePolling();
}
/**
final FullGroup lastGroupState = lastGroupStates.get(groupId);
if (lastGroupState != null) {
groupStatusListener.onGroupAdded(lastGroupState);
+ if (!lastScenes.isEmpty()) {
+ groupStatusListener.onScenesUpdated(lastScenes);
+ }
}
return true;
}