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.
This commit is contained in:
Bernhard Kirchen 2024-03-23 17:35:28 +01:00
parent d935283d1f
commit 1c51c2de40
2 changed files with 4 additions and 2 deletions

View File

@ -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";

View File

@ -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<uint32_t>::max() / 2;
return (_lastUpdate - since) < halfOfAllMillis;
}