Skip to content

Commit

Permalink
fix dto for load curves
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 21, 2024
1 parent 21a738e commit ee94028
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -76,21 +73,4 @@ public static IntervalReading[] fromAgregat(ConsumptionReport.Datas agregat) {
return result;
}

public static IntervalReading[] fromLabelsAndDatas(List<String> labels, List<Double> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
});
Expand All @@ -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;
});
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
}
Expand Down

0 comments on commit ee94028

Please sign in to comment.