Merge pull request #201 from jonasmalacofilho/only-parse-sensors-json-once

Only parse the JSON from sensors once
This commit is contained in:
Vasilii
2021-04-28 18:34:02 +03:00
committed by GitHub
2 changed files with 27 additions and 18 deletions

View File

@@ -26,8 +26,8 @@ var CommandLineUtil = class {
} catch (e) { } catch (e) {
logError(e); logError(e);
} finally { } finally {
this._updated = true;
callback(); callback();
this._updated = true;
} }
}); });
} catch(e){ } catch(e){

View File

@@ -12,6 +12,30 @@ var SensorsUtil = class extends CommandLineUtil.CommandLineUtil {
this._argv = path ? [path, '-A', '-j'] : null; this._argv = path ? [path, '-A', '-j'] : null;
} }
// Avoid parsing the data more than once.
execute(callback) {
super.execute(() => {
let data = [];
try {
data = JSON.parse(this._output.join(''));
} catch (e) {
try {
// fix for wrong lm_sensors output
// https://github.com/UshakovVasilii/gnome-shell-extension-freon/issues/114#issuecomment-491613545
let lineRemoved = this._output.filter(l => l.trim() !== ',').join('\n');
let errorRemoved = lineRemoved.replace(/ERROR.*Can't read/, "");
errorRemoved = errorRemoved.replace(/ERROR.*I\/O error/, "");
data = JSON.parse(errorRemoved);
} catch (e) {
global.log(e.toString());
return [];
}
}
this._data = data;
callback();
});
}
get temp() { get temp() {
return this._parseGenericSensorsOutput(/^temp\d+_input/, 'temp'); return this._parseGenericSensorsOutput(/^temp\d+_input/, 'temp');
} }
@@ -37,25 +61,10 @@ var SensorsUtil = class extends CommandLineUtil.CommandLineUtil {
} }
_parseSensorsOutput(sensorFilter, sensorType, gpuFlag) { _parseSensorsOutput(sensorFilter, sensorType, gpuFlag) {
if(!this._output) if(!this._data)
return []; return [];
let data = [] const data = this._data;
try {
data = JSON.parse(this._output.join(''));
} catch (e) {
try {
// fix for wrong lm_sensors output
// https://github.com/UshakovVasilii/gnome-shell-extension-freon/issues/114#issuecomment-491613545
let lineRemoved = this._output.filter(l => l.trim() !== ',').join('\n');
let errorRemoved = lineRemoved.replace(/ERROR.*Can't read/, "");
errorRemoved = errorRemoved.replace(/ERROR.*I\/O error/, "");
data = JSON.parse(errorRemoved);
} catch (e) {
global.log(e.toString());
return [];
}
}
let sensors = []; let sensors = [];
for (var chipset in data) { for (var chipset in data) {