Fine-tune logging, fix a few messages, fix indentation
This commit is contained in:
parent
c6e8145afc
commit
1a0b730a1c
3 changed files with 79 additions and 98 deletions
|
@ -24,69 +24,50 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
|
||||||
|
|
||||||
// Number: charge percentage, e.g. 42
|
// Number: charge percentage, e.g. 42
|
||||||
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
|
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)
|
// Number: battery/charging current, e.g. -1450000 (-145mA)
|
||||||
currentFile = new QFile("/sys/class/power_supply/battery/current_now", this);
|
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?)
|
// String: charging, discharging, full, empty, unknown (others?)
|
||||||
stateFile = new QFile("/sys/class/power_supply/battery/status", this);
|
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
|
// Number: 0 or 1
|
||||||
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this);
|
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;
|
QString filename;
|
||||||
|
|
||||||
// e.g. for Sony Xperia XA2
|
// e.g. for Sony Xperia XA2
|
||||||
filename = "/sys/class/power_supply/battery/input_suspend";
|
filename = "/sys/class/power_supply/battery/input_suspend";
|
||||||
if(!chargingEnabledFile && QFile::exists(filename)) {
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
||||||
chargingEnabledFile = new QFile(filename, this);
|
chargingEnabledFile = new QFile(filename, this);
|
||||||
enableChargingValue = 0;
|
enableChargingValue = 0;
|
||||||
disableChargingValue = 1;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// e.g. for Sony Xperia Z3 Compact Tablet
|
||||||
// Use DBus instead of polling the files - where is the documentation?
|
filename = "/sys/class/power_supply/battery/charging_enabled";
|
||||||
// QFileWatcher doesn't work, because e.g. the charge file
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
||||||
// isn't a real file, and it doesn't "change" when the value changes.
|
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();
|
updateData();
|
||||||
}
|
}
|
||||||
|
@ -100,25 +81,16 @@ void Battery::updateData()
|
||||||
if(nextCharge != charge) {
|
if(nextCharge != charge) {
|
||||||
charge = nextCharge;
|
charge = nextCharge;
|
||||||
emit chargeChanged(charge);
|
emit chargeChanged(charge);
|
||||||
logV(QString("Battery: %1").arg(charge));
|
logV(QString("Battery: %1%").arg(charge));
|
||||||
}
|
}
|
||||||
chargeFile->close();
|
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)) {
|
if(chargerConnectedFile && chargerConnectedFile->open(QIODevice::ReadOnly)) {
|
||||||
nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt();
|
nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt();
|
||||||
if(nextChargerConnected != chargerConnected) {
|
if(nextChargerConnected != chargerConnected) {
|
||||||
chargerConnected = nextChargerConnected;
|
chargerConnected = nextChargerConnected;
|
||||||
emit chargerConnectedChanged(chargerConnected);
|
emit chargerConnectedChanged(chargerConnected);
|
||||||
logV(QString("Charger is connected: %1").arg(chargerConnected));
|
logV(QString("Charger: %1").arg(chargerConnected ? "connected" : "disconnected"));
|
||||||
}
|
}
|
||||||
chargerConnectedFile->close();
|
chargerConnectedFile->close();
|
||||||
}
|
}
|
||||||
|
@ -127,10 +99,19 @@ void Battery::updateData()
|
||||||
if(nextState != state) {
|
if(nextState != state) {
|
||||||
state = nextState;
|
state = nextState;
|
||||||
emit stateChanged(state);
|
emit stateChanged(state);
|
||||||
logV(QString("Charging status: %1").arg(state));
|
logV("State: " + state);
|
||||||
}
|
}
|
||||||
stateFile->close();
|
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; }
|
int Battery::getCharge(){ return charge; }
|
||||||
|
@ -141,6 +122,4 @@ QString Battery::getState() { return state; }
|
||||||
|
|
||||||
bool Battery::getChargingEnabled() { return chargingEnabled; }
|
bool Battery::getChargingEnabled() { return chargingEnabled; }
|
||||||
|
|
||||||
bool Battery::getChargerConnected() {
|
bool Battery::getChargerConnected() { return chargerConnected; }
|
||||||
return chargerConnected;
|
|
||||||
}
|
|
||||||
|
|
|
@ -50,34 +50,29 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject
|
||||||
// ENABLE/DISABLE CHARGING
|
// ENABLE/DISABLE CHARGING
|
||||||
QString filename;
|
QString filename;
|
||||||
|
|
||||||
// e.g. for Sony Xperia XA2
|
// e.g. for Sony Xperia XA2
|
||||||
filename = "/sys/class/power_supply/battery/input_suspend";
|
filename = "/sys/class/power_supply/battery/input_suspend";
|
||||||
if(!chargingEnabledFile && QFile::exists(filename)) {
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
||||||
chargingEnabledFile = new QFile(filename, this);
|
chargingEnabledFile = new QFile(filename, this);
|
||||||
enableChargingValue = 0;
|
enableChargingValue = 0;
|
||||||
disableChargingValue = 1;
|
disableChargingValue = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// e.g. for Sony Xperia Z3 Compact Tablet
|
// e.g. for Sony Xperia Z3 Compact Tablet
|
||||||
filename = "/sys/class/power_supply/battery/charging_enabled";
|
filename = "/sys/class/power_supply/battery/charging_enabled";
|
||||||
if(!chargingEnabledFile && QFile::exists(filename)) {
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
||||||
chargingEnabledFile = new QFile(filename, this);
|
chargingEnabledFile = new QFile(filename, this);
|
||||||
enableChargingValue = 1;
|
enableChargingValue = 1;
|
||||||
disableChargingValue = 0;
|
disableChargingValue = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// e.g. for Jolla Phone
|
// e.g. for Jolla Phone
|
||||||
filename = "/sys/class/power_supply/usb/charger_disable";
|
filename = "/sys/class/power_supply/usb/charger_disable";
|
||||||
if(!chargingEnabledFile && QFile::exists(filename)) {
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
||||||
chargingEnabledFile = new QFile(filename, this);
|
chargingEnabledFile = new QFile(filename, this);
|
||||||
enableChargingValue = 0;
|
enableChargingValue = 0;
|
||||||
disableChargingValue = 1;
|
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 we found a usable file, check that it is writable
|
||||||
if(chargingEnabledFile) {
|
if(chargingEnabledFile) {
|
||||||
|
@ -86,9 +81,17 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject
|
||||||
chargingEnabledFile->close();
|
chargingEnabledFile->close();
|
||||||
}
|
}
|
||||||
else {
|
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(updateTimer, SIGNAL(timeout()), this, SLOT(updateData()));
|
||||||
connect(settings, SIGNAL(resetTimers()), this, SLOT(resetTimers()));
|
connect(settings, SIGNAL(resetTimers()), this, SLOT(resetTimers()));
|
||||||
|
@ -125,7 +128,7 @@ void Battery::updateData()
|
||||||
if(nextChargerConnected != chargerConnected) {
|
if(nextChargerConnected != chargerConnected) {
|
||||||
chargerConnected = nextChargerConnected;
|
chargerConnected = nextChargerConnected;
|
||||||
emit chargerConnectedChanged(chargerConnected);
|
emit chargerConnectedChanged(chargerConnected);
|
||||||
logV(QString("Charger was %1").arg(chargerConnected ? "connected" : "disconnected"));
|
logV(QString("Charger: %1").arg(chargerConnected ? "connected" : "disconnected"));
|
||||||
}
|
}
|
||||||
chargerConnectedFile->close();
|
chargerConnectedFile->close();
|
||||||
}
|
}
|
||||||
|
@ -134,7 +137,7 @@ void Battery::updateData()
|
||||||
if(nextState != state) {
|
if(nextState != state) {
|
||||||
state = nextState;
|
state = nextState;
|
||||||
emit stateChanged(state);
|
emit stateChanged(state);
|
||||||
logV("Charging state: " + state);
|
logV("State: " + state);
|
||||||
|
|
||||||
// Hide/show notification right away
|
// Hide/show notification right away
|
||||||
resetTimers();
|
resetTimers();
|
||||||
|
@ -196,7 +199,6 @@ void Battery::showHighNotification() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Battery::showLowNotification() {
|
void Battery::showLowNotification() {
|
||||||
logD(QString("Low notification if %1% < %2%)").arg(charge).arg(settings->getLowAlert()));
|
|
||||||
if(settings->getLowNotificationsInterval() < 610 && charge <= settings->getLowAlert() && state != "charging") {
|
if(settings->getLowNotificationsInterval() < 610 && charge <= settings->getLowAlert() && state != "charging") {
|
||||||
logV(QString("Notification: %1").arg(settings->getNotificationTitle().arg(charge)));
|
logV(QString("Notification: %1").arg(settings->getNotificationTitle().arg(charge)));
|
||||||
notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile());
|
notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile());
|
||||||
|
@ -266,9 +268,9 @@ void Battery::shutdown() {
|
||||||
logD("Low battery notification stopped");
|
logD("Low battery notification stopped");
|
||||||
}
|
}
|
||||||
// ENABLE/DISABLE CHARGING
|
// 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 "
|
logE("ERROR! Could not restore charger status! Your device "
|
||||||
"may not charge until reboot! If that doesn't help, "
|
"may not charge until reboot! If that doesn't help, "
|
||||||
"uninstall Battery Buddy and reboot your device.");
|
"uninstall Battery Buddy and reboot your device.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,7 +158,7 @@ void Settings::updateConfig(const QString path) {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
watcher->addPath(path);
|
watcher->addPath(path);
|
||||||
logD("Config file added to watchlist.");
|
logD("Config file added to watchlist");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(restartTimers) {
|
if(restartTimers) {
|
||||||
|
|
Loading…
Reference in a new issue