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 b577ad25d02..e7d6a540b19 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() @@ -293,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"); @@ -452,4 +457,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 2d96ceb05be..0463994e875 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; @@ -85,9 +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 if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) { delay = OFFLINE_DELAY; - logger.debug("Thing '{}' is not ONLINE, using special refresh interval", thingUID); + logger.debug("Thing '{}' is not ONLINE, special refresh interval {} used", thingUID, delay); } else { delay = calcDelay(); }