fixed bug battery().percent for macOS

This commit is contained in:
Sebastian Hildebrandt 2018-02-15 19:50:21 +01:00
parent ab894f1a6e
commit bf7eec239b
3 changed files with 22 additions and 19 deletions

View File

@ -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()` |

View File

@ -98,13 +98,14 @@ module.exports = function (callback) {
if (_darwin) {
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,'internalbattery', '=');
const line = util.getValue(lines,'internal', 'Battery');
let parts = line.split(';');
if (parts && parts[0]) {
let parts2 = parts[0].split('\t');
@ -114,7 +115,8 @@ module.exports = function (callback) {
}
if (result.maxcapacity && result.currentcapacity) {
result.hasbattery = true;
result.percent = percent !== -1 ? percent : 100.0 * result.currentcapacity / result.maxcapacity;
result.percent = percent !== -1 ? percent : Math.round(100.0 * result.currentcapacity / result.maxcapacity);
}
}
if (callback) { callback(result); }
resolve(result);

View File

@ -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 '';
}