Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/EnergyAccounting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
#include "LittleFS.h"
#include "AmsStorage.h"
#include "FirmwareVersion.h"
#include "crc.h"
#include <cmath>
#include <cstddef>

// Clamp NaN/Inf to 0 so corrupt floats never leak into JSON/MQTT output
static float sanitize(float v) {
if(std::isnan(v) || std::isinf(v)) return 0.0;
return v;
}

#if defined(AMS_REMOTE_DEBUG)
EnergyAccounting::EnergyAccounting(RemoteDebug* debugger, EnergyAccountingRealtimeData* rtd) {
Expand All @@ -16,7 +25,13 @@ EnergyAccounting::EnergyAccounting(Stream* Stream, EnergyAccountingRealtimeData*
#endif
data.version = 1;
this->debugger = debugger;
if(rtd->magic != 0x6A) {
realtimeData = rtd;
// The realtime struct lives in non-initialized RAM on ESP32 so it survives a
// reboot. A single magic byte is not enough to tell valid data from garbage left
// by a previous firmware (a layout change can shift fields while the byte still
// matches), which produced NaN/Inf values in the API. Validate with a CRC too.
uint16_t crc = crc16((uint8_t*) rtd, offsetof(EnergyAccountingRealtimeData, crc));
if(rtd->magic != 0x6A || rtd->crc != crc) {
rtd->magic = 0x6A;
rtd->currentHour = 0;
rtd->currentDay = 0;
Expand All @@ -29,8 +44,12 @@ EnergyAccounting::EnergyAccounting(Stream* Stream, EnergyAccountingRealtimeData*
rtd->incomeDay = 0;
rtd->lastImportUpdateMillis = 0;
rtd->lastExportUpdateMillis = 0;
updateRealtimeCrc();
}
realtimeData = rtd;
}

void EnergyAccounting::updateRealtimeCrc() {
realtimeData->crc = crc16((uint8_t*) realtimeData, offsetof(EnergyAccountingRealtimeData, crc));
}

void EnergyAccounting::setup(AmsDataStorage *ds, EnergyAccountingConfig *config) {
Expand Down Expand Up @@ -189,6 +208,7 @@ bool EnergyAccounting::update(time_t now, uint64_t lastUpdatedMillis, uint8_t li
while(getMonthMax() > config->thresholds[realtimeData->currentThresholdIdx] && realtimeData->currentThresholdIdx < 10) realtimeData->currentThresholdIdx++;
}

updateRealtimeCrc();
return ret;
}

Expand Down Expand Up @@ -231,7 +251,7 @@ void EnergyAccounting::calcDayCost() {
}

float EnergyAccounting::getUseThisHour() {
return realtimeData->use;
return sanitize(realtimeData->use);
}

float EnergyAccounting::getUseToday() {
Expand Down Expand Up @@ -265,7 +285,7 @@ float EnergyAccounting::getUseLastMonth() {
}

float EnergyAccounting::getProducedThisHour() {
return realtimeData->produce;
return sanitize(realtimeData->produce);
}

float EnergyAccounting::getProducedToday() {
Expand Down Expand Up @@ -299,11 +319,11 @@ float EnergyAccounting::getProducedLastMonth() {
}

float EnergyAccounting::getCostThisHour() {
return realtimeData->costHour;
return sanitize(realtimeData->costHour);
}

float EnergyAccounting::getCostToday() {
return realtimeData->costDay;
return sanitize(realtimeData->costDay);
}

float EnergyAccounting::getCostYesterday() {
Expand All @@ -319,11 +339,11 @@ float EnergyAccounting::getCostLastMonth() {
}

float EnergyAccounting::getIncomeThisHour() {
return realtimeData->incomeHour;
return sanitize(realtimeData->incomeHour);
}

float EnergyAccounting::getIncomeToday() {
return realtimeData->incomeDay;
return sanitize(realtimeData->incomeDay);
}

float EnergyAccounting::getIncomeYesterday() {
Expand Down
2 changes: 2 additions & 0 deletions src/EnergyAccounting.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct EnergyAccountingRealtimeData {
float incomeDay;
unsigned long lastImportUpdateMillis;
unsigned long lastExportUpdateMillis;
uint16_t crc;
};


Expand Down Expand Up @@ -134,6 +135,7 @@ class EnergyAccounting {

void calcDayCost();
bool updateMax(uint16_t val, uint8_t day, uint8_t hour);
void updateRealtimeCrc();
};

#endif
Loading