Merge pull request #40 from nephros/health

PoC/RfC: read and display battery health and temperature
This commit is contained in:
Matti Viljanen 2021-05-02 13:34:42 +03:00 committed by GitHub
commit 4a535edd59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 0 deletions

View file

@ -31,6 +31,11 @@ Page {
"empty": qsTr("empty", "Battery fully depleted"), "empty": qsTr("empty", "Battery fully depleted"),
"unknown": qsTr("unknown", "Battery not detected, or faulty, or something") "unknown": qsTr("unknown", "Battery not detected, or faulty, or something")
} }
property variant healthText: {
"good": qsTr("Good", "Battery is OK"),
"warm": qsTr("Warm", "Battery is warm"),
"overheat": qsTr("Overheated", "Battery is very hot"),
}
property bool serviceRunning: true property bool serviceRunning: true
Timer { Timer {
@ -166,6 +171,16 @@ Page {
label: qsTr("State:") label: qsTr("State:")
value: statusText[battery.state] value: statusText[battery.state]
} }
MyDetailItem {
label: qsTr("Health:")
value: healthText[battery.health]
}
MyDetailItem {
label: qsTr("Temperature:")
// TODO: use weird degrees for US users
//value: useImperial ? celsiusToFahrenheit(battery.temperature) + " F" : Math.floor(battery.temperature / 10) + " °C"
value: Math.floor(battery.temperature / 10) + " °C"
}
} }
} }
Column { Column {

View file

@ -38,6 +38,14 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this); chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this);
logE("Reading charger status from" + chargerConnectedFile->fileName()); logE("Reading charger status from" + chargerConnectedFile->fileName());
// Number: temperature
temperatureFile = new QFile("/sys/class/power_supply/battery/temp", this);
logE("Temperature file: " + temperatureFile->fileName());
// String: health state
healthFile = new QFile("/sys/class/power_supply/battery/health", this);
logE("Health file: " + healthFile->fileName());
QString filename; QString filename;
// e.g. for Sony Xperia XA2 // e.g. for Sony Xperia XA2
@ -112,6 +120,26 @@ void Battery::updateData()
} }
currentFile->close(); currentFile->close();
} }
if(healthFile && healthFile->open(QIODevice::ReadOnly)) {
nextHealth = (QString(healthFile->readLine().trimmed().toLower()));
if(nextHealth != health) {
health = nextHealth;
emit healthChanged(health);
logV("Health: " + health);
}
healthFile->close();
}
if(temperatureFile && temperatureFile->open(QIODevice::ReadOnly)) {
nextTemperature = temperatureFile->readLine().trimmed().toInt();
if(nextTemperature != temperature) {
temperature = nextTemperature;
emit temperatureChanged(temperature);
// TODO: factor might be different depending on device
// X10 has degrees * 10
logD(QString("Temperature: %1°C").arg(temperature / 10));
}
temperatureFile->close();
}
} }
int Battery::getCharge(){ return charge; } int Battery::getCharge(){ return charge; }
@ -120,6 +148,10 @@ int Battery::getCurrent(){ return current; }
QString Battery::getState() { return state; } QString Battery::getState() { return state; }
QString Battery::getHealth() { return health; }
int Battery::getTemperature(){ return temperature; }
bool Battery::getChargingEnabled() { return chargingEnabled; } bool Battery::getChargingEnabled() { return chargingEnabled; }
bool Battery::getChargerConnected() { return chargerConnected; } bool Battery::getChargerConnected() { return chargerConnected; }

View file

@ -36,6 +36,9 @@ class Battery : public QObject
Q_PROPERTY(QString state READ getState NOTIFY stateChanged) Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
Q_PROPERTY(bool chargingEnabled READ getChargingEnabled NOTIFY chargingEnabledChanged) Q_PROPERTY(bool chargingEnabled READ getChargingEnabled NOTIFY chargingEnabledChanged)
Q_PROPERTY(QString health READ getHealth NOTIFY healthChanged)
Q_PROPERTY(int temperature READ getTemperature NOTIFY temperatureChanged)
public: public:
Battery(Settings* newSettings, Logger* newLogger, QObject* parent = nullptr); Battery(Settings* newSettings, Logger* newLogger, QObject* parent = nullptr);
~Battery(); ~Battery();
@ -46,6 +49,9 @@ public:
bool getChargerConnected(); bool getChargerConnected();
QString getState(); QString getState();
QString getHealth();
int getTemperature();
bool getChargingEnabled(); bool getChargingEnabled();
public slots: public slots:
@ -60,6 +66,9 @@ private:
Settings* settings = nullptr; Settings* settings = nullptr;
Logger* logger = nullptr; Logger* logger = nullptr;
QFile* temperatureFile = nullptr;
QFile* healthFile = nullptr;
// Default values: // Default values:
int charge = 100; // 100% full int charge = 100; // 100% full
int current = 0; // Not charging/discharging int current = 0; // Not charging/discharging
@ -67,6 +76,10 @@ private:
QString state = "idle"; // dis/charging, idle, unknown QString state = "idle"; // dis/charging, idle, unknown
bool chargingEnabled = true; // Only ever disabled manually bool chargingEnabled = true; // Only ever disabled manually
QString health = "Good"; // Good, Warm, Overheat. Might have Cold or Overvoltage depending on driver
int temperature = 0; // freezing
int enableChargingValue = 1; int enableChargingValue = 1;
int disableChargingValue = 0; int disableChargingValue = 0;
bool chargerIsEnabled = true; bool chargerIsEnabled = true;
@ -77,12 +90,17 @@ private:
QString nextState = state; QString nextState = state;
bool nextChargingEnabled = chargingEnabled; bool nextChargingEnabled = chargingEnabled;
QString nextHealth = health;
int nextTemperature = temperature;
signals: signals:
void chargeChanged(int); void chargeChanged(int);
void currentChanged(int); void currentChanged(int);
void stateChanged(QString); void stateChanged(QString);
void chargingEnabledChanged(bool); void chargingEnabledChanged(bool);
void chargerConnectedChanged(bool); void chargerConnectedChanged(bool);
void healthChanged(QString);
void temperatureChanged(int);
}; };
#endif // BATTERY_H #endif // BATTERY_H