From f43fe5f86443bad7a26835060b2d4367610896eb Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Thu, 28 Jul 2022 13:40:23 +0200 Subject: [PATCH] Fixed cost overflow --- src/AmsToMqttBridge.ino | 8 ++++---- src/EnergyAccounting.cpp | 42 +++++++++++++++++++++++++--------------- src/EnergyAccounting.h | 2 +- src/web/AmsWebServer.cpp | 6 +++--- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index ac84093f..b8799ce2 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -1677,7 +1677,7 @@ void configFileParse() { sDs = true; } else if(strncmp(buf, "energyaccounting ", 17) == 0) { uint8_t i = 0; - EnergyAccountingData ead = { 3, 0, + EnergyAccountingData ead = { 4, 0, 0, 0, 0, 0, 0, // Peak 1 0, 0, // Peak 2 @@ -1699,13 +1699,13 @@ void configFileParse() { } } else if(i == 3) { double val = String(pch).toDouble(); - ead.costYesterday = val * 100; + ead.costYesterday = val * 10; } else if(i == 4) { double val = String(pch).toDouble(); - ead.costThisMonth = val * 100; + ead.costThisMonth = val; } else if(i == 5) { double val = String(pch).toDouble(); - ead.costLastMonth = val * 100; + ead.costLastMonth = val; } else if(i >= 6 && i < 18) { uint8_t hour = i-6; if(hour%2 == 0) { diff --git a/src/EnergyAccounting.cpp b/src/EnergyAccounting.cpp index 1262a07d..75cdd07d 100644 --- a/src/EnergyAccounting.cpp +++ b/src/EnergyAccounting.cpp @@ -45,7 +45,7 @@ bool EnergyAccounting::update(AmsData* amsData) { if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Initializing data at %lld\n", (int64_t) now); if(!load()) { if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EnergyAccounting) Unable to load existing data\n"); - data = { 3, local.Month, + data = { 4, local.Month, 0, 0, 0, 0, 0, // Peak 1 0, 0, // Peak 2 @@ -57,7 +57,7 @@ bool EnergyAccounting::update(AmsData* amsData) { for(uint8_t i = 0; i < 5; i++) { debugger->printf("(EnergyAccounting) Peak hour from day %d: %d\n", data.peaks[i].day, data.peaks[i].value*10); } - debugger->printf("(EnergyAccounting) Loaded cost yesterday: %d, this month: %d, last month: %d\n", data.costYesterday, data.costThisMonth, data.costLastMonth); + debugger->printf("(EnergyAccounting) Loaded cost yesterday: %d, this month: %d, last month: %d\n", data.costYesterday / 10.0, data.costThisMonth, data.costLastMonth); } init = true; } @@ -85,8 +85,8 @@ bool EnergyAccounting::update(AmsData* amsData) { if(local.Day != currentDay) { if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EnergyAccounting) New day %d\n", local.Day); - data.costYesterday = costDay * 100; - data.costThisMonth += costDay * 100; + data.costYesterday = costDay * 10; + data.costThisMonth += costDay; costDay = 0; currentDay = local.Day; ret = true; @@ -173,7 +173,7 @@ double EnergyAccounting::getCostToday() { } double EnergyAccounting::getCostYesterday() { - return data.costYesterday / 100.0; + return data.costYesterday / 10.0; } double EnergyAccounting::getUseThisMonth() { @@ -192,11 +192,11 @@ double EnergyAccounting::getUseThisMonth() { } double EnergyAccounting::getCostThisMonth() { - return (data.costThisMonth / 100.0) + getCostToday(); + return data.costThisMonth + getCostToday(); } -double EnergyAccounting::getCostLastMonth() { - return data.costLastMonth / 100.0; +uint16_t EnergyAccounting::getCostLastMonth() { + return data.costLastMonth; } uint8_t EnergyAccounting::getCurrentThreshold() { @@ -248,12 +248,22 @@ bool EnergyAccounting::load() { file.readBytes(buf, file.size()); if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Data version %d\n", buf[0]); - if(buf[0] == 3) { + if(buf[0] == 4) { EnergyAccountingData* data = (EnergyAccountingData*) buf; memcpy(&this->data, data, sizeof(this->data)); + } else if(buf[0] == 3) { + EnergyAccountingData* data = (EnergyAccountingData*) buf; + this->data = { 4, data->month, + (uint16_t) (data->costYesterday / 10), (uint16_t) (data->costThisMonth / 100), (uint16_t) (data->costLastMonth / 100), + data->peaks[0].day, data->peaks[0].value, + data->peaks[1].day, data->peaks[1].value, + data->peaks[2].day, data->peaks[2].value, + data->peaks[3].day, data->peaks[3].value, + data->peaks[4].day, data->peaks[4].value + }; ret = true; } else { - data = { 3, 0, + data = { 4, 0, 0, 0, 0, 0, 0, // Peak 1 0, 0, // Peak 2 @@ -264,9 +274,9 @@ bool EnergyAccounting::load() { if(buf[0] == 2) { EnergyAccountingData1* data = (EnergyAccountingData1*) buf; this->data.month = data->month; - this->data.costYesterday = data->costYesterday; - this->data.costThisMonth = data->costThisMonth; - this->data.costLastMonth = data->costLastMonth; + this->data.costYesterday = (uint16_t) (data->costYesterday / 10); + this->data.costThisMonth = (uint16_t) (data->costThisMonth / 100); + this->data.costLastMonth = (uint16_t) (data->costLastMonth / 100); uint8_t b = 0; for(uint8_t i = sizeof(this->data); i < file.size(); i+=2) { this->data.peaks[b].day = b; @@ -278,9 +288,9 @@ bool EnergyAccounting::load() { } else if(buf[0] == 1) { EnergyAccountingData1* data = (EnergyAccountingData1*) buf; this->data.month = data->month; - this->data.costYesterday = data->costYesterday; - this->data.costThisMonth = data->costThisMonth; - this->data.costLastMonth = data->costLastMonth; + this->data.costYesterday = (uint16_t) (data->costYesterday / 10); + this->data.costThisMonth = (uint16_t) (data->costThisMonth / 100); + this->data.costLastMonth = (uint16_t) (data->costLastMonth / 100); this->data.peaks[0].day = 1; this->data.peaks[0].value = data->maxHour; ret = true; diff --git a/src/EnergyAccounting.h b/src/EnergyAccounting.h index a6a8266d..96c82a2d 100644 --- a/src/EnergyAccounting.h +++ b/src/EnergyAccounting.h @@ -48,7 +48,7 @@ public: double getCostYesterday(); double getUseThisMonth(); double getCostThisMonth(); - double getCostLastMonth(); + uint16_t getCostLastMonth(); float getMonthMax(); uint8_t getCurrentThreshold(); diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 870eebb2..a980109e 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -2429,9 +2429,9 @@ void AmsWebServer::configFileDownload() { ead.version, ead.month, 0.0, // Old max - ead.costYesterday / 100.0, - ead.costThisMonth / 100.0, - ead.costLastMonth / 100.0, + ead.costYesterday / 10.0, + ead.costThisMonth, + ead.costLastMonth, ead.peaks[0].day, ead.peaks[0].value / 100.0, ead.peaks[1].day,