From 1c51c2de40a4a6c61680ef83f8b6f27ed2c4f1cd Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Sat, 23 Mar 2024 17:35:28 +0100 Subject: [PATCH] fix and harden BatteryStats update timestamp handling * updating the SoC or value shall also update the general timestamp, as the latter is defined as "any value changed", which includes SoC and voltage, of course. * if the last update is not a valid timestamp at all, the updateAvailable method must always return false, obviously. --- include/BatteryStats.h | 4 ++-- src/BatteryStats.cpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/BatteryStats.h b/include/BatteryStats.h index 86bfa94d7..86e1750b2 100644 --- a/include/BatteryStats.h +++ b/include/BatteryStats.h @@ -45,12 +45,12 @@ class BatteryStats { void setSoC(float soc, uint8_t precision, uint32_t timestamp) { _soc = soc; _socPrecision = precision; - _lastUpdateSoC = timestamp; + _lastUpdateSoC = _lastUpdate = timestamp; } void setVoltage(float voltage, uint32_t timestamp) { _voltage = voltage; - _lastUpdateVoltage = timestamp; + _lastUpdateVoltage = _lastUpdate = timestamp; } String _manufacturer = "unknown"; diff --git a/src/BatteryStats.cpp b/src/BatteryStats.cpp index b924f64ae..563562c82 100644 --- a/src/BatteryStats.cpp +++ b/src/BatteryStats.cpp @@ -53,6 +53,8 @@ static void addLiveViewAlarm(JsonVariant& root, std::string const& name, bool BatteryStats::updateAvailable(uint32_t since) const { + if (_lastUpdate == 0) { return false; } // no data at all processed yet + auto constexpr halfOfAllMillis = std::numeric_limits::max() / 2; return (_lastUpdate - since) < halfOfAllMillis; }