Skip to content

Commit

Permalink
avoid too frequent SmartShunt data copies (#596)
Browse files Browse the repository at this point in the history
currently the whole SmartShunt data structure is copied to the
BatteryStats instance in every loop, even though the data cannot
possibly have changed. this is quite an expensive task to do in every
loop. this change tracks the last update timestamp and only does the
copy operation if an actual updated data structure was received from
the smart shunt.
  • Loading branch information
schlimmchen authored Jan 7, 2024
1 parent 2806b6d commit a012d81
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/VictronSmartShunt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class VictronSmartShunt : public BatteryProvider {
std::shared_ptr<BatteryStats> getStats() const final { return _stats; }

private:
uint32_t _lastUpdate = 0;
std::shared_ptr<VictronSmartShuntStats> _stats =
std::make_shared<VictronSmartShuntStats>();
};
4 changes: 4 additions & 0 deletions src/VictronSmartShunt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ bool VictronSmartShunt::init(bool verboseLogging)
void VictronSmartShunt::loop()
{
VeDirectShunt.loop();

if (VeDirectShunt.getLastUpdate() <= _lastUpdate) { return; }

_stats->updateFrom(VeDirectShunt.veFrame);
_lastUpdate = VeDirectShunt.getLastUpdate();
}

0 comments on commit a012d81

Please sign in to comment.