Skip to content

Commit

Permalink
Check data availability for the peak consumption of yesterday
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Jan 16, 2021
1 parent b32eb3c commit 28bac8f
Showing 1 changed file with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
LocalDate today = LocalDate.now();
Consumption consumption = getConsumptionData(today.minusDays(15), today);
if (consumption != null) {
logData(consumption.aggregats.days, "Day", false, DateTimeFormatter.ISO_LOCAL_DATE, false);
logData(consumption.aggregats.weeks, "Week", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false);
consumption = getConsumptionAfterChecks(consumption);
logData(consumption.aggregats.days, "Day", false, DateTimeFormatter.ISO_LOCAL_DATE, false, false);
logData(consumption.aggregats.weeks, "Week", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false, false);
consumption = getConsumptionAfterChecks(consumption, false, true);
}
return consumption;
});
Expand All @@ -106,13 +106,9 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
LocalDate from = to.minusDays(2);
Consumption consumption = getPowerData(from, to);
if (consumption != null) {
try {
checkData(consumption);
} catch (LinkyException e) {
logger.debug("Power data: {}", e.getMessage());
return null;
}
logData(consumption.aggregats.days, "Day", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false);
logData(consumption.aggregats.days, "Day (peak)", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false,
false);
consumption = getConsumptionAfterChecks(consumption, true, false);
}
return consumption;
});
Expand All @@ -121,8 +117,9 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
LocalDate today = LocalDate.now();
Consumption consumption = getConsumptionData(today.withDayOfMonth(1).minusMonths(1), today);
if (consumption != null) {
logData(consumption.aggregats.months, "Month", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false);
consumption = getConsumptionAfterChecks(consumption);
logData(consumption.aggregats.months, "Month", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false,
false);
consumption = getConsumptionAfterChecks(consumption, false, true);
}
return consumption;
});
Expand All @@ -131,8 +128,8 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpC
LocalDate today = LocalDate.now();
Consumption consumption = getConsumptionData(LocalDate.of(today.getYear() - 1, 1, 1), today);
if (consumption != null) {
logData(consumption.aggregats.years, "Year", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false);
consumption = getConsumptionAfterChecks(consumption);
logData(consumption.aggregats.years, "Year", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, false, false);
consumption = getConsumptionAfterChecks(consumption, false, true);
}
return consumption;
});
Expand Down Expand Up @@ -447,14 +444,19 @@ public synchronized void handleCommand(ChannelUID channelUID, Command command) {
}
}

private @Nullable Consumption getConsumptionAfterChecks(Consumption consumption) {
private @Nullable Consumption getConsumptionAfterChecks(Consumption consumption, boolean checkFirst,
boolean checkLast) {
try {
checkData(consumption);
} catch (LinkyException e) {
logger.debug("Consumption data: {}", e.getMessage());
return null;
}
if (!isDataLastDayAvailable(consumption)) {
if (checkFirst && !isDataFirstDayAvailable(consumption)) {
logger.debug("Data including yesterday are not yet available");
return null;
}
if (checkLast && !isDataLastDayAvailable(consumption)) {
logger.debug("Data including yesterday are not yet available");
return null;
}
Expand Down Expand Up @@ -488,19 +490,29 @@ public void checkData(Consumption consumption) throws LinkyException {
}
}

private boolean isDataFirstDayAvailable(Consumption consumption) {
Aggregate days = consumption.aggregats.days;
logData(days, "First day", false, DateTimeFormatter.ISO_LOCAL_DATE, true, false);
return days.datas != null && days.datas.size() > 0 && !days.datas.get(0).isNaN();
}

private boolean isDataLastDayAvailable(Consumption consumption) {
Aggregate days = consumption.aggregats.days;
logData(days, "Last day", false, DateTimeFormatter.ISO_LOCAL_DATE, true);
logData(days, "Last day", false, DateTimeFormatter.ISO_LOCAL_DATE, false, true);
return days.datas != null && days.datas.size() > 0 && !days.datas.get(days.datas.size() - 1).isNaN();
}

private void logData(Aggregate aggregate, String title, boolean withDateFin, DateTimeFormatter dateTimeFormatter,
boolean onlyLast) {
boolean onlyFirst, boolean onlyLast) {
if (logger.isDebugEnabled()) {
int size = (aggregate.datas == null || aggregate.periodes == null) ? 0
: (aggregate.datas.size() <= aggregate.periodes.size() ? aggregate.datas.size()
: aggregate.periodes.size());
if (onlyLast) {
if (onlyFirst) {
if (size > 0) {
logData(aggregate, 0, title, withDateFin, dateTimeFormatter);
}
} else if (onlyLast) {
if (size > 0) {
logData(aggregate, size - 1, title, withDateFin, dateTimeFormatter);
}
Expand Down

0 comments on commit 28bac8f

Please sign in to comment.