[netatmo] Add a buffer to lower Home API requests (#16562)

* Adding CacheCapability to HomeAPI

---------

Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: gael@lhopital.org <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2024-03-25 11:41:33 +01:00 committed by GitHub
parent 9dd2827148
commit 0a8e218123
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 20 deletions

View File

@ -17,33 +17,33 @@ import java.time.Instant;
import java.util.List; import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.netatmo.internal.api.WeatherApi; import org.openhab.binding.netatmo.internal.api.RestManager;
import org.openhab.binding.netatmo.internal.api.dto.NAObject; import org.openhab.binding.netatmo.internal.api.dto.NAObject;
import org.openhab.binding.netatmo.internal.handler.CommonInterface; import org.openhab.binding.netatmo.internal.handler.CommonInterface;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* {@link CacheWeatherCapability} give the ability to buffer weather related requests and reduce server requests * {@link CacheCapability} give the ability to buffer RestManager related requests and reduce server requests
* *
* @author Gaël L'hopital - Initial contribution * @author Gaël L'hopital - Initial contribution
* *
*/ */
@NonNullByDefault @NonNullByDefault
public abstract class CacheWeatherCapability extends RestCapability<WeatherApi> { public abstract class CacheCapability<T extends RestManager> extends RestCapability<T> {
private final Logger logger = LoggerFactory.getLogger(CacheWeatherCapability.class); private final Logger logger = LoggerFactory.getLogger(CacheCapability.class);
private final Duration validity; private final Duration validity;
private List<NAObject> lastResult = List.of(); private List<NAObject> lastResult = List.of();
private Instant requestTS = Instant.MIN; private Instant requestTS = Instant.MIN;
public CacheWeatherCapability(CommonInterface handler, Duration validity) { public CacheCapability(CommonInterface handler, Duration validity, Class<T> restManagerClazz) {
super(handler, WeatherApi.class); super(handler, restManagerClazz);
this.validity = validity; this.validity = validity;
} }
@Override @Override
protected synchronized List<NAObject> updateReadings(WeatherApi api) { protected synchronized List<NAObject> updateReadings(T api) {
Instant now = Instant.now(); Instant now = Instant.now();
if (requestTS.plus(validity).isBefore(now)) { if (requestTS.plus(validity).isBefore(now)) {
@ -58,5 +58,5 @@ public abstract class CacheWeatherCapability extends RestCapability<WeatherApi>
return lastResult; return lastResult;
} }
protected abstract List<NAObject> getFreshData(WeatherApi api); protected abstract List<NAObject> getFreshData(T api);
} }

View File

@ -14,6 +14,7 @@ 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.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -42,7 +43,7 @@ import org.slf4j.LoggerFactory;
* *
*/ */
@NonNullByDefault @NonNullByDefault
public class HomeCapability extends RestCapability<HomeApi> { public class HomeCapability extends CacheCapability<HomeApi> {
private final Logger logger = LoggerFactory.getLogger(HomeCapability.class); private final Logger logger = LoggerFactory.getLogger(HomeCapability.class);
private final Set<FeatureArea> featureAreas = new HashSet<>(); private final Set<FeatureArea> featureAreas = new HashSet<>();
@ -50,7 +51,7 @@ public class HomeCapability extends RestCapability<HomeApi> {
private final Set<String> homeIds = new HashSet<>(3); private final Set<String> homeIds = new HashSet<>(3);
public HomeCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) { public HomeCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) {
super(handler, HomeApi.class); super(handler, Duration.ofSeconds(2), HomeApi.class);
this.descriptionProvider = descriptionProvider; this.descriptionProvider = descriptionProvider;
} }
@ -105,7 +106,7 @@ public class HomeCapability extends RestCapability<HomeApi> {
} }
@Override @Override
protected List<NAObject> updateReadings(HomeApi api) { protected List<NAObject> getFreshData(HomeApi api) {
List<NAObject> result = new ArrayList<>(); List<NAObject> result = new ArrayList<>();
homeIds.stream().filter(id -> !id.isEmpty()).forEach(id -> { homeIds.stream().filter(id -> !id.isEmpty()).forEach(id -> {
try { try {

View File

@ -44,12 +44,12 @@ import org.slf4j.LoggerFactory;
* *
*/ */
@NonNullByDefault @NonNullByDefault
public class MeasureCapability extends CacheWeatherCapability { public class MeasureCapability extends CacheCapability<WeatherApi> {
private final Logger logger = LoggerFactory.getLogger(MeasureCapability.class); private final Logger logger = LoggerFactory.getLogger(MeasureCapability.class);
private final Map<String, State> measures = new HashMap<>(); private final Map<String, State> measures = new HashMap<>();
public MeasureCapability(CommonInterface handler, List<ChannelHelper> helpers) { public MeasureCapability(CommonInterface handler, List<ChannelHelper> helpers) {
super(handler, Duration.ofMinutes(30)); super(handler, Duration.ofMinutes(30), WeatherApi.class);
MeasuresChannelHelper measureChannelHelper = (MeasuresChannelHelper) helpers.stream() MeasuresChannelHelper measureChannelHelper = (MeasuresChannelHelper) helpers.stream()
.filter(c -> c instanceof MeasuresChannelHelper).findFirst() .filter(c -> c instanceof MeasuresChannelHelper).findFirst()
.orElseThrow(() -> new IllegalArgumentException( .orElseThrow(() -> new IllegalArgumentException(

View File

@ -83,7 +83,7 @@ public class RefreshCapability extends Capability {
handler.proceedWithUpdate(); handler.proceedWithUpdate();
long delay; long delay;
if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) { if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) {
logger.debug("Module is not ONLINE; special refresh interval is used"); logger.debug("{} is not ONLINE, special refresh interval is used", thingUID);
delay = OFFLINE_INTERVAL.toSeconds(); delay = OFFLINE_INTERVAL.toSeconds();
if (probing()) { if (probing()) {
dataTimeStamp0 = Instant.MIN; dataTimeStamp0 = Instant.MIN;
@ -93,7 +93,7 @@ public class RefreshCapability extends Capability {
: (probing() ? PROBING_INTERVAL : dataValidity.minus(dataAge()).plus(DEFAULT_DELAY)).toSeconds(); : (probing() ? PROBING_INTERVAL : dataValidity.minus(dataAge()).plus(DEFAULT_DELAY)).toSeconds();
} }
delay = delay < 2 ? PROBING_INTERVAL.toSeconds() : delay; delay = delay < 2 ? PROBING_INTERVAL.toSeconds() : delay;
logger.debug("Module refreshed, next one in {}s", delay); logger.debug("{} refreshed, next one in {}s", thingUID, delay);
freeJobAndReschedule(delay); freeJobAndReschedule(delay);
} }
@ -104,13 +104,13 @@ public class RefreshCapability extends Capability {
if (probing()) { if (probing()) {
if (Instant.MIN.equals(dataTimeStamp0)) { if (Instant.MIN.equals(dataTimeStamp0)) {
dataTimeStamp0 = tsInstant; dataTimeStamp0 = tsInstant;
logger.debug("First data timestamp is {}", dataTimeStamp0); logger.debug("First data timestamp of {} is {}", thingUID, dataTimeStamp0);
} else if (tsInstant.isAfter(dataTimeStamp0)) { } else if (tsInstant.isAfter(dataTimeStamp0)) {
dataValidity = Duration.between(dataTimeStamp0, tsInstant); dataValidity = Duration.between(dataTimeStamp0, tsInstant);
refreshConfigured = true; refreshConfigured = true;
logger.debug("Data validity period identified to be {}", dataValidity); logger.debug("Data validity period of {} identified to be {}", thingUID, dataValidity);
} else { } else {
logger.debug("Data validity period not yet found, data timestamp unchanged"); logger.debug("Data validity period of {} not yet found, data timestamp unchanged", thingUID);
} }
} }
dataTimeStamp = tsInstant; dataTimeStamp = tsInstant;

View File

@ -30,11 +30,11 @@ import org.slf4j.LoggerFactory;
* *
*/ */
@NonNullByDefault @NonNullByDefault
public class WeatherCapability extends CacheWeatherCapability { public class WeatherCapability extends CacheCapability<WeatherApi> {
private final Logger logger = LoggerFactory.getLogger(WeatherCapability.class); private final Logger logger = LoggerFactory.getLogger(WeatherCapability.class);
public WeatherCapability(CommonInterface handler) { public WeatherCapability(CommonInterface handler) {
super(handler, Duration.ofSeconds(2)); super(handler, Duration.ofSeconds(2), WeatherApi.class);
} }
@Override @Override