From 53381ab391d68a1939d6854ce16bf982a138d944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sl=C3=A1vek=20Banko?= Date: Wed, 8 Jan 2020 01:49:53 +0100 Subject: tdehwlib: Adjusted parsing of battery information + Added initialization of all values Some batteries do not provide all values and, without initialization, values may contain dubious data. + Added reading of current battery capacity value in percent (capacity) Some batteries may provide the current capacity value in percent instead of the current energy value. + Added check whether the battery provides current energy value (charge_now or energy_now) The current energy is calculated if the value is not provided in the battery information. + Added reading of estimated time to empty battery (time_to_empty_now) The remaining time is calculated if the value is not provided in the battery information. If the battery does not provide charging current information, the estimated time to charge the battery cannot be calculated. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Slávek Banko (cherry picked from commit 86c1d896c748bf5117ccfe28b20e48a7e2a70c8d) --- tdecore/tdehw/tdehardwaredevices.cpp | 37 +++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'tdecore/tdehw/tdehardwaredevices.cpp') diff --git a/tdecore/tdehw/tdehardwaredevices.cpp b/tdecore/tdehw/tdehardwaredevices.cpp index 9895dc6b5..386fcbc77 100644 --- a/tdecore/tdehw/tdehardwaredevices.cpp +++ b/tdecore/tdehw/tdehardwaredevices.cpp @@ -2974,6 +2974,9 @@ void TDEHardwareDevices::updateExistingDeviceInformation(TDEGenericDevice* exist TQDir valuesdir(valuesnodename); valuesdir.setFilter(TQDir::All); TQString nodename; + double bdevice_capacity = 0; + bool bdevice_has_energy = false; + bool bdevice_has_time_to_empty = false; const TQFileInfoList *dirlist = valuesdir.entryInfoList(); if (dirlist) { TQFileInfoListIterator valuesdirit(*dirlist); @@ -3043,6 +3046,9 @@ void TDEHardwareDevices::updateExistingDeviceInformation(TDEGenericDevice* exist if (nodename == "alarm") { bdevice->internalSetAlarmEnergy(line.toDouble()/1000000.0); } + else if (nodename == "capacity") { + bdevice_capacity = line.toDouble(); + } else if (nodename == "charge_full" || nodename == "energy_full") { bdevice->internalSetMaximumEnergy(line.toDouble()/1000000.0); } @@ -3051,6 +3057,7 @@ void TDEHardwareDevices::updateExistingDeviceInformation(TDEGenericDevice* exist } else if (nodename == "charge_now" || nodename == "energy_now") { bdevice->internalSetEnergy(line.toDouble()/1000000.0); + bdevice_has_energy = true; } else if (nodename == "manufacturer") { bdevice->internalSetVendorName(line.stripWhiteSpace()); @@ -3073,6 +3080,11 @@ void TDEHardwareDevices::updateExistingDeviceInformation(TDEGenericDevice* exist else if (nodename == "technology") { bdevice->internalSetTechnology(line); } + else if (nodename == "time_to_empty_now") { + // Convert from minutes to seconds + bdevice->internalSetTimeRemaining(line.toDouble()*60); + bdevice_has_time_to_empty = true; + } else if (nodename == "voltage_min_design") { bdevice->internalSetMinimumVoltage(line.toDouble()/1000000.0); } @@ -3085,16 +3097,23 @@ void TDEHardwareDevices::updateExistingDeviceInformation(TDEGenericDevice* exist } } - // Calculate time remaining - // Discharge/charge rate is in watt-hours - // Energy is in watt-hours - // Therefore, energy/rate = time in hours - // Convert to seconds... - if (bdevice->status() == TDEBatteryStatus::Charging) { - bdevice->internalSetTimeRemaining(((bdevice->maximumEnergy()-bdevice->energy())/bdevice->dischargeRate())*60*60); + // Calculate current energy if missing + if (!bdevice_has_energy) { + bdevice->internalSetEnergy(bdevice_capacity*bdevice->maximumEnergy()/100); } - else { - bdevice->internalSetTimeRemaining((bdevice->energy()/bdevice->dischargeRate())*60*60); + + // Calculate time remaining + if (bdevice->dischargeRate() > 0) { + // Discharge/charge rate is in watt-hours + // Energy is in watt-hours + // Therefore, energy/rate = time in hours + // Convert to seconds... + if (bdevice->status() == TDEBatteryStatus::Charging) { + bdevice->internalSetTimeRemaining(((bdevice->maximumEnergy()-bdevice->energy())/bdevice->dischargeRate())*60*60); + } + else if (!bdevice_has_time_to_empty) { + bdevice->internalSetTimeRemaining((bdevice->energy()/bdevice->dischargeRate())*60*60); + } } } -- cgit v1.2.1