mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[netatmo] Prevent IndexOutOfBoundsException
(#17448)
* Preventing IndexOutOfBoundsException Signed-off-by: Gaël L'hopital <gael@lhopital.org>
This commit is contained in:
parent
b61a205de3
commit
6528e84a45
@ -81,19 +81,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();
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user