Skip to content

Commit

Permalink
Correction for current and previous week, month, year
Browse files Browse the repository at this point in the history
Signed-off-by: Gaël L'hopital <[email protected]>
  • Loading branch information
clinique committed Oct 11, 2024
1 parent 94cba60 commit 5c0c9f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
public class LinkyHandlerFactory extends BaseThingHandlerFactory {
private static final DateTimeFormatter LINKY_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
private static final int REQUEST_BUFFER_SIZE = 8000;
private static final int RESPONSE_BUFFER_SIZE = 200000;

private final Logger logger = LoggerFactory.getLogger(LinkyHandlerFactory.class);
private final Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
Expand Down Expand Up @@ -83,6 +84,7 @@ public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID, sslContextFactory);
httpClient.setFollowRedirects(false);
httpClient.setRequestBufferSize(REQUEST_BUFFER_SIZE);
httpClient.setResponseBufferSize(RESPONSE_BUFFER_SIZE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ private synchronized void updatePowerData() {
}
}

private void setCurrentAndPrevious(Aggregate periods, String currentChannel, String previousChannel) {
double currentValue = 0.0;
double previousValue = 0.0;
if (!periods.datas.isEmpty()) {
currentValue = periods.datas.get(periods.datas.size() - 1);
if (periods.datas.size() > 1) {
previousValue = periods.datas.get(periods.datas.size() - 2);
}
}
updateKwhChannel(currentChannel, currentValue);
updateKwhChannel(previousChannel, previousValue);
}

/**
* Request new dayly/weekly data and updates channels
*/
Expand All @@ -227,17 +240,7 @@ private synchronized void updateDailyWeeklyData() {
cachedDailyData.getValue().ifPresentOrElse(values -> {
Aggregate days = values.aggregats.days;
updateKwhChannel(YESTERDAY, days.datas.get(days.datas.size() - 1));
int idxLast = days.periodes.get(days.periodes.size() - 1).dateDebut.get(weekFields.dayOfWeek()) == 7 ? 2
: 1;
Aggregate weeks = values.aggregats.weeks;
if (weeks.datas.size() > idxLast) {
updateKwhChannel(LAST_WEEK, weeks.datas.get(idxLast));
}
if (weeks.datas.size() > (idxLast + 1)) {
updateKwhChannel(THIS_WEEK, weeks.datas.get(idxLast + 1));
} else {
updateKwhChannel(THIS_WEEK, 0.0);
}
setCurrentAndPrevious(values.aggregats.weeks, THIS_WEEK, LAST_WEEK);
}, () -> {
updateKwhChannel(YESTERDAY, Double.NaN);
if (ZonedDateTime.now().get(weekFields.dayOfWeek()) == 1) {
Expand All @@ -255,22 +258,15 @@ private synchronized void updateDailyWeeklyData() {
*/
private synchronized void updateMonthlyData() {
if (isLinked(LAST_MONTH) || isLinked(THIS_MONTH)) {
cachedMonthlyData.getValue().ifPresentOrElse(values -> {
Aggregate months = values.aggregats.months;
updateKwhChannel(LAST_MONTH, months.datas.get(0));
if (months.datas.size() > 1) {
updateKwhChannel(THIS_MONTH, months.datas.get(1));
} else {
updateKwhChannel(THIS_MONTH, 0.0);
}
}, () -> {
if (ZonedDateTime.now().getDayOfMonth() == 1) {
updateKwhChannel(THIS_MONTH, 0.0);
updateKwhChannel(LAST_MONTH, Double.NaN);
} else {
updateKwhChannel(THIS_MONTH, Double.NaN);
}
});
cachedMonthlyData.getValue().ifPresentOrElse(
values -> setCurrentAndPrevious(values.aggregats.months, THIS_MONTH, LAST_MONTH), () -> {
if (ZonedDateTime.now().getDayOfMonth() == 1) {
updateKwhChannel(THIS_MONTH, 0.0);
updateKwhChannel(LAST_MONTH, Double.NaN);
} else {
updateKwhChannel(THIS_MONTH, Double.NaN);
}
});
}
}

Expand All @@ -279,22 +275,15 @@ private synchronized void updateMonthlyData() {
*/
private synchronized void updateYearlyData() {
if (isLinked(LAST_YEAR) || isLinked(THIS_YEAR)) {
cachedYearlyData.getValue().ifPresentOrElse(values -> {
Aggregate years = values.aggregats.years;
updateKwhChannel(LAST_YEAR, years.datas.get(0));
if (years.datas.size() > 1) {
updateKwhChannel(THIS_YEAR, years.datas.get(1));
} else {
updateKwhChannel(THIS_YEAR, 0.0);
}
}, () -> {
if (ZonedDateTime.now().getDayOfYear() == 1) {
updateKwhChannel(THIS_YEAR, 0.0);
updateKwhChannel(LAST_YEAR, Double.NaN);
} else {
updateKwhChannel(THIS_YEAR, Double.NaN);
}
});
cachedYearlyData.getValue().ifPresentOrElse(
values -> setCurrentAndPrevious(values.aggregats.years, THIS_MONTH, LAST_MONTH), () -> {
if (ZonedDateTime.now().getDayOfYear() == 1) {
updateKwhChannel(THIS_YEAR, 0.0);
updateKwhChannel(LAST_YEAR, Double.NaN);
} else {
updateKwhChannel(THIS_YEAR, Double.NaN);
}
});
}
}

Expand Down

0 comments on commit 5c0c9f9

Please sign in to comment.