From 32be215191cce01eaec218c5201f4c78b0edd70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Thu, 6 Oct 2022 14:44:26 +0200 Subject: [PATCH] [netatmo] Handling camera ping timeouts (#13487) * Solves #13486 Signed-off-by: clinique --- .../binding/netatmo/internal/api/SecurityApi.java | 15 ++++++++++++--- .../netatmo/internal/handler/CommonInterface.java | 4 +++- .../handler/capability/CameraCapability.java | 14 +++++++++----- .../handler/capability/SecurityCapability.java | 7 +------ 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/SecurityApi.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/SecurityApi.java index bbb9038bfe903..aa2157b6e7984 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/SecurityApi.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/SecurityApi.java @@ -21,6 +21,7 @@ import javax.ws.rs.core.UriBuilder; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea; import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FloodLightMode; import org.openhab.binding.netatmo.internal.api.dto.Home; @@ -28,6 +29,8 @@ import org.openhab.binding.netatmo.internal.api.dto.HomeEvent.NAEventsDataResponse; import org.openhab.binding.netatmo.internal.api.dto.Ping; import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Base class for all Security related endpoints @@ -36,6 +39,8 @@ */ @NonNullByDefault public class SecurityApi extends RestManager { + private final Logger logger = LoggerFactory.getLogger(SecurityApi.class); + public SecurityApi(ApiBridgeHandler apiClient) { super(apiClient, FeatureArea.SECURITY); } @@ -91,10 +96,14 @@ public Collection getDeviceEvents(String homeId, String deviceId, Str throw new NetatmoException("home should not be null"); } - public String ping(String vpnUrl) throws NetatmoException { + public @Nullable String ping(String vpnUrl) { UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING); - Ping response = get(uriBuilder, Ping.class); - return response.getStatus(); + try { + return get(uriBuilder, Ping.class).getStatus(); + } catch (NetatmoException e) { + logger.debug("Pinging {} failed : {}", vpnUrl, e.getMessage()); + return null; + } } public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java index 18eb8b3ffede2..27e8b6089af45 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java @@ -155,7 +155,9 @@ default void setNewData(NAObject newData) { finalReason = thingStatusReason; } } - if (!newData.isIgnoredForThingUpdate()) { + // Prevent turning ONLINE myself if in the meantime something turned account OFFLINE + ApiBridgeHandler accountHandler = getAccountHandler(); + if (accountHandler != null && accountHandler.isConnected() && !newData.isIgnoredForThingUpdate()) { setThingStatus(finalReason == null ? ThingStatus.ONLINE : ThingStatus.OFFLINE, ThingStatusDetail.NONE, finalReason); } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CameraCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CameraCapability.java index 7c526b3cc05d4..910c018c56cb3 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CameraCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CameraCapability.java @@ -49,6 +49,7 @@ public class CameraCapability extends HomeSecurityThingCapability { private final ChannelUID personChannelUID; protected @Nullable String localUrl; + protected @Nullable String vpnUrl; public CameraCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider, List channelHelpers) { @@ -62,12 +63,15 @@ public CameraCapability(CommonInterface handler, NetatmoDescriptionProvider desc @Override public void updateHomeStatusModule(HomeStatusModule newData) { super.updateHomeStatusModule(newData); - String vpnUrl = newData.getVpnUrl(); - if (vpnUrl != null) { - localUrl = newData.isLocal() ? securityCapability.map(cap -> cap.ping(vpnUrl)).orElse(null) : null; - cameraHelper.setUrls(vpnUrl, localUrl); - eventHelper.setUrls(vpnUrl, localUrl); + // Per documentation vpn_url expires every 3 hours and on camera reboot. So useless to reping it if not changed + String newVpnUrl = newData.getVpnUrl(); + if (newVpnUrl != null && !newVpnUrl.equals(vpnUrl)) { + // This will also decrease the number of requests emitted toward Netatmo API. + localUrl = newData.isLocal() ? securityCapability.map(cap -> cap.ping(newVpnUrl)).orElse(null) : null; + cameraHelper.setUrls(newVpnUrl, localUrl); + eventHelper.setUrls(newVpnUrl, localUrl); } + vpnUrl = newVpnUrl; if (!SdCardStatus.SD_CARD_WORKING.equals(newData.getSdStatus()) || !AlimentationStatus.ALIM_CORRECT_POWER.equals(newData.getAlimStatus())) { statusReason = String.format("%s, %s", newData.getSdStatus(), newData.getAlimStatus()); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java index 6fa9661e5728f..2803973576f51 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java @@ -145,12 +145,7 @@ public void setPersonAway(String personId, boolean away) { public @Nullable String ping(String vpnUrl) { return getApi().map(api -> { - try { - return api.ping(vpnUrl); - } catch (NetatmoException e) { - logger.warn("Error pinging camera '{}' : {}", vpnUrl, e.getMessage()); - return null; - } + return api.ping(vpnUrl); }).orElse(null); }