Add to service and log charging/discharging current

This commit is contained in:
Matti Viljanen 2022-08-07 22:48:07 +03:00
parent c9788b3500
commit 4c243e913d
No known key found for this signature in database
GPG key ID: CF32A1495158F888
2 changed files with 28 additions and 0 deletions

View file

@ -48,6 +48,21 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
if(chargeFile) logE("Battery charge file: " + chargeFile->fileName()); if(chargeFile) logE("Battery charge file: " + chargeFile->fileName());
else logE("Battery charge file: not found!"); else logE("Battery charge file: not found!");
// Charging/discharging current in microamps, e.g. -1450000 (-145mA)
filenames.clear();
filenames << "/sys/class/power_supply/battery/current_now"
<< "/sys/class/power_supply/dollar_cove_battery/current_now";
foreach(const QString& file, filenames) {
if(!currentFile && QFile::exists(file)) {
currentFile = new QFile(file, this);
break;
}
}
if(currentFile) logE("Charging/discharging current file: " + currentFile->fileName());
else logE("Charging/discharging current file: not found!");
// Battery/charging status: charging, discharging, full, empty, unknown (others?) // Battery/charging status: charging, discharging, full, empty, unknown (others?)
filenames.clear(); filenames.clear();
filenames << "/sys/class/power_supply/battery/status" filenames << "/sys/class/power_supply/battery/status"
@ -206,6 +221,16 @@ void Battery::updateData()
} }
chargerConnectedFile->close(); chargerConnectedFile->close();
} }
if(currentFile->open(QIODevice::ReadOnly)) {
nextCurrent = currentFile->readLine().trimmed().toInt();
if(nextCurrent != current) {
current = nextCurrent;
logV(QString("Current: %1mA").arg(current / 1000));
}
currentFile->close();
}
if(stateFile->open(QIODevice::ReadOnly)) { if(stateFile->open(QIODevice::ReadOnly)) {
nextState = (QString(stateFile->readLine().trimmed().toLower())); nextState = (QString(stateFile->readLine().trimmed().toLower()));
if(nextState != state) { if(nextState != state) {

View file

@ -78,6 +78,7 @@ private:
Logger *logger; Logger *logger;
QFile *chargeFile = nullptr; QFile *chargeFile = nullptr;
QFile *chargerConnectedFile = nullptr; QFile *chargerConnectedFile = nullptr;
QFile *currentFile = nullptr;
QFile *stateFile = nullptr; QFile *stateFile = nullptr;
QFile *chargingEnabledFile = nullptr; QFile *chargingEnabledFile = nullptr;
QFile *temperatureFile = nullptr; QFile *temperatureFile = nullptr;
@ -93,6 +94,7 @@ private:
// Default values: // Default values:
int charge = 100; // 100% full int charge = 100; // 100% full
int current = 0; // Charging/discharging current in microamps
bool chargerConnected = false; // Charger plugged in bool chargerConnected = false; // Charger plugged in
QString state = "idle"; // dis/charging, idle, unknown QString state = "idle"; // dis/charging, idle, unknown
bool chargingEnabled = true; // Only ever disabled manually bool chargingEnabled = true; // Only ever disabled manually
@ -105,6 +107,7 @@ private:
bool chargerIsEnabled = true; bool chargerIsEnabled = true;
int nextCharge = charge; int nextCharge = charge;
int nextCurrent = current;
bool nextChargerConnected = chargerConnected; bool nextChargerConnected = chargerConnected;
QString nextState = state; QString nextState = state;
bool nextChargingEnabled = chargingEnabled; bool nextChargingEnabled = chargingEnabled;