Better handle non-existent files
This commit is contained in:
parent
c6f9000aa9
commit
f0dd0e5018
2 changed files with 30 additions and 20 deletions
|
@ -38,15 +38,19 @@ 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());
|
||||||
|
|
||||||
|
QString filename;
|
||||||
|
|
||||||
// Number: temperature
|
// Number: temperature
|
||||||
temperatureFile = new QFile("/sys/class/power_supply/battery/temp", this);
|
filename = "/sys/class/power_supply/battery/temp";
|
||||||
logE("Temperature file: " + temperatureFile->fileName());
|
if(!temperatureFile && QFile::exists(filename)) {
|
||||||
|
temperatureFile = new QFile(filename, this);
|
||||||
|
}
|
||||||
|
|
||||||
// String: health state
|
// String: health state
|
||||||
healthFile = new QFile("/sys/class/power_supply/battery/health", this);
|
filename = "/sys/class/power_supply/battery/health";
|
||||||
logE("Health file: " + healthFile->fileName());
|
if(!healthFile && QFile::exists(filename)) {
|
||||||
|
healthFile = new QFile(filename, this);
|
||||||
QString filename;
|
}
|
||||||
|
|
||||||
// e.g. for Sony Xperia XA2
|
// e.g. for Sony Xperia XA2
|
||||||
filename = "/sys/class/power_supply/battery/input_suspend";
|
filename = "/sys/class/power_supply/battery/input_suspend";
|
||||||
|
|
|
@ -39,26 +39,31 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject
|
||||||
|
|
||||||
// Number: charge percentage, e.g. 42
|
// Number: charge percentage, e.g. 42
|
||||||
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
|
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
|
||||||
logE("Capacity file: " + chargeFile->fileName());
|
logE("Capacity file: " + chargeFile->fileName() + (chargeFile->exists() ? " OK" : " doesn't exist"));
|
||||||
|
|
||||||
// String: charging, discharging, full, empty, unknown (others?)
|
// String: charging, discharging, full, empty, unknown (others?)
|
||||||
stateFile = new QFile("/sys/class/power_supply/battery/status", this);
|
stateFile = new QFile("/sys/class/power_supply/battery/status", this);
|
||||||
logE("Charge state file: " + stateFile->fileName());
|
logE("Charge state file: " + stateFile->fileName() + (stateFile->exists() ? " OK" : " doesn't exist"));
|
||||||
|
|
||||||
// Number: 0 or 1
|
// Number: 0 or 1
|
||||||
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this);
|
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this);
|
||||||
logE("Charger status file: " + chargerConnectedFile->fileName());
|
logE("Charger status file: " + chargerConnectedFile->fileName() + (chargerConnectedFile->exists() ? " OK" : " doesn't exist"));
|
||||||
|
|
||||||
|
QString filename;
|
||||||
|
|
||||||
// Number: temperature
|
// Number: temperature
|
||||||
temperatureFile = new QFile("/sys/class/power_supply/battery/temp", this);
|
filename = "/sys/class/power_supply/battery/temp";
|
||||||
logE("Temperature file: " + temperatureFile->fileName());
|
if(!temperatureFile && QFile::exists(filename)) {
|
||||||
|
temperatureFile = new QFile(filename, this);
|
||||||
|
}
|
||||||
|
logE("Temperature file: " + filename + (QFile::exists(filename) ? " OK" : " doesn't exist"));
|
||||||
|
|
||||||
// String: Good, Warm, Overheat, (others?)
|
// String: health state
|
||||||
healthFile = new QFile("/sys/class/power_supply/battery/health", this);
|
filename = "/sys/class/power_supply/battery/health";
|
||||||
logE("Health state file: " + healthFile->fileName());
|
if(!healthFile && QFile::exists(filename)) {
|
||||||
|
healthFile = new QFile(filename, this);
|
||||||
// ENABLE/DISABLE CHARGING
|
}
|
||||||
QString filename;
|
logE("Battery health file: " + filename + (QFile::exists(filename) ? " OK" : " doesn't exist"));
|
||||||
|
|
||||||
// e.g. for Sony Xperia XA2
|
// e.g. for Sony Xperia XA2
|
||||||
filename = "/sys/class/power_supply/battery/input_suspend";
|
filename = "/sys/class/power_supply/battery/input_suspend";
|
||||||
|
@ -86,7 +91,7 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject
|
||||||
|
|
||||||
// If we found a usable file, check that it is writable
|
// If we found a usable file, check that it is writable
|
||||||
if(chargingEnabledFile) {
|
if(chargingEnabledFile) {
|
||||||
logE("Charger control file: " + chargingEnabledFile->fileName());
|
logE("Charger control file: " + chargingEnabledFile->fileName() + (chargingEnabledFile->exists() ? " OK" : " doesn't exist"));
|
||||||
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
|
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
|
||||||
chargingEnabledFile->close();
|
chargingEnabledFile->close();
|
||||||
}
|
}
|
||||||
|
@ -156,7 +161,7 @@ void Battery::updateData()
|
||||||
stateFile->close();
|
stateFile->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(temperatureFile->open(QIODevice::ReadOnly)) {
|
if(temperatureFile && temperatureFile->open(QIODevice::ReadOnly)) {
|
||||||
nextTemperature = temperatureFile->readLine().trimmed().toInt();
|
nextTemperature = temperatureFile->readLine().trimmed().toInt();
|
||||||
if(nextTemperature != temperature) {
|
if(nextTemperature != temperature) {
|
||||||
temperature = nextTemperature;
|
temperature = nextTemperature;
|
||||||
|
@ -165,7 +170,8 @@ void Battery::updateData()
|
||||||
}
|
}
|
||||||
temperatureFile->close();
|
temperatureFile->close();
|
||||||
}
|
}
|
||||||
if(healthFile->open(QIODevice::ReadOnly)) {
|
|
||||||
|
if(healthFile && healthFile->open(QIODevice::ReadOnly)) {
|
||||||
nextHealth = (QString(healthFile->readLine().trimmed().toLower()));
|
nextHealth = (QString(healthFile->readLine().trimmed().toLower()));
|
||||||
if(nextHealth != health) {
|
if(nextHealth != health) {
|
||||||
health = nextHealth;
|
health = nextHealth;
|
||||||
|
|
Loading…
Reference in a new issue