cpuTemperature() optimized parsing linux
This commit is contained in:
parent
894cd355b3
commit
6b19ac674f
@ -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 |
|
||||
|
||||
@ -508,7 +508,7 @@ si.cpuCurrentspeed().then(data => console.log(data));</code></pre class="example
|
||||
<td colspan="7">
|
||||
<h5>Example</h5>
|
||||
<pre><code class="js">const si = require('systeminformation');
|
||||
si.cpuCurrentspeed().then(data => console.log(data));</code></pre class="example">
|
||||
si.cpuTemperature().then(data => console.log(data));</code></pre class="example">
|
||||
<pre class="example">
|
||||
{ main: 42, cores: [], max: 42 }
|
||||
</pre>
|
||||
|
||||
@ -83,6 +83,11 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th scope="row">4.23.10</th>
|
||||
<td>2020-05-01</td>
|
||||
<td><span class="code">cpuTemperature()</span> optimized parsing linux</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">4.23.9</th>
|
||||
<td>2020-04-29</td>
|
||||
|
||||
@ -168,7 +168,7 @@
|
||||
<img class="logo" src="assets/logo.png">
|
||||
<div class="title">systeminformation</div>
|
||||
<div class="subtitle"><span id="typed"></span></div>
|
||||
<div class="version">Current Version: <span id="version">4.23.9</span></div>
|
||||
<div class="version">Current Version: <span id="version">4.23.10</span></div>
|
||||
<button class="btn btn-light" onclick="location.href='https://github.com/sebhildebrandt/systeminformation'">View on Github <i class=" fab fa-github"></i></button>
|
||||
</div>
|
||||
<div class="down">
|
||||
|
||||
116
lib/cpu.js
116
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;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user