From af6ecc5d47a51a62a9e8d6de672d0cb87ac21f21 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 21 Sep 2024 20:03:46 +0200 Subject: [PATCH] Adjustments of clock parsing from dlms --- lib/AmsDataStorage/src/AmsDataStorage.cpp | 52 ++++++++++++++++++++++- lib/MeterCommunicators/src/IEC6205675.cpp | 8 ++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/AmsDataStorage/src/AmsDataStorage.cpp b/lib/AmsDataStorage/src/AmsDataStorage.cpp index 12a80cad..9a703d3d 100644 --- a/lib/AmsDataStorage/src/AmsDataStorage.cpp +++ b/lib/AmsDataStorage/src/AmsDataStorage.cpp @@ -68,10 +68,30 @@ bool AmsDataStorage::update(AmsData* data, time_t now) { // Clear hours between last update and now if(!isDayHappy(now) && day.lastMeterReadTime > now) { + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Day was updated in the future, resetting\n")); day.activeImport = importCounter; day.activeExport = exportCounter; day.lastMeterReadTime = now; - } else if((importCounter > 0 && day.activeImport == 0) || now - day.lastMeterReadTime > 86400) { + } else if(importCounter > 0 && day.activeImport == 0) { + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Initializing day data\n")); + day.activeImport = importCounter; + day.activeExport = exportCounter; + day.lastMeterReadTime = now; + for(int i = 0; i<24; i++) { + setHourImport(i, 0); + setHourExport(i, 0); + } + } else if(now - day.lastMeterReadTime > 86400) { + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Day was updated to long ago, clearing\n")); day.activeImport = importCounter; day.activeExport = exportCounter; day.lastMeterReadTime = now; @@ -83,6 +103,10 @@ bool AmsDataStorage::update(AmsData* data, time_t now) { tmElements_t last; breakTime(day.lastMeterReadTime, last); uint8_t endHour = utc.Hour; + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Clearing hours from %d to %d\n"), last.Hour, endHour); if(last.Hour > utc.Hour){ for(int i = 0; i < utc.Hour; i++) { setHourImport(i, 0); @@ -98,10 +122,30 @@ bool AmsDataStorage::update(AmsData* data, time_t now) { // Clear days between last update and now if(!isMonthHappy(now) && month.lastMeterReadTime > now) { + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Month was updated in the future, resetting\n")); month.activeImport = importCounter; month.activeExport = exportCounter; month.lastMeterReadTime = now; - } else if((importCounter > 0 && month.activeImport == 0) || now - month.lastMeterReadTime > 2682000) { + } else if(importCounter > 0 && month.activeImport == 0) { + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Initializing month data\n")); + month.activeImport = importCounter; + month.activeExport = exportCounter; + month.lastMeterReadTime = now; + for(int i = 1; i<=31; i++) { + setDayImport(i, 0); + setDayExport(i, 0); + } + } else if(now - month.lastMeterReadTime > 2682000) { + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Month was updated to long ago, clearing\n")); month.activeImport = importCounter; month.activeExport = exportCounter; month.lastMeterReadTime = now; @@ -113,6 +157,10 @@ bool AmsDataStorage::update(AmsData* data, time_t now) { tmElements_t last; breakTime(tz->toLocal(month.lastMeterReadTime), last); uint8_t endDay = ltz.Day; + #if defined(AMS_REMOTE_DEBUG) + if (debugger->isActive(RemoteDebug::DEBUG)) + #endif + debugger->printf_P(PSTR("Clearing days from %d to %d\n"), last.Day, endDay); if(last.Day > ltz.Day) { for(int i = 1; i < ltz.Day; i++) { setDayImport(i, 0); diff --git a/lib/MeterCommunicators/src/IEC6205675.cpp b/lib/MeterCommunicators/src/IEC6205675.cpp index 2c026803..e9698ab5 100644 --- a/lib/MeterCommunicators/src/IEC6205675.cpp +++ b/lib/MeterCommunicators/src/IEC6205675.cpp @@ -404,10 +404,10 @@ IEC6205675::IEC6205675(const char* d, uint8_t useMeterType, MeterConfig* meterCo if(meterTs != NULL) { AmsOctetTimestamp* amst = (AmsOctetTimestamp*) meterTs; time_t ts = decodeCosemDateTime(amst->dt); - if(meterType == AmsTypeAidon || meterType == AmsTypeKamstrup) { - meterTimestamp = ts - 3600; - } else { - meterTimestamp = ts; + if(amst->dt.deviation == 0x8000) { // Deviation not specified, adjust from localtime to UTC + meterTimestamp = tz.toUTC(ts); + } else if(meterType == AmsTypeAidon) { + meterTimestamp = ts - 3600; // 21.09.24, the clock is now correct } }