From a90ed40aeeb2824d73c7835067acb7f828947e63 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Tue, 5 Jul 2022 21:48:56 +0200 Subject: [PATCH] Fixed config file loading of energyaccounting --- src/AmsToMqttBridge.ino | 43 +++++++++++++++++++++++++++++----------- src/EnergyAccounting.cpp | 20 ++++++++++++++----- src/EnergyAccounting.h | 4 ++-- src/web/AmsWebServer.cpp | 2 +- 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 6f12dc46..dcf10686 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -1423,6 +1423,8 @@ void configFileParse() { bool lNtp = false; bool lEntsoe = false; bool lEac = false; + bool sEa = false; + bool sDs = false; SystemConfig sys; WiFiConfig wifi; @@ -1649,6 +1651,7 @@ void configFileParse() { i++; } ds.setDayData(day); + sDs = true; } else if(strncmp(buf, "monthplot ", 10) == 0) { int i = 0; MonthDataPoints month = { 5 }; // Use a version we know the multiplier of the data points @@ -1671,13 +1674,17 @@ void configFileParse() { i++; } ds.setMonthData(month); + sDs = true; } else if(strncmp(buf, "energyaccounting ", 17) == 0) { - int i = 0; - EnergyAccountingData ead = { 1 }; + uint8_t i = 0; + EnergyAccountingData ead = { 2 }; + long hours = 0; uint16_t *maxHours = NULL; char * pch = strtok (buf+17," "); while (pch != NULL) { - if(i == 1) { + if(i == 0) { + // Ignore version + } else if(i == 1) { long val = String(pch).toInt(); ead.month = val; } else if(i == 2) { @@ -1693,21 +1700,33 @@ void configFileParse() { double val = String(pch).toDouble(); ead.costLastMonth = val * 100; } else if(i == 6) { - int val = String(pch).toInt(); - if(val > 0) { - maxHours = new uint16_t[val]; + hours = String(pch).toInt(); + debugD("Got %d max hours", hours); + Serial.flush(); + if(hours > 0 && hours < 6) { + maxHours = new uint16_t[hours]; + for(uint8_t x = 0; x < hours; x++) { + maxHours[x] = 0; + } } - } else { + } else if(i >= 7 && i < hours+7) { + uint8_t hour = i-7; double val = String(pch).toDouble(); - maxHours[i-7] = val * 100; + debugD(" hour %d: %.2f", hour, val); + maxHours[hour] = val * 100; } pch = strtok (NULL, " "); i++; } ea.setData(ead); - if(maxHours != NULL) { - ea.setMaxHours(maxHours); + if(maxHours == NULL) { + maxHours = new uint16_t[3]; + for(i = 0; i < 3; i++) { + maxHours[i] = 0; + } } + ea.setMaxHours(maxHours); + sEa = true; } memset(buf, 0, 1024); } @@ -1727,7 +1746,7 @@ void configFileParse() { if(lDomo) config.setDomoticzConfig(domo); if(lNtp) config.setNtpConfig(ntp); if(lEntsoe) config.setEntsoeConfig(entsoe); - ds.save(); - ea.save(); + if(sDs) ds.save(); + if(sEa) ea.save(); config.save(); } diff --git a/src/EnergyAccounting.cpp b/src/EnergyAccounting.cpp index 678d254c..9ba793b1 100644 --- a/src/EnergyAccounting.cpp +++ b/src/EnergyAccounting.cpp @@ -93,7 +93,7 @@ bool EnergyAccounting::update(AmsData* amsData) { if(debugger->isActive(RemoteDebug::INFO)) { debugger->printf("(EnergyAccounting) Current max calculated from %d hours with highest consumption\n", config->hours); for(uint8_t i = 0; i < config->hours; i++) { - debugger->printf("(EnergyAccounting) hour %d: %d\n", i+1, maxHours[i]*10); + debugger->printf("(EnergyAccounting) hour %d: %.2f\n", i+1, maxHours[i]/100.0); } } @@ -259,8 +259,10 @@ bool EnergyAccounting::load() { memcpy(&this->data, data, sizeof(this->data)); uint8_t b = 0; for(uint8_t i = sizeof(this->data); i < file.size(); i+=2) { - memcpy(&this->maxHours[b++], buf+i, 2); - if(b > config->hours) break; + memcpy(&this->maxHours[b], buf+i, 2); + if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Loading max hour %d: %.2f\n", b, this->maxHours[b] / 100.0); + b++; + if(b >= config->hours) break; } ret = true; } else if(data->version == 1) { @@ -268,6 +270,7 @@ bool EnergyAccounting::load() { for(uint8_t i = 0; i < config->hours; i++) { maxHours[i] = data->unused; } + data->unused = 0; data->version = 2; ret = true; } else { @@ -296,7 +299,7 @@ bool EnergyAccounting::save() { File file = LittleFS.open(FILE_ENERGYACCOUNTING, "w"); char buf[sizeof(data)+sizeof(this->maxHours)]; memcpy(buf, &data, sizeof(data)); - for(uint8_t i = 0; i < config->hours; i++) { + for(uint8_t i = 0; i < sizeof(this->maxHours)/2; i++) { memcpy(buf+sizeof(data)+(i*2), &this->maxHours[i], 2); } for(uint8_t i = 0; i < sizeof(buf); i++) { @@ -322,7 +325,14 @@ uint16_t * EnergyAccounting::getMaxHours() { } void EnergyAccounting::setMaxHours(uint16_t * maxHours) { - for(uint8_t i = 0; i < config->hours; i++) { + if(this->maxHours == NULL) { + if(config == NULL) { + this->maxHours = new uint16_t[sizeof(maxHours)/2]; + } else { + this->maxHours = new uint16_t[config->hours]; + } + } + for(uint8_t i = 0; i < sizeof(this->maxHours)/2; i++) { this->maxHours[i] = maxHours[i]; } } diff --git a/src/EnergyAccounting.h b/src/EnergyAccounting.h index 56edc362..7fbe448c 100644 --- a/src/EnergyAccounting.h +++ b/src/EnergyAccounting.h @@ -23,6 +23,7 @@ public: void setTimezone(Timezone*); EnergyAccountingConfig* getConfig(); bool update(AmsData* amsData); + bool load(); bool save(); double getUseThisHour(); @@ -53,9 +54,8 @@ private: uint8_t currentHour = 0, currentDay = 0, currentThresholdIdx = 0; double use, costHour, costDay; EnergyAccountingData data = { 0, 0, 0, 0, 0, 0 }; - uint16_t *maxHours; + uint16_t *maxHours = NULL; - bool load(); void calcDayCost(); }; diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 7fc340e7..e9476ee7 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -2442,7 +2442,7 @@ void AmsWebServer::configFileDownload() { uint16_t *maxHours = ea->getMaxHours(); server.sendContent(buf, snprintf(buf, BufferSize, " %d", eac.hours)); for(int i = 0; i < eac.hours; i++) { - server.sendContent(buf, snprintf(buf, BufferSize, " %d", maxHours[i])); + server.sendContent(buf, snprintf(buf, BufferSize, " %.2f", maxHours[i]/100.0)); } } server.sendContent("\n");