add health and temperature reading

This commit is contained in:
Peter G. (nephros) 2021-05-02 10:23:32 +02:00
parent 91d8974b06
commit e5f90a50db
2 changed files with 50 additions and 0 deletions

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);
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;
// e.g. for Sony Xperia XA2
@ -112,6 +120,26 @@ void Battery::updateData()
}
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; }
@ -120,6 +148,10 @@ int Battery::getCurrent(){ return current; }
QString Battery::getState() { return state; }
QString Battery::getHealth() { return health; }
int Battery::getTemperature(){ return temperature; }
bool Battery::getChargingEnabled() { return chargingEnabled; }
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(bool chargingEnabled READ getChargingEnabled NOTIFY chargingEnabledChanged)
Q_PROPERTY(QString health READ getHealth NOTIFY healthChanged)
Q_PROPERTY(int temperature READ getTemperature NOTIFY temperatureChanged)
public:
Battery(Settings* newSettings, Logger* newLogger, QObject* parent = nullptr);
~Battery();
@ -46,6 +49,9 @@ public:
bool getChargerConnected();
QString getState();
QString getHealth();
int getTemperature();
bool getChargingEnabled();
public slots:
@ -60,6 +66,9 @@ private:
Settings* settings = nullptr;
Logger* logger = nullptr;
QFile* temperatureFile = nullptr;
QFile* healthFile = nullptr;
// Default values:
int charge = 100; // 100% full
int current = 0; // Not charging/discharging
@ -67,6 +76,10 @@ private:
QString state = "idle"; // dis/charging, idle, unknown
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 disableChargingValue = 0;
bool chargerIsEnabled = true;
@ -77,12 +90,17 @@ private:
QString nextState = state;
bool nextChargingEnabled = chargingEnabled;
QString nextHealth = health;
int nextTemperature = temperature;
signals:
void chargeChanged(int);
void currentChanged(int);
void stateChanged(QString);
void chargingEnabledChanged(bool);
void chargerConnectedChanged(bool);
void healthChanged(QString);
void temperatureChanged(int);
};
#endif // BATTERY_H