Merge pull request #570 from plakak/fix/linux-battery-capacity
fix: linux battery capacity
This commit is contained in:
commit
46c4307a41
@ -90,19 +90,33 @@ module.exports = function (callback) {
|
||||
} else if (fs.existsSync('/sys/class/power_supply/BAT0/uevent')) {
|
||||
battery_path = '/sys/class/power_supply/BAT0/';
|
||||
}
|
||||
|
||||
let acConnected = false;
|
||||
let acPath = '';
|
||||
if (fs.existsSync('/sys/class/power_supply/AC/online')) {
|
||||
acPath = '/sys/class/power_supply/AC/online';
|
||||
} else if (fs.existsSync('/sys/class/power_supply/AC0/online')) {
|
||||
acPath = '/sys/class/power_supply/AC0/online';
|
||||
}
|
||||
|
||||
if (acPath) {
|
||||
const file = fs.readFileSync(acPath);
|
||||
acConnected = file.toString().trim() === '1';
|
||||
}
|
||||
|
||||
if (battery_path) {
|
||||
fs.readFile(battery_path + 'uevent', function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
|
||||
result.isCharging = (util.getValue(lines, 'POWER_SUPPLY_STATUS', '=').toLowerCase() === 'charging');
|
||||
result.acConnected = result.isCharging;
|
||||
result.acConnected = acConnected || result.isCharging;
|
||||
result.voltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_NOW', '='), 10) / 1000000.0;
|
||||
result.capacityUnit = result.voltage ? 'mWh' : 'mAh';
|
||||
result.cycleCount = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CYCLE_COUNT', '='), 10);
|
||||
result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '='), 10) / 1000.0 / (result.voltage || 1));
|
||||
result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL_DESIGN', '='), 10) / 1000.0 / (result.voltage || 1)) | result.maxcapacity;
|
||||
result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10) / 1000.0 / (result.voltage || 1));
|
||||
result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '='), 10) / 1000.0 * (result.voltage || 1));
|
||||
result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL_DESIGN', '='), 10) / 1000.0 * (result.voltage || 1));
|
||||
result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10) / 1000.0 * (result.voltage || 1));
|
||||
if (!result.maxCapacity) {
|
||||
result.maxCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL', '='), 10) / 1000.0;
|
||||
result.designedCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL_DESIGN', '='), 10) / 1000.0 | result.maxCapacity;
|
||||
|
||||
@ -112,7 +112,7 @@ function getValue(lines, property, separator, trimmed) {
|
||||
if (trimmed) {
|
||||
line = line.trim();
|
||||
}
|
||||
if (line.startsWith(property)) {
|
||||
if (line.startsWith(property) && line.match(property + separator)) {
|
||||
const parts = trimmed ? lines[i].trim().split(separator) : lines[i].split(separator);
|
||||
if (parts.length >= 2) {
|
||||
parts.shift();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user