Improve code

This commit is contained in:
UshakovVasilii
2014-07-01 23:58:27 +04:00
parent e5c60c5d87
commit 6a4501129b
6 changed files with 352 additions and 294 deletions

View File

@@ -0,0 +1,42 @@
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const CommandLineUtil = Me.imports.commandLineUtil;
const AticonfigUtil = new Lang.Class({
Name: 'AticonfigUtil',
Extends: CommandLineUtil.CommandLineUtil,
detect: function(){
let path = GLib.find_program_in_path('aticonfig');
this._argv = path ? [path, '--odgt'] : null;
return this._argv != null;
},
/*
Default Adapter - AMD Radeon R9 200 Series
Sensor 0: Temperature - 37.00 C
*/
get temp() {
if(!this._output)
return [];
let label = null;
let temp = null;
for each(let line in this._output) {
if(!line)
continue;
let r;
if(line.indexOf('Adapter') > 0)
label = (r = /Adapter \- (.*)/.exec(line)) ? r[1] : undefined;
if(line.indexOf('Temperature') > 0)
temp = (r = /Temperature \- (\d{1,3}.\d{1,2})/.exec(line)) ? parseFloat(r[1]) : undefined;
}
if(!label || !temp)
return [];
return [{ label : label.trim(), temp : temp}];
}
});

View File

@@ -0,0 +1,53 @@
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const CommandLineUtil = new Lang.Class({
Name: 'CommandLineUtil',
_init: function(){
this._argv = null;
},
execute: function(callback) {
try{
this._callback = callback;
let [exit, pid, stdinFd, stdoutFd, stderrFd] =
GLib.spawn_async_with_pipes(null, /* cwd */
this._argv, /* args */
null, /* env */
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null /* child_setup */);
let stdout = new Gio.UnixInputStream({fd: stdoutFd, close_fd: true});
let outReader = new Gio.DataInputStream({base_stream: stdout});
GLib.close(stdinFd);
GLib.close(stderrFd);
let childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, Lang.bind(this, function(pid, status, requestObj) {
let output = [];
let [line, size] = [null, 0];
while (([line, size] = outReader.read_line(null)) != null && line != null) {
if(line)
output.push(line.toString());
}
stdout.close(null);
GLib.source_remove(childWatch);
this._output = output;
callback();
}));
} catch(e){
global.log(e.toString());
}
},
get available(){
return this._argv != null;
},
destroy: function(){
this._argv = null;
}
});

View File

@@ -10,8 +10,11 @@ const Gio = imports.gi.Gio;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const Utilities = Me.imports.utilities;
const UDisks2 = Me.imports.udisks2;
const AticonfigUtil = Me.imports.aticonfigUtil;
const HddtempUtil = Me.imports.hddtempUtil;
const SensorsUtil = Me.imports.sensorsUtil;
const Gettext = imports.gettext.domain(Me.metadata['gettext-domain']);
const _ = Gettext.gettext;
@@ -67,10 +70,11 @@ const FreonMenuButton = new Lang.Class({
this._sensorMenuItems = {};
this._output = {};
this._output.sensors = '';
this._output.hddtemp = '';
this._output.aticonfig = '';
this._utils = {
aticonfig: new AticonfigUtil.AticonfigUtil(),
hddtemp: new HddtempUtil.HddtempUtil(),
sensors: new SensorsUtil.SensorsUtil()
};
this._settings = Convenience.getSettings();
@@ -91,10 +95,10 @@ const FreonMenuButton = new Lang.Class({
this.actor.add_actor(this._menuLayout);
this.sensorsArgv = Utilities.detectSensors();
this._utils.sensors.detect();
this._initDriveUtility();
if(this._settings.get_boolean('show-aticonfig-temp'))
this.aticonfigArgv = Utilities.detectAtiConfig();
this._utils.aticonfig.detect();
this._settingChangedSignals = [];
this._addSettingChangedSignal('update-time', Lang.bind(this, this._updateTimeChanged));
@@ -151,7 +155,7 @@ const FreonMenuButton = new Lang.Class({
_initDriveUtility : function(){
switch(this._settings.get_string('drive-utility')){
case 'hddtemp':
this.hddtempArgv = Utilities.detectHDDTemp();
this._utils.hddtemp.detect();
break;
case 'udisks2':
this._udisks2 = new UDisks2.UDisks2(Lang.bind(this, function() {
@@ -166,7 +170,7 @@ const FreonMenuButton = new Lang.Class({
this._udisks2.destroy();
this._udisks2 = null;
}
this.hddtempArgv = null;
this._utils.hddtemp.destroy();
},
_updateTimeChanged : function(){
@@ -175,7 +179,10 @@ const FreonMenuButton = new Lang.Class({
},
_showAtiConfigChanged : function(){
this.aticonfigArgv = this._settings.get_boolean('show-aticonfig-temp') ? Utilities.detectAtiConfig() : undefined;
if(this._settings.get_boolean('show-aticonfig-temp'))
this._utils.aticonfig.detect();
else
this._utils.aticonfig.destroy();
this._querySensors();
},
@@ -201,24 +208,20 @@ const FreonMenuButton = new Lang.Class({
},
_querySensors: function(){
if (this.sensorsArgv){
Utilities.spawnCmd(this.sensorsArgv, Lang.bind(this,function(stdout){
this._output.sensors = stdout;
if (this._utils.sensors.available){
this._utils.sensors.execute(Lang.bind(this,function(){
this._updateDisplay();
}));
}
if (this.hddtempArgv){
Utilities.spawnCmd(this.hddtempArgv, Lang.bind(this,function(stdout){
this._output.hddtemp = stdout;
if (this._utils.hddtemp.available){
this._utils.hddtemp.execute(Lang.bind(this,function(){
this._updateDisplay();
}));
}
if (this.aticonfigArgv){
Utilities.spawnCmd(this.aticonfigArgv, Lang.bind(this,function(stdout){
this._output.aticonfig = stdout;
if (this._utils.aticonfig.available){
this._utils.aticonfig.execute(Lang.bind(this,function(){
this._updateDisplay();
}));
}
@@ -226,27 +229,22 @@ const FreonMenuButton = new Lang.Class({
_updateDisplay: function(){
let gpuTempInfo = [];
if(this.aticonfigArgv)
gpuTempInfo = Utilities.parseAtiConfigOutput(this._output.aticonfig);
if (this._utils.aticonfig.available)
gpuTempInfo = this._utils.aticonfig.temp;
let sensorsTempInfo = [];
sensorsTempInfo = Utilities.parseSensorsOutput(this._output.sensors,Utilities.parseSensorsTemperatureLine);
sensorsTempInfo = sensorsTempInfo.filter(Utilities.filterTemperature);
let sensorsTempInfo = this._utils.sensors.temp;
let fanInfo = [];
if (this._settings.get_boolean('show-fan-rpm')){
fanInfo = Utilities.parseSensorsOutput(this._output.sensors,Utilities.parseFanRPMLine);
fanInfo = fanInfo.filter(Utilities.filterFan);
}
if (this._settings.get_boolean('show-fan-rpm'))
fanInfo = this._utils.sensors.rpm;
let voltageInfo = [];
if (this._settings.get_boolean('show-voltage')){
voltageInfo = Utilities.parseSensorsOutput(this._output.sensors,Utilities.parseVoltageLine);
}
if (this._settings.get_boolean('show-voltage'))
voltageInfo = this._utils.sensors.volt;
let driveTempInfo = [];
if(this.hddtempArgv) {
driveTempInfo = Utilities.parseHddTempOutput(this._output.hddtemp, !(/nc$/.exec(this.hddtempArgv[0])) ? ': ' : '|');
if(this._utils.hddtemp.available) {
driveTempInfo = this._utils.hddtemp.temp;
} else if(this._settings.get_string('drive-utility') == 'udisks2'){
driveTempInfo = this._udisks2.getHDDTemp();
}
@@ -258,7 +256,7 @@ const FreonMenuButton = new Lang.Class({
let tempInfo = gpuTempInfo.concat(sensorsTempInfo).concat(driveTempInfo);
if (this.sensorsArgv && tempInfo.length > 0){
if (tempInfo.length > 0){
let sum = 0; //sum
let max = 0; //max temp
for each (let temp in tempInfo){
@@ -267,37 +265,37 @@ const FreonMenuButton = new Lang.Class({
max = temp['temp'];
}
let sensorsList = [];
let sensors = [];
for each (let temp in tempInfo){
sensorsList.push({type:'temperature', label:temp['label'], value:this._formatTemp(temp['temp'])});
sensors.push({type:'temperature', label:temp['label'], value:this._formatTemp(temp['temp'])});
}
if (tempInfo.length > 0){
sensorsList.push({type : 'separator'});
sensors.push({type : 'separator'});
// Add average and maximum entries
sensorsList.push({type:'temperature', label:_("Average"), value:this._formatTemp(sum/tempInfo.length)});
sensorsList.push({type:'temperature', label:_("Maximum"), value:this._formatTemp(max)});
sensors.push({type:'temperature', label:_("Average"), value:this._formatTemp(sum/tempInfo.length)});
sensors.push({type:'temperature', label:_("Maximum"), value:this._formatTemp(max)});
if(fanInfo.length > 0 || voltageInfo.length > 0)
sensorsList.push({type : 'separator'});
sensors.push({type : 'separator'});
}
for each (let fan in fanInfo){
sensorsList.push({type:'fan',label:fan['label'], value:_("%drpm").format(fan['rpm'])});
sensors.push({type:'fan',label:fan['label'], value:_("%drpm").format(fan['rpm'])});
}
if (fanInfo.length > 0 && voltageInfo.length > 0){
sensorsList.push({type : 'separator'});
sensors.push({type : 'separator'});
}
for each (let voltage in voltageInfo){
sensorsList.push({type : 'voltage', label:voltage['label'], value:_("%s%.2fV").format(((voltage['volt'] >= 0) ? '+' : '-'), voltage['volt'])});
sensors.push({type : 'voltage', label:voltage['label'], value:_("%s%.2fV").format(((voltage['volt'] >= 0) ? '+' : '-'), voltage['volt'])});
}
let needAppendMenuItems = false;
let mainSensor = this._settings.get_string('main-sensor');
let sensorCount = 0;
for each (let s in sensorsList) {
for each (let s in sensors) {
if(s.type != 'separator') {
sensorCount++;
if (mainSensor == s.label) {
@@ -323,7 +321,7 @@ const FreonMenuButton = new Lang.Class({
}
if(Object.keys(this._sensorMenuItems).length==sensorCount){
for each (let s in sensorsList) {
for each (let s in sensors) {
if(s.type != 'separator') {
let item = this._sensorMenuItems[s.label];
if(item) {
@@ -340,7 +338,7 @@ const FreonMenuButton = new Lang.Class({
if(needAppendMenuItems){
global.log('[FREON] Render all MenuItems');
this.menu.removeAll();
this._appendMenuItems(sensorsList);
this._appendMenuItems(sensors);
}
} else {
this._sensorMenuItems = {};
@@ -348,9 +346,9 @@ const FreonMenuButton = new Lang.Class({
this.statusLabel.set_text(_("Error"));
let item = new PopupMenu.PopupMenuItem(
(this.sensorsArgv
this._utils.sensors.available
? _("Please run sensors-detect as root.")
: _("Please install lm_sensors.")) + "\n" + _("If this doesn\'t help, click here to report with your sensors output!")
: _("Please install lm_sensors.\nIf this doesn\'t help, click here to report with your sensors output!")
);
item.connect('activate',function() {
Util.spawn(["xdg-open", "https://github.com/UshakovVasilii/gnome-shell-extension-freon/issues"]);
@@ -359,10 +357,10 @@ const FreonMenuButton = new Lang.Class({
}
},
_appendMenuItems : function(sensorsList){
_appendMenuItems : function(sensors){
this._sensorMenuItems = {};
let mainSensor = this._settings.get_string('main-sensor');
for each (let s in sensorsList){
for each (let s in sensors){
if(s.type == 'separator'){
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
} else {

View File

@@ -0,0 +1,84 @@
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const CommandLineUtil = Me.imports.commandLineUtil;
const HddtempUtil = new Lang.Class({
Name: 'HddtempUtil',
Extends: CommandLineUtil.CommandLineUtil,
detect: function(){
let hddtempArgv = GLib.find_program_in_path('hddtemp');
if(hddtempArgv) {
// check if this user can run hddtemp directly.
if(!GLib.spawn_command_line_sync(hddtempArgv)[3]){
this._argv = [hddtempArgv];
return true;
}
}
// doesn't seem to be the case… is it running as a daemon?
// Check first for systemd
let systemctl = GLib.find_program_in_path('systemctl');
let pidof = GLib.find_program_in_path('pidof');
let nc = GLib.find_program_in_path('nc');
let pid = undefined;
if(systemctl) {
let activeState = GLib.spawn_command_line_sync(systemctl + " show hddtemp.service -p ActiveState")[1].toString().trim();
if(activeState == "ActiveState=active") {
let output = GLib.spawn_command_line_sync(systemctl + " show hddtemp.service -p MainPID")[1].toString().trim();
if(output.length && output.split("=").length == 2)
pid = Number(output.split("=")[1].trim());
}
}
// systemd isn't used on this system, try sysvinit instead
if(!pid && pidof) {
let output = GLib.spawn_command_line_sync("pidof hddtemp")[1].toString().trim();
if(output.length)
pid = Number(output.trim());
}
if(nc && pid) {
// get daemon command line
let cmdline = GLib.file_get_contents('/proc/'+pid+'/cmdline');
// get port or assume default
let match = /(-p\W*|--port=)(\d{1,5})/.exec(cmdline)
let port = match ? parseInt(match[2]) : 7634;
// use net cat to get data
this._argv = [nc, 'localhost', port.toString()];
return true;
}
// not found
return false;
},
get temp() {
if(!this._output)
return [];
let sep = /nc$/.exec(this._argv[0]) ? '|' : ': ';
let hddtempOutput = [];
if (this._output.join().indexOf(sep+sep) > 0) {
hddtempOutput = this._output.join().split(sep+sep);
} else {
hddtempOutput = this._output;
}
let sensors = [];
for each(let line in hddtempOutput) {
let fields = line.split(sep).filter(function(e){ return e; });
let sensor = { label: fields[1], temp: parseFloat(fields[2])};
//push only if the temp is a Number
if (!isNaN(sensor.temp))
sensors.push(sensor);
}
return sensors;
}
});

View File

@@ -0,0 +1,124 @@
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const CommandLineUtil = Me.imports.commandLineUtil;
const SensorsUtil = new Lang.Class({
Name: 'SensorsUtil',
Extends: CommandLineUtil.CommandLineUtil,
detect: function(){
let path = GLib.find_program_in_path('sensors');
this._argv = path ? [path] : null;
return this._argv != null;
},
get temp() {
let s = this._parseSensorsOutput(this._parseSensorsTemperatureLine);
return s.filter(function(e){
return e.temp > 0 && e.temp < 115;
});
},
get rpm() {
let s = this._parseSensorsOutput(this._parseFanRPMLine);
return s.filter(function(e){
return e.rpm > 0;
});
},
get volt() {
return this._parseSensorsOutput(this._parseVoltageLine);
},
_parseSensorsOutput: function(parser) {
if(!this._output)
return [];
let feature_label = undefined;
let feature_value = undefined;
let sensors = [];
//iterate through each lines
for(let i = 0; i < this._output.length; i++){
// ignore chipset driver name and 'Adapter:' line for now
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) {
sensors.push(feature);
feature = undefined;
}
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;
}
});

View File

@@ -1,243 +0,0 @@
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Gettext = imports.gettext.domain(Me.metadata['gettext-domain']);
const _ = Gettext.gettext;
function detectSensors() {
let path = GLib.find_program_in_path('sensors');
return path ? [path] : undefined;
}
function detectHDDTemp() {
let hddtempArgv = GLib.find_program_in_path('hddtemp');
if(hddtempArgv) {
// check if this user can run hddtemp directly.
if(!GLib.spawn_command_line_sync(hddtempArgv)[3])
return [hddtempArgv];
}
// doesn't seem to be the case… is it running as a daemon?
// Check first for systemd
let systemctl = GLib.find_program_in_path('systemctl');
let pidof = GLib.find_program_in_path('pidof');
let nc = GLib.find_program_in_path('nc');
let pid = undefined;
if(systemctl) {
let activeState = GLib.spawn_command_line_sync(systemctl + " show hddtemp.service -p ActiveState")[1].toString().trim();
if(activeState == "ActiveState=active") {
let output = GLib.spawn_command_line_sync(systemctl + " show hddtemp.service -p MainPID")[1].toString().trim();
if(output.length && output.split("=").length == 2) {
pid = Number(output.split("=")[1].trim());
}
}
}
// systemd isn't used on this system, try sysvinit instead
if(!pid && pidof) {
let output = GLib.spawn_command_line_sync("pidof hddtemp")[1].toString().trim();
if(output.length) {
pid = Number(output.trim());
}
}
if(nc && pid)
{
// get daemon command line
let cmdline = GLib.file_get_contents('/proc/'+pid+'/cmdline');
// get port or assume default
let match = /(-p\W*|--port=)(\d{1,5})/.exec(cmdline)
let port = match ? parseInt(match[2]) : 7634;
// use net cat to get data
return [nc, 'localhost', port.toString()];
}
// not found
return undefined;
}
function detectAtiConfig() {
let path = GLib.find_program_in_path('aticonfig');
return path ? [path, '--odgt'] : undefined;
}
function parseSensorsOutput(txt,parser) {
let sensors_output = txt.split("\n");
let feature_label = undefined;
let feature_value = undefined;
let sensors = new Array();
//iterate through each lines
for(let i = 0; i < sensors_output.length; i++){
// ignore chipset driver name and 'Adapter:' line for now
i += 2;
// get every feature of the chip
while(sensors_output[i]){
// if it is not a continutation of a feature line
if(sensors_output[i].indexOf(' ') != 0){
let feature = parser(feature_label, feature_value);
if (feature){
sensors.push(feature);
feature = undefined;
}
[feature_label, feature_value] = sensors_output[i].split(':');
}
else{
feature_value += sensors_output[i];
}
i++;
}
}
let feature = parser(feature_label, feature_value);
if (feature) {
sensors.push(feature);
feature = undefined;
}
return sensors;
}
function parseSensorsTemperatureLine(label, value) {
let sensor = undefined;
if(label != undefined && value != 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){
sensor = new Array();
let r;
sensor['label'] = label.trim();
sensor['temp'] = parseFloat(curValue.split(' ')[0]);
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 sensor;
}
function parseFanRPMLine(label, value) {
let sensor = undefined;
if(label != undefined && value != 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){
sensor = new Array();
let r;
sensor['label'] = label.trim();
sensor['rpm'] = parseFloat(curValue.split(' ')[0]);
sensor['min'] = (r = /min=(\d{1,5})/.exec(value)) ? parseFloat(r[1]) : undefined;
}
}
return sensor;
}
function parseVoltageLine(label, value) {
let sensor = undefined;
if(label != undefined && value != 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){
sensor = new Array();
let r;
sensor['label'] = label.trim();
sensor['volt'] = parseFloat(curValue.split(' ')[0]);
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 sensor;
}
function parseHddTempOutput(txt, sep) {
let hddtemp_output = [];
if (txt.indexOf(sep+sep) > 0) {
hddtemp_output = txt.split(sep+sep);
} else {
hddtemp_output = txt.split("\n");
}
hddtemp_output = hddtemp_output.filter(function(e){ return e; });
let sensors = [];
for each(let line in hddtemp_output) {
let fields = line.split(sep).filter(function(e){ return e; });
let sensor = { label: fields[1], temp: parseFloat(fields[2])};
//push only if the temp is a Number
if (!isNaN(sensor.temp))
sensors.push(sensor);
}
return sensors;
}
/*
Default Adapter - AMD Radeon R9 200 Series
Sensor 0: Temperature - 37.00 C
*/
function parseAtiConfigOutput(txt) {
if(!txt)
return [];
let output = txt.split('\n');
let label = null;
let temp = null;
for each(let line in output) {
if(!line)
continue;
let r;
if(line.indexOf('Adapter') > 0)
label = (r = /Adapter \- (.*)/.exec(line)) ? r[1] : undefined;
if(line.indexOf('Temperature') > 0)
temp = (r = /Temperature \- (\d{1,3}.\d{1,2})/.exec(line)) ? parseFloat(r[1]) : undefined;
}
if(!label || !temp)
return [];
return [{ label : label.trim(), temp : temp}];
}
function filterTemperature(tempInfo) {
return tempInfo['temp'] > 0 && tempInfo['temp'] < 115;
}
function filterFan(fanInfo) {
return fanInfo['rpm'] > 0;
}
function filterVoltage(voltageInfo) {
return true;
}
function spawnCmd(argv, callback) {
try{
this._callback = callback;
let [exit, pid, stdinFd, stdoutFd, stderrFd] =
GLib.spawn_async_with_pipes(null, /* cwd */
argv, /* args */
null, /* env */
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null /* child_setup */);
let stdout = new Gio.UnixInputStream({fd: stdoutFd, close_fd: true});
let outReader = new Gio.DataInputStream({base_stream: stdout});
GLib.close(stdinFd);
GLib.close(stderrFd);
let childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, Lang.bind(this, function(pid, status, requestObj) {
let output = '';
let [line, size] = [null, 0];
while (([line, size] = outReader.read_line(null)) != null && line != null) {
output += line.toString() + '\n';
}
stdout.close(null);
GLib.source_remove(childWatch);
callback(output);
}));
} catch(e){
global.log(e.toString());
}
}