Utilize JSON parser for sensorUtil

This commit is contained in:
Thomas Ingvarsson
2018-09-20 23:05:32 +02:00
parent b3affd57dc
commit 4391db1c96

View File

@@ -11,136 +11,73 @@ var SensorsUtil = new Lang.Class({
_init: function() { _init: function() {
this.parent(); this.parent();
let path = GLib.find_program_in_path('sensors'); let path = GLib.find_program_in_path('sensors');
this._argv = path ? [path] : null; // -A: Do not show adapter -j: JSON output
this._argv = path ? [path, '-A', 'j'] : null;
}, },
get temp() { get temp() {
return this._parseGenericSensorsOutput(this._parseSensorsTemperatureLine); return this._parseGenericSensorsOutput(/^temp\d_input/);
// wrong lm_sensors not problem of this application #16
// return s.filter(function(e){
// return e.temp > 0 && e.temp < 115;
// });
}, },
get gpu() { get gpu() {
return this._parseGpuSensorsOutput(this._parseSensorsTemperatureLine); return this._parseGpuSensorsOutput(/^temp\d_input/);
}, },
get rpm() { get rpm() {
// 0 is normal value for turned off fan return this._parseGenericSensorsOutput(/^fan\d_input/);
return this._parseGenericSensorsOutput(this._parseFanRPMLine);
}, },
get volt() { get volt() {
return this._parseGenericSensorsOutput(this._parseVoltageLine); return this._parseGenericSensorsOutput(/^in\d_input/);
}, },
_parseGenericSensorsOutput: function(parser) { _parseGenericSensorsOutput: function(filter) {
return this._parseSensorsOutput(parser, false); return this._parseSensorsOutput(filter, false);
}, },
_parseGpuSensorsOutput: function(parser) { _parseGpuSensorsOutput: function(filter) {
return this._parseSensorsOutput(parser, true); return this._parseSensorsOutput(filter, true);
}, },
_parseSensorsOutput: function(parser, gpuFlag) { _parseSensorsOutput: function(filter, gpuFlag) {
if(!this._output) if(!this._output)
return []; return [];
let feature_label = undefined; // Prep output as one big string for JSON parser
let feature_value = undefined; let output = this._output.join('');
let sensors = [];
//iterate through each lines
for(let i = 0; i < this._output.length; i++){
let isGpuDriver = this._output[i].indexOf("radeon") != -1 let data = []
|| this._output[i].indexOf("amdgpu") != -1 try {
|| this._output[i].indexOf("nouveau") != -1; data = JSON.parse(output);
} catch (e) {
if (gpuFlag != isGpuDriver) { global.log(e.toString());
// skip driver if gpu requested and driver is not a gpu or the opposite return [];
continue;
}
// skip chipset driver name and 'Adapter:' lines
i += 2;
// get every feature of the chip
while(this._output[i]){
// if it is not a continutation of a feature line
if(this._output[i].indexOf(' ') != 0){
let feature = parser(feature_label, feature_value);
if (feature){
sensors.push(feature);
feature = undefined;
}
[feature_label, feature_value] = this._output[i].split(':');
} else{
feature_value += this._output[i];
}
i++;
}
} }
let feature = parser(feature_label, feature_value);
if (feature) { let sensors = [];
sensors.push(feature); for (var chipset in data) {
feature = undefined; let gpuFilter = /(radeon|amdgpu|nouveau)/;
if (!data.hasOwnProperty(chipset) || gpuFlag != gpuFilter.test(chipset))
continue;
let chipsetSensors = data[chipset]
for (var sensor in chipsetSensors) {
if (!chipsetSensors.hasOwnProperty(sensor))
continue;
let fields = chipsetSensors[sensor];
for (key in fields) {
if (fields.hasOwnProperty(key) && filter.test(key)) {
let feature = {
label: sensor,
temp: parseFloat(fields[key])
};
sensors.push(feature);
break;
}
}
}
} }
return sensors; return sensors;
},
_parseSensorsTemperatureLine: function(label, value) {
if(label == undefined || value == undefined)
return undefined;
let curValue = value.trim().split(' ')[0];
// does the current value look like a temperature unit (°C)?
if(curValue.indexOf("C", curValue.length - "C".length) !== -1){
return {
label: label.trim(),
temp: parseFloat(curValue.split(' ')[0])
};
// let r;
// sensor['low'] = (r = /low=\+(\d{1,3}.\d)/.exec(value)) ? parseFloat(r[1]) : undefined;
// sensor['high'] = (r = /high=\+(\d{1,3}.\d)/.exec(value)) ? parseFloat(r[1]) : undefined;
// sensor['crit'] = (r = /crit=\+(\d{1,3}.\d)/.exec(value)) ? parseFloat(r[1]) : undefined;
// sensor['hyst'] = (r = /hyst=\+(\d{1,3}.\d)/.exec(value)) ? parseFloat(r[1]) : undefined;
}
return undefined;
},
_parseFanRPMLine: function(label, value) {
if(label == undefined || value == undefined)
return undefined;
let curValue = value.trim().split(' ')[0];
// does the current value look like a fan rpm line?
if(curValue.indexOf("RPM", curValue.length - "RPM".length) !== -1){
return {
label: label.trim(),
rpm: parseFloat(curValue.split(' ')[0])
};
// let r;
// sensor['min'] = (r = /min=(\d{1,5})/.exec(value)) ? parseFloat(r[1]) : undefined;
}
return undefined;
},
_parseVoltageLine: function(label, value) {
if(label == undefined || value == undefined)
return undefined;
let curValue = value.trim().split(' ')[0];
// does the current value look like a voltage line?
if(curValue.indexOf("V", curValue.length - "V".length) !== -1){
return {
label: label.trim(),
volt: parseFloat(curValue.split(' ')[0])
};
// let r;
// sensor['min'] = (r = /min=(\d{1,3}.\d)/.exec(value)) ? parseFloat(r[1]) : undefined;
// sensor['max'] = (r = /max=(\d{1,3}.\d)/.exec(value)) ? parseFloat(r[1]) : undefined;
}
return undefined;
} }
}); });