diff --git a/CHANGELOG.md b/CHANGELOG.md index e671d69..537fdba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.44.0 | 2018-08-25 | `battery()` bugfix & added type, model, manufacturer, serial | | 3.43.0 | 2018-08-25 | `cpuCurrentspeed()` added cpu speed for all cores | | 3.42.10 | 2018-08-25 | `processes()` optimized start time parsing | | 3.42.9 | 2018-08-08 | `cpuTemperature()` optimized parsing | diff --git a/README.md b/README.md index f789012..9e77faf 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,13 @@ async function cpu() { (last 7 major and minor version releases) +- Version 3.44.0: `battery()` added type, model, manufacturer, serial, timeremaining - Version 3.43.0: added speed per CPU core `cpuCurrentspeed()` - Version 3.42.0: added parent process PID `processes()` - Version 3.41.0: first partial `SunOS` support - Version 3.40.0: extended `versions()` (php, redis, mongodb) - Version 3.39.0: added `versions().mysql` and `versions().nginx`, start implementing `SunOS` support - Version 3.38.0: added `battery.acconnected` -- Version 3.37.0: extended FreeBSD support `networkStats()` - ... You can find all changes here: [detailed changelog][changelog-url] @@ -198,7 +198,12 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | | maxcapacity | X | | X | X | | max capacity of battery | | | currentcapacity | X | | X | X | | current capacity of battery | | | percent | X | X | X | X | | charging level in percent | +| | timeremaining | X | | X | | | minutes left (if discharging) | | | acconnected | X | X | X | X | | AC connected | +| | type | X | | X | | | battery type | +| | model | X | | X | | | model | +| | manufacturer | X | | X | | | manufacturer | +| | serial | X | | X | | | battery serial | | si.graphics(cb) | {...} | X | | X | X | | arrays of graphics controllers and displays | | | controllers[]| X | | X | X | | graphics controllers array | | | ...[0].model | X | | X | X | | graphics controller model | diff --git a/lib/battery.js b/lib/battery.js index ee70f04..9609e0e 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -40,47 +40,58 @@ module.exports = function (callback) { maxcapacity: 0, currentcapacity: 0, percent: 0, - acconnected: true + timeremaining: -1, + acconnected: true, + type: '', + model: '', + manufacturer: '', + serial: '' }; if (_linux) { let battery_path = ''; - if (fs.existsSync('/sys/class/power_supply/BAT1/status')) { + if (fs.existsSync('/sys/class/power_supply/BAT1/uevent')) { battery_path = '/sys/class/power_supply/BAT1/'; - } else if (fs.existsSync('/sys/class/power_supply/BAT0/status')) { + } else if (fs.existsSync('/sys/class/power_supply/BAT0/uevent')) { battery_path = '/sys/class/power_supply/BAT0/'; } if (battery_path) { - exec('cat ' + battery_path + 'status', function (error, stdout) { + exec('cat ' + battery_path + 'uevent', function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); - if (lines.length > 0 && lines[0]) result.ischarging = (lines[0].trim().toLowerCase() === 'charging'); + + result.ischarging = (util.getValue(lines, 'POWER_SUPPLY_STATUS', '=').toLowerCase() === 'charging'); result.acconnected = result.ischarging; - } - exec('cat ' + battery_path + 'cyclec_ount', function (error, stdout) { - if (!error) { - let lines = stdout.toString().split('\n'); - if (lines.length > 0 && lines[0]) result.cyclecount = parseFloat(lines[0].trim()); + result.cyclecount = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CYCLE_COUNT', '='), 10); + result.maxcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '='), 10); + if (!result.maxcapacity) { + result.maxcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL', '='), 10); } - exec('cat ' + battery_path + 'charge_full', function (error, stdout) { - if (!error) { - let lines = stdout.toString().split('\n'); - if (lines.length > 0 && lines[0]) result.maxcapacity = parseFloat(lines[0].trim()); + result.currentcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10); + if (!result.currentcapacity) { + result.currentcapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10); + } + const percent = util.getValue(lines, 'POWER_SUPPLY_CAPACITY', '='); + const energy = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10); + const power = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_POWER_NOW', '='), 10); + + result.percent = parseInt('0' + percent, 10); + if (result.maxcapacity && result.currentcapacity) { + result.hasbattery = true; + if (!percent) { + result.percent = 100.0 * result.currentcapacity / result.maxcapacity; } - exec('cat ' + battery_path + 'charge_now', function (error, stdout) { - if (!error) { - let lines = stdout.toString().split('\n'); - if (lines.length > 0 && lines[0]) result.currentcapacity = parseFloat(lines[0].trim()); - } - if (result.maxcapacity && result.currentcapacity) { - result.hasbattery = true; - result.percent = 100.0 * result.currentcapacity / result.maxcapacity; - } - if (callback) { callback(result); } - resolve(result); - }); - }); - }); + } + if (energy && power) { + result.timeremaining = Math.floor(energy / power * 60); + } + result.type = util.getValue(lines, 'POWER_SUPPLY_TECHNOLOGY', '='); + result.model = util.getValue(lines, 'POWER_SUPPLY_MODEL_NAME', '='); + result.manufacturer = util.getValue(lines, 'POWER_SUPPLY_MANUFACTURER', '='); + result.serial = util.getValue(lines, 'POWER_SUPPLY_SERIAL_NUMBER', '='); + if (callback) { callback(result); } + resolve(result); + } }); } else { if (callback) { callback(result); } @@ -105,12 +116,14 @@ module.exports = function (callback) { } 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|BatterySerialNumber|TimeRemaining"; 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.maxcapacity = parseInt('0' + util.getValue(lines, 'maxcapacity', '='), 10); result.currentcapacity = parseInt('0' + util.getValue(lines, 'currentcapacity', '='), 10); + result.manufacturer = 'Apple'; + result.serial = util.getValue(lines, 'BatterySerialNumber', '='); let percent = -1; const line = util.getValue(lines, 'internal', 'Battery'); let parts = line.split(';'); @@ -129,7 +142,11 @@ module.exports = function (callback) { } if (result.maxcapacity && result.currentcapacity) { result.hasbattery = true; + result.type = 'Li-ion'; result.percent = percent !== -1 ? percent : Math.round(100.0 * result.currentcapacity / result.maxcapacity); + if (!result.ischarging) { + result.timeremaining = parseInt('0' + util.getValue(lines, 'TimeRemaining', '='), 10); + } } } if (callback) { callback(result); } @@ -161,7 +178,7 @@ module.exports = function (callback) { }); } catch (e) { if (callback) { callback(result); } - resolve(result); + resolve(result); } } });