From 179d5835fdfb7f6866517b804b985327effab7cf Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 5 Apr 2018 21:37:26 +0200 Subject: [PATCH] optimized battery().ischarging for macOS --- CHANGELOG.md | 1 + docs/PULL_REQUEST_TEMPLATE.md | 2 +- lib/battery.js | 22 +++++++++++++--------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d068844..3cd23fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.37.10 | 2018-04-05 | `battery().ischarging` optimized for macOS | | 3.37.9 | 2018-04-03 | optimized `processes()`, bugfix `networkInterfaceDefault()` | | 3.37.8 | 2018-03-25 | optimized `networkDefaultInterface()` detection, fixed network `operstate` MacOS | | 3.37.7 | 2018-03-13 | celebrating 4th birthday | diff --git a/docs/PULL_REQUEST_TEMPLATE.md b/docs/PULL_REQUEST_TEMPLATE.md index 9667a73..69104a4 100644 --- a/docs/PULL_REQUEST_TEMPLATE.md +++ b/docs/PULL_REQUEST_TEMPLATE.md @@ -5,7 +5,7 @@ Fixes # #### Changes proposed: * [ ] Fix -* [ ] Add +* [ ] Enhancement * [ ] Remove * [ ] Update diff --git a/lib/battery.js b/lib/battery.js index 4933098..5566033 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -87,11 +87,11 @@ module.exports = function (callback) { if (_freebsd || _openbsd) { exec('sysctl hw.acpi.battery hw.acpi.acline', function (error, stdout) { let lines = stdout.toString().split('\n'); - const batteries = parseInt('0' + util.getValue(lines,'hw.acpi.battery.units'), 10); - const percent = parseInt('0' + util.getValue(lines,'hw.acpi.battery.life'), 10); + const batteries = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.units'), 10); + const percent = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.life'), 10); result.hasbattery = (batteries > 0); result.cyclecount = -1; - result.ischarging = util.getValue(lines,'hw.acpi.acline') !== '1'; + result.ischarging = util.getValue(lines, 'hw.acpi.acline') !== '1'; result.maxcapacity = -1; result.currentcapacity = -1; result.percent = batteries ? percent : -1; @@ -104,12 +104,11 @@ module.exports = function (callback) { 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); + 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); let percent = -1; - const line = util.getValue(lines,'internal', 'Battery'); + const line = util.getValue(lines, 'internal', 'Battery'); let parts = line.split(';'); if (parts && parts[0]) { let parts2 = parts[0].split('\t'); @@ -117,10 +116,15 @@ module.exports = function (callback) { percent = parseFloat(parts2[1].trim().replace('%', '')); } } + if (parts && parts[1]) { + result.ischarging = parts[1].trim() !== 'discharging'; + } else { + result.ischarging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes'; + } 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);