cpuTemperature() optimized parsing linux

This commit is contained in:
Sebastian Hildebrandt 2020-05-01 10:23:23 +02:00
parent 894cd355b3
commit 6b19ac674f
5 changed files with 81 additions and 45 deletions

View File

@ -30,6 +30,7 @@ For major (breaking) changes - version 3 and 2 see end of page.
| Version | Date | Comment | | 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.9 | 2020-04-29 | `currentLoad()` workarround for no os.cpus info |
| 4.23.8 | 2020-04-26 | `getMacAddresses()` fix added try catch | | 4.23.8 | 2020-04-26 | `getMacAddresses()` fix added try catch |
| 4.23.7 | 2020-04-26 | `getCpuCurrentSpeedSync()` workarround fix | | 4.23.7 | 2020-04-26 | `getCpuCurrentSpeedSync()` workarround fix |

View File

@ -508,7 +508,7 @@ si.cpuCurrentspeed().then(data => console.log(data));</code></pre class="example
<td colspan="7"> <td colspan="7">
<h5>Example</h5> <h5>Example</h5>
<pre><code class="js">const si = require('systeminformation'); <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"> <pre class="example">
{ main: 42, cores: [], max: 42 } { main: 42, cores: [], max: 42 }
</pre> </pre>

View File

@ -83,6 +83,11 @@
</tr> </tr>
</thead> </thead>
<tbody> <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> <tr>
<th scope="row">4.23.9</th> <th scope="row">4.23.9</th>
<td>2020-04-29</td> <td>2020-04-29</td>

View File

@ -168,7 +168,7 @@
<img class="logo" src="assets/logo.png"> <img class="logo" src="assets/logo.png">
<div class="title">systeminformation</div> <div class="title">systeminformation</div>
<div class="subtitle"><span id="typed"></span></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> <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>
<div class="down"> <div class="down">

View File

@ -757,56 +757,85 @@ function cpuTemperature(callback) {
max: -1.0 max: -1.0
}; };
if (_linux) { 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) { if (!error) {
let lines = stdout.toString().split('\n'); let parts = stdout.toString().split('---');
lines.forEach(function (line) { let labels = parts[0].split('\n');
let regex = /[+-]([^°]*)/g; let temps = parts[1].split('\n');
let temps = line.match(regex); temps.shift();
let firstPart = line.split(':')[0].toUpperCase(); for (let i = 0; i < temps.length; i++) {
if (firstPart.indexOf('PHYSICAL') !== -1 || firstPart.indexOf('PACKAGE') !== -1) { if (temps[i] && (labels[i] === undefined || (labels[i] && labels[i].toLowerCase().startsWith('core')))) {
result.main = parseFloat(temps); 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.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); let maxtmp = Math.max.apply(Math, result.cores);
result.max = (maxtmp > result.main) ? maxtmp : result.main; result.max = (maxtmp > result.main) ? maxtmp : result.main;
} }
if (callback) { callback(result); } if (result.main !== -1) {
resolve(result); if (callback) { callback(result); }
} else { resolve(result);
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);
});
}
});
} }
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) { if (_freebsd || _openbsd || _netbsd) {
@ -1327,3 +1356,4 @@ function fullLoad(callback) {
} }
exports.fullLoad = fullLoad; exports.fullLoad = fullLoad;