diff --git a/CHANGELOG.md b/CHANGELOG.md index 926fc75..ee61bd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ For major (breaking) changes - version 3 and 2 see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | +| 4.23.10 | 2020-05-01 | `cpuTemperature()` optimized parsing linux | | 4.23.9 | 2020-04-29 | `currentLoad()` workarround for no os.cpus info | | 4.23.8 | 2020-04-26 | `getMacAddresses()` fix added try catch | | 4.23.7 | 2020-04-26 | `getCpuCurrentSpeedSync()` workarround fix | diff --git a/docs/cpu.html b/docs/cpu.html index 27b5edd..64b29e6 100644 --- a/docs/cpu.html +++ b/docs/cpu.html @@ -508,7 +508,7 @@ si.cpuCurrentspeed().then(data => console.log(data));
Example
const si = require('systeminformation');
-si.cpuCurrentspeed().then(data => console.log(data));
+si.cpuTemperature().then(data => console.log(data));
 { main: 42, cores: [], max: 42 }
 
diff --git a/docs/history.html b/docs/history.html index d6565d6..698ee92 100644 --- a/docs/history.html +++ b/docs/history.html @@ -83,6 +83,11 @@ + + 4.23.10 + 2020-05-01 + cpuTemperature() optimized parsing linux + 4.23.9 2020-04-29 diff --git a/docs/index.html b/docs/index.html index d44e651..7153e9a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -168,7 +168,7 @@
systeminformation
-
Current Version: 4.23.9
+
Current Version: 4.23.10
diff --git a/lib/cpu.js b/lib/cpu.js index 17e2f15..ffec10d 100644 --- a/lib/cpu.js +++ b/lib/cpu.js @@ -757,56 +757,85 @@ function cpuTemperature(callback) { max: -1.0 }; if (_linux) { - exec('sensors', function (error, stdout) { + const cmd = 'cat /sys/class/hwmon/hwmon1/temp*_la*;echo "---";cat /sys/class/hwmon/hwmon1/temp*_i*'; + exec(cmd, function (error, stdout) { if (!error) { - let lines = stdout.toString().split('\n'); - lines.forEach(function (line) { - let regex = /[+-]([^°]*)/g; - let temps = line.match(regex); - let firstPart = line.split(':')[0].toUpperCase(); - if (firstPart.indexOf('PHYSICAL') !== -1 || firstPart.indexOf('PACKAGE') !== -1) { - result.main = parseFloat(temps); + let parts = stdout.toString().split('---'); + let labels = parts[0].split('\n'); + let temps = parts[1].split('\n'); + temps.shift(); + for (let i = 0; i < temps.length; i++) { + if (temps[i] && (labels[i] === undefined || (labels[i] && labels[i].toLowerCase().startsWith('core')))) { + result.cores.push(Math.round(parseInt(temps[i], 10) / 100) / 10); + } else if (temps[i] && labels[i] && result.main === -1) { + result.main = Math.round(parseInt(temps[i], 10) / 100) / 10; } - if (firstPart.indexOf('CORE ') !== -1) { - result.cores.push(parseFloat(temps)); - } - }); + } if (result.cores.length > 0) { + if (result.main === -1) { + result.main = Math.round(result.cores.reduce((a, b) => a + b, 0) / result.cores.length); + } let maxtmp = Math.max.apply(Math, result.cores); result.max = (maxtmp > result.main) ? maxtmp : result.main; } - if (callback) { callback(result); } - resolve(result); - } else { - fs.stat('/sys/class/thermal/thermal_zone0/temp', function (err) { - if (err === null) { - fs.readFile('/sys/class/thermal/thermal_zone0/temp', function (error, stdout) { - if (!error) { - let lines = stdout.toString().split('\n'); - if (lines.length > 0) { - result.main = parseFloat(lines[0]) / 1000.0; - result.max = result.main; - } - } - if (callback) { callback(result); } - resolve(result); - }); - } else { - exec('/opt/vc/bin/vcgencmd measure_temp', function (error, stdout) { - if (!error) { - let lines = stdout.toString().split('\n'); - if (lines.length > 0 && lines[0].indexOf('=')) { - result.main = parseFloat(lines[0].split('=')[1]); - result.max = result.main; - } - } - if (callback) { callback(result); } - resolve(result); - }); - } - }); - + if (result.main !== -1) { + if (callback) { callback(result); } + resolve(result); + } } + exec('sensors', function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + lines.forEach(function (line) { + let regex = /[+-]([^°]*)/g; + let temps = line.match(regex); + let firstPart = line.split(':')[0].toUpperCase(); + if (firstPart.indexOf('PHYSICAL') !== -1 || firstPart.indexOf('PACKAGE') !== -1) { + result.main = parseFloat(temps); + } + if (firstPart.indexOf('CORE ') !== -1) { + result.cores.push(parseFloat(temps)); + } + }); + if (result.cores.length > 0) { + if (result.main === -1) { + result.main = Math.round(result.cores.reduce((a, b) => a + b, 0) / result.cores.length); + } + let maxtmp = Math.max.apply(Math, result.cores); + result.max = (maxtmp > result.main) ? maxtmp : result.main; + } + if (callback) { callback(result); } + resolve(result); + } else { + fs.stat('/sys/class/thermal/thermal_zone0/temp', function (err) { + if (err === null) { + fs.readFile('/sys/class/thermal/thermal_zone0/temp', function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + if (lines.length > 0) { + result.main = parseFloat(lines[0]) / 1000.0; + result.max = result.main; + } + } + if (callback) { callback(result); } + resolve(result); + }); + } else { + exec('/opt/vc/bin/vcgencmd measure_temp', function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + if (lines.length > 0 && lines[0].indexOf('=')) { + result.main = parseFloat(lines[0].split('=')[1]); + result.max = result.main; + } + } + if (callback) { callback(result); } + resolve(result); + }); + } + }); + } + }); }); } if (_freebsd || _openbsd || _netbsd) { @@ -1327,3 +1356,4 @@ function fullLoad(callback) { } exports.fullLoad = fullLoad; +