Skip to content

Commit

Permalink
[linky] Set channel to UNDEF when data not yet available (openhab#9774)
Browse files Browse the repository at this point in the history
Fix openhab#9666

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and thinkingstone committed Nov 7, 2021
1 parent 8f5fdd9 commit 9922230
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.WeekFields;
Expand Down Expand Up @@ -214,9 +215,11 @@ private synchronized void updatePowerData() {
*/
private synchronized void updateDailyData() {
if (isLinked(YESTERDAY)) {
cachedDailyData.getValue().ifPresent(values -> {
cachedDailyData.getValue().ifPresentOrElse(values -> {
Aggregate days = values.aggregats.days;
updateKwhChannel(YESTERDAY, days.datas.get(days.datas.size() - 1));
}, () -> {
updateKwhChannel(YESTERDAY, Double.NaN);
});
}
}
Expand All @@ -226,7 +229,7 @@ private synchronized void updateDailyData() {
*/
private synchronized void updateWeeklyData() {
if (isLinked(LAST_WEEK) || isLinked(THIS_WEEK)) {
cachedDailyData.getValue().ifPresent(values -> {
cachedDailyData.getValue().ifPresentOrElse(values -> {
Aggregate days = values.aggregats.days;
int idxLast = days.periodes.get(days.periodes.size() - 1).dateDebut.get(weekFields.dayOfWeek()) == 7 ? 2
: 1;
Expand All @@ -239,6 +242,13 @@ private synchronized void updateWeeklyData() {
} else {
updateKwhChannel(THIS_WEEK, 0.0);
}
}, () -> {
if (ZonedDateTime.now().get(weekFields.dayOfWeek()) == 1) {
updateKwhChannel(THIS_WEEK, 0.0);
updateKwhChannel(LAST_WEEK, Double.NaN);
} else {
updateKwhChannel(THIS_WEEK, Double.NaN);
}
});
}
}
Expand All @@ -248,14 +258,21 @@ private synchronized void updateWeeklyData() {
*/
private synchronized void updateMonthlyData() {
if (isLinked(LAST_MONTH) || isLinked(THIS_MONTH)) {
cachedMonthlyData.getValue().ifPresent(values -> {
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);
}
});
}
}
Expand All @@ -265,14 +282,21 @@ private synchronized void updateMonthlyData() {
*/
private synchronized void updateYearlyData() {
if (isLinked(LAST_YEAR) || isLinked(THIS_YEAR)) {
cachedYearlyData.getValue().ifPresent(values -> {
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);
}
});
}
}
Expand Down

0 comments on commit 9922230

Please sign in to comment.