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 | | 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.37.0 | 2018-02-11 | extended FreeBSD support `networkStats()` |
| 3.36.0 | 2018-02-11 | extended FreeBSD support `networkConnections()` | | 3.36.0 | 2018-02-11 | extended FreeBSD support `networkConnections()` |
| 3.35.0 | 2018-02-11 | extended FreeBSD support `processLoad()` | | 3.35.0 | 2018-02-11 | extended FreeBSD support `processLoad()` |

View File

@ -97,24 +97,26 @@ module.exports = function (callback) {
} }
if (_darwin) { if (_darwin) {
exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|MaxCapacity|CurrentCapacity";pmset -g batt | grep %', function (error, stdout) { 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'); if (stdout) {
result.cyclecount = parseInt('0' + util.getValue(lines,'cyclecount', '='), 10); let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n');
result.ischarging = util.getValue(lines,'ischarging', '=').toLowerCase() === 'yes'; result.cyclecount = parseInt('0' + util.getValue(lines,'cyclecount', '='), 10);
result.maxcapacity = parseInt('0' + util.getValue(lines,'maxcapacity', '='), 10); result.ischarging = util.getValue(lines,'ischarging', '=').toLowerCase() === 'yes';
result.currentcapacity = parseInt('0' + util.getValue(lines,'currentcapacity', '='), 10); result.maxcapacity = parseInt('0' + util.getValue(lines,'maxcapacity', '='), 10);
let percent = -1; result.currentcapacity = parseInt('0' + util.getValue(lines,'currentcapacity', '='), 10);
const line = util.getValue(lines,'internalbattery', '='); let percent = -1;
let parts = line.split(';'); const line = util.getValue(lines,'internal', 'Battery');
if (parts && parts[0]) { let parts = line.split(';');
let parts2 = parts[0].split('\t'); if (parts && parts[0]) {
if (parts2 && parts2[1]) { let parts2 = parts[0].split('\t');
percent = parseFloat(parts2[1].trim().replace('%', '')); if (parts2 && parts2[1]) {
percent = parseFloat(parts2[1].trim().replace('%', ''));
}
} }
} if (result.maxcapacity && result.currentcapacity) {
if (result.maxcapacity && result.currentcapacity) { result.hasbattery = true;
result.hasbattery = true; result.percent = percent !== -1 ? percent : Math.round(100.0 * result.currentcapacity / result.maxcapacity);
result.percent = percent !== -1 ? percent : 100.0 * result.currentcapacity / result.maxcapacity; }
} }
if (callback) { callback(result); } if (callback) { callback(result); }
resolve(result); resolve(result);

View File

@ -73,7 +73,7 @@ function getValue(lines, property, separator, trimmed) {
const parts = lines[i].split(separator); const parts = lines[i].split(separator);
if (parts.length >= 2) { if (parts.length >= 2) {
parts.shift(); parts.shift();
return parts.join(':').trim(); return parts.join(separator).trim();
} else { } else {
return ''; return '';
} }