Skip to content

Commit

Permalink
remove also the refreshinterval on ExpiringDayCache constructor, we d…
Browse files Browse the repository at this point in the history
…on't need it anymore

Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Feb 7, 2025
1 parent fead647 commit d71513f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class ExpiringDayCache<V> {
private final String name;
private final int beginningHour;
private final int beginningMinute;
private final int refreshInterval;

@Nullable
private Supplier<@Nullable V> action;
Expand All @@ -59,35 +58,14 @@ public class ExpiringDayCache<V> {
* @param beginningHour the hour in the day at which the validity period is starting
* @param action the action to retrieve/calculate the value
*/
public ExpiringDayCache(String name, int beginningHour, int beginningMinute, int refreshInterval,
Supplier<@Nullable V> action) {
public ExpiringDayCache(String name, int beginningHour, int beginningMinute, Supplier<@Nullable V> action) {
this.name = name;
this.beginningHour = beginningHour;
this.beginningMinute = beginningMinute;
this.refreshInterval = refreshInterval;
this.expiresAt = calcAlreadyExpired();
this.action = action;
}

/**
* Create a new instance.
*
* @param name the name of this cache
* @param beginningHour the hour in the day at which the validity period is starting
* @param action the action to retrieve/calculate the value
*/
public ExpiringDayCache(String name, int beginningHour, int beginningMinute, int refreshInterval) {
this.name = name;
this.beginningHour = beginningHour;
this.beginningMinute = beginningMinute;
this.refreshInterval = refreshInterval;
this.expiresAt = calcAlreadyExpired();
}

public void setAction(Supplier<@Nullable V> action) {
this.action = action;
}

/**
* Returns the value - possibly from the cache, if it is still valid.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
this.weekFields = WeekFields.of(localeProvider.getLocale());
this.timeZoneProvider = timeZoneProvider;

this.cachedDailyData = new ExpiringDayCache<>("daily cache", REFRESH_HOUR_OF_DAY, REFRESH_MINUTE_OF_DAY,
REFRESH_INTERVAL_IN_MIN);

this.cachedDailyData.setAction(() -> {
this.cachedDailyData = new ExpiringDayCache<>("daily cache", REFRESH_HOUR_OF_DAY, REFRESH_MINUTE_OF_DAY, () -> {
LocalDate today = LocalDate.now();
Consumption consumption = getConsumptionData(today.minusDays(15), today);

Expand All @@ -122,29 +119,28 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
return consumption;
});

this.cachedPowerData = new ExpiringDayCache<>("power cache", REFRESH_HOUR_OF_DAY, REFRESH_MINUTE_OF_DAY,
REFRESH_INTERVAL_IN_MIN, () -> {
// We request data for yesterday and the day before yesterday, even if the data for the day before
// yesterday
// is not needed by the binding. This is only a workaround to an API bug that will return
// INTERNAL_SERVER_ERROR rather than the expected data with a NaN value when the data for yesterday
// is not
// yet available.
// By requesting two days, the API is not failing and you get the expected NaN value for yesterday
// when the
// data is not yet available.
LocalDate today = LocalDate.now();
Consumption consumption = getPowerData(today.minusDays(2), today);
if (consumption != null) {
logData(consumption.aggregats.days, "Day (peak)", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME,
Target.ALL);
consumption = getConsumptionAfterChecks(consumption, Target.LAST);
}
return consumption;
});
this.cachedPowerData = new ExpiringDayCache<>("power cache", REFRESH_HOUR_OF_DAY, REFRESH_MINUTE_OF_DAY, () -> {
// We request data for yesterday and the day before yesterday, even if the data for the day before
// yesterday
// is not needed by the binding. This is only a workaround to an API bug that will return
// INTERNAL_SERVER_ERROR rather than the expected data with a NaN value when the data for yesterday
// is not
// yet available.
// By requesting two days, the API is not failing and you get the expected NaN value for yesterday
// when the
// data is not yet available.
LocalDate today = LocalDate.now();
Consumption consumption = getPowerData(today.minusDays(2), today);
if (consumption != null) {
logData(consumption.aggregats.days, "Day (peak)", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME,
Target.ALL);
consumption = getConsumptionAfterChecks(consumption, Target.LAST);
}
return consumption;
});

this.cachedMonthlyData = new ExpiringDayCache<>("monthly cache", REFRESH_HOUR_OF_DAY, REFRESH_MINUTE_OF_DAY,
REFRESH_INTERVAL_IN_MIN, () -> {
() -> {
LocalDate today = LocalDate.now();
Consumption consumption = getConsumptionData(today.withDayOfMonth(1).minusMonths(1), today);
if (consumption != null) {
Expand All @@ -156,7 +152,7 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
});

this.cachedYearlyData = new ExpiringDayCache<>("yearly cache", REFRESH_HOUR_OF_DAY, REFRESH_MINUTE_OF_DAY,
REFRESH_INTERVAL_IN_MIN, () -> {
() -> {
LocalDate today = LocalDate.now();
Consumption consumption = getConsumptionData(LocalDate.of(today.getYear() - 1, 1, 1), today);
if (consumption != null) {
Expand Down

0 comments on commit d71513f

Please sign in to comment.