From 4ff238d3f4216bdf461b895c102c41a8c108fbc7 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Fri, 2 Apr 2021 22:31:35 +0200 Subject: [PATCH] [somfytahoma] Faster feedback for commands initiated by the binding (#10409) Also fix references to OH2 Signed-off-by: Laurent Garnier --- .../handler/SomfyTahomaBridgeHandler.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java index 8895d8288..2fa94bfe9 100644 --- a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java +++ b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java @@ -166,9 +166,7 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { */ private void initPolling() { stopPolling(); - pollFuture = scheduler.scheduleWithFixedDelay(() -> { - getTahomaUpdates(); - }, 10, thingConfig.getRefresh(), TimeUnit.SECONDS); + scheduleGetUpdates(10); statusFuture = scheduler.scheduleWithFixedDelay(() -> { refreshTahomaStates(); @@ -179,6 +177,21 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { }, RECONCILIATION_TIME, RECONCILIATION_TIME, TimeUnit.SECONDS); } + private void scheduleGetUpdates(long delay) { + pollFuture = scheduler.schedule(() -> { + getTahomaUpdates(); + scheduleNextGetUpdates(); + }, delay, TimeUnit.SECONDS); + } + + private void scheduleNextGetUpdates() { + ScheduledFuture localPollFuture = pollFuture; + if (localPollFuture != null) { + localPollFuture.cancel(false); + } + scheduleGetUpdates(executions.isEmpty() ? thingConfig.getRefresh() : 2); + } + public synchronized void login() { String url; @@ -638,9 +651,9 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { } private boolean sendCommandInternal(String io, String command, String params, String url) { - String value = params.equals("[]") ? command : params.replace("\"", ""); + String value = params.equals("[]") ? command : command + " " + params.replace("\"", ""); String urlParameters = "{\"label\":\"" + getThingLabelByURL(io) + " - " + value - + " - OH2\",\"actions\":[{\"deviceURL\":\"" + io + "\",\"commands\":[{\"name\":\"" + command + + " - openHAB\",\"actions\":[{\"deviceURL\":\"" + io + "\",\"commands\":[{\"name\":\"" + command + "\",\"parameters\":" + params + "}]}]}"; SomfyTahomaApplyResponse response = invokeCallToURL(url, urlParameters, HttpMethod.POST, SomfyTahomaApplyResponse.class); @@ -648,6 +661,7 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { if (!response.getExecId().isEmpty()) { logger.debug("Exec id: {}", response.getExecId()); registerExecution(io, response.getExecId()); + scheduleNextGetUpdates(); } else { logger.debug("ExecId is empty!"); return false; @@ -689,7 +703,7 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { // Return label from Tahoma return th.getProperties().get(NAME_STATE).replace("\"", ""); } - // Return label from OH2 + // Return label from the thing String label = th.getLabel(); return label != null ? label.replace("\"", "") : ""; } @@ -717,6 +731,7 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { } if (execId != null) { registerExecution(id, execId); + scheduleNextGetUpdates(); } }