From 0ffc18a7f61674d8eb6c1d4848083ad6e3457fa1 Mon Sep 17 00:00:00 2001 From: "gael@lhopital.org" Date: Fri, 3 Jan 2025 17:54:52 +0100 Subject: [PATCH 1/4] Working on jlaur proposal Signed-off-by: gael@lhopital.org --- .../netatmo/internal/handler/ApiBridgeHandler.java | 6 ++++++ .../handler/capability/RefreshCapability.java | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java index 64a6acbebe3..128adeaabdd 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.ArrayDeque; @@ -161,6 +162,7 @@ public class ApiBridgeHandler extends BaseBridgeHandler { } logger.debug("Connected to Netatmo API."); + freeConnectJob(); ApiHandlerConfiguration configuration = getConfiguration(); if (!configuration.webHookUrl.isBlank() @@ -452,4 +454,8 @@ public class ApiBridgeHandler extends BaseBridgeHandler { public Optional getWebHookServlet() { return webHookServlet; } + + public @Nullable Duration getTimeBeforeReconnect() { + return connectJob.map(job -> Duration.ofSeconds(job.getDelay(TimeUnit.SECONDS))).orElse(null); + } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java index 6749d9918ba..de1d2ff4a46 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java @@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.netatmo.internal.api.dto.NAObject; +import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler; import org.openhab.binding.netatmo.internal.handler.CommonInterface; import org.openhab.core.thing.ThingStatus; import org.slf4j.Logger; @@ -86,8 +87,15 @@ public class RefreshCapability extends Capability { Duration delay; handler.proceedWithUpdate(); if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) { - delay = OFFLINE_DELAY; - logger.debug("Thing '{}' is not ONLINE, using special refresh interval", thingUID); + if (handler.getAccountHandler() instanceof ApiBridgeHandler accountHandler + && !ThingStatus.ONLINE.equals(accountHandler.getThing().getStatus())) { + delay = accountHandler.getTimeBeforeReconnect(); + delay = delay != null ? delay.plus(ASAP) : OFFLINE_DELAY; + } else { + delay = OFFLINE_DELAY; + logger.debug("Thing '{}' is not ONLINE, special refresh interval {} used", thingUID, OFFLINE_DELAY); + } + } else { delay = calcDelay(); } From f0ce2fc9fed7f52082a0aad2adefa5989663b529 Mon Sep 17 00:00:00 2001 From: "gael@lhopital.org" Date: Fri, 3 Jan 2025 18:09:42 +0100 Subject: [PATCH 2/4] Completing Signed-off-by: gael@lhopital.org --- .../netatmo/internal/handler/ApiBridgeHandler.java | 11 +++++++---- .../handler/capability/RefreshCapability.java | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java index 128adeaabdd..d268551b708 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java @@ -295,11 +295,14 @@ public class ApiBridgeHandler extends BaseBridgeHandler { public synchronized T executeUri(URI uri, HttpMethod method, Class clazz, @Nullable String payload, @Nullable String contentType, int retryCount) throws NetatmoException { + if (connectJob.isPresent()) { + throw new NetatmoException("Connection pending, request will not be accepted in the meantime."); + } + + logger.debug("executeUri {} {} ", method.toString(), uri); + Request request = httpClient.newRequest(uri).method(method).timeout(TIMEOUT_S, TimeUnit.SECONDS); + try { - logger.debug("executeUri {} {} ", method.toString(), uri); - - Request request = httpClient.newRequest(uri).method(method).timeout(TIMEOUT_S, TimeUnit.SECONDS); - if (!authenticate(null, null)) { prepareReconnection(getConfiguration().reconnectInterval, "@text/status-bridge-offline", null, null); throw new NetatmoException("Not authenticated"); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java index de1d2ff4a46..adee890d330 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java @@ -91,9 +91,10 @@ public class RefreshCapability extends Capability { && !ThingStatus.ONLINE.equals(accountHandler.getThing().getStatus())) { delay = accountHandler.getTimeBeforeReconnect(); delay = delay != null ? delay.plus(ASAP) : OFFLINE_DELAY; + logger.debug("Bridge is not ONLINE, will wait for him to come-back {}", thingUID, delay); } else { delay = OFFLINE_DELAY; - logger.debug("Thing '{}' is not ONLINE, special refresh interval {} used", thingUID, OFFLINE_DELAY); + logger.debug("Thing '{}' is not ONLINE, special refresh interval {} used", thingUID, delay); } } else { From eded3ca791344d129fddcfcace143e1d95536b48 Mon Sep 17 00:00:00 2001 From: clinique Date: Sat, 4 Jan 2025 10:53:17 +0100 Subject: [PATCH 3/4] Fixed log string error Signed-off-by: clinique --- .../netatmo/internal/handler/capability/RefreshCapability.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java index adee890d330..1199465faf6 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java @@ -91,7 +91,7 @@ public class RefreshCapability extends Capability { && !ThingStatus.ONLINE.equals(accountHandler.getThing().getStatus())) { delay = accountHandler.getTimeBeforeReconnect(); delay = delay != null ? delay.plus(ASAP) : OFFLINE_DELAY; - logger.debug("Bridge is not ONLINE, will wait for him to come-back {}", thingUID, delay); + logger.debug("Bridge is not ONLINE, will wait for him to come-back {}", delay); } else { delay = OFFLINE_DELAY; logger.debug("Thing '{}' is not ONLINE, special refresh interval {} used", thingUID, delay); From e868d3264a651324126f014c97b14c1adeaf145a Mon Sep 17 00:00:00 2001 From: clinique Date: Sat, 4 Jan 2025 10:59:42 +0100 Subject: [PATCH 4/4] Simplyfy status logic check Signed-off-by: clinique --- .../handler/capability/RefreshCapability.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java index 1199465faf6..9c8bf1f28c4 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java @@ -86,17 +86,14 @@ public class RefreshCapability extends Capability { private void proceedWithUpdate() { Duration delay; handler.proceedWithUpdate(); - if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) { - if (handler.getAccountHandler() instanceof ApiBridgeHandler accountHandler - && !ThingStatus.ONLINE.equals(accountHandler.getThing().getStatus())) { - delay = accountHandler.getTimeBeforeReconnect(); - delay = delay != null ? delay.plus(ASAP) : OFFLINE_DELAY; - logger.debug("Bridge is not ONLINE, will wait for him to come-back {}", delay); - } else { - delay = OFFLINE_DELAY; - logger.debug("Thing '{}' is not ONLINE, special refresh interval {} used", thingUID, delay); - } - + if (handler.getAccountHandler() instanceof ApiBridgeHandler accountHandler + && !ThingStatus.ONLINE.equals(accountHandler.getThing().getStatus())) { + delay = accountHandler.getTimeBeforeReconnect(); + delay = delay != null ? delay.plus(ASAP) : OFFLINE_DELAY; + logger.debug("Bridge is not ONLINE, will wait for him to come-back {}", delay); + } else if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) { + delay = OFFLINE_DELAY; + logger.debug("Thing '{}' is not ONLINE, special refresh interval {} used", thingUID, delay); } else { delay = calcDelay(); }