diff --git a/application/src/battery.cpp b/application/src/battery.cpp index 2b6f757..06baa98 100644 --- a/application/src/battery.cpp +++ b/application/src/battery.cpp @@ -24,69 +24,50 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO // Number: charge percentage, e.g. 42 chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this); - logV("Capacity file: " + chargeFile->fileName()); + logE("Capacity file: " + chargeFile->fileName()); // Number: battery/charging current, e.g. -1450000 (-145mA) currentFile = new QFile("/sys/class/power_supply/battery/current_now", this); - logV("Charge state file: " + currentFile->fileName()); + logE("Charge state file: " + currentFile->fileName()); // String: charging, discharging, full, empty, unknown (others?) stateFile = new QFile("/sys/class/power_supply/battery/status", this); - logV("Charger status file: " + stateFile->fileName()); + logE("Charger status file: " + stateFile->fileName()); // Number: 0 or 1 chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this); - logV("Reading charger status from" + chargerConnectedFile->fileName()); + logE("Reading charger status from" + chargerConnectedFile->fileName()); QString filename; - // e.g. for Sony Xperia XA2 - filename = "/sys/class/power_supply/battery/input_suspend"; - if(!chargingEnabledFile && QFile::exists(filename)) { - chargingEnabledFile = new QFile(filename, this); - enableChargingValue = 0; - disableChargingValue = 1; - } - - // e.g. for Sony Xperia Z3 Compact Tablet - filename = "/sys/class/power_supply/battery/charging_enabled"; - if(!chargingEnabledFile && QFile::exists(filename)) { - chargingEnabledFile = new QFile(filename, this); - enableChargingValue = 1; - disableChargingValue = 0; - } - - // e.g. for Jolla Phone - filename = "/sys/class/power_supply/usb/charger_disable"; - if(!chargingEnabledFile && QFile::exists(filename)) { - chargingEnabledFile = new QFile(filename, this); - enableChargingValue = 0; - disableChargingValue = 1; - } - - if(!chargingEnabledFile && !QHostInfo::localHostName().contains("SailfishEmul")) { - logE("Charger control file not found!"); - logE("Please contact the developer with your device model!"); - } - - // If we found a usable file, check that it is writable - if(chargingEnabledFile) { - if(chargingEnabledFile->open(QIODevice::WriteOnly)) { - logV("Controlling charging via" + chargingEnabledFile->fileName()); - chargingEnabledFile->close(); - } - else { - logE("Charger control file" + chargingEnabledFile->fileName() + "is not writable"); - logE("Charger control feature disabled"); - delete chargingEnabledFile; - chargingEnabledFile = Q_NULLPTR; - } + // e.g. for Sony Xperia XA2 + filename = "/sys/class/power_supply/battery/input_suspend"; + if(!chargingEnabledFile && QFile::exists(filename)) { + chargingEnabledFile = new QFile(filename, this); + enableChargingValue = 0; + disableChargingValue = 1; } - // TODO - // Use DBus instead of polling the files - where is the documentation? - // QFileWatcher doesn't work, because e.g. the charge file - // isn't a real file, and it doesn't "change" when the value changes. + // e.g. for Sony Xperia Z3 Compact Tablet + filename = "/sys/class/power_supply/battery/charging_enabled"; + if(!chargingEnabledFile && QFile::exists(filename)) { + chargingEnabledFile = new QFile(filename, this); + enableChargingValue = 1; + disableChargingValue = 0; + } + + // e.g. for Jolla Phone + filename = "/sys/class/power_supply/usb/charger_disable"; + if(!chargingEnabledFile && QFile::exists(filename)) { + chargingEnabledFile = new QFile(filename, this); + enableChargingValue = 0; + disableChargingValue = 1; + } + + if(!chargingEnabledFile && !QHostInfo::localHostName().contains("SailfishEmul")) { + logE("Charger control file not found!"); + logE("Please contact the developer with your device model!"); + } updateData(); } @@ -100,25 +81,16 @@ void Battery::updateData() if(nextCharge != charge) { charge = nextCharge; emit chargeChanged(charge); - logV(QString("Battery: %1").arg(charge)); + logV(QString("Battery: %1%").arg(charge)); } chargeFile->close(); } - if(currentFile && currentFile->open(QIODevice::ReadOnly)) { - nextCurrent = currentFile->readLine().trimmed().toInt(); - if(nextCurrent != current) { - current = nextCurrent; - emit currentChanged(current); - logV(QString("Current: %1").arg(current)); - } - currentFile->close(); - } if(chargerConnectedFile && chargerConnectedFile->open(QIODevice::ReadOnly)) { nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt(); if(nextChargerConnected != chargerConnected) { chargerConnected = nextChargerConnected; emit chargerConnectedChanged(chargerConnected); - logV(QString("Charger is connected: %1").arg(chargerConnected)); + logV(QString("Charger: %1").arg(chargerConnected ? "connected" : "disconnected")); } chargerConnectedFile->close(); } @@ -127,10 +99,19 @@ void Battery::updateData() if(nextState != state) { state = nextState; emit stateChanged(state); - logV(QString("Charging status: %1").arg(state)); + logV("State: " + state); } stateFile->close(); } + if(currentFile && currentFile->open(QIODevice::ReadOnly)) { + nextCurrent = currentFile->readLine().trimmed().toInt(); + if(nextCurrent != current) { + current = nextCurrent; + emit currentChanged(current); + logD(QString("Current: %1mA").arg(current / 1000)); + } + currentFile->close(); + } } int Battery::getCharge(){ return charge; } @@ -141,6 +122,4 @@ QString Battery::getState() { return state; } bool Battery::getChargingEnabled() { return chargingEnabled; } -bool Battery::getChargerConnected() { - return chargerConnected; -} +bool Battery::getChargerConnected() { return chargerConnected; } diff --git a/service/src/battery.cpp b/service/src/battery.cpp index 188f41f..c705000 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -50,34 +50,29 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject // ENABLE/DISABLE CHARGING QString filename; - // e.g. for Sony Xperia XA2 - filename = "/sys/class/power_supply/battery/input_suspend"; - if(!chargingEnabledFile && QFile::exists(filename)) { - chargingEnabledFile = new QFile(filename, this); - enableChargingValue = 0; - disableChargingValue = 1; - } + // e.g. for Sony Xperia XA2 + filename = "/sys/class/power_supply/battery/input_suspend"; + if(!chargingEnabledFile && QFile::exists(filename)) { + chargingEnabledFile = new QFile(filename, this); + enableChargingValue = 0; + disableChargingValue = 1; + } - // e.g. for Sony Xperia Z3 Compact Tablet - filename = "/sys/class/power_supply/battery/charging_enabled"; - if(!chargingEnabledFile && QFile::exists(filename)) { - chargingEnabledFile = new QFile(filename, this); - enableChargingValue = 1; - disableChargingValue = 0; - } + // e.g. for Sony Xperia Z3 Compact Tablet + filename = "/sys/class/power_supply/battery/charging_enabled"; + if(!chargingEnabledFile && QFile::exists(filename)) { + chargingEnabledFile = new QFile(filename, this); + enableChargingValue = 1; + disableChargingValue = 0; + } - // e.g. for Jolla Phone - filename = "/sys/class/power_supply/usb/charger_disable"; - if(!chargingEnabledFile && QFile::exists(filename)) { - chargingEnabledFile = new QFile(filename, this); - enableChargingValue = 0; - disableChargingValue = 1; - } - - if(!chargingEnabledFile && QHostInfo::localHostName().contains("SailfishEmul")) { - logE("Charger control file not found!"); - logE("Please contact the developer with your device model!"); - } + // e.g. for Jolla Phone + filename = "/sys/class/power_supply/usb/charger_disable"; + if(!chargingEnabledFile && QFile::exists(filename)) { + chargingEnabledFile = new QFile(filename, this); + enableChargingValue = 0; + disableChargingValue = 1; + } // If we found a usable file, check that it is writable if(chargingEnabledFile) { @@ -86,9 +81,17 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject chargingEnabledFile->close(); } else { - logE("Charger control file is not writable!"); + logE("Charger control file" + chargingEnabledFile->fileName() + "is not writable"); + logE("Charger control feature disabled"); + delete chargingEnabledFile; + chargingEnabledFile = Q_NULLPTR; } } + else if(!QHostInfo::localHostName().contains("SailfishEmul")) { + logE("Charger control file not found!"); + logE("Please contact the developer with your device model!"); + } + connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData())); connect(settings, SIGNAL(resetTimers()), this, SLOT(resetTimers())); @@ -125,7 +128,7 @@ void Battery::updateData() if(nextChargerConnected != chargerConnected) { chargerConnected = nextChargerConnected; emit chargerConnectedChanged(chargerConnected); - logV(QString("Charger was %1").arg(chargerConnected ? "connected" : "disconnected")); + logV(QString("Charger: %1").arg(chargerConnected ? "connected" : "disconnected")); } chargerConnectedFile->close(); } @@ -134,7 +137,7 @@ void Battery::updateData() if(nextState != state) { state = nextState; emit stateChanged(state); - logV("Charging state: " + state); + logV("State: " + state); // Hide/show notification right away resetTimers(); @@ -196,7 +199,6 @@ void Battery::showHighNotification() { } void Battery::showLowNotification() { - logD(QString("Low notification if %1% < %2%)").arg(charge).arg(settings->getLowAlert())); if(settings->getLowNotificationsInterval() < 610 && charge <= settings->getLowAlert() && state != "charging") { logV(QString("Notification: %1").arg(settings->getNotificationTitle().arg(charge))); notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile()); @@ -266,9 +268,9 @@ void Battery::shutdown() { logD("Low battery notification stopped"); } // ENABLE/DISABLE CHARGING - if(!QHostInfo::localHostName().contains("SailfishEmul") && !setChargingEnabled(true)) { + if(!setChargingEnabled(true) && !QHostInfo::localHostName().contains("SailfishEmul")) { logE("ERROR! Could not restore charger status! Your device " - "may not charge until reboot! If that doesn't help, " - "uninstall Battery Buddy and reboot your device."); + "may not charge until reboot! If that doesn't help, " + "uninstall Battery Buddy and reboot your device."); } } diff --git a/service/src/settings.cpp b/service/src/settings.cpp index 6bb0450..daa4ca1 100644 --- a/service/src/settings.cpp +++ b/service/src/settings.cpp @@ -158,7 +158,7 @@ void Settings::updateConfig(const QString path) { } else { watcher->addPath(path); - logD("Config file added to watchlist."); + logD("Config file added to watchlist"); } if(restartTimers) {