Fixed data storage update

This commit is contained in:
Gunnar Skjold
2023-05-04 11:44:22 +02:00
parent 60c7cea724
commit 804e43824b
2 changed files with 6 additions and 8 deletions

View File

@@ -574,19 +574,20 @@ bool AmsDataStorage::isHappy() {
bool AmsDataStorage::isDayHappy() {
time_t now = time(nullptr);
if(now < FirmwareVersion::BuildEpoch) return false;
tmElements_t tm, last;
if(now < day.lastMeterReadTime) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(AmsDataStorage) Day data timestamp %lu < %lu\n"), (int32_t) now, (int32_t) day.lastMeterReadTime);
return false;
}
breakTime(tz->toLocal(now), tm);
breakTime(tz->toLocal(day.lastMeterReadTime), last);
if(now-day.lastMeterReadTime > 3600) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(AmsDataStorage) Day data timestamp age %lu - %lu > 3600\n"), (int32_t) now, (int32_t) day.lastMeterReadTime);
return false;
}
if(tm.Hour > last.Hour) {
tmElements_t tm, last;
breakTime(tz->toLocal(now), tm);
breakTime(tz->toLocal(day.lastMeterReadTime), last);
if(tm.Hour != last.Hour) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(AmsDataStorage) Day data hour of last timestamp %d > %d\n"), tm.Hour, last.Hour);
return false;
}

View File

@@ -408,7 +408,6 @@ bool wifiConnected = false;
unsigned long lastTemperatureRead = 0;
unsigned long lastSysupdate = 0;
unsigned long lastErrorBlink = 0;
unsigned long lastDataStoreUpdate = 0;
int lastError = 0;
bool meterAutodetect = false;
@@ -546,7 +545,7 @@ void loop() {
debugW_P(PSTR("Used %dms to read HAN port (false)"), millis()-start);
}
}
if(now > lastDataStoreUpdate && now - lastDataStoreUpdate > 3600000 && !ds.isHappy()) {
if(!ds.isHappy() && now - meterState.getLastUpdateMillis() > 1800000) {
handleClear(now);
}
} catch(const std::exception& e) {
@@ -586,7 +585,6 @@ void handleClear(unsigned long now) {
AmsData nullData;
debugI_P(PSTR("Clearing data that have not been updated"));
ds.update(&nullData);
lastDataStoreUpdate = now;
}
}
@@ -1197,7 +1195,6 @@ void handleDataSuccess(AmsData* data) {
if(saveData) {
debugI_P(PSTR("Saving data"));
ds.save();
lastDataStoreUpdate = millis();
}
}