🚀 A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!
Vue awesomedashboarddockerhomelabhomepagemitnodejsorganizationproductivitypwaself-hostedstartpagevue
✨ Permit users to choose Celsius or Fahrenheit.
Chris Nielsene3360349bba9
diff --git a/docs/widgets.md b/docs/widgets.mdindex e3c67e20..cc9a1450 100644--- a/docs/widgets.md+++ b/docs/widgets.md@@ -2380,12 +2380,19 @@ You'll need to enable the sensors plugin to use this widget, using: `--enable-pl<p align="center"><img width="400" src="https://i.ibb.co/xSs4Gqd/gl-cpu-temp.png" /></p>+#### Options++**Field** | **Type** | **Required** | **Description**+--- | --- | --- | ---+**`units`** | `string` | _Optional_ | Use `C` to display temperatures in Celsius or `F` to use Fahrenheit. Defaults to `C`.+#### Example```yaml- type: gl-cpu-tempoptions:hostname: http://192.168.130.2:61208+ units: C```---diff --git a/src/components/Widgets/GlCpuTemp.vue b/src/components/Widgets/GlCpuTemp.vueindex 37356d44..9fcc83ed 100644--- a/src/components/Widgets/GlCpuTemp.vue+++ b/src/components/Widgets/GlCpuTemp.vue@@ -2,7 +2,7 @@<div class="glances-temp-wrapper" v-if="tempData"><div class="temp-row" v-for="sensor in tempData" :key="sensor.label"><p class="label">{{ sensor.label | formatLbl }}</p>- <p :class="`temp range-${sensor.color}`">{{ sensor.value | formatVal(sensor.sensorType) }}</p>+ <p :class="`temp range-${sensor.color}`">{{ sensor.value | formatVal(sensor.unit) }}</p></div></div></template>@@ -10,7 +10,7 @@<script>import WidgetMixin from '@/mixins/WidgetMixin';import GlancesMixin from '@/mixins/GlancesMixin';-import { capitalize, fahrenheitToCelsius } from '@/utils/MiscHelpers';+import { capitalize, celsiusToFahrenheit, fahrenheitToCelsius } from '@/utils/MiscHelpers';export default {mixins: [WidgetMixin, GlancesMixin],@@ -29,18 +29,45 @@ export default {formatLbl(lbl) {return capitalize(lbl);},- formatVal(val, sensorType) {- switch (sensorType) {- case 'rpm':+ formatVal(val, unit) {+ switch (unit) {+ case 'R':return `${Math.round(val)} rpm`;- case 'percentage':+ case '%':return `${Math.round(val)}%`;default:- return `${Math.round(val)}°C`;+ return `${Math.round(val)}°${unit}`;}},},methods: {+ getDesiredUnits() {+ return this.options.units ?? 'C';+ },+ getDisplayValue(rawValue, units) {+ const desiredUnits = this.getDesiredUnits();+ if (units === desiredUnits) {+ return rawValue;+ }++ return desiredUnits === 'C'+ ? fahrenheitToCelsius(rawValue)+ : celsiusToFahrenheit(rawValue);+ },+ getCelsiusValue(rawValue, units) {+ if (units !== 'F' && units !== 'C') {+ return Number.NaN;+ }++ return units === 'C' ? rawValue : fahrenheitToCelsius(rawValue);+ },+ getFahrenheitValue(rawValue, units) {+ if (units !== 'F' && units !== 'C') {+ return Number.NaN;+ }++ return units === 'F' ? rawValue : celsiusToFahrenheit(rawValue);+ },getTempColor(temp) {if (temp <= 50) return 'green';if (temp > 50 && temp < 75) return 'yellow';@@ -54,45 +81,53 @@ export default {return 'green';},processData(sensorData) {- const results = [];- sensorData.forEach((sensor) => {- // 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+ this.tempData = sensorData.map(sensor => {switch (sensor.unit) {case 'F':- sensorValue = fahrenheitToCelsius(sensorValue);- color = fahrenheitToCelsius(sensorValue);- break;-- // R is for RPM and is typically for fans+ case 'C':+ return this.processTemperatureSensor(sensor);case 'R':- color = 'grey';- sensorType = 'rpm';- break;-- // For instance, battery levels+ return this.processFanSensor(sensor);case '%':- sensorType = 'percentage';- color = this.getPercentageColor(sensorValue);- break;-- // Nothing to do here, already covered by default values+ return this.processBatterySensor(sensor);default:- break;+ // Justification: This is a recoverable error that developers+ // should nevertheless be warned about.+ // eslint-disable-next-line+ console.warn('Unrecognized unit', sensor.unit);+ return null;}+ }).filter(Boolean);+ },+ processBatterySensor({ label, unit, value }) {+ const color = this.getPercentageColor(value);+ return {+ color,+ label,+ unit,+ value,+ };+ },+ processFanSensor({ label, unit, value }) {+ return {+ color: 'grey',+ label,+ unit,+ value,+ };+ },+ processTemperatureSensor({ label, unit, value: originalValue }) {+ const celsiusValue = this.getCelsiusValue(originalValue, unit);+ const color = this.getTempColor(celsiusValue);+ const displayValue = this.getDisplayValue(originalValue, unit);+ const displayUnits = this.getDesiredUnits();- results.push({- label: sensor.label,- value: sensorValue,- color,- sensorType,- });- });- this.tempData = results;+ return {+ color,+ label,+ unit: displayUnits,+ value: displayValue,+ };},},};diff --git a/src/utils/MiscHelpers.js b/src/utils/MiscHelpers.jsindex 200650f4..8b3719c0 100644--- a/src/utils/MiscHelpers.js+++ b/src/utils/MiscHelpers.js@@ -165,6 +165,11 @@ export const getValueFromCss = (colorVar) => {return cssProps.getPropertyValue(`--${colorVar}`).trim();};+/* Given a temperature in Celsius, returns value in Fahrenheit */+export const celsiusToFahrenheit = (celsius) => {+ return Math.round((celsius * 1.8) + 32);+};+/* Given a temperature in Fahrenheit, returns value in Celsius */export const fahrenheitToCelsius = (fahrenheit) => {return Math.round(((fahrenheit - 32) * 5) / 9);