From 522b33cc9ac1942f0b7d3b0dab881256e137eaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Fri, 20 Sep 2024 15:34:43 +0200 Subject: [PATCH] [netatmo] Prevent `IndexOutOfBoundsException` (#17448) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Preventing IndexOutOfBoundsException Signed-off-by: Gaƫl L'hopital Signed-off-by: Ciprian Pascu --- .../netatmo/internal/api/SecurityApi.java | 19 ++++++++++--------- .../capability/SecurityCapability.java | 12 ++++++++---- 2 files changed, 18 insertions(+), 13 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 e7c08e8157a..28825bf3ef6 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 @@ -80,19 +80,20 @@ public class SecurityApi extends RestManager { throw new NetatmoException("home should not be null"); } - public List getHomeEvents(String homeId, @Nullable ZonedDateTime freshestEventTime) - throws NetatmoException { + public List getHomeEvents(String homeId, ZonedDateTime freshestEventTime) throws NetatmoException { List 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); + // we have to rewind to the latest event just after freshestEventTime + if (events.size() > 0) { + HomeEvent oldestRetrieved = events.get(events.size() - 1); + while (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)) + // Remove unneeded events being before freshestEventTime + return events.stream().filter(event -> event.getTime().isAfter(freshestEventTime)) .sorted(Comparator.comparing(HomeEvent::getTime).reversed()).toList(); } 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 f583dfeabf9..f3dca854c85 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 @@ -12,6 +12,8 @@ */ package org.openhab.binding.netatmo.internal.handler.capability; +import java.time.Instant; +import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collection; @@ -48,11 +50,13 @@ import org.slf4j.LoggerFactory; */ @NonNullByDefault class SecurityCapability extends RestCapability { + private final static ZonedDateTime ZDT_REFERENCE = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0), + ZoneId.systemDefault()); + private final Logger logger = LoggerFactory.getLogger(SecurityCapability.class); - private final Map eventBuffer = new HashMap<>(); - private @Nullable ZonedDateTime freshestEventTime; + private ZonedDateTime freshestEventTime = ZDT_REFERENCE; private NAObjectMap persons = new NAObjectMap<>(); private NAObjectMap modules = new NAObjectMap<>(); private String securityId = ""; @@ -64,7 +68,7 @@ class SecurityCapability extends RestCapability { @Override public void initialize() { super.initialize(); - freshestEventTime = null; + freshestEventTime = ZDT_REFERENCE; securityId = handler.getThingConfigAs(HomeConfiguration.class).getIdForArea(FeatureArea.SECURITY); } @@ -132,7 +136,7 @@ class SecurityCapability extends RestCapability { eventBuffer.put(personId, event); } } - if (freshestEventTime == null || event.getTime().isAfter(freshestEventTime)) { + if (event.getTime().isAfter(freshestEventTime)) { freshestEventTime = event.getTime(); } }