[netatmo] Buffer last event requests per child modules (#13489)

* Buffer last event requests per child modules
This is targetted to decrease the number of requests transmitted to Netatmo API.
Solves #13358

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-10-07 11:56:15 +02:00 committed by GitHub
parent cc6e4fad61
commit 56d72ca1c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 103 additions and 54 deletions

View File

@ -47,7 +47,7 @@ public class EnergyApi extends RestManager {
public void switchSchedule(String homeId, String scheduleId) throws NetatmoException { public void switchSchedule(String homeId, String scheduleId) throws NetatmoException {
UriBuilder uriBuilder = getAppUriBuilder(SUB_PATH_SWITCH_SCHEDULE, PARAM_HOME_ID, homeId, PARAM_SCHEDULE_ID, UriBuilder uriBuilder = getAppUriBuilder(SUB_PATH_SWITCH_SCHEDULE, PARAM_HOME_ID, homeId, PARAM_SCHEDULE_ID,
scheduleId); scheduleId);
post(uriBuilder, ApiResponse.Ok.class, null, null); post(uriBuilder, ApiResponse.Ok.class, null);
} }
/** /**
@ -64,7 +64,7 @@ public class EnergyApi extends RestManager {
*/ */
public void setThermMode(String homeId, String mode) throws NetatmoException { public void setThermMode(String homeId, String mode) throws NetatmoException {
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_SET_THERM_MODE, PARAM_HOME_ID, homeId, PARAM_MODE, mode); UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_SET_THERM_MODE, PARAM_HOME_ID, homeId, PARAM_MODE, mode);
post(uriBuilder, ApiResponse.Ok.class, null, null); post(uriBuilder, ApiResponse.Ok.class, null);
} }
/** /**
@ -88,6 +88,6 @@ public class EnergyApi extends RestManager {
uriBuilder.queryParam("temp", temp > THERM_MAX_SETPOINT ? THERM_MAX_SETPOINT : temp); uriBuilder.queryParam("temp", temp > THERM_MAX_SETPOINT ? THERM_MAX_SETPOINT : temp);
} }
} }
post(uriBuilder, ApiResponse.Ok.class, null, null); post(uriBuilder, ApiResponse.Ok.class, null);
} }
} }

View File

@ -52,14 +52,13 @@ public abstract class RestManager {
return executeUri(uriBuilder, HttpMethod.GET, clazz, null, null); return executeUri(uriBuilder, HttpMethod.GET, clazz, null, null);
} }
protected <T extends ApiResponse<?>> T post(UriBuilder uriBuilder, Class<T> clazz, @Nullable String payload, protected <T extends ApiResponse<?>> T post(UriBuilder uriBuilder, Class<T> clazz, @Nullable String payload)
@Nullable String contentType) throws NetatmoException { throws NetatmoException {
return executeUri(uriBuilder, HttpMethod.POST, clazz, payload, contentType); return executeUri(uriBuilder, HttpMethod.POST, clazz, payload, payload == null ? null : CONTENT_APP_JSON);
} }
protected <T> T post(URI uri, Class<T> clazz, Map<String, String> entries) throws NetatmoException { protected <T> T post(URI uri, Class<T> clazz, Map<String, String> entries) throws NetatmoException {
return apiBridge.executeUri(uri, POST, clazz, toRequest(entries), return apiBridge.executeUri(uri, POST, clazz, toRequest(entries), CONTENT_APP_FORM, 3);
"application/x-www-form-urlencoded;charset=UTF-8", 3);
} }
private <T extends ApiResponse<?>> T executeUri(UriBuilder uriBuilder, HttpMethod method, Class<T> clazz, private <T extends ApiResponse<?>> T executeUri(UriBuilder uriBuilder, HttpMethod method, Class<T> clazz,

View File

@ -16,7 +16,6 @@ import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
import java.net.URI; import java.net.URI;
import java.util.Collection; import java.util.Collection;
import java.util.stream.Collectors;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
@ -52,7 +51,7 @@ public class SecurityApi extends RestManager {
*/ */
public void dropWebhook() throws NetatmoException { public void dropWebhook() throws NetatmoException {
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK); UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_DROP_WEBHOOK);
post(uriBuilder, ApiResponse.Ok.class, null, null); post(uriBuilder, ApiResponse.Ok.class, null);
} }
/** /**
@ -63,29 +62,12 @@ public class SecurityApi extends RestManager {
*/ */
public boolean addwebhook(URI uri) throws NetatmoException { public boolean addwebhook(URI uri) throws NetatmoException {
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADD_WEBHOOK, PARAM_URL, uri.toString()); UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_ADD_WEBHOOK, PARAM_URL, uri.toString());
post(uriBuilder, ApiResponse.Ok.class, null, null); post(uriBuilder, ApiResponse.Ok.class, null);
return true; return true;
} }
public Collection<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException { private Collection<HomeEvent> getEvents(@Nullable Object... params) throws NetatmoException {
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, params);
PARAM_OFFSET, 1);
NAEventsDataResponse response = get(uriBuilder, NAEventsDataResponse.class);
BodyResponse<Home> body = response.getBody();
if (body != null) {
Home home = body.getElement();
if (home != null) {
return home.getEvents().stream().filter(event -> personId.equals(event.getPersonId()))
.collect(Collectors.toList());
}
}
throw new NetatmoException("home should not be null");
}
public Collection<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType)
throws NetatmoException {
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GET_EVENTS, PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId,
PARAM_DEVICES_TYPE, deviceType);
BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody(); BodyResponse<Home> body = get(uriBuilder, NAEventsDataResponse.class).getBody();
if (body != null) { if (body != null) {
Home home = body.getElement(); Home home = body.getElement();
@ -96,6 +78,19 @@ 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 {
return getEvents(PARAM_HOME_ID, homeId);
}
public Collection<HomeEvent> getPersonEvents(String homeId, String personId) throws NetatmoException {
return getEvents(PARAM_HOME_ID, homeId, PARAM_PERSON_ID, personId, PARAM_OFFSET, 1);
}
public Collection<HomeEvent> getDeviceEvents(String homeId, String deviceId, String deviceType)
throws NetatmoException {
return getEvents(PARAM_HOME_ID, homeId, PARAM_DEVICE_ID, deviceId, PARAM_DEVICES_TYPE, deviceType);
}
public @Nullable String ping(String vpnUrl) { public @Nullable String ping(String vpnUrl) {
UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING); UriBuilder uriBuilder = UriBuilder.fromUri(vpnUrl).path(PATH_COMMAND).path(SUB_PATH_PING);
try { try {
@ -109,22 +104,18 @@ public class SecurityApi extends RestManager {
public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException { public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoException {
UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_CHANGESTATUS); UriBuilder uriBuilder = UriBuilder.fromUri(localCameraURL).path(PATH_COMMAND).path(SUB_PATH_CHANGESTATUS);
uriBuilder.queryParam(PARAM_STATUS, setOn ? "on" : "off"); uriBuilder.queryParam(PARAM_STATUS, setOn ? "on" : "off");
post(uriBuilder, ApiResponse.Ok.class, null, null); post(uriBuilder, ApiResponse.Ok.class, null);
} }
public void changeFloodLightMode(String homeId, String cameraId, FloodLightMode mode) throws NetatmoException { public void changeFloodLightMode(String homeId, String cameraId, FloodLightMode mode) throws NetatmoException {
UriBuilder uriBuilder = getAppUriBuilder(PATH_STATE); UriBuilder uriBuilder = getAppUriBuilder(PATH_STATE);
String payload = String.format( String payload = String.format(PAYLOAD_FLOODLIGHT, homeId, cameraId, mode.name().toLowerCase());
"{\"home\": {\"id\":\"%s\",\"modules\": [ {\"id\":\"%s\",\"floodlight\":\"%s\"} ]}}", homeId, cameraId, post(uriBuilder, ApiResponse.Ok.class, payload);
mode.name().toLowerCase());
post(uriBuilder, ApiResponse.Ok.class, payload, "application/json;charset=utf-8");
} }
public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException { public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
UriBuilder uriBuilder = getAppUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME); UriBuilder uriBuilder = getAppUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
String payload = String.format( String payload = String.format(away ? PAYLOAD_PERSON_AWAY : PAYLOAD_PERSON_HOME, homeId, personId);
away ? "{\"home_id\":\"%s\",\"person_id\":\"%s\"}" : "{\"home_id\":\"%s\",\"person_ids\":[\"%s\"]}", post(uriBuilder, ApiResponse.Ok.class, payload);
homeId, personId);
post(uriBuilder, ApiResponse.Ok.class, payload, "application/json;charset=utf-8");
} }
} }

View File

@ -114,6 +114,10 @@ public class NetatmoConstants {
} }
} }
// Content types
public static final String CONTENT_APP_JSON = "application/json;charset=utf-8";
public static final String CONTENT_APP_FORM = "application/x-www-form-urlencoded;charset=UTF-8";
// Netatmo API urls // Netatmo API urls
public static final String URL_API = "https://api.netatmo.com/"; public static final String URL_API = "https://api.netatmo.com/";
public static final String URL_APP = "https://app.netatmo.net/"; public static final String URL_APP = "https://app.netatmo.net/";
@ -152,6 +156,11 @@ public class NetatmoConstants {
public static final String PARAM_STATUS = "status"; public static final String PARAM_STATUS = "status";
public static final String PARAM_DEVICES_TYPE = "device_types"; public static final String PARAM_DEVICES_TYPE = "device_types";
// Payloads
public static final String PAYLOAD_FLOODLIGHT = "{\"home\": {\"id\":\"%s\",\"modules\": [ {\"id\":\"%s\",\"floodlight\":\"%s\"} ]}}";
public static final String PAYLOAD_PERSON_AWAY = "{\"home_id\":\"%s\",\"person_id\":\"%s\"}";
public static final String PAYLOAD_PERSON_HOME = "{\"home_id\":\"%s\",\"person_ids\":[\"%s\"]}";
// Autentication process params // Autentication process params
public static final String PARAM_ERROR = "error"; public static final String PARAM_ERROR = "error";

View File

@ -15,7 +15,6 @@ package org.openhab.binding.netatmo.internal.handler.capability;
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*; import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -101,9 +100,8 @@ public class CameraCapability extends HomeSecurityThingCapability {
public List<NAObject> updateReadings() { public List<NAObject> updateReadings() {
List<NAObject> result = new ArrayList<>(); List<NAObject> result = new ArrayList<>();
securityCapability.ifPresent(cap -> { securityCapability.ifPresent(cap -> {
Collection<HomeEvent> events = cap.getDeviceEvents(handler.getId(), moduleType.apiName); HomeEvent event = cap.getLastDeviceEvent(handler.getId(), moduleType.apiName);
if (!events.isEmpty()) { if (event != null) {
HomeEvent event = events.iterator().next();
result.add(event); result.add(event);
result.addAll(event.getSubevents()); result.addAll(event.getSubevents());
} }

View File

@ -113,7 +113,7 @@ public class HomeCapability extends RestCapability<HomeApi> {
result.add(homeStatus); result.add(homeStatus);
} }
} catch (NetatmoException e) { } catch (NetatmoException e) {
logger.warn("Error gettting Home informations : {}", e.getMessage()); logger.warn("Error getting Home informations : {}", e.getMessage());
} }
return result; return result;
} }

View File

@ -16,7 +16,6 @@ import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -90,9 +89,9 @@ public class PersonCapability extends HomeSecurityThingCapability {
public List<NAObject> updateReadings() { public List<NAObject> updateReadings() {
List<NAObject> result = new ArrayList<>(); List<NAObject> result = new ArrayList<>();
securityCapability.ifPresent(cap -> { securityCapability.ifPresent(cap -> {
Collection<HomeEvent> events = cap.getPersonEvents(handler.getId()); HomeEvent event = cap.getLastPersonEvent(handler.getId());
if (!events.isEmpty()) { if (event != null) {
result.add(events.iterator().next()); result.add(event);
} }
}); });
return result; return result;

View File

@ -12,8 +12,11 @@
*/ */
package org.openhab.binding.netatmo.internal.handler.capability; package org.openhab.binding.netatmo.internal.handler.capability;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
@ -27,6 +30,7 @@ import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule; import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule;
import org.openhab.binding.netatmo.internal.api.dto.HomeStatusPerson; import org.openhab.binding.netatmo.internal.api.dto.HomeStatusPerson;
import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus; import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus;
import org.openhab.binding.netatmo.internal.api.dto.NAObject;
import org.openhab.binding.netatmo.internal.deserialization.NAObjectMap; import org.openhab.binding.netatmo.internal.deserialization.NAObjectMap;
import org.openhab.binding.netatmo.internal.handler.CommonInterface; import org.openhab.binding.netatmo.internal.handler.CommonInterface;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -42,6 +46,8 @@ import org.slf4j.LoggerFactory;
class SecurityCapability extends RestCapability<SecurityApi> { 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<>();
SecurityCapability(CommonInterface handler) { SecurityCapability(CommonInterface handler) {
super(handler, SecurityApi.class); super(handler, SecurityApi.class);
} }
@ -110,7 +116,55 @@ class SecurityCapability extends RestCapability<SecurityApi> {
}); });
} }
public Collection<HomeEvent> getDeviceEvents(String cameraId, String deviceType) { @Override
protected List<NAObject> updateReadings(SecurityApi api) {
List<NAObject> result = new ArrayList<>();
try {
Collection<HomeEvent> lastEvents = api.getHomeEvents(handler.getId());
lastEvents.stream().forEach(event -> {
HomeEvent previousEvent = eventBuffer.get(event.getCameraId());
if (previousEvent == null || previousEvent.getTime().isBefore(event.getTime())) {
eventBuffer.put(event.getCameraId(), event);
}
String personId = event.getPersonId();
if (personId != null) {
previousEvent = eventBuffer.get(personId);
if (previousEvent == null || previousEvent.getTime().isBefore(event.getTime())) {
eventBuffer.put(personId, event);
}
}
});
} catch (NetatmoException e) {
logger.warn("Error retrieving last events for home '{}' : {}", handler.getId(), e.getMessage());
}
return result;
}
public @Nullable HomeEvent getLastPersonEvent(String personId) {
HomeEvent event = eventBuffer.get(personId);
if (event == null) {
Collection<HomeEvent> events = requestPersonEvents(personId);
if (!events.isEmpty()) {
event = events.iterator().next();
eventBuffer.put(personId, event);
}
}
return event;
}
public @Nullable HomeEvent getLastDeviceEvent(String cameraId, String deviceType) {
HomeEvent event = eventBuffer.get(cameraId);
if (event == null) {
Collection<HomeEvent> events = requestDeviceEvents(cameraId, deviceType);
if (!events.isEmpty()) {
event = events.iterator().next();
eventBuffer.put(cameraId, event);
}
}
return event;
}
private Collection<HomeEvent> requestDeviceEvents(String cameraId, String deviceType) {
return getApi().map(api -> { return getApi().map(api -> {
try { try {
return api.getDeviceEvents(handler.getId(), cameraId, deviceType); return api.getDeviceEvents(handler.getId(), cameraId, deviceType);
@ -121,7 +175,7 @@ class SecurityCapability extends RestCapability<SecurityApi> {
}).orElse(List.of()); }).orElse(List.of());
} }
public Collection<HomeEvent> getPersonEvents(String personId) { private Collection<HomeEvent> requestPersonEvents(String personId) {
return getApi().map(api -> { return getApi().map(api -> {
try { try {
return api.getPersonEvents(handler.getId(), personId); return api.getPersonEvents(handler.getId(), personId);

View File

@ -13,7 +13,6 @@
package org.openhab.binding.netatmo.internal.handler.capability; package org.openhab.binding.netatmo.internal.handler.capability;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -41,9 +40,9 @@ public class SmokeCapability extends HomeSecurityThingCapability {
public List<NAObject> updateReadings() { public List<NAObject> updateReadings() {
List<NAObject> result = new ArrayList<>(); List<NAObject> result = new ArrayList<>();
securityCapability.ifPresent(cap -> { securityCapability.ifPresent(cap -> {
Collection<HomeEvent> events = cap.getDeviceEvents(handler.getId(), moduleType.apiName); HomeEvent event = cap.getLastDeviceEvent(handler.getId(), moduleType.apiName);
if (!events.isEmpty()) { if (event != null) {
result.add(events.iterator().next()); result.add(event);
} }
}); });
return result; return result;