mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[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:
parent
9dd2827148
commit
0a8e218123
@ -17,33 +17,33 @@ import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
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.handler.CommonInterface;
|
||||
import org.slf4j.Logger;
|
||||
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
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public abstract class CacheWeatherCapability extends RestCapability<WeatherApi> {
|
||||
private final Logger logger = LoggerFactory.getLogger(CacheWeatherCapability.class);
|
||||
public abstract class CacheCapability<T extends RestManager> extends RestCapability<T> {
|
||||
private final Logger logger = LoggerFactory.getLogger(CacheCapability.class);
|
||||
private final Duration validity;
|
||||
|
||||
private List<NAObject> lastResult = List.of();
|
||||
private Instant requestTS = Instant.MIN;
|
||||
|
||||
public CacheWeatherCapability(CommonInterface handler, Duration validity) {
|
||||
super(handler, WeatherApi.class);
|
||||
public CacheCapability(CommonInterface handler, Duration validity, Class<T> restManagerClazz) {
|
||||
super(handler, restManagerClazz);
|
||||
this.validity = validity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized List<NAObject> updateReadings(WeatherApi api) {
|
||||
protected synchronized List<NAObject> updateReadings(T api) {
|
||||
Instant now = Instant.now();
|
||||
|
||||
if (requestTS.plus(validity).isBefore(now)) {
|
||||
@ -58,5 +58,5 @@ public abstract class CacheWeatherCapability extends RestCapability<WeatherApi>
|
||||
return lastResult;
|
||||
}
|
||||
|
||||
protected abstract List<NAObject> getFreshData(WeatherApi api);
|
||||
protected abstract List<NAObject> getFreshData(T api);
|
||||
}
|
@ -14,6 +14,7 @@ package org.openhab.binding.netatmo.internal.handler.capability;
|
||||
|
||||
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -42,7 +43,7 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class HomeCapability extends RestCapability<HomeApi> {
|
||||
public class HomeCapability extends CacheCapability<HomeApi> {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(HomeCapability.class);
|
||||
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);
|
||||
|
||||
public HomeCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) {
|
||||
super(handler, HomeApi.class);
|
||||
super(handler, Duration.ofSeconds(2), HomeApi.class);
|
||||
this.descriptionProvider = descriptionProvider;
|
||||
}
|
||||
|
||||
@ -105,7 +106,7 @@ public class HomeCapability extends RestCapability<HomeApi> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<NAObject> updateReadings(HomeApi api) {
|
||||
protected List<NAObject> getFreshData(HomeApi api) {
|
||||
List<NAObject> result = new ArrayList<>();
|
||||
homeIds.stream().filter(id -> !id.isEmpty()).forEach(id -> {
|
||||
try {
|
||||
|
@ -44,12 +44,12 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class MeasureCapability extends CacheWeatherCapability {
|
||||
public class MeasureCapability extends CacheCapability<WeatherApi> {
|
||||
private final Logger logger = LoggerFactory.getLogger(MeasureCapability.class);
|
||||
private final Map<String, State> measures = new HashMap<>();
|
||||
|
||||
public MeasureCapability(CommonInterface handler, List<ChannelHelper> helpers) {
|
||||
super(handler, Duration.ofMinutes(30));
|
||||
super(handler, Duration.ofMinutes(30), WeatherApi.class);
|
||||
MeasuresChannelHelper measureChannelHelper = (MeasuresChannelHelper) helpers.stream()
|
||||
.filter(c -> c instanceof MeasuresChannelHelper).findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
|
@ -83,7 +83,7 @@ public class RefreshCapability extends Capability {
|
||||
handler.proceedWithUpdate();
|
||||
long delay;
|
||||
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();
|
||||
if (probing()) {
|
||||
dataTimeStamp0 = Instant.MIN;
|
||||
@ -93,7 +93,7 @@ public class RefreshCapability extends Capability {
|
||||
: (probing() ? PROBING_INTERVAL : dataValidity.minus(dataAge()).plus(DEFAULT_DELAY)).toSeconds();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@ -104,13 +104,13 @@ public class RefreshCapability extends Capability {
|
||||
if (probing()) {
|
||||
if (Instant.MIN.equals(dataTimeStamp0)) {
|
||||
dataTimeStamp0 = tsInstant;
|
||||
logger.debug("First data timestamp is {}", dataTimeStamp0);
|
||||
logger.debug("First data timestamp of {} is {}", thingUID, dataTimeStamp0);
|
||||
} else if (tsInstant.isAfter(dataTimeStamp0)) {
|
||||
dataValidity = Duration.between(dataTimeStamp0, tsInstant);
|
||||
refreshConfigured = true;
|
||||
logger.debug("Data validity period identified to be {}", dataValidity);
|
||||
logger.debug("Data validity period of {} identified to be {}", thingUID, dataValidity);
|
||||
} 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;
|
||||
|
@ -30,11 +30,11 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class WeatherCapability extends CacheWeatherCapability {
|
||||
public class WeatherCapability extends CacheCapability<WeatherApi> {
|
||||
private final Logger logger = LoggerFactory.getLogger(WeatherCapability.class);
|
||||
|
||||
public WeatherCapability(CommonInterface handler) {
|
||||
super(handler, Duration.ofSeconds(2));
|
||||
super(handler, Duration.ofSeconds(2), WeatherApi.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user