Skip to content

Commit

Permalink
fix and harden BatteryStats update timestamp handling
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
schlimmchen committed Mar 23, 2024
1 parent d935283 commit 1c51c2d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/BatteryStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions src/BatteryStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 1c51c2d

Please sign in to comment.