]> git.basschouten.com Git - openhab-addons.git/commitdiff
[netatmo] Add a buffer to lower Home API requests (#16562)
authorGaël L'hopital <gael@lhopital.org>
Mon, 25 Mar 2024 10:41:33 +0000 (11:41 +0100)
committerGitHub <noreply@github.com>
Mon, 25 Mar 2024 10:41:33 +0000 (11:41 +0100)
* Adding CacheCapability to HomeAPI

---------

Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: gael@lhopital.org <gael@lhopital.org>
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CacheCapability.java [new file with mode: 0644]
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CacheWeatherCapability.java [deleted file]
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/MeasureCapability.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/RefreshCapability.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/WeatherCapability.java

diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CacheCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CacheCapability.java
new file mode 100644 (file)
index 0000000..b2ecd79
--- /dev/null
@@ -0,0 +1,62 @@
+/**
+ * Copyright (c) 2010-2024 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.netatmo.internal.handler.capability;
+
+import java.time.Duration;
+import java.time.Instant;
+import java.util.List;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.openhab.binding.netatmo.internal.api.RestManager;
+import org.openhab.binding.netatmo.internal.api.dto.NAObject;
+import org.openhab.binding.netatmo.internal.handler.CommonInterface;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link CacheCapability} give the ability to buffer RestManager related requests and reduce server requests
+ *
+ * @author Gaël L'hopital - Initial contribution
+ *
+ */
+@NonNullByDefault
+public abstract class CacheCapability<T extends RestManager> extends RestCapability<T> {
+    private final Logger logger = LoggerFactory.getLogger(CacheCapability.class);
+    private final Duration validity;
+
+    private List<NAObject> lastResult = List.of();
+    private Instant requestTS = Instant.MIN;
+
+    public CacheCapability(CommonInterface handler, Duration validity, Class<T> restManagerClazz) {
+        super(handler, restManagerClazz);
+        this.validity = validity;
+    }
+
+    @Override
+    protected synchronized List<NAObject> updateReadings(T api) {
+        Instant now = Instant.now();
+
+        if (requestTS.plus(validity).isBefore(now)) {
+            logger.debug("{} requesting fresh data for {}", getClass().getSimpleName(), thingUID);
+            List<NAObject> result = getFreshData(api);
+            if (!result.isEmpty()) {
+                lastResult = result;
+                requestTS = now;
+            }
+        }
+
+        return lastResult;
+    }
+
+    protected abstract List<NAObject> getFreshData(T api);
+}
diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CacheWeatherCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/CacheWeatherCapability.java
deleted file mode 100644 (file)
index c472029..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * Copyright (c) 2010-2024 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.netatmo.internal.handler.capability;
-
-import java.time.Duration;
-import java.time.Instant;
-import java.util.List;
-
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.openhab.binding.netatmo.internal.api.WeatherApi;
-import org.openhab.binding.netatmo.internal.api.dto.NAObject;
-import org.openhab.binding.netatmo.internal.handler.CommonInterface;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * {@link CacheWeatherCapability} give the ability to buffer weather related requests and reduce server requests
- *
- * @author Gaël L'hopital - Initial contribution
- *
- */
-@NonNullByDefault
-public abstract class CacheWeatherCapability extends RestCapability<WeatherApi> {
-    private final Logger logger = LoggerFactory.getLogger(CacheWeatherCapability.class);
-    private final Duration validity;
-
-    private List<NAObject> lastResult = List.of();
-    private Instant requestTS = Instant.MIN;
-
-    public CacheWeatherCapability(CommonInterface handler, Duration validity) {
-        super(handler, WeatherApi.class);
-        this.validity = validity;
-    }
-
-    @Override
-    protected synchronized List<NAObject> updateReadings(WeatherApi api) {
-        Instant now = Instant.now();
-
-        if (requestTS.plus(validity).isBefore(now)) {
-            logger.debug("{} requesting fresh data for {}", getClass().getSimpleName(), thingUID);
-            List<NAObject> result = getFreshData(api);
-            if (!result.isEmpty()) {
-                lastResult = result;
-                requestTS = now;
-            }
-        }
-
-        return lastResult;
-    }
-
-    protected abstract List<NAObject> getFreshData(WeatherApi api);
-}
index b2e7695a8e4261cb54c6fcdefc91ea26e406aa3e..6e1de1292c4a2a923623f5a137370bedcb004ade 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.netatmo.internal.handler.capability;
 
 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
 
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -42,7 +43,7 @@ import org.slf4j.LoggerFactory;
  *
  */
 @NonNullByDefault
-public class HomeCapability extends RestCapability<HomeApi> {
+public class HomeCapability extends CacheCapability<HomeApi> {
 
     private final Logger logger = LoggerFactory.getLogger(HomeCapability.class);
     private final Set<FeatureArea> featureAreas = new HashSet<>();
@@ -50,7 +51,7 @@ public class HomeCapability extends RestCapability<HomeApi> {
     private final Set<String> homeIds = new HashSet<>(3);
 
     public HomeCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) {
-        super(handler, HomeApi.class);
+        super(handler, Duration.ofSeconds(2), HomeApi.class);
         this.descriptionProvider = descriptionProvider;
     }
 
@@ -105,7 +106,7 @@ public class HomeCapability extends RestCapability<HomeApi> {
     }
 
     @Override
-    protected List<NAObject> updateReadings(HomeApi api) {
+    protected List<NAObject> getFreshData(HomeApi api) {
         List<NAObject> result = new ArrayList<>();
         homeIds.stream().filter(id -> !id.isEmpty()).forEach(id -> {
             try {
index 11151a36d00a8d344ae199f3e3071be659e2394b..56d018c9a94e0c0be98a41c62eb6deb84e1e5698 100644 (file)
@@ -44,12 +44,12 @@ import org.slf4j.LoggerFactory;
  *
  */
 @NonNullByDefault
-public class MeasureCapability extends CacheWeatherCapability {
+public class MeasureCapability extends CacheCapability<WeatherApi> {
     private final Logger logger = LoggerFactory.getLogger(MeasureCapability.class);
     private final Map<String, State> measures = new HashMap<>();
 
     public MeasureCapability(CommonInterface handler, List<ChannelHelper> helpers) {
-        super(handler, Duration.ofMinutes(30));
+        super(handler, Duration.ofMinutes(30), WeatherApi.class);
         MeasuresChannelHelper measureChannelHelper = (MeasuresChannelHelper) helpers.stream()
                 .filter(c -> c instanceof MeasuresChannelHelper).findFirst()
                 .orElseThrow(() -> new IllegalArgumentException(
index f26b771f6a3ad07fa99cc40ba3f6eb36da0e7da8..26aba9e1c71d84a4dd34136b7f3dcff5181f5151 100644 (file)
@@ -83,7 +83,7 @@ public class RefreshCapability extends Capability {
         handler.proceedWithUpdate();
         long delay;
         if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) {
-            logger.debug("Module is not ONLINE; special refresh interval is used");
+            logger.debug("{} is not ONLINE, special refresh interval is used", thingUID);
             delay = OFFLINE_INTERVAL.toSeconds();
             if (probing()) {
                 dataTimeStamp0 = Instant.MIN;
@@ -93,7 +93,7 @@ public class RefreshCapability extends Capability {
                     : (probing() ? PROBING_INTERVAL : dataValidity.minus(dataAge()).plus(DEFAULT_DELAY)).toSeconds();
         }
         delay = delay < 2 ? PROBING_INTERVAL.toSeconds() : delay;
-        logger.debug("Module refreshed, next one in {}s", delay);
+        logger.debug("{} refreshed, next one in {}s", thingUID, delay);
         freeJobAndReschedule(delay);
     }
 
@@ -104,13 +104,13 @@ public class RefreshCapability extends Capability {
             if (probing()) {
                 if (Instant.MIN.equals(dataTimeStamp0)) {
                     dataTimeStamp0 = tsInstant;
-                    logger.debug("First data timestamp is {}", dataTimeStamp0);
+                    logger.debug("First data timestamp of {} is {}", thingUID, dataTimeStamp0);
                 } else if (tsInstant.isAfter(dataTimeStamp0)) {
                     dataValidity = Duration.between(dataTimeStamp0, tsInstant);
                     refreshConfigured = true;
-                    logger.debug("Data validity period identified to be {}", dataValidity);
+                    logger.debug("Data validity period of {} identified to be {}", thingUID, dataValidity);
                 } else {
-                    logger.debug("Data validity period not yet found, data timestamp unchanged");
+                    logger.debug("Data validity period of {} not yet found, data timestamp unchanged", thingUID);
                 }
             }
             dataTimeStamp = tsInstant;
index 7faec8efef24a1541078d102719fc8d9c9208c41..4789af458773a42270fad6d10fcec670177d2b2b 100644 (file)
@@ -30,11 +30,11 @@ import org.slf4j.LoggerFactory;
  *
  */
 @NonNullByDefault
-public class WeatherCapability extends CacheWeatherCapability {
+public class WeatherCapability extends CacheCapability<WeatherApi> {
     private final Logger logger = LoggerFactory.getLogger(WeatherCapability.class);
 
     public WeatherCapability(CommonInterface handler) {
-        super(handler, Duration.ofSeconds(2));
+        super(handler, Duration.ofSeconds(2), WeatherApi.class);
     }
 
     @Override