Skip to content

Commit

Permalink
systemstats: Add "Remaining time" property
Browse files Browse the repository at this point in the history
Reports the time until the battery is fully charged (when charging)
or empty (when discharging).

It doesn't do any rounding, so the value is reported down to the second
even though it's of course nowhere as accurate.
  • Loading branch information
kbroulik committed Oct 23, 2023
1 parent b41ecb9 commit 5f5dd29
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/systemstats/livedataobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ LiveDataObject::LiveDataObject(QAlphaCloud::Connector *connector, const QString
// m_batteryDischargeProperty->setMax(photovoltaicDesignPower * 1000);
connect(m_batteryDischargeProperty, &KSysGuard::SensorProperty::subscribedChanged, this, &LiveDataObject::update);

m_batteryTimeProperty = new KSysGuard::SensorProperty(QStringLiteral("batteryTime"), tr("Remaining Time"), 0, this);
m_batteryTimeProperty->setShortName(tr("Remaining"));
m_batteryTimeProperty->setUnit(KSysGuard::Unit::UnitTime);
m_batteryTimeProperty->setVariantType(QVariant::Int);
connect(m_batteryTimeProperty, &KSysGuard::SensorProperty::subscribedChanged, this, &LiveDataObject::update);

#if PRESENTATION_BUILD
//: Sensor object name with live data
setName(tr("Live"));
Expand Down Expand Up @@ -161,7 +167,8 @@ void LiveDataObject::update()
m_gridConsumptionProperty->setValue(gridConsumption);

m_batterySocProperty->setValue(m_liveData->batterySoc());
m_batteryEnergyProperty->setValue(std::round(m_batteryRemainingCapacityWh * m_liveData->batterySoc() / 100.0));
const int batteryEnergy = std::round(m_batteryRemainingCapacityWh * m_liveData->batterySoc() / 100.0);
m_batteryEnergyProperty->setValue(batteryEnergy);

const int batteryPower = m_liveData->batteryPower();
int batteryCharge = 0;
Expand All @@ -175,6 +182,18 @@ void LiveDataObject::update()

m_batteryChargeProperty->setValue(batteryCharge);
m_batteryDischargeProperty->setValue(batteryDischarge);

if (batteryCharge > 0) {
m_batteryTimeProperty->setName(tr("Time until full"));

if (m_batteryRemainingCapacityWh > 0) {
const int neededCapacity = std::max(0, m_batteryRemainingCapacityWh - batteryEnergy);
m_batteryTimeProperty->setValue(neededCapacity / qreal(batteryCharge) * 60 * 60);
}
} else if (batteryDischarge > 0) {
m_batteryTimeProperty->setName(tr("Time until empty"));
m_batteryTimeProperty->setValue(batteryEnergy / qreal(batteryDischarge) * 60 * 60);
}
}

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

KSysGuard::SensorProperty *m_batteryChargeProperty = nullptr;
KSysGuard::SensorProperty *m_batteryDischargeProperty = nullptr;

KSysGuard::SensorProperty *m_batteryTimeProperty = nullptr;
// TODO battery status property

// Mirrored from StorageSystemsModel
Expand Down

0 comments on commit 5f5dd29

Please sign in to comment.