From a459bc5ed63099e1157aa116f9148161bb180ff7 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 3 Apr 2018 20:40:47 +0200 Subject: [PATCH 01/10] optimized processes(), bugfix networkInterfaceDefault() --- CHANGELOG.md | 1 + lib/filesystem.js | 16 ++--- lib/graphics.js | 2 +- lib/network.js | 16 ++--- lib/processes.js | 168 ++++++++++++++++++++++++++++++++++++---------- lib/system.js | 6 +- 6 files changed, 153 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fc1f8c..d068844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 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 | | 3.37.6 | 2018-03-12 | updated docs: fixed `diskLayout`and `mamlayout` | diff --git a/lib/filesystem.js b/lib/filesystem.js index 3fb2fe6..a8cb83c 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -399,16 +399,16 @@ function fsStats(callback) { }); } if (_darwin) { - exec("ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n '/IOBlockStorageDriver/,/Statistics/p' | grep 'Statistics' | tr -cd '01234567890,\n' | awk -F',' '{print $3, $10}'", function (error, stdout) { + exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); lines.forEach(function (line) { line = line.trim(); if (line !== '') { - line = line.split(' '); + line = line.split(','); - rx += parseInt(line[0]); - wx += parseInt(line[1]); + rx += parseInt(line[2]); + wx += parseInt(line[9]); } }); result = calcFsSpeed(rx, wx); @@ -507,7 +507,7 @@ function disksIO(callback) { // prints Block layer statistics for all mounted volumes // var cmd = "for mount in `lsblk | grep / | sed -r 's/│ └─//' | cut -d ' ' -f 1`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done"; // var cmd = "for mount in `lsblk | grep / | sed 's/[│└─├]//g' | awk '{$1=$1};1' | cut -d ' ' -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done"; - let cmd = "for mount in `lsblk | grep ' disk ' | sed 's/[│└─├]//g' | awk '{$1=$1};1' | cut -d ' ' -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done"; + let cmd = 'for mount in `lsblk | grep " disk " | sed "s/[│└─├]//g" | awk \'{$1=$1};1\' | cut -d " " -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r "s/ +/;/g" | sed -r "s/^;//"; done'; exec(cmd, function (error, stdout) { if (!error) { @@ -536,15 +536,15 @@ function disksIO(callback) { }); } if (_darwin) { - exec("ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n '/IOBlockStorageDriver/,/Statistics/p' | grep 'Statistics' | tr -cd '01234567890,\n' | awk -F',' '{print $1, $11}'", function (error, stdout) { + exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); lines.forEach(function (line) { line = line.trim(); if (line !== '') { - line = line.split(' '); + line = line.split(','); - rIO += parseInt(line[1]); + rIO += parseInt(line[10]); wIO += parseInt(line[0]); } }); diff --git a/lib/graphics.js b/lib/graphics.js index 6987620..44fc4ae 100644 --- a/lib/graphics.js +++ b/lib/graphics.js @@ -306,7 +306,7 @@ function graphics(callback) { let lines = stdout.toString().split('\n'); result.controllers = parseLinesLinuxControllers(lines); } - let cmd = "xdpyinfo 2>/dev/null | grep 'depth of root window' | awk '{ print $5 }'"; + let cmd = 'xdpyinfo 2>/dev/null | grep \'depth of root window\' | awk \'{ print $5 }\''; exec(cmd, function (error, stdout) { let depth = 0; if (!error) { diff --git a/lib/network.js b/lib/network.js index 5f541ca..10b0913 100644 --- a/lib/network.js +++ b/lib/network.js @@ -54,8 +54,8 @@ function getDefaultNetworkInterface() { } if (_linux || _darwin || _freebsd || _openbsd) { let cmd = ''; - if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"'; - if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk "{print $2}"'; + if (_linux) cmd = 'route 2>/dev/null | grep default | awk \'{print $8}\''; + if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk \'{print $2}\''; if (_freebsd || _openbsd) cmd = 'route get 0.0.0.0 | grep interface:'; let result = execSync(cmd); ifacename = result.toString().split('\n')[0]; @@ -88,7 +88,7 @@ function getMacAddresses() { for (let i = 0; i < lines.length; i++) { if (lines[i] && lines[i][0] !== ' ') { if (isIpAvailable) { - let nextline = lines[i+1].trim().split(' '); + let nextline = lines[i + 1].trim().split(' '); if (nextline[0] === 'link/ether') { iface = lines[i].split(' ')[1]; iface = iface.slice(0, iface.length - 1); @@ -224,7 +224,7 @@ function calcNetworkSpeed(iface, rx, tx, operstate) { function networkStats(iface, callback) { - function parseLinesWindowsNics(sections){ + function parseLinesWindowsNics(sections) { let nics = []; for (let i in sections) { if (sections.hasOwnProperty(i)) { @@ -245,7 +245,7 @@ function networkStats(iface, callback) { return nics; } - function parseLinesWindowsPerfData(sections){ + function parseLinesWindowsPerfData(sections) { let perfData = []; for (let i in sections) { if (sections.hasOwnProperty(i)) { @@ -254,8 +254,8 @@ function networkStats(iface, callback) { let lines = sections[i].trim().split('\r\n'); perfData.push({ name: util.getValue(lines, 'Name', '=').replace(/[()\[\] ]+/g, '').toLowerCase(), - rx: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='),10), - tx: parseInt(util.getValue(lines, 'BytesSentPersec', '='),10) + rx: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='), 10), + tx: parseInt(util.getValue(lines, 'BytesSentPersec', '='), 10) }); } } @@ -443,7 +443,7 @@ function networkConnections(callback) { let result = []; if (_linux || _freebsd || _openbsd) { let cmd = 'netstat -tuna | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"'; - if (_freebsd || _openbsd) cmd = 'netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"' + if (_freebsd || _openbsd) cmd = 'netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"'; exec(cmd, function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); diff --git a/lib/processes.js b/lib/processes.js index ee72aa2..e761f50 100644 --- a/lib/processes.js +++ b/lib/processes.js @@ -24,10 +24,6 @@ const _windows = (_platform === 'win32'); const _freebsd = (_platform === 'freebsd'); const _openbsd = (_platform === 'openbsd'); -const opts = { - windowsHide: true -}; - const NOT_SUPPORTED = 'not supported'; let _process_cpu = { @@ -54,7 +50,7 @@ let _winStatusValues = { function parseTimeWin(time) { time = time || ''; if (time) { - return (time.substr(0,4) + '-' + time.substr(4,2) + '-' + time.substr(6,2) + ' ' + time.substr(8,2) + ':' + time.substr(10,2) + ':' + time.substr(12,2)); + return (time.substr(0, 4) + '-' + time.substr(4, 2) + '-' + time.substr(6, 2) + ' ' + time.substr(8, 2) + ':' + time.substr(10, 2) + ':' + time.substr(12, 2)); } else { return ''; } @@ -81,11 +77,11 @@ function services(srv, callback) { let srvs = srv.split('|'); let data = []; let dataSrv = []; - + if (_linux || _freebsd || _openbsd || _darwin) { - let comm = (_darwin) ? 'ps -caxm -o pcpu,pmem,comm' : 'ps axo pcpu,pmem,comm'; + let comm = (_darwin) ? 'ps -caxo pcpu,pmem,comm' : 'ps -axo pcpu,pmem,comm'; if (srv !== '' && srvs.length > 0) { - exec(comm + " | grep -v grep | egrep '" + srv + "'", function (error, stdout) { + exec(comm + ' | grep -v grep | egrep "' + srv + '"', function (error, stdout) { if (!error) { let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n'); srvs.forEach(function (srv) { @@ -106,16 +102,35 @@ function services(srv, callback) { if (callback) { callback(data); } resolve(data); } else { - srvs.forEach(function (srv) { - data.push({ - 'name': srv, - 'running': false, - 'pcpu': 0, - 'pmem': 0 - }); + exec('ps -o comm | grep -v grep | egrep "' + srv + '"', function (error, stdout) { + if (!error) { + let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n'); + srvs.forEach(function (srv) { + let ps = lines.filter(function (e) { + return e.indexOf(srv) !== -1; + }); + data.push({ + 'name': srv, + 'running': ps.length > 0, + 'pcpu': 0, + 'pmem': 0 + }); + }); + if (callback) { callback(data); } + resolve(data); + } else { + srvs.forEach(function (srv) { + data.push({ + 'name': srv, + 'running': false, + 'pcpu': 0, + 'pmem': 0 + }); + }); + if (callback) { callback(data); } + resolve(data); + } }); - if (callback) { callback(data); } - resolve(data); } }); } else { @@ -124,7 +139,7 @@ function services(srv, callback) { } } if (_windows) { - exec(util.getWmic() + ' service get /value', {maxBuffer: 1024 * 1000, windowsHide: true}, function (error, stdout) { + exec(util.getWmic() + ' service get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) { if (!error) { let serviceSections = stdout.split(/\n\s*\n/); for (let i = 0; i < serviceSections.length; i++) { @@ -140,7 +155,7 @@ function services(srv, callback) { 'pmem': 0 }); dataSrv.push(srv); - } + } } } let srvsMissing = srvs.filter(function (e) { @@ -154,7 +169,7 @@ function services(srv, callback) { 'pmem': 0 }); }); - + if (callback) { callback(data); } resolve(data); } else { @@ -170,7 +185,7 @@ function services(srv, callback) { resolve(data); } }); - } + } } else { if (callback) { callback({}); } resolve({}); @@ -239,7 +254,7 @@ function processes(callback) { if (result.substr(-1) === ':') { result = result.substr(0, result.length - 1); } - if (result.substr(0,1) !== '[') { + if (result.substr(0, 1) !== '[') { let parts = result.split('/'); result = parts[parts.length - 1]; } @@ -315,6 +330,50 @@ function processes(callback) { } return result; } + function parseProcesses2(lines) { + + function formatDateTime(time) { + const month = ('0' + (time.getMonth() + 1).toString()).substr(-2); + const year = time.getFullYear().toString(); + const day = ('0' + time.getDay().toString()).substr(-2); + const hours = time.getHours().toString(); + const mins = time.getMinutes().toString(); + const secs = ('0' + time.getSeconds().toString()).substr(-2); + + return (year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + secs); + } + + let result = []; + lines.forEach(function (line) { + if (line.trim() !== '') { + line = line.trim().replace(/ +/g, ' ').replace(/,+/g, '.'); + const parts = line.split(' '); + const command = parts.slice(8).join(' '); + const pmem = parseFloat((1.0 * parseInt(parts[2]) * 1024 / os.totalmem()).toFixed(1)); + const elapsed_parts = parts[4].split(':'); + const started = formatDateTime(new Date(Date.now() - (elapsed_parts.length > 1 ? (elapsed_parts[0] * 60 + elapsed_parts[1]) * 1000 : elapsed_parts[0] * 1000))); + + result.push({ + pid: parseInt(parts[0]), + name: getName(command), + pcpu: 0, + pcpuu: 0, + pcpus: 0, + pmem: pmem, + priority: 0, + mem_vsz: parseInt(parts[1]), + mem_rss: parseInt(parts[2]), + nice: parseInt(parts[3]), + started: started, + state: (parts[5] === 'R' ? 'running' : (parts[5] === 'S' ? 'sleeping' : (parts[5] === 'T' ? 'stopped' : (parts[5] === 'W' ? 'paging' : (parts[5] === 'X' ? 'dead' : (parts[5] === 'Z' ? 'zombie' : ((parts[5] === 'D' || parts[5] === 'U') ? 'blocked' : 'unknown'))))))), + tty: parts[6], + user: parts[7], + command: command + }); + } + }); + return result; + } function parseProcStat(line) { let parts = line.replace(/ +/g, ' ').split(' '); @@ -420,9 +479,9 @@ function processes(callback) { if ((_process_cpu.ms && Date.now() - _process_cpu.ms >= 500) || _process_cpu.ms === 0) { if (_linux || _freebsd || _openbsd || _darwin) { - if (_linux) cmd = 'ps axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,ni:5,start:20,state:20,tty:20,user:20,command'; - if (_freebsd || _openbsd) cmd = 'ps axo pid,pcpu,pmem,pri,vsz,rss,ni,start,state,tty,user,command'; - if (_darwin) cmd = 'ps acxo pid,pcpu,pmem,pri,vsz,rss,nice,start,state,tty,user,command -r'; + if (_linux) cmd = 'ps -axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,ni:5,start:20,state:20,tty:20,user:20,command'; + if (_freebsd || _openbsd) cmd = 'ps -axo pid,pcpu,pmem,pri,vsz,rss,ni,start,state,tty,user,command'; + if (_darwin) cmd = 'ps -acxo pid,pcpu,pmem,pri,vsz,rss,nice,start,state,tty,user,command -r'; exec(cmd, function (error, stdout) { if (!error) { result.list = parseProcesses(stdout.toString().split('\n')); @@ -489,11 +548,36 @@ function processes(callback) { if (callback) { callback(result); } resolve(result); } + } else { + cmd = 'ps -o pid,vsz,rss,nice,etime,stat,tty,user,comm'; + exec(cmd, function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + lines.shift(); + + result.list = parseProcesses2(lines); + result.all = result.list.length; + result.running = result.list.filter(function (e) { + return e.state === 'running'; + }).length; + result.blocked = result.list.filter(function (e) { + return e.state === 'blocked'; + }).length; + result.sleeping = result.list.filter(function (e) { + return e.state === 'sleeping'; + }).length; + if (callback) { callback(result); } + resolve(result); + } else { + if (callback) { callback(result); } + resolve(result); + } + }); } }); } if (_windows) { - exec(util.getWmic() + ' process get /value', {maxBuffer: 1024 * 1000, windowsHide: true}, function (error, stdout) { + exec(util.getWmic() + ' process get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) { if (!error) { let processSections = stdout.split(/\n\s*\n/); let procs = []; @@ -617,18 +701,30 @@ function processLoad(proc, callback) { }; if (proc) { - exec('ps aux | grep ' + proc + ' | grep -v grep', function (error, stdout) { + exec('ps -axo pid,pcpu,pmem,comm | grep ' + proc + ' | grep -v grep', function (error, stdout) { if (!error) { - let data = stdout.replace(/ +/g, ' ').split(' '); + let lines = stdout.toString().split('\n'); - if (data.length > 2) { - result = { - 'proc': proc, - 'pid': data[1], - 'cpu': parseFloat(data[2].replace(',', '.')), - 'mem': parseFloat(data[3].replace(',', '.')) - }; - } + let pid = 0; + let cpu = 0; + let mem = 0; + + lines.forEach(function (line) { + let data = line.replace(/ +/g, ' ').split(' '); + if (data.length > 3) { + pid = (!pid ? parseInt(data[0]) : 0); + cpu = cpu + parseFloat(data[1].replace(',', '.')); + mem = mem + parseFloat(data[2].replace(',', '.')); + } + }); + // TODO: calc process_cpu - ps is not accurate in linux! + + result = { + 'proc': proc, + 'pid': pid, + 'cpu': parseFloat((cpu / lines.length).toFixed(2)), + 'mem': parseFloat((mem / lines.length).toFixed(2)) + }; } if (callback) { callback(result); } resolve(result); diff --git a/lib/system.js b/lib/system.js index 726cbb9..42f0ca8 100644 --- a/lib/system.js +++ b/lib/system.js @@ -164,7 +164,7 @@ function system(callback) { } if (_windows) { // exec('wmic csproduct get', function (error, stdout) { - // ToDo: refactor /value + // TODO: refactor /value exec(util.getWmic() + ' csproduct get /value', opts, function (error, stdout) { if (!error) { // let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0)[0].trim().split(/\s\s+/); @@ -225,7 +225,7 @@ function bios(callback) { resolve(result); } if (_windows) { - // ToDo: check BIOS windows + // TODO: check BIOS windows exec(util.getWmic() + ' bios get /value', opts, function (error, stdout) { if (!error) { let lines = stdout.toString().split('\r\n'); @@ -311,7 +311,7 @@ function baseboard(callback) { }); } if (_windows) { - // ToDo: check BIOS windows + // TODO: check BIOS windows exec(util.getWmic() + ' baseboard get /value', opts, function (error, stdout) { if (!error) { let lines = stdout.toString().split('\r\n'); From 0c61ef0443795cdfc1b71a13aaf3f63f06bc3fe9 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 3 Apr 2018 20:40:55 +0200 Subject: [PATCH 02/10] 3.37.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a4b3be3..f939f4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "systeminformation", - "version": "3.37.8", + "version": "3.37.9", "description": "Simple system and OS information library", "license": "MIT", "author": "Sebastian Hildebrandt (https://plus-innovations.com)", From 179d5835fdfb7f6866517b804b985327effab7cf Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 5 Apr 2018 21:37:26 +0200 Subject: [PATCH 03/10] 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); From 8e599018fe72f0e8f75de4ba3304324d96e9ff9f Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 5 Apr 2018 21:37:34 +0200 Subject: [PATCH 04/10] 3.37.10 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f939f4d..3df3a68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "systeminformation", - "version": "3.37.9", + "version": "3.37.10", "description": "Simple system and OS information library", "license": "MIT", "author": "Sebastian Hildebrandt (https://plus-innovations.com)", From 306be41a012b89bfcaffaf93100dbfda2f983e3c Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 5 Apr 2018 22:46:53 +0200 Subject: [PATCH 05/10] another optimization battery().ischarging for macOS --- CHANGELOG.md | 1 + lib/battery.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cd23fe..98413b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.37.11 | 2018-04-05 | another optimization `battery().ischarging` for macOS | | 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 | diff --git a/lib/battery.js b/lib/battery.js index 5566033..c39f7f2 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -117,7 +117,7 @@ module.exports = function (callback) { } } if (parts && parts[1]) { - result.ischarging = parts[1].trim() !== 'discharging'; + result.ischarging = parts[1].trim() !== 'charged'; } else { result.ischarging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes'; } From 6d30c351fc62df23f26a7537bda19971b3e54385 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 5 Apr 2018 22:47:00 +0200 Subject: [PATCH 06/10] 3.37.11 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3df3a68..4f4016b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "systeminformation", - "version": "3.37.10", + "version": "3.37.11", "description": "Simple system and OS information library", "license": "MIT", "author": "Sebastian Hildebrandt (https://plus-innovations.com)", From c912bdc6fe48dca436d62f29b308ae25929761f1 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 5 Apr 2018 23:16:38 +0200 Subject: [PATCH 07/10] another optimization battery().ischarging for macOS --- CHANGELOG.md | 1 + lib/battery.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98413b1..414f999 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.37.12 | 2018-04-05 | another optimization `battery().ischarging` for macOS | | 3.37.11 | 2018-04-05 | another optimization `battery().ischarging` for macOS | | 3.37.10 | 2018-04-05 | `battery().ischarging` optimized for macOS | | 3.37.9 | 2018-04-03 | optimized `processes()`, bugfix `networkInterfaceDefault()` | diff --git a/lib/battery.js b/lib/battery.js index c39f7f2..0d245a8 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -117,7 +117,7 @@ module.exports = function (callback) { } } if (parts && parts[1]) { - result.ischarging = parts[1].trim() !== 'charged'; + result.ischarging = !(parts[1].trim() === 'charged' || parts[1].trim() === 'discharging'); } else { result.ischarging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes'; } From bab29ea573ee8791104084cfafe438e90f756ac2 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 5 Apr 2018 23:16:45 +0200 Subject: [PATCH 08/10] 3.37.12 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4f4016b..339703e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "systeminformation", - "version": "3.37.11", + "version": "3.37.12", "description": "Simple system and OS information library", "license": "MIT", "author": "Sebastian Hildebrandt (https://plus-innovations.com)", From d681955d08c68eaf7c3b31d3c6a9fab955defbd9 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Fri, 6 Apr 2018 00:36:33 +0200 Subject: [PATCH 09/10] added battery().acconnected --- CHANGELOG.md | 1 + README.md | 3 ++- lib/battery.js | 10 ++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 414f999..9ae5c3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.38.0 | 2018-04-06 | added `battery().acconnected` | | 3.37.12 | 2018-04-05 | another optimization `battery().ischarging` for macOS | | 3.37.11 | 2018-04-05 | another optimization `battery().ischarging` for macOS | | 3.37.10 | 2018-04-05 | `battery().ischarging` optimized for macOS | diff --git a/README.md b/README.md index 6973848..4986110 100644 --- a/README.md +++ b/README.md @@ -53,13 +53,13 @@ async function cpu() { ### Latest Activity (last 7 major and minor version releases) +- Version 3.38.0: added `battery.acconnected` - Version 3.37.0: extended FreeBSD support `networkStats()` - Version 3.36.0: extended FreeBSD support `networkConnections()` - Version 3.35.0: extended FreeBSD support `processLoad()` - Version 3.34.0: first partial FreeBSD support - Version 3.33.0: added bios `bios()` and main board `baseboard()` information - Version 3.32.0: extended `memLayout()` - added manufacturer -- Version 3.31.0: extended windows support `cpuFlags()` (partially) - ... You can find all changes here: [detailed changelog][changelog-url] @@ -196,6 +196,7 @@ 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 | +| | acconnected | X | X | X | X | AC connected | | 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 0d245a8..66049c3 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -38,7 +38,8 @@ module.exports = function (callback) { ischarging: false, maxcapacity: 0, currentcapacity: 0, - percent: 0 + percent: 0, + acconnected: true }; if (_linux) { @@ -53,6 +54,7 @@ module.exports = function (callback) { if (!error) { let lines = stdout.toString().split('\n'); if (lines.length > 0 && lines[0]) result.ischarging = (lines[0].trim().toLowerCase() === 'charging'); + result.acconnected = result.ischarging; } exec('cat ' + battery_path + 'cyclec_ount', function (error, stdout) { if (!error) { @@ -92,6 +94,7 @@ module.exports = function (callback) { result.hasbattery = (batteries > 0); result.cyclecount = -1; result.ischarging = util.getValue(lines, 'hw.acpi.acline') !== '1'; + result.acconnected = result.ischarging; result.maxcapacity = -1; result.currentcapacity = -1; result.percent = batteries ? percent : -1; @@ -117,9 +120,11 @@ module.exports = function (callback) { } } if (parts && parts[1]) { - result.ischarging = !(parts[1].trim() === 'charged' || parts[1].trim() === 'discharging'); + result.ischarging = (parts[1].trim() === 'charging'); + result.acconnected = (parts[1].trim() !== 'discharging'); } else { result.ischarging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes'; + result.acconnected = result.ischarging; } if (result.maxcapacity && result.currentcapacity) { result.hasbattery = true; @@ -142,6 +147,7 @@ module.exports = function (callback) { result.percent = parseInt(util.getValue(lines, 'EstimatedChargeRemaining', '=') || 0); result.currentcapacity = parseInt(result.maxcapacity * result.percent / 100); result.ischarging = (status >= 6 && status <= 9) || (!(status === 3) && !(status === 1) && result.percent < 100); + result.acconnected = result.ischarging; } } if (callback) { callback(result); } From 900d503669191ae969753962950e9ad24a651fb9 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Fri, 6 Apr 2018 00:36:39 +0200 Subject: [PATCH 10/10] 3.38.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 339703e..1e2273e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "systeminformation", - "version": "3.37.12", + "version": "3.38.0", "description": "Simple system and OS information library", "license": "MIT", "author": "Sebastian Hildebrandt (https://plus-innovations.com)",