Add state to battery data, return translated state

This commit is contained in:
Matti Viljanen 2019-01-08 20:16:10 +02:00
parent 10fe796d1c
commit 7ef7775c89
No known key found for this signature in database
GPG key ID: CF32A1495158F888
2 changed files with 54 additions and 17 deletions

View file

@ -19,8 +19,12 @@
Battery::Battery(QObject* parent) : QObject(parent) Battery::Battery(QObject* parent) : QObject(parent)
{ {
chargeFile = new QFile("/run/state/namespaces/Battery/ChargePercentage"); // Number, meaning percentage, e.g. 42 // Number: meaning percentage, e.g. 42
chargingFile = new QFile("/run/state/namespaces/Battery/IsCharging"); // Number, 0 or 1 chargeFile = new QFile("/run/state/namespaces/Battery/ChargePercentage");
// Number: 0 or 1
chargingFile = new QFile("/run/state/namespaces/Battery/IsCharging");
// String: charging, discharging, idle, unknown (others?)
stateFile = new QFile("/run/state/namespaces/Battery/ChargingState");
// TODO // TODO
// What if the files can't be opened? // What if the files can't be opened?
@ -35,22 +39,47 @@ void Battery::updateData()
{ {
if(chargeFile->open(QIODevice::ReadOnly)) { if(chargeFile->open(QIODevice::ReadOnly)) {
nextCharge = chargeFile->readAll().toInt(); nextCharge = chargeFile->readAll().toInt();
if(nextCharge != currentCharge) { qDebug() << "Charge:" << nextCharge;
currentCharge = nextCharge; if(nextCharge != charge) {
charge = nextCharge;
emit chargeChanged(); emit chargeChanged();
} }
chargeFile->close(); chargeFile->close();
} }
if(chargingFile->open(QIODevice::ReadOnly)) { if(chargingFile->open(QIODevice::ReadOnly)) {
nextCharging = (chargingFile->readAll().toInt() == 0 ? false : true); nextCharging = (chargingFile->readAll().toInt() == 0 ? false : true);
if(nextCharging != isCharging) { qDebug() << "Charging:" << nextCharge;
isCharging = nextCharging; if(nextCharging != charging) {
charging = nextCharging;
emit chargingChanged(); emit chargingChanged();
} }
chargingFile->close(); chargingFile->close();
} }
if(stateFile->open(QIODevice::ReadOnly)) {
nextStatus = (QString(stateFile->readAll()));
qDebug() << "Status:" << nextStatus;
if(nextStatus != state) {
state = nextStatus;
// Update translated text accordingly
if(state == "idle")
stateTr = tr("idle", "Battery in charger, not using nor charging battery");
else if(state == "discharging")
stateTr = tr("discharging", "Charger not plugged in, battery discharging");
else if(state == "charging")
stateTr = tr("charging", "Charger plugged in and battery charging");
else if(state == "unknown")
stateTr = tr("unknown", "Battery not detected, or faulty, or something");
// Finally, emit the signal
emit stateChanged();
}
stateFile->close();
}
} }
int Battery::getCharge(){ return currentCharge; } int Battery::getCharge(){ return charge; }
bool Battery::getCharging() { return isCharging; } bool Battery::getCharging() { return charging; }
QString Battery::getState() { return stateTr; }

View file

@ -1,7 +1,5 @@
#ifndef BATTERY_H #ifndef BATTERY_H
#define BATTERY_H #define BATTERY_H
#include <QObject>
/** /**
* Battery Buddy, a Sailfish application to prolong battery lifetime * Battery Buddy, a Sailfish application to prolong battery lifetime
* *
@ -19,14 +17,18 @@
* *
* Author: Matti Viljanen * Author: Matti Viljanen
*/ */
#include <QObject>
#include <QString> #include <QString>
#include <QFile> #include <QFile>
#include <QDebug>
class Battery : public QObject class Battery : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int charge READ getCharge NOTIFY chargeChanged ) Q_PROPERTY(int charge READ getCharge NOTIFY chargeChanged )
Q_PROPERTY(bool charging READ getCharging NOTIFY chargingChanged) Q_PROPERTY(bool charging READ getCharging NOTIFY chargingChanged)
Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
public: public:
Battery(QObject* parent = nullptr); Battery(QObject* parent = nullptr);
@ -34,6 +36,7 @@ public:
int getCharge(); int getCharge();
bool getCharging(); bool getCharging();
QString getState();
public slots: public slots:
void updateData(); void updateData();
@ -41,17 +44,22 @@ public slots:
private: private:
QFile* chargeFile; QFile* chargeFile;
QFile* chargingFile; QFile* chargingFile;
QFile* stateFile;
// Default values // Default values:
int currentCharge = 100; int charge = 100; // 100% full
bool isCharging = true; bool charging = true; // Charger plugged in
QString state = "idle"; // Not charging
QString stateTr = state; // Translated state text
int nextCharge = 100; int nextCharge = charge;
bool nextCharging = true; bool nextCharging = charging;
QString nextStatus = state;
signals: signals:
int chargeChanged(); int chargeChanged();
bool chargingChanged(); bool chargingChanged();
QString stateChanged();
}; };
#endif // BATTERY_H #endif // BATTERY_H