Improve logging, settings loading and file operations

This commit is contained in:
Matti Viljanen 2020-03-21 01:17:15 +02:00
parent 4cd2dba0d7
commit 85d42458ca
No known key found for this signature in database
GPG key ID: CF32A1495158F888
5 changed files with 85 additions and 48 deletions

View file

@ -21,40 +21,66 @@ Battery::Battery(Settings* newSettings, QObject* parent) : QObject(parent)
{
settings = newSettings;
// Number: meaning percentage, e.g. 42
// Number: charge percentage, e.g. 42
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
qInfo() << "Reading capacity from" << chargeFile->fileName();
// String: charging, discharging, full, empty, unknown (others?)
stateFile = new QFile("/sys/class/power_supply/battery/status", this);
qInfo() << "Reading charge state from" << stateFile->fileName();
// Number: 0 or 1
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present");
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this);
qInfo() << "Reading charger status from" << chargerConnectedFile->fileName();
// ENABLE/DISABLE CHARGING
chargingEnabledFile = new QFile(this);
// e.g. for Sony Xperia XA2
if(QFile::exists(QString("/sys/class/power_supply/battery/input_suspend"))) {
chargingEnabledFile = new QFile("/sys/class/power_supply/battery/input_suspend");
chargingEnabledFile->setFileName("/sys/class/power_supply/battery/input_suspend");
if(chargingEnabledFile->exists()) {
enableChargingValue = 0;
disableChargingValue = 1;
}
// e.g. for Sony Xperia Z3 Compact Tablet
else if(QFile::exists(QString("/sys/class/power_supply/battery/charging_enabled"))) {
chargingEnabledFile = new QFile("/sys/class/power_supply/battery/charging_enabled");
chargingEnabledFile->setFileName("/sys/class/power_supply/battery/charging_enabled");
if(chargingEnabledFile->exists()) {
enableChargingValue = 1;
disableChargingValue = 0;
}
// e.g. for Jolla Phone
else if(QFile::exists(QString("/sys/class/power_supply/usb/charger_disable"))) {
chargingEnabledFile = new QFile("/sys/class/power_supply/usb/charger_disable");
chargingEnabledFile->setFileName("/sys/class/power_supply/usb/charger_disable");
if(chargingEnabledFile->exists()) {
enableChargingValue = 0;
disableChargingValue = 1;
}
else
else {
delete chargingEnabledFile;
chargingEnabledFile = Q_NULLPTR;
qWarning() << "Charger control file not found!";
qWarning() << "Please contact the developer with your device model!";
}
if(chargingEnabledFile) {
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
qInfo() << "Controlling charging via" << chargingEnabledFile->fileName();
chargingEnabledFile->close();
}
else {
delete chargingEnabledFile;
chargingEnabledFile = Q_NULLPTR;
qWarning() << "Charger control file" << chargingEnabledFile->fileName() << "is not writable";
qWarning() << "Charger control feature disabled";
}
}
// TODO
// Implement DBus mechanism for reading battery status
// Implement DBus mechanism for reading battery status, or try
// QFileSystemWatcher again without /run/state/namespaces/Battery/
// thingamabob - it is deprecated anyway.
updateData();
}
@ -68,6 +94,7 @@ void Battery::updateData()
if(nextCharge != charge) {
charge = nextCharge;
emit chargeChanged();
qDebug() << "Battery:" << charge;
}
chargeFile->close();
}
@ -76,6 +103,7 @@ void Battery::updateData()
if(nextChargerConnected != chargerConnected) {
chargerConnected = nextChargerConnected;
emit chargerConnectedChanged();
qDebug() << "Charger is connected:" << chargerConnected;
}
chargerConnectedFile->close();
}
@ -84,6 +112,7 @@ void Battery::updateData()
if(nextState != state) {
state = nextState;
emit stateChanged();
qDebug() << "Charging status:" << state;
}
stateFile->close();
}
@ -99,7 +128,7 @@ void Battery::updateData()
// chargingEnabledFile->close();
// }
if(settings->getLimitEnabled()) {
if(chargingEnabledFile && settings->getLimitEnabled()) {
if(chargingEnabled && charge >= settings->getHighLimit()) {
setChargingEnabled(false);
}
@ -120,6 +149,13 @@ void Battery::setChargingEnabled(bool isEnabled) {
if(chargingEnabledFile->write(QString("%1").arg(isEnabled ? enableChargingValue : disableChargingValue).toLatin1())) {
chargingEnabled = isEnabled;
emit chargingEnabledChanged();
if(isEnabled) {
qInfo() << "Charging resumed";
}
else {
qInfo() << "Charging paused";
}
}
chargingEnabledFile->close();
}

View file

@ -21,6 +21,7 @@
#include <QObject>
#include <QString>
#include <QFile>
#include <QDebug>
#include "settings.h"
class Battery : public QObject
@ -50,7 +51,7 @@ private:
QFile* chargeFile;
QFile* chargerConnectedFile;
QFile* stateFile;
QFile* chargingEnabledFile;
QFile* chargingEnabledFile = Q_NULLPTR;
Settings* settings;
// Default values:

View file

@ -26,6 +26,7 @@
#include <QQuickView>
#include <QQmlEngine>
#include <QTimer>
#include <QDebug>
#include "battery.h"
#include "settings.h"
@ -42,6 +43,8 @@ int main(int argc, char *argv[])
//
// To display the view, call "show()" (will show fullscreen on device).
qInfo() << "Starting Battery Buddy...";
QGuiApplication* app = SailfishApp::application(argc, argv);
QQuickView* view = SailfishApp::createView();
@ -58,8 +61,12 @@ int main(int argc, char *argv[])
view->setSource(SailfishApp::pathTo("qml/harbour-batterybuddy.qml"));
view->showFullScreen();
qInfo() << "Launching GUI...";
int retval = app->exec();
qInfo() << "Exiting...";
updater->stop();
battery->blockSignals(true);
battery->setChargingEnabled(true);
@ -68,5 +75,7 @@ int main(int argc, char *argv[])
delete battery;
delete settings;
qInfo() << "Goodbye!";
return retval;
}

View file

@ -23,46 +23,23 @@ Settings::Settings(QObject *parent) : QObject(parent)
if(mySettings.contains("lowerLimit")) {
mySettings.setValue(sLowAlert, mySettings.value("lowerLimit"));
mySettings.remove("lowerLimit");
qInfo() << "Migrated old lowerLimit value";
}
if(mySettings.contains("upperLimit")) {
mySettings.setValue(sHighAlert, mySettings.value("upperLimit"));
mySettings.remove("upperLimit");
qInfo() << "Migrated old upperLimit value";
}
// Read in the values
int tempValue;
tempValue = mySettings.value(sLowAlert, lowAlert).toInt();
if(tempValue >= 10 && tempValue <= 99) {
lowAlert = tempValue;
emit lowAlertChanged();
}
tempValue = mySettings.value(sHighAlert, highAlert).toInt();
if(tempValue >= 10 && tempValue <= 99) {
highAlert = tempValue;
emit highAlertChanged();
}
tempValue = mySettings.value(sInterval, interval).toInt();
if(tempValue >= 60 && tempValue <= 600) {
interval = tempValue;
emit intervalChanged();
}
limitEnabled = (mySettings.value(sLimitEnabled, sLimitEnabled).toInt() == 1);
emit limitEnabledChanged();
tempValue = mySettings.value(sLowLimit).toInt();
if(tempValue >= 20 && tempValue <= 95) {
lowLimit = tempValue;
emit lowLimitChanged();
}
tempValue = mySettings.value(sHighLimit, highLimit).toInt();
if(tempValue >= 20 && tempValue <= 95) {
highLimit = tempValue;
emit highLimitChanged();
}
loadInteger(sLowAlert, &lowAlert, 10, 99);
loadInteger(sHighAlert, &highAlert, 11, 100);
loadInteger(sInterval, &interval, 60, 600);
loadInteger(sLimitEnabled, &limitEnabled, 0, 1);
loadInteger(sLowLimit, &lowLimit, 20, 94);
loadInteger(sHighLimit, &highLimit, 21, 95);
qInfo() << "Loaded" << sLimitEnabled << limitEnabled;
}
Settings::~Settings()
@ -70,7 +47,17 @@ Settings::~Settings()
mySettings.setValue(sLowAlert, QByteArray::number(lowAlert));
mySettings.setValue(sHighAlert, QByteArray::number(highAlert));
mySettings.setValue(sInterval, QByteArray::number(interval));
mySettings.setValue(sLimitEnabled, QByteArray::number(limitEnabled ? 1 : 0));
mySettings.setValue(sLimitEnabled, QByteArray::number(limitEnabled));
mySettings.setValue(sLowLimit, QByteArray::number(lowLimit));
mySettings.setValue(sHighLimit, QByteArray::number(highLimit));
qInfo() << "Settings saved";
}
int Settings::bound(int value, int min, int max) {
return (value <= min ? min : (value >= max ? max : value));
}
void Settings::loadInteger(const char* key, int *value, int min, int max) {
*value = bound(mySettings.value(key, *value).toInt(), min, max);
qInfo() << "Loaded" << key << *value;
}

View file

@ -20,6 +20,7 @@
#include <QObject>
#include <QSettings>
#include <QDebug>
class Settings : public QObject
{
@ -42,7 +43,7 @@ public:
int getInterval() { return interval; }
int getLowLimit() { return lowLimit; }
int getHighLimit() { return highLimit; }
bool getLimitEnabled() { return limitEnabled; }
bool getLimitEnabled() { return limitEnabled == 1; }
QString getLowAlertFile() { return lowAlertFile; }
void setLowAlert(int newLimit) { lowAlert = newLimit; }
@ -50,7 +51,7 @@ public:
void setInterval(int newInterval) { interval = newInterval; }
void setLowLimit(int newLimit) { lowLimit = newLimit; }
void setHighLimit(int newLimit) { highLimit = newLimit; }
void setLimitEnabled(bool newEnabled) { limitEnabled = newEnabled; }
void setLimitEnabled(bool newEnabled) { limitEnabled = (newEnabled ? 1 : 0); }
QString getHighAlertFile() { return highAlertFile; }
@ -61,7 +62,7 @@ private:
int lowAlert = 25;
int highAlert = 75;
int interval = 60;
bool limitEnabled = false;
int limitEnabled = 0; // Converted to boolean for QML
int lowLimit = 65;
int highLimit = 70;
QString lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav";
@ -77,6 +78,9 @@ private:
const char* sLowAlertFile = "lowAlertFile";
const char* sHighAlertFile = "highAlertFile";
int bound(int value, int min, int max);
void loadInteger(const char *key, int* value, int min, int max);
signals:
int lowAlertChanged();
int highAlertChanged();