battery() bugfix & added type, model, manufacturer, serial, timeremaining
This commit is contained in:
+47
-30
@@ -40,47 +40,58 @@ module.exports = function (callback) {
|
||||
maxcapacity: 0,
|
||||
currentcapacity: 0,
|
||||
percent: 0,
|
||||
acconnected: true
|
||||
timeremaining: -1,
|
||||
acconnected: true,
|
||||
type: '',
|
||||
model: '',
|
||||
manufacturer: '',
|
||||
serial: ''
|
||||
};
|
||||
|
||||
if (_linux) {
|
||||
let battery_path = '';
|
||||
if (fs.existsSync('/sys/class/power_supply/BAT1/status')) {
|
||||
if (fs.existsSync('/sys/class/power_supply/BAT1/uevent')) {
|
||||
battery_path = '/sys/class/power_supply/BAT1/';
|
||||
} else if (fs.existsSync('/sys/class/power_supply/BAT0/status')) {
|
||||
} else if (fs.existsSync('/sys/class/power_supply/BAT0/uevent')) {
|
||||
battery_path = '/sys/class/power_supply/BAT0/';
|
||||
}
|
||||
if (battery_path) {
|
||||
exec('cat ' + battery_path + 'status', function (error, stdout) {
|
||||
exec('cat ' + battery_path + 'uevent', function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
if (lines.length > 0 && lines[0]) result.ischarging = (lines[0].trim().toLowerCase() === 'charging');
|
||||
|
||||
result.ischarging = (util.getValue(lines, 'POWER_SUPPLY_STATUS', '=').toLowerCase() === 'charging');
|
||||
result.acconnected = result.ischarging;
|
||||
}
|
||||
exec('cat ' + battery_path + 'cyclec_ount', function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
if (lines.length > 0 && lines[0]) result.cyclecount = parseFloat(lines[0].trim());
|
||||
result.cyclecount = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CYCLE_COUNT', '='), 10);
|
||||
result.maxcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '='), 10);
|
||||
if (!result.maxcapacity) {
|
||||
result.maxcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL', '='), 10);
|
||||
}
|
||||
exec('cat ' + battery_path + 'charge_full', function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
if (lines.length > 0 && lines[0]) result.maxcapacity = parseFloat(lines[0].trim());
|
||||
result.currentcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10);
|
||||
if (!result.currentcapacity) {
|
||||
result.currentcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10);
|
||||
}
|
||||
const percent = util.getValue(lines, 'POWER_SUPPLY_CAPACITY', '=');
|
||||
const energy = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10);
|
||||
const power = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_POWER_NOW', '='), 10);
|
||||
|
||||
result.percent = parseInt('0' + percent, 10);
|
||||
if (result.maxcapacity && result.currentcapacity) {
|
||||
result.hasbattery = true;
|
||||
if (!percent) {
|
||||
result.percent = 100.0 * result.currentcapacity / result.maxcapacity;
|
||||
}
|
||||
exec('cat ' + battery_path + 'charge_now', function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
if (lines.length > 0 && lines[0]) result.currentcapacity = parseFloat(lines[0].trim());
|
||||
}
|
||||
if (result.maxcapacity && result.currentcapacity) {
|
||||
result.hasbattery = true;
|
||||
result.percent = 100.0 * result.currentcapacity / result.maxcapacity;
|
||||
}
|
||||
if (callback) { callback(result); }
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
if (energy && power) {
|
||||
result.timeremaining = Math.floor(energy / power * 60);
|
||||
}
|
||||
result.type = util.getValue(lines, 'POWER_SUPPLY_TECHNOLOGY', '=');
|
||||
result.model = util.getValue(lines, 'POWER_SUPPLY_MODEL_NAME', '=');
|
||||
result.manufacturer = util.getValue(lines, 'POWER_SUPPLY_MANUFACTURER', '=');
|
||||
result.serial = util.getValue(lines, 'POWER_SUPPLY_SERIAL_NUMBER', '=');
|
||||
if (callback) { callback(result); }
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (callback) { callback(result); }
|
||||
@@ -105,12 +116,14 @@ module.exports = function (callback) {
|
||||
}
|
||||
|
||||
if (_darwin) {
|
||||
exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|MaxCapacity|CurrentCapacity"; pmset -g batt | grep %', function (error, stdout) {
|
||||
exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|MaxCapacity|CurrentCapacity|BatterySerialNumber|TimeRemaining"; pmset -g batt | grep %', function (error, stdout) {
|
||||
if (stdout) {
|
||||
let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n');
|
||||
result.cyclecount = parseInt('0' + util.getValue(lines, 'cyclecount', '='), 10);
|
||||
result.maxcapacity = parseInt('0' + util.getValue(lines, 'maxcapacity', '='), 10);
|
||||
result.currentcapacity = parseInt('0' + util.getValue(lines, 'currentcapacity', '='), 10);
|
||||
result.manufacturer = 'Apple';
|
||||
result.serial = util.getValue(lines, 'BatterySerialNumber', '=');
|
||||
let percent = -1;
|
||||
const line = util.getValue(lines, 'internal', 'Battery');
|
||||
let parts = line.split(';');
|
||||
@@ -129,7 +142,11 @@ module.exports = function (callback) {
|
||||
}
|
||||
if (result.maxcapacity && result.currentcapacity) {
|
||||
result.hasbattery = true;
|
||||
result.type = 'Li-ion';
|
||||
result.percent = percent !== -1 ? percent : Math.round(100.0 * result.currentcapacity / result.maxcapacity);
|
||||
if (!result.ischarging) {
|
||||
result.timeremaining = parseInt('0' + util.getValue(lines, 'TimeRemaining', '='), 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (callback) { callback(result); }
|
||||
@@ -161,7 +178,7 @@ module.exports = function (callback) {
|
||||
});
|
||||
} catch (e) {
|
||||
if (callback) { callback(result); }
|
||||
resolve(result);
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user