diff --git a/CHANGELOG.md b/CHANGELOG.md index d2f98b8..b5f26a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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()` | diff --git a/lib/battery.js b/lib/battery.js index 2c514b3..eaacfff 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -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 : 100.0 * result.currentcapacity / result.maxcapacity; + if (result.maxcapacity && result.currentcapacity) { + result.hasbattery = true; + result.percent = percent !== -1 ? percent : Math.round(100.0 * result.currentcapacity / result.maxcapacity); + } } if (callback) { callback(result); } resolve(result); diff --git a/lib/util.js b/lib/util.js index aad4f9c..9081894 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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 ''; }