From 817de3868399c2c5297bee09eaa95e5b43473179 Mon Sep 17 00:00:00 2001 From: jlaur Date: Mon, 20 Dec 2021 20:32:48 +0100 Subject: [PATCH] [hdpowerview] Update positions after triggering scene/scene group (#11768) * Update positions after triggering scene/scene group. * Compare enum values directly with == * Fix re-entrant calls. Fixes #11697 Signed-off-by: Jacob Laursen --- .../internal/HDPowerViewWebTargets.java | 1 - .../handler/HDPowerViewHubHandler.java | 30 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewWebTargets.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewWebTargets.java index 8fee72166c96a..16efb896a6a98 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewWebTargets.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewWebTargets.java @@ -237,7 +237,6 @@ public void enableScheduledEvent(int scheduledEventId, boolean enable) * @param query the http query parameter * @param jsonCommand the request command content (as a json string) * @return the response content (as a json string) - * @throws HubProcessingException * @throws HubMaintenanceException * @throws HubProcessingException */ diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java index 9a75c03f318d3..10936ed60f207 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewHubHandler.java @@ -72,11 +72,13 @@ * * @author Andy Lintner - Initial contribution * @author Andrew Fiddian-Green - Added support for secondary rail positions - * @author Jacob Laursen - Add support for scene groups and automations + * @author Jacob Laursen - Added support for scene groups and automations */ @NonNullByDefault public class HDPowerViewHubHandler extends BaseBridgeHandler { + private static final long INITIAL_SOFT_POLL_DELAY_MS = 5_000; + private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubHandler.class); private final HttpClient httpClient; private final HDPowerViewTranslationProvider translationProvider; @@ -113,7 +115,7 @@ public HDPowerViewHubHandler(Bridge bridge, HttpClient httpClient, @Override public void handleCommand(ChannelUID channelUID, Command command) { - if (RefreshType.REFRESH.equals(command)) { + if (RefreshType.REFRESH == command) { requestRefreshShadePositions(); return; } @@ -129,12 +131,16 @@ public void handleCommand(ChannelUID channelUID, Command command) { throw new ProcessingException("Web targets not initialized"); } int id = Integer.parseInt(channelUID.getIdWithoutGroup()); - if (sceneChannelTypeUID.equals(channel.getChannelTypeUID()) && OnOffType.ON.equals(command)) { + if (sceneChannelTypeUID.equals(channel.getChannelTypeUID()) && OnOffType.ON == command) { webTargets.activateScene(id); - } else if (sceneGroupChannelTypeUID.equals(channel.getChannelTypeUID()) && OnOffType.ON.equals(command)) { + // Reschedule soft poll for immediate shade position update. + scheduleSoftPoll(0); + } else if (sceneGroupChannelTypeUID.equals(channel.getChannelTypeUID()) && OnOffType.ON == command) { webTargets.activateSceneCollection(id); + // Reschedule soft poll for immediate shade position update. + scheduleSoftPoll(0); } else if (automationChannelTypeUID.equals(channel.getChannelTypeUID())) { - webTargets.enableScheduledEvent(id, OnOffType.ON.equals(command)); + webTargets.enableScheduledEvent(id, OnOffType.ON == command); } } catch (HubMaintenanceException e) { // exceptions are logged in HDPowerViewWebTargets @@ -189,14 +195,22 @@ public void dispose() { } private void schedulePoll() { + scheduleSoftPoll(INITIAL_SOFT_POLL_DELAY_MS); + scheduleHardPoll(); + } + + private void scheduleSoftPoll(long initialDelay) { ScheduledFuture future = this.pollFuture; if (future != null) { future.cancel(false); } - logger.debug("Scheduling poll for 5000ms out, then every {}ms", refreshInterval); - this.pollFuture = scheduler.scheduleWithFixedDelay(this::poll, 5000, refreshInterval, TimeUnit.MILLISECONDS); + logger.debug("Scheduling poll for {} ms out, then every {} ms", initialDelay, refreshInterval); + this.pollFuture = scheduler.scheduleWithFixedDelay(this::poll, initialDelay, refreshInterval, + TimeUnit.MILLISECONDS); + } - future = this.hardRefreshPositionFuture; + private void scheduleHardPoll() { + ScheduledFuture future = this.hardRefreshPositionFuture; if (future != null) { future.cancel(false); }