fixed bug battery().percent for macOS
This commit is contained in:
parent
ab894f1a6e
commit
bf7eec239b
@ -100,7 +100,8 @@ Other changes
|
||||
|
||||
| Version | Date | Comment |
|
||||
| -------------- | -------------- | -------- |
|
||||
| 3.37.1 | 2018-02-13 | fixed bug `battery.ischarging()` for macOS |
|
||||
| 3.37.2 | 2018-02-15 | fixed bug `battery().percent` for macOS |
|
||||
| 3.37.1 | 2018-02-13 | fixed bug `battery().ischarging` for macOS |
|
||||
| 3.37.0 | 2018-02-11 | extended FreeBSD support `networkStats()` |
|
||||
| 3.36.0 | 2018-02-11 | extended FreeBSD support `networkConnections()` |
|
||||
| 3.35.0 | 2018-02-11 | extended FreeBSD support `processLoad()` |
|
||||
|
||||
@ -97,24 +97,26 @@ module.exports = function (callback) {
|
||||
}
|
||||
|
||||
if (_darwin) {
|
||||
exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|MaxCapacity|CurrentCapacity";pmset -g batt | grep %', function (error, stdout) {
|
||||
let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n');
|
||||
result.cyclecount = parseInt('0' + util.getValue(lines,'cyclecount', '='), 10);
|
||||
result.ischarging = util.getValue(lines,'ischarging', '=').toLowerCase() === 'yes';
|
||||
result.maxcapacity = parseInt('0' + util.getValue(lines,'maxcapacity', '='), 10);
|
||||
result.currentcapacity = parseInt('0' + util.getValue(lines,'currentcapacity', '='), 10);
|
||||
let percent = -1;
|
||||
const line = util.getValue(lines,'internalbattery', '=');
|
||||
let parts = line.split(';');
|
||||
if (parts && parts[0]) {
|
||||
let parts2 = parts[0].split('\t');
|
||||
if (parts2 && parts2[1]) {
|
||||
percent = parseFloat(parts2[1].trim().replace('%', ''));
|
||||
exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|MaxCapacity|CurrentCapacity"; 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.ischarging = util.getValue(lines,'ischarging', '=').toLowerCase() === 'yes';
|
||||
result.maxcapacity = parseInt('0' + util.getValue(lines,'maxcapacity', '='), 10);
|
||||
result.currentcapacity = parseInt('0' + util.getValue(lines,'currentcapacity', '='), 10);
|
||||
let percent = -1;
|
||||
const line = util.getValue(lines,'internal', 'Battery');
|
||||
let parts = line.split(';');
|
||||
if (parts && parts[0]) {
|
||||
let parts2 = parts[0].split('\t');
|
||||
if (parts2 && parts2[1]) {
|
||||
percent = parseFloat(parts2[1].trim().replace('%', ''));
|
||||
}
|
||||
}
|
||||
if (result.maxcapacity && result.currentcapacity) {
|
||||
result.hasbattery = true;
|
||||
result.percent = percent !== -1 ? percent : Math.round(100.0 * result.currentcapacity / result.maxcapacity);
|
||||
}
|
||||
}
|
||||
if (result.maxcapacity && result.currentcapacity) {
|
||||
result.hasbattery = true;
|
||||
result.percent = percent !== -1 ? percent : 100.0 * result.currentcapacity / result.maxcapacity;
|
||||
}
|
||||
if (callback) { callback(result); }
|
||||
resolve(result);
|
||||
|
||||
@ -73,7 +73,7 @@ function getValue(lines, property, separator, trimmed) {
|
||||
const parts = lines[i].split(separator);
|
||||
if (parts.length >= 2) {
|
||||
parts.shift();
|
||||
return parts.join(':').trim();
|
||||
return parts.join(separator).trim();
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user