[netatmo] Prevent IndexOutOfBoundsException (#17448)

* Preventing IndexOutOfBoundsException

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Gaël L'hopital 2024-09-20 15:34:43 +02:00 committed by Ciprian Pascu
parent c555eb9d95
commit 522b33cc9a
2 changed files with 18 additions and 13 deletions

View File

@ -80,19 +80,20 @@ public class SecurityApi extends RestManager {
throw new NetatmoException("home should not be null");
}
public List<HomeEvent> getHomeEvents(String homeId, @Nullable ZonedDateTime freshestEventTime)
throws NetatmoException {
public List<HomeEvent> getHomeEvents(String homeId, 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);
// 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();
}

View File

@ -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<SecurityApi> {
private final static ZonedDateTime ZDT_REFERENCE = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0),
ZoneId.systemDefault());
private final Logger logger = LoggerFactory.getLogger(SecurityCapability.class);
private final Map<String, HomeEvent> eventBuffer = new HashMap<>();
private @Nullable ZonedDateTime freshestEventTime;
private ZonedDateTime freshestEventTime = ZDT_REFERENCE;
private NAObjectMap<HomeDataPerson> persons = new NAObjectMap<>();
private NAObjectMap<HomeDataModule> modules = new NAObjectMap<>();
private String securityId = "";
@ -64,7 +68,7 @@ class SecurityCapability extends RestCapability<SecurityApi> {
@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<SecurityApi> {
eventBuffer.put(personId, event);
}
}
if (freshestEventTime == null || event.getTime().isAfter(freshestEventTime)) {
if (event.getTime().isAfter(freshestEventTime)) {
freshestEventTime = event.getTime();
}
}