Skip to content

Commit

Permalink
systemstats: Consider discharge cap in remaining time calculation
Browse files Browse the repository at this point in the history
The battery is typically configured not to discharge below 10%,
which skewes the remaining time calculation since there's an
additional half-a-kilowatt or so remaining that will not acutally
be used.

Rather than introduce a fully-fledged wrapper around the
getDisChargeConfigInfo (which also includes configurable time
settings), just call it directly for this one particular use case.
  • Loading branch information
kbroulik committed Feb 22, 2024
1 parent 642e196 commit 6e48288
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/systemstats/livedataobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <cmath>

#include <QAlphaCloud/ApiRequest>
#include <QAlphaCloud/LastPowerData>
#include <QAlphaCloud/QAlphaCloud>
#include <QAlphaCloud/StorageSystemsModel>
Expand Down Expand Up @@ -190,9 +191,12 @@ void LiveDataObject::update()
const int neededCapacity = std::max(0, m_batteryRemainingCapacityWh - batteryEnergy);
m_batteryTimeProperty->setValue(neededCapacity / qreal(batteryCharge) * 60 * 60);
}
// TODO consider batHighCap.
} else if (batteryDischarge > 0) {
m_batteryTimeProperty->setName(tr("Time until empty"));
m_batteryTimeProperty->setValue(batteryEnergy / qreal(batteryDischarge) * 60 * 60);
const int dischargeCapEnergy = m_batteryRemainingCapacityWh * m_batteryDischargeSoc / 100.0;
const int remainingEnergy = std::max(0, batteryEnergy - dischargeCapEnergy);
m_batteryTimeProperty->setValue(remainingEnergy / qreal(batteryDischarge) * 60 * 60);
}
}

Expand All @@ -201,6 +205,22 @@ void LiveDataObject::updateSystem(const QModelIndex &index)
m_batteryRemainingCapacityWh = index.data(static_cast<int>(StorageSystemsModel::Roles::BatteryRemainingCapacity)).toInt();

m_batteryEnergyProperty->setMax(m_batteryRemainingCapacityWh);

// Query discharge cap so remaining time calculation is more accurate since
// it will usually not go all the way to zero percent.
auto *dischargeRequest = new ApiRequest(m_liveData->connector(), QLatin1String("getDisChargeConfigInfo"));
dischargeRequest->setSysSn(m_liveData->serialNumber());
connect(dischargeRequest, &ApiRequest::finished, this, [this, dischargeRequest] {
if (dischargeRequest->error() != QAlphaCloud::ErrorCode::NoError) {
qWarning() << "Failed to read discharge info config" << dischargeRequest->error() << dischargeRequest->errorString();
// TODO try again at some point.
} else {
m_batteryDischargeSoc = dischargeRequest->data().toObject().value(QLatin1String("batUseCap")).toDouble();
}
});
dischargeRequest->send();

// TODO also check getChargeConfigInfo for batHighCap.
}

void LiveDataObject::reload()
Expand Down
2 changes: 2 additions & 0 deletions src/systemstats/livedataobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class LiveDataObject : public KSysGuard::SensorObject

// Mirrored from StorageSystemsModel
int m_batteryRemainingCapacityWh = 0;
// When discharging the battery stops (so remaining time is more accurate).
qreal m_batteryDischargeSoc = 0.0;

QTimer m_rateLimitTimer;
bool m_updatePending = false;
Expand Down

0 comments on commit 6e48288

Please sign in to comment.