From df5ada9340f2510660dd26d26580935e36fe701b Mon Sep 17 00:00:00 2001 From: Luis Pabon Date: Mon, 3 Apr 2023 17:02:18 +0100 Subject: [PATCH] Tweak GlCpuTemp widget to support other sensor units The current code assumes all sensors report temperature, when in fact glances' sensor plugin also reports rpm (eg fan speeds) and percentages (eg batteries). Changes: * Add support for percentages * Add support for rpm Note: this widget should really be called something like `GlSensors`, but I haven't been able to easily find a way of renaming it without breaking people's existing configs and workflows. --- src/components/Widgets/GlCpuTemp.vue | 54 ++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/src/components/Widgets/GlCpuTemp.vue b/src/components/Widgets/GlCpuTemp.vue index af915080..37356d44 100644 --- a/src/components/Widgets/GlCpuTemp.vue +++ b/src/components/Widgets/GlCpuTemp.vue @@ -2,7 +2,7 @@

{{ sensor.label | formatLbl }}

-

{{ sensor.value | formatVal }}

+

{{ sensor.value | formatVal(sensor.sensorType) }}

@@ -29,8 +29,15 @@ export default { formatLbl(lbl) { return capitalize(lbl); }, - formatVal(val) { - return `${Math.round(val)}°C`; + formatVal(val, sensorType) { + switch (sensorType) { + case 'rpm': + return `${Math.round(val)} rpm`; + case 'percentage': + return `${Math.round(val)}%`; + default: + return `${Math.round(val)}°C`; + } }, }, methods: { @@ -40,14 +47,49 @@ export default { if (temp >= 75) return 'red'; return 'grey'; }, + getPercentageColor(percentage) { + if (percentage < 20) return 'red'; + if (percentage < 50) return 'orange'; + if (percentage < 75) return 'yellow'; + return 'green'; + }, processData(sensorData) { const results = []; sensorData.forEach((sensor) => { - const tempC = sensor.unit === 'F' ? fahrenheitToCelsius(sensor.value) : sensor.value; + // Start by assuming the sensor's unit is degrees Celsius + let sensorValue = sensor.value; + let color = this.getTempColor(sensorValue); + let sensorType = 'temperature'; + + // Now, override above if sensor unit is actually of a different type + switch (sensor.unit) { + case 'F': + sensorValue = fahrenheitToCelsius(sensorValue); + color = fahrenheitToCelsius(sensorValue); + break; + + // R is for RPM and is typically for fans + case 'R': + color = 'grey'; + sensorType = 'rpm'; + break; + + // For instance, battery levels + case '%': + sensorType = 'percentage'; + color = this.getPercentageColor(sensorValue); + break; + + // Nothing to do here, already covered by default values + default: + break; + } + results.push({ label: sensor.label, - value: tempC, - color: this.getTempColor(tempC), + value: sensorValue, + color, + sensorType, }); }); this.tempData = results;