Handle reading and setting max charge value at daemon startup

This commit is contained in:
Matti Viljanen 2023-01-01 00:46:21 +02:00
parent f5580e18f1
commit c1313dc014
No known key found for this signature in database
GPG key ID: CF32A1495158F888
2 changed files with 13 additions and 2 deletions

View file

@ -78,9 +78,18 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
if(maxChargeCurrentFile->open(QIODevice::WriteOnly)) { if(maxChargeCurrentFile->open(QIODevice::WriteOnly)) {
maxChargeCurrentFile->close(); maxChargeCurrentFile->close();
if(maxChargeCurrentFile->open(QIODevice::ReadOnly)) { if(maxChargeCurrentFile->open(QIODevice::ReadOnly)) {
// Read and store the default max current
maxSupportedChargeCurrent = maxChargeCurrentFile->readLine().trimmed().toInt(); maxSupportedChargeCurrent = maxChargeCurrentFile->readLine().trimmed().toInt();
logL(QString("Maximum supported charge current: %1mA").arg(maxSupportedChargeCurrent / 1000)); logL(QString("Maximum supported charge current: %1mA").arg(maxSupportedChargeCurrent / 1000));
maxChargeCurrentFile->close(); maxChargeCurrentFile->close();
settings->setMaxSupportedChargeCurrent(maxSupportedChargeCurrent);
// Read and maybe set the user-set max current
maxChargeCurrent = settings->getMaxChargeCurrent();
if(maxChargeCurrent != maxSupportedChargeCurrent) {
setMaxChargeCurrent(maxChargeCurrent);
}
} }
} }
else { else {
@ -92,7 +101,6 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
else { else {
logL("Max charge current file: " + notFound); logL("Max charge current file: " + notFound);
} }
settings->setMaxSupportedChargeCurrent(maxSupportedChargeCurrent);
// Battery/charging status: charging, discharging, full, empty, unknown (others?) // Battery/charging status: charging, discharging, full, empty, unknown (others?)
filenames.clear(); filenames.clear();
@ -219,7 +227,7 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
} }
Battery::~Battery() { Battery::~Battery() {
this->setMaxChargeCurrent(maxSupportedChargeCurrent); setMaxChargeCurrent(maxSupportedChargeCurrent);
updateTimer->stop(); updateTimer->stop();
highNotifyTimer->stop(); highNotifyTimer->stop();

View file

@ -204,4 +204,7 @@ int Settings::getLogLevel() { return logLevel; }
void Settings::setMaxSupportedChargeCurrent(int newCurrent) { void Settings::setMaxSupportedChargeCurrent(int newCurrent) {
mySettings->setValue(sMaxSupportedChargeCurrent, QByteArray::number(newCurrent)); mySettings->setValue(sMaxSupportedChargeCurrent, QByteArray::number(newCurrent));
if(mySettings->value(sMaxChargeCurrent, QVariant::fromValue(0)).toInt() == 0) {
mySettings->setValue(sMaxChargeCurrent, QByteArray::number(newCurrent));
}
} }