From ee940285c02e2c523995c06b56b6b48a871fed7d Mon Sep 17 00:00:00 2001 From: Laurent ARNAL Date: Sat, 21 Dec 2024 08:38:59 +0100 Subject: [PATCH] fix dto for load curves Signed-off-by: Laurent ARNAL --- .../linky/internal/dto/MeterReading.java | 32 +++----------- .../handler/EnedisWebBridgeHandler.java | 2 +- .../linky/internal/handler/LinkyHandler.java | 44 +++++++++---------- 3 files changed, 29 insertions(+), 49 deletions(-) diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/dto/MeterReading.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/dto/MeterReading.java index 029785c717e7f..69e294ad4def5 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/dto/MeterReading.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/dto/MeterReading.java @@ -12,10 +12,6 @@ */ package org.openhab.binding.linky.internal.dto; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.List; - import org.openhab.binding.linky.internal.dto.ConsumptionReport.Data; import com.google.gson.annotations.SerializedName; @@ -43,7 +39,7 @@ public class MeterReading { public ReadingType readingType; @SerializedName("interval_reading") - public IntervalReading[] dayValue; + public IntervalReading[] baseValue; public IntervalReading[] weekValue; public IntervalReading[] monthValue; public IntervalReading[] yearValue; @@ -53,10 +49,11 @@ public static MeterReading convertFromComsumptionReport(ConsumptionReport comsum result.readingType = new ReadingType(); if (comsumptionReport.consumptions.aggregats != null) { - result.dayValue = fromAgregat(comsumptionReport.consumptions.aggregats.days); - } else { - // result.dayValue = fromLabelsAndDatas(comsumptionReport.consumptions.labels, - // comsumptionReport.consumptions.data); + if (comsumptionReport.consumptions.aggregats.days != null) { + result.baseValue = fromAgregat(comsumptionReport.consumptions.aggregats.days); + } else if (comsumptionReport.consumptions.aggregats.heure != null) { + result.baseValue = fromAgregat(comsumptionReport.consumptions.aggregats.heure); + } } return result; @@ -76,21 +73,4 @@ public static IntervalReading[] fromAgregat(ConsumptionReport.Datas agregat) { return result; } - public static IntervalReading[] fromLabelsAndDatas(List labels, List datas) { - int size = datas.size(); - IntervalReading[] result = new IntervalReading[size]; - - for (int i = 0; i < size; i++) { - Double data = datas.get(i); - String label = labels.get(i); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS[X]"); - ZonedDateTime dt = ZonedDateTime.parse(label, formatter); - - result[i] = new IntervalReading(); - result[i].value = data; - result[i].date = dt.toLocalDateTime(); - } - - return result; - } } diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java index 8989170362bb2..5d5597032e329 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java @@ -83,7 +83,7 @@ public class EnedisWebBridgeHandler extends LinkyBridgeHandler { private static final String LOAD_CURVE_CONSUMPTION_URL = PRM_INFO_BASE_URL + "%s/prms/%s/donnees-energetiques?mesuresTypeCode=COURBE&mesuresCorrigees=false&typeDonnees=CONS&dateDebut=%s"; - private static final DateTimeFormatter API_DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + private static final DateTimeFormatter API_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd"); private static final DateTimeFormatter API_DATE_FORMAT_YEAR_FIRST = DateTimeFormatter.ofPattern("yyyy-MM-dd"); private static final String URL_ENEDIS_AUTHENTICATE = BASE_URL + "/authenticate?target=" + URL_COMPTE_PART; diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java index 2939194daa797..f5e7a1935c945 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java @@ -117,7 +117,7 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider) { MeterReading meterReading = getConsumptionData(today.minusDays(1095), today); meterReading = getMeterReadingAfterChecks(meterReading); if (meterReading != null) { - logData(meterReading.dayValue, "Day", DateTimeFormatter.ISO_LOCAL_DATE, Target.ALL); + logData(meterReading.baseValue, "Day", DateTimeFormatter.ISO_LOCAL_DATE, Target.ALL); logData(meterReading.weekValue, "Week", DateTimeFormatter.ISO_LOCAL_DATE_TIME, Target.ALL); } return meterReading; @@ -135,7 +135,7 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider) { MeterReading meterReading = getPowerData(today.minusDays(1095), today); meterReading = getMeterReadingAfterChecks(meterReading); if (meterReading != null) { - logData(meterReading.dayValue, "Day (peak)", DateTimeFormatter.ISO_LOCAL_DATE, Target.ALL); + logData(meterReading.baseValue, "Day (peak)", DateTimeFormatter.ISO_LOCAL_DATE, Target.ALL); } return meterReading; }); @@ -154,7 +154,7 @@ public LinkyHandler(Thing thing, LocaleProvider localeProvider) { MeterReading meterReading = getLoadCurveConsumption(today.minusDays(6), today); meterReading = getMeterReadingAfterChecks(meterReading); if (meterReading != null) { - logData(meterReading.dayValue, "Day (peak)", DateTimeFormatter.ISO_LOCAL_DATE, Target.ALL); + logData(meterReading.baseValue, "Day (peak)", DateTimeFormatter.ISO_LOCAL_DATE, Target.ALL); } return meterReading; }); @@ -373,21 +373,21 @@ private int getTempoIdx(String color) { private synchronized void updatePowerData() { dailyConsumptionMaxPower.getValue().ifPresentOrElse(values -> { - int dSize = values.dayValue.length; + int dSize = values.baseValue.length; - updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_1, values.dayValue[dSize - 1].value); + updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_1, values.baseValue[dSize - 1].value); updateState(DAILY_GROUP, PEAK_POWER_TS_DAY_MINUS_1, - new DateTimeType(values.dayValue[dSize - 1].date.atZone(ZoneId.systemDefault()))); + new DateTimeType(values.baseValue[dSize - 1].date.atZone(ZoneId.systemDefault()))); - updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_2, values.dayValue[dSize - 2].value); + updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_2, values.baseValue[dSize - 2].value); updateState(DAILY_GROUP, PEAK_POWER_TS_DAY_MINUS_2, - new DateTimeType(values.dayValue[dSize - 2].date.atZone(ZoneId.systemDefault()))); + new DateTimeType(values.baseValue[dSize - 2].date.atZone(ZoneId.systemDefault()))); - updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_3, values.dayValue[dSize - 3].value); + updatekVAChannel(DAILY_GROUP, PEAK_POWER_DAY_MINUS_3, values.baseValue[dSize - 3].value); updateState(DAILY_GROUP, PEAK_POWER_TS_DAY_MINUS_3, - new DateTimeType(values.dayValue[dSize - 3].date.atZone(ZoneId.systemDefault()))); + new DateTimeType(values.baseValue[dSize - 3].date.atZone(ZoneId.systemDefault()))); - updatePowerTimeSeries(DAILY_GROUP, MAX_POWER_CHANNEL, values.dayValue); + updatePowerTimeSeries(DAILY_GROUP, MAX_POWER_CHANNEL, values.baseValue); updatePowerTimeSeries(WEEKLY_GROUP, MAX_POWER_CHANNEL, values.weekValue); updatePowerTimeSeries(MONTHLY_GROUP, MAX_POWER_CHANNEL, values.monthValue); updatePowerTimeSeries(YEARLY_GROUP, MAX_POWER_CHANNEL, values.yearValue); @@ -411,9 +411,9 @@ private synchronized void updateEnergyData() { dailyConsumption.getValue().ifPresentOrElse(values -> { int dSize = values.dayValue.length; - updateKwhChannel(DAILY_GROUP, DAY_MINUS_1, values.dayValue[dSize - 1].value); - updateKwhChannel(DAILY_GROUP, DAY_MINUS_2, values.dayValue[dSize - 2].value); - updateKwhChannel(DAILY_GROUP, DAY_MINUS_3, values.dayValue[dSize - 3].value); + updateKwhChannel(DAILY_GROUP, DAY_MINUS_1, values.baseValue[dSize - 1].value); + updateKwhChannel(DAILY_GROUP, DAY_MINUS_2, values.baseValue[dSize - 2].value); + updateKwhChannel(DAILY_GROUP, DAY_MINUS_3, values.baseValue[dSize - 3].value); int idxCurrentYear = values.yearValue.length - 1; int idxCurrentWeek = values.weekValue.length - 1; @@ -709,8 +709,8 @@ public synchronized void handleCommand(ChannelUID channelUID, Command command) { if (meterReading != null) { if (meterReading.weekValue == null) { - LocalDate startDate = meterReading.dayValue[0].date.toLocalDate(); - LocalDate endDate = meterReading.dayValue[meterReading.dayValue.length - 1].date.toLocalDate(); + LocalDate startDate = meterReading.baseValue[0].date.toLocalDate(); + LocalDate endDate = meterReading.baseValue[meterReading.dayValue.length - 1].date.toLocalDate(); int weeksNum = Weeks.between(startDate, endDate).getAmount() + 2; int monthsNum = Months.between(startDate, endDate).getAmount() + 2; @@ -730,13 +730,13 @@ public synchronized void handleCommand(ChannelUID channelUID, Command command) { meterReading.yearValue[idx] = new IntervalReading(); } - int size = meterReading.dayValue.length; - int baseYear = meterReading.dayValue[0].date.getYear(); - int baseMonth = meterReading.dayValue[0].date.getMonthValue(); - int baseWeek = meterReading.dayValue[0].date.get(WeekFields.of(Locale.FRANCE).weekOfYear()); + int size = meterReading.baseValue.length; + int baseYear = meterReading.baseValue[0].date.getYear(); + int baseMonth = meterReading.baseValue[0].date.getMonthValue(); + int baseWeek = meterReading.baseValue[0].date.get(WeekFields.of(Locale.FRANCE).weekOfYear()); for (int idx = 0; idx < size; idx++) { - IntervalReading ir = meterReading.dayValue[idx]; + IntervalReading ir = meterReading.baseValue[idx]; LocalDateTime dt = ir.date; double value = ir.value; value = value / divider; @@ -778,7 +778,7 @@ public synchronized void handleCommand(ChannelUID channelUID, Command command) { private void checkData(@Nullable MeterReading meterReading) throws LinkyException { if (meterReading != null) { - if (meterReading.dayValue.length == 0) { + if (meterReading.baseValue.length == 0) { throw new LinkyException("Invalid meterReading data: no day period"); } }