Skip to content

Commit

Permalink
don't query temp, volt, fan if disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
corecoding committed Aug 26, 2022
1 parent 598cccd commit 1ae51e3
Showing 1 changed file with 120 additions and 106 deletions.
226 changes: 120 additions & 106 deletions sensors.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,45 @@ var Sensors = GObject.registerClass({
}

query(callback, dwell) {
if (this._hardware_detected) {
this._queryTempVoltFan(callback);
} else {
if (!this._hardware_detected) {
// we could set _hardware_detected in discoverHardwareMonitors, but by
// doing it here, we guarantee avoidance of race conditions
this._hardware_detected = true;
this._discoverHardwareMonitors(callback);
}

for (let sensor in this._sensorIcons) {
if (sensor == 'temperature' || sensor == 'voltage' || sensor == 'fan')
continue;

if (this._settings.get_boolean('show-' + sensor)) {
let method = '_query' + sensor[0].toUpperCase() + sensor.slice(1);
this[method](callback, dwell);
}
}
}

_queryTempVoltFan(callback) {
for (let label in this._tempVoltFanSensors) {
let sensor = this._tempVoltFanSensors[label];
_queryTempVoltFan(callback, type) {
for (let label in this._tempVoltFanSensors[type]) {
let sensor = this._tempVoltFanSensors[type][label];

new FileModule.File(sensor['path']).read().then(value => {
this._returnValue(callback, label, value, sensor['type'], sensor['format']);
this._returnValue(callback, label, value, type, sensor['format']);
}).catch(err => {
this._returnValue(callback, label, 'disabled', sensor['type'], sensor['format']);
this._returnValue(callback, label, 'disabled', type, sensor['format']);
});
}
}

_queryTemperature(callback) {
this._queryTempVoltFan(callback, 'temperature');
}

_queryVoltage(callback) {
this._queryTempVoltFan(callback, 'voltage');
}

_queryFan(callback) {
this._queryTempVoltFan(callback, 'fan');
}

_queryMemory(callback) {
// check memory info
new FileModule.File('/proc/meminfo').read().then(lines => {
Expand Down Expand Up @@ -252,88 +261,6 @@ var Sensors = GObject.registerClass({
}).catch(err => { });
}

_queryBattery(callback) {
let battery_slot = this._settings.get_int('battery-slot');

// addresses issue #161
let batt_key = 'BAT';
if (battery_slot == 3) {
batt_key = 'CMB';
battery_slot = 0;
}

let battery_path = '/sys/class/power_supply/' + batt_key + battery_slot + '/';

new FileModule.File(battery_path + 'status').read().then(value => {
this._returnValue(callback, 'State', value, 'battery', '');
}).catch(err => { });

new FileModule.File(battery_path + 'cycle_count').read().then(value => {
if (value > 0 || (value == 0 && !this._settings.get_boolean('hide-zeros')))
this._returnValue(callback, 'Cycles', value, 'battery', '');
}).catch(err => { });

new FileModule.File(battery_path + 'charge_full').read().then(charge_full => {
new FileModule.File(battery_path + 'voltage_min_design').read().then(voltage_min_design => {
this._returnValue(callback, 'Energy (full)', charge_full * voltage_min_design, 'battery', 'watt-hour');
new FileModule.File(battery_path + 'charge_full_design').read().then(charge_full_design => {
this._returnValue(callback, 'Capacity', (charge_full / charge_full_design), 'battery', 'percent');
this._returnValue(callback, 'Energy (design)', charge_full_design * voltage_min_design, 'battery', 'watt-hour');
}).catch(err => { });

new FileModule.File(battery_path + 'voltage_now').read().then(voltage_now => {
this._returnValue(callback, 'Voltage', voltage_now / 1000, 'battery', 'in');

new FileModule.File(battery_path + 'current_now').read().then(current_now => {
let watt = current_now * voltage_now;
this._returnValue(callback, 'Rate', watt, 'battery', 'watt');
this._returnValue(callback, 'battery', watt, 'battery-group', 'watt');

new FileModule.File(battery_path + 'charge_now').read().then(charge_now => {
let rest_pwr = voltage_min_design * charge_now;
this._returnValue(callback, 'Energy (now)', rest_pwr, 'battery', 'watt-hour');

//let time_left_h = rest_pwr / last_pwr;
//this._returnValue(callback, 'time_left_h', time_left_h, 'battery', '');

let level = charge_now / charge_full;
this._returnValue(callback, 'Percentage', level, 'battery', 'percent');
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => {
new FileModule.File(battery_path + 'energy_full').read().then(energy_full => {
new FileModule.File(battery_path + 'voltage_min_design').read().then(voltage_min_design => {
this._returnValue(callback, 'Energy (full)', energy_full * 1000000, 'battery', 'watt-hour');
new FileModule.File(battery_path + 'energy_full_design').read().then(energy_full_design => {
this._returnValue(callback, 'Capacity', (energy_full / energy_full_design), 'battery', 'percent');
this._returnValue(callback, 'Energy (design)', energy_full_design * 1000000, 'battery', 'watt-hour');
}).catch(err => { });

new FileModule.File(battery_path + 'voltage_now').read().then(voltage_now => {
this._returnValue(callback, 'Voltage', voltage_now / 1000, 'battery', 'in');

new FileModule.File(battery_path + 'power_now').read().then(power_now => {
this._returnValue(callback, 'Rate', power_now * 1000000, 'battery', 'watt');
this._returnValue(callback, 'battery', power_now * 1000000, 'battery-group', 'watt');

new FileModule.File(battery_path + 'energy_now').read().then(energy_now => {
this._returnValue(callback, 'Energy (now)', energy_now * 1000000, 'battery', 'watt-hour');

//let time_left_h = energy_now / last_pwr;
//this._returnValue(callback, 'time_left_h', time_left_h, 'battery', '');

let level = energy_now / energy_full;
this._returnValue(callback, 'Percentage', level, 'battery', 'percent');
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
});
}

_queryNetwork(callback, dwell) {
// check network speed
let directions = ['tx', 'rx'];
Expand Down Expand Up @@ -438,21 +365,106 @@ var Sensors = GObject.registerClass({
this._returnValue(callback, 'storage', avail, 'storage-group', 'storage');
}

_queryBattery(callback) {
let battery_slot = this._settings.get_int('battery-slot');

// addresses issue #161
let batt_key = 'BAT';
if (battery_slot == 3) {
batt_key = 'CMB';
battery_slot = 0;
}

let battery_path = '/sys/class/power_supply/' + batt_key + battery_slot + '/';

new FileModule.File(battery_path + 'status').read().then(value => {
this._returnValue(callback, 'State', value, 'battery', '');
}).catch(err => { });

new FileModule.File(battery_path + 'cycle_count').read().then(value => {
if (value > 0 || (value == 0 && !this._settings.get_boolean('hide-zeros')))
this._returnValue(callback, 'Cycles', value, 'battery', '');
}).catch(err => { });

new FileModule.File(battery_path + 'charge_full').read().then(charge_full => {
new FileModule.File(battery_path + 'voltage_min_design').read().then(voltage_min_design => {
this._returnValue(callback, 'Energy (full)', charge_full * voltage_min_design, 'battery', 'watt-hour');
new FileModule.File(battery_path + 'charge_full_design').read().then(charge_full_design => {
this._returnValue(callback, 'Capacity', (charge_full / charge_full_design), 'battery', 'percent');
this._returnValue(callback, 'Energy (design)', charge_full_design * voltage_min_design, 'battery', 'watt-hour');
}).catch(err => { });

new FileModule.File(battery_path + 'voltage_now').read().then(voltage_now => {
this._returnValue(callback, 'Voltage', voltage_now / 1000, 'battery', 'in');

new FileModule.File(battery_path + 'current_now').read().then(current_now => {
let watt = current_now * voltage_now;
this._returnValue(callback, 'Rate', watt, 'battery', 'watt');
this._returnValue(callback, 'battery', watt, 'battery-group', 'watt');

new FileModule.File(battery_path + 'charge_now').read().then(charge_now => {
let rest_pwr = voltage_min_design * charge_now;
this._returnValue(callback, 'Energy (now)', rest_pwr, 'battery', 'watt-hour');

//let time_left_h = rest_pwr / last_pwr;
//this._returnValue(callback, 'time_left_h', time_left_h, 'battery', '');

let level = charge_now / charge_full;
this._returnValue(callback, 'Percentage', level, 'battery', 'percent');
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => {
new FileModule.File(battery_path + 'energy_full').read().then(energy_full => {
new FileModule.File(battery_path + 'voltage_min_design').read().then(voltage_min_design => {
this._returnValue(callback, 'Energy (full)', energy_full * 1000000, 'battery', 'watt-hour');
new FileModule.File(battery_path + 'energy_full_design').read().then(energy_full_design => {
this._returnValue(callback, 'Capacity', (energy_full / energy_full_design), 'battery', 'percent');
this._returnValue(callback, 'Energy (design)', energy_full_design * 1000000, 'battery', 'watt-hour');
}).catch(err => { });

new FileModule.File(battery_path + 'voltage_now').read().then(voltage_now => {
this._returnValue(callback, 'Voltage', voltage_now / 1000, 'battery', 'in');

new FileModule.File(battery_path + 'power_now').read().then(power_now => {
this._returnValue(callback, 'Rate', power_now * 1000000, 'battery', 'watt');
this._returnValue(callback, 'battery', power_now * 1000000, 'battery-group', 'watt');

new FileModule.File(battery_path + 'energy_now').read().then(energy_now => {
this._returnValue(callback, 'Energy (now)', energy_now * 1000000, 'battery', 'watt-hour');

//let time_left_h = energy_now / last_pwr;
//this._returnValue(callback, 'time_left_h', time_left_h, 'battery', '');

let level = energy_now / energy_full;
this._returnValue(callback, 'Percentage', level, 'battery', 'percent');
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
}).catch(err => { });
});
}

_returnValue(callback, label, value, type, format) {
callback(label, value, type, format);
}

_discoverHardwareMonitors(callback) {
this._tempVoltFanSensors = {};
this._tempVoltFanSensors = { 'temperature': {}, 'voltage': {}, 'fan': {} };

let hwbase = '/sys/class/hwmon/';

// process sensor_types now so it is not called multiple times below
let sensor_types = {};

if (this._settings.get_boolean('show-temperature'))
sensor_types['temp'] = 'temperature';

if (this._settings.get_boolean('show-voltage'))
sensor_types['in'] = 'voltage';

if (this._settings.get_boolean('show-fan'))
sensor_types['fan'] = 'fan';

Expand Down Expand Up @@ -589,25 +601,27 @@ var Sensors = GObject.registerClass({
if (label == 'Package id 1') label = 'Processor 1';
label = label.replace('Package id', 'CPU');

// check if this label already exists
if (label in this._tempVoltFanSensors) {
for (let i = 2; i <= 9; i++) {
// append an incremented number to end
let new_label = label + ' ' + i;

// if new label is available, use it
if (!(new_label in this._tempVoltFanSensors)) {
label = new_label;
break;
let types = [ 'temperature', 'voltage', 'fan' ];
for (let type of types) {
// check if this label already exists
if (label in this._tempVoltFanSensors[type]) {
for (let i = 2; i <= 9; i++) {
// append an incremented number to end
let new_label = label + ' ' + i;

// if new label is available, use it
if (!(new_label in this._tempVoltFanSensors[type])) {
label = new_label;
break;
}
}
}
}

// update screen on initial build to prevent delay on update
this._returnValue(callback, label, value, obj['type'], obj['format']);

this._tempVoltFanSensors[label] = {
'type': obj['type'],
this._tempVoltFanSensors[obj['type']][label] = {
'format': obj['format'],
'path': obj['input']
};
Expand Down

0 comments on commit 1ae51e3

Please sign in to comment.