Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[netatmo] Prevent IndexOutOfBoundsException #17448

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
freshestEventTime = event.getTime();
}
}
Expand Down