Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: "State of Operation", "Float voltage" and "Absorption Voltage" #1576

Merged
merged 4 commits into from
Feb 4, 2025
Merged
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
3 changes: 3 additions & 0 deletions include/solarcharger/DummyStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class DummyStats : public Stats {
std::optional<uint16_t> getPanelPowerWatts() const final { return std::nullopt; }
std::optional<float> getYieldTotal() const final { return std::nullopt; }
std::optional<float> getYieldDay() const final { return std::nullopt; }
std::optional<StateOfOperation> getStateOfOperation() const final { return std::nullopt; }
std::optional<float> getFloatVoltage() const final { return std::nullopt; }
std::optional<float> getAbsorptionVoltage() const final { return std::nullopt; }
void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const final {}
void mqttPublish() const final {}
void mqttPublishSensors(const boolean forcePublish) const final {}
Expand Down
10 changes: 10 additions & 0 deletions include/solarcharger/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Stats {
// sum of today's yield of all MPPT charge controllers in Wh
virtual std::optional<float> getYieldDay() const;

// state of operation from the first available controller
enum class StateOfOperation : uint8_t { Off = 0, Bulk = 1, Absorption = 2, Float = 3, Various = 255 };
virtual std::optional<Stats::StateOfOperation> getStateOfOperation() const;

// float voltage from the first available charge controller
virtual std::optional<float> getFloatVoltage() const;

// absorption voltage from the first available charge controller
virtual std::optional<float> getAbsorptionVoltage() const;

// convert stats to JSON for web application live view
virtual void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const;

Expand Down
3 changes: 3 additions & 0 deletions include/solarcharger/mqtt/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ friend class Provider;
std::optional<uint16_t> getPanelPowerWatts() const final { return std::nullopt; }
std::optional<float> getYieldTotal() const final { return std::nullopt; }
std::optional<float> getYieldDay() const final { return std::nullopt; }
std::optional<StateOfOperation> getStateOfOperation() const final { return std::nullopt; }
std::optional<float> getFloatVoltage() const final { return std::nullopt; }
std::optional<float> getAbsorptionVoltage() const final { return std::nullopt; }

void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const final;

Expand Down
3 changes: 3 additions & 0 deletions include/solarcharger/victron/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Stats : public ::SolarChargers::Stats {
std::optional<uint16_t> getPanelPowerWatts() const final;
std::optional<float> getYieldTotal() const final;
std::optional<float> getYieldDay() const final;
std::optional<StateOfOperation> getStateOfOperation() const final;
std::optional<float> getFloatVoltage() const final;
std::optional<float> getAbsorptionVoltage() const final;

void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const final;
void mqttPublish() const final;
Expand Down
41 changes: 41 additions & 0 deletions src/solarcharger/victron/Stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,47 @@ std::optional<float> Stats::getYieldDay() const
return sum;
}

std::optional<Stats::StateOfOperation> Stats::getStateOfOperation() const
{
for (auto const& entry : _data) {
if (!entry.second) { continue; }
// see victron protocol documentation for CS values
switch (entry.second->currentState_CS) {
case 0: return Stats::StateOfOperation::Off;
case 3: return Stats::StateOfOperation::Bulk;
case 246:
case 4: return Stats::StateOfOperation::Absorption;
case 5: return Stats::StateOfOperation::Float;
default: return Stats::StateOfOperation::Various;
}
}
return std::nullopt;
}

std::optional<float> Stats::getFloatVoltage() const
{
for (auto const& entry : _data) {
if (!entry.second) { continue; }
auto voltage = entry.second->BatteryFloatMilliVolt;
if (voltage.first > 0) { // only return valid and not outdated value
return voltage.second / 1000.0;
}
}
return std::nullopt;
}

std::optional<float> Stats::getAbsorptionVoltage() const
{
for (auto const& entry : _data) {
if (!entry.second) { continue; }
auto voltage = entry.second->BatteryAbsorptionMilliVolt;
if (voltage.first > 0) { // only return valid and not outdated value
return voltage.second / 1000.0;
}
}
return std::nullopt;
}

void Stats::getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const
{
::SolarChargers::Stats::getLiveViewData(root, fullUpdate, lastPublish);
Expand Down