[netatmo] Ensure all events are retrieved since they are buffered (#13505)
* Adding logic to ensure we retrieve all events. Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
parent
c2d792d2f3
commit
359c97b8ac
|
@ -15,7 +15,10 @@ package org.openhab.binding.netatmo.internal.api;
|
||||||
import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
|
import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Collection;
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.ws.rs.core.UriBuilder;
|
import javax.ws.rs.core.UriBuilder;
|
||||||
|
|
||||||
|
@ -66,7 +69,7 @@ public class SecurityApi extends RestManager {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
|
private List<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
|
||||||
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
|
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
|
||||||
BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
|
BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
|
@ -78,16 +81,27 @@ public class SecurityApi extends RestManager {
|
||||||
throw new NetatmoException("home should not be null");
|
throw new NetatmoException("home should not be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<HomeEvent> getHomeEvents(String homeId) throws NetatmoException {
|
public List<HomeEvent> getHomeEvents(String homeId, @Nullable ZonedDateTime freshestEventTime)
|
||||||
return getEvents(PARAM_HOME_ID, homeId);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove unneeded events being before oldestKnown
|
||||||
|
return events.stream().filter(event -> freshestEventTime == null || event.getTime().isAfter(freshestEventTime))
|
||||||
|
.sorted(Comparator.comparing(HomeEvent::getTime).reversed()).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
|
public List<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
|
||||||
return getEvents(PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, PARAM_OFFSET, 1);
|
return getEvents(PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, PARAM_OFFSET, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType)
|
public List<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType) throws NetatmoException {
|
||||||
throws NetatmoException {
|
|
||||||
return getEvents(PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId, PARAM_DEVICES_TYPE, deviceType);
|
return getEvents(PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId, PARAM_DEVICES_TYPE, deviceType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -147,6 +147,7 @@ public class NetatmoConstants {
|
||||||
public static final String PARAM_HOME_ID = "home_id";
|
public static final String PARAM_HOME_ID = "home_id";
|
||||||
public static final String PARAM_ROOM_ID = "room_id";
|
public static final String PARAM_ROOM_ID = "room_id";
|
||||||
public static final String PARAM_PERSON_ID = "person_id";
|
public static final String PARAM_PERSON_ID = "person_id";
|
||||||
|
public static final String PARAM_EVENT_ID = "event_id";
|
||||||
public static final String PARAM_SCHEDULE_ID = "schedule_id";
|
public static final String PARAM_SCHEDULE_ID = "schedule_id";
|
||||||
public static final String PARAM_OFFSET = "offset";
|
public static final String PARAM_OFFSET = "offset";
|
||||||
public static final String PARAM_GATEWAY_TYPE = "gateway_types";
|
public static final String PARAM_GATEWAY_TYPE = "gateway_types";
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
*/
|
*/
|
||||||
package org.openhab.binding.netatmo.internal.handler.capability;
|
package org.openhab.binding.netatmo.internal.handler.capability;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -47,11 +48,18 @@ class SecurityCapability extends RestCapability<SecurityApi> {
|
||||||
private final Logger logger = LoggerFactory.getLogger(SecurityCapability.class);
|
private final Logger logger = LoggerFactory.getLogger(SecurityCapability.class);
|
||||||
|
|
||||||
private static final Map<String, HomeEvent> eventBuffer = new HashMap<>();
|
private static final Map<String, HomeEvent> eventBuffer = new HashMap<>();
|
||||||
|
private @Nullable ZonedDateTime freshestEventTime;
|
||||||
|
|
||||||
SecurityCapability(CommonInterface handler) {
|
SecurityCapability(CommonInterface handler) {
|
||||||
super(handler, SecurityApi.class);
|
super(handler, SecurityApi.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize() {
|
||||||
|
super.initialize();
|
||||||
|
freshestEventTime = null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void updateHomeData(HomeData homeData) {
|
protected void updateHomeData(HomeData homeData) {
|
||||||
NAObjectMap<HomeDataPerson> persons = homeData.getPersons();
|
NAObjectMap<HomeDataPerson> persons = homeData.getPersons();
|
||||||
|
@ -120,8 +128,7 @@ class SecurityCapability extends RestCapability<SecurityApi> {
|
||||||
protected List<NAObject> updateReadings(SecurityApi api) {
|
protected List<NAObject> updateReadings(SecurityApi api) {
|
||||||
List<NAObject> result = new ArrayList<>();
|
List<NAObject> result = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
Collection<HomeEvent> lastEvents = api.getHomeEvents(handler.getId());
|
for (HomeEvent event : api.getHomeEvents(handler.getId(), freshestEventTime)) {
|
||||||
lastEvents.stream().forEach(event -> {
|
|
||||||
HomeEvent previousEvent = eventBuffer.get(event.getCameraId());
|
HomeEvent previousEvent = eventBuffer.get(event.getCameraId());
|
||||||
if (previousEvent == null || previousEvent.getTime().isBefore(event.getTime())) {
|
if (previousEvent == null || previousEvent.getTime().isBefore(event.getTime())) {
|
||||||
eventBuffer.put(event.getCameraId(), event);
|
eventBuffer.put(event.getCameraId(), event);
|
||||||
|
@ -133,7 +140,10 @@ class SecurityCapability extends RestCapability<SecurityApi> {
|
||||||
eventBuffer.put(personId, event);
|
eventBuffer.put(personId, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
if (freshestEventTime == null || event.getTime().isAfter(freshestEventTime)) {
|
||||||
|
freshestEventTime = event.getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (NetatmoException e) {
|
} catch (NetatmoException e) {
|
||||||
logger.warn("Error retrieving last events for home '{}' : {}", handler.getId(), e.getMessage());
|
logger.warn("Error retrieving last events for home '{}' : {}", handler.getId(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue