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)
{
chargeFile = new QFile("/run/state/namespaces/Battery/ChargePercentage"); // Number, meaning percentage, e.g. 42
chargingFile = new QFile("/run/state/namespaces/Battery/IsCharging"); // Number, 0 or 1
// Number: meaning percentage, e.g. 42
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
// What if the files can't be opened?
@ -35,22 +39,47 @@ void Battery::updateData()
{
if(chargeFile->open(QIODevice::ReadOnly)) {
nextCharge = chargeFile->readAll().toInt();
if(nextCharge != currentCharge) {
currentCharge = nextCharge;
qDebug() << "Charge:" << nextCharge;
if(nextCharge != charge) {
charge = nextCharge;
emit chargeChanged();
}
chargeFile->close();
}
if(chargingFile->open(QIODevice::ReadOnly)) {
nextCharging = (chargingFile->readAll().toInt() == 0 ? false : true);
if(nextCharging != isCharging) {
isCharging = nextCharging;
qDebug() << "Charging:" << nextCharge;
if(nextCharging != charging) {
charging = nextCharging;
emit chargingChanged();
}
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
#define BATTERY_H
#include <QObject>
/**
* Battery Buddy, a Sailfish application to prolong battery lifetime
*
@ -19,14 +17,18 @@
*
* Author: Matti Viljanen
*/
#include <QObject>
#include <QString>
#include <QFile>
#include <QDebug>
class Battery : public QObject
{
Q_OBJECT
Q_PROPERTY(int charge READ getCharge NOTIFY chargeChanged )
Q_PROPERTY(bool charging READ getCharging NOTIFY chargingChanged)
Q_PROPERTY(int charge READ getCharge NOTIFY chargeChanged )
Q_PROPERTY(bool charging READ getCharging NOTIFY chargingChanged)
Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
public:
Battery(QObject* parent = nullptr);
@ -34,6 +36,7 @@ public:
int getCharge();
bool getCharging();
QString getState();
public slots:
void updateData();
@ -41,17 +44,22 @@ public slots:
private:
QFile* chargeFile;
QFile* chargingFile;
QFile* stateFile;
// Default values
int currentCharge = 100;
bool isCharging = true;
// Default values:
int charge = 100; // 100% full
bool charging = true; // Charger plugged in
QString state = "idle"; // Not charging
QString stateTr = state; // Translated state text
int nextCharge = 100;
bool nextCharging = true;
int nextCharge = charge;
bool nextCharging = charging;
QString nextStatus = state;
signals:
int chargeChanged();
bool chargingChanged();
QString stateChanged();
};
#endif // BATTERY_H