optimized battery().ischarging for macOS

This commit is contained in:
Sebastian Hildebrandt 2018-04-05 21:37:26 +02:00
parent 0c61ef0443
commit 179d5835fd
3 changed files with 15 additions and 10 deletions

View File

@ -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 |

View File

@ -5,7 +5,7 @@ Fixes #
#### Changes proposed:
* [ ] Fix
* [ ] Add
* [ ] Enhancement
* [ ] Remove
* [ ] Update

View File

@ -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);