]> git.basschouten.com Git - openhab-addons.git/commitdiff
[netatmo] Ensure all events are retrieved since they are buffered (#13505)
authorGaël L'hopital <gael@lhopital.org>
Sun, 9 Oct 2022 10:17:08 +0000 (12:17 +0200)
committerGitHub <noreply@github.com>
Sun, 9 Oct 2022 10:17:08 +0000 (12:17 +0200)
* Adding logic to ensure we retrieve all events.

Signed-off-by: clinique <gael@lhopital.org>
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/SecurityApi.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/NetatmoConstants.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java

index 7b0e49b9b8bddc3ddf9e8823c73b53f2c08d66e7..3e839b12086545f6cea5f45c7f524349c2870187 100644 (file)
@@ -15,7 +15,10 @@ package org.openhab.binding.netatmo.internal.api;
 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
 
 import java.net.URI;
-import java.util.Collection;
+import java.time.ZonedDateTime;
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
 
 import javax.ws.rs.core.UriBuilder;
 
@@ -66,7 +69,7 @@ public class SecurityApi extends RestManager {
         return true;
     }
 
-    private Collection<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
+    private List<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
         BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
         if (body != null) {
@@ -78,16 +81,27 @@ public class SecurityApi extends RestManager {
         throw new NetatmoException("home should not be null");
     }
 
-    public Collection<HomeEvent> getHomeEvents(String homeId) throws NetatmoException {
-        return getEvents(PARAM_HOME_ID, homeId);
+    public List<HomeEvent> getHomeEvents(String homeId, @Nullable ZonedDateTime freshestEventTime)
+            throws NetatmoException {
+        List<HomeEvent> events = getEvents(PARAM_HOME_ID, homeId);
+
+        // we have to rewind to the latest event just after oldestKnown
+        HomeEvent oldestRetrieved = events.get(events.size() - 1);
+        while (freshestEventTime != null && oldestRetrieved.getTime().isAfter(freshestEventTime)) {
+            events.addAll(getEvents(PARAM_HOME_ID, homeId, PARAM_EVENT_ID, oldestRetrieved.getId()));
+            oldestRetrieved = events.get(events.size() - 1);
+        }
+
+        // Remove unneeded events being before oldestKnown
+        return events.stream().filter(event -> freshestEventTime == null || event.getTime().isAfter(freshestEventTime))
+                .sorted(Comparator.comparing(HomeEvent::getTime).reversed()).collect(Collectors.toList());
     }
 
-    public Collection<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
+    public List<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
         return getEvents(PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, PARAM_OFFSET, 1);
     }
 
-    public Collection<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType)
-            throws NetatmoException {
+    public List<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType) throws NetatmoException {
         return getEvents(PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId, PARAM_DEVICES_TYPE, deviceType);
     }
 
index f864790c4e17c8bcab5ca8db3bc09b0728741725..6d10366966f3a42bed8ed39b52bb6070391ae11c 100644 (file)
@@ -147,6 +147,7 @@ public class NetatmoConstants {
     public static final String PARAM_HOME_ID = "home_id";
     public static final String PARAM_ROOM_ID = "room_id";
     public static final String PARAM_PERSON_ID = "person_id";
+    public static final String PARAM_EVENT_ID = "event_id";
     public static final String PARAM_SCHEDULE_ID = "schedule_id";
     public static final String PARAM_OFFSET = "offset";
     public static final String PARAM_GATEWAY_TYPE = "gateway_types";
index 380eaa2d13059036b5903329a81e5f5fbe90a9c9..d7c3a2469af541a2f24b47d9a95be25be6128644 100644 (file)
@@ -12,6 +12,7 @@
  */
 package org.openhab.binding.netatmo.internal.handler.capability;
 
+import java.time.ZonedDateTime;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -47,11 +48,18 @@ class SecurityCapability extends RestCapability<SecurityApi> {
     private final Logger logger = LoggerFactory.getLogger(SecurityCapability.class);
 
     private static final Map<String, HomeEvent> eventBuffer = new HashMap<>();
+    private @Nullable ZonedDateTime freshestEventTime;
 
     SecurityCapability(CommonInterface handler) {
         super(handler, SecurityApi.class);
     }
 
+    @Override
+    public void initialize() {
+        super.initialize();
+        freshestEventTime = null;
+    }
+
     @Override
     protected void updateHomeData(HomeData homeData) {
         NAObjectMap<HomeDataPerson> persons = homeData.getPersons();
@@ -120,8 +128,7 @@ class SecurityCapability extends RestCapability<SecurityApi> {
     protected List<NAObject> updateReadings(SecurityApi api) {
         List<NAObject> result = new ArrayList<>();
         try {
-            Collection<HomeEvent> lastEvents = api.getHomeEvents(handler.getId());
-            lastEvents.stream().forEach(event -> {
+            for (HomeEvent event : api.getHomeEvents(handler.getId(), freshestEventTime)) {
                 HomeEvent previousEvent = eventBuffer.get(event.getCameraId());
                 if (previousEvent == null || previousEvent.getTime().isBefore(event.getTime())) {
                     eventBuffer.put(event.getCameraId(), event);
@@ -133,7 +140,10 @@ class SecurityCapability extends RestCapability<SecurityApi> {
                         eventBuffer.put(personId, event);
                     }
                 }
-            });
+                if (freshestEventTime == null || event.getTime().isAfter(freshestEventTime)) {
+                    freshestEventTime = event.getTime();
+                }
+            }
         } catch (NetatmoException e) {
             logger.warn("Error retrieving last events for home '{}' : {}", handler.getId(), e.getMessage());
         }