Skip to content

Commit

Permalink
[netatmo] Prevent IndexOutOfBoundsException (openhab#17448)
Browse files Browse the repository at this point in the history
* Preventing IndexOutOfBoundsException

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
  • Loading branch information
clinique authored Sep 20, 2024
1 parent 52ea5d6 commit deca335
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,20 @@ private List<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoExce
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,11 +50,13 @@
*/
@NonNullByDefault
class SecurityCapability extends RestCapability<SecurityApi> {
private final Logger logger = LoggerFactory.getLogger(SecurityCapability.class);
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 = "";
Expand All @@ -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);
}

Expand Down Expand Up @@ -132,7 +136,7 @@ protected List<NAObject> updateReadings(SecurityApi api) {
eventBuffer.put(personId, event);
}
}
if (freshestEventTime == null || event.getTime().isAfter(freshestEventTime)) {
if (event.getTime().isAfter(freshestEventTime)) {
freshestEventTime = event.getTime();
}
}
Expand Down

0 comments on commit deca335

Please sign in to comment.