Skip to content

Commit e336034

Browse files
committed
✨ Permit users to choose Celsius or Fahrenheit.
1 parent c71476d commit e336034

File tree

3 files changed

+86
-39
lines changed

3 files changed

+86
-39
lines changed

docs/widgets.md

+7
Original file line numberDiff line numberDiff line change
@@ -2380,12 +2380,19 @@ You'll need to enable the sensors plugin to use this widget, using: `--enable-pl
23802380

23812381
<p align="center"><img width="400" src="https://i.ibb.co/xSs4Gqd/gl-cpu-temp.png" /></p>
23822382

2383+
#### Options
2384+
2385+
**Field** | **Type** | **Required** | **Description**
2386+
--- | --- | --- | ---
2387+
**`units`** | `string` | _Optional_ | Use `C` to display temperatures in Celsius or `F` to use Fahrenheit. Defaults to `C`.
2388+
23832389
#### Example
23842390

23852391
```yaml
23862392
- type: gl-cpu-temp
23872393
options:
23882394
hostname: http://192.168.130.2:61208
2395+
units: C
23892396
```
23902397

23912398
---

src/components/Widgets/GlCpuTemp.vue

+74-39
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<div class="glances-temp-wrapper" v-if="tempData">
33
<div class="temp-row" v-for="sensor in tempData" :key="sensor.label">
44
<p class="label">{{ sensor.label | formatLbl }}</p>
5-
<p :class="`temp range-${sensor.color}`">{{ sensor.value | formatVal(sensor.sensorType) }}</p>
5+
<p :class="`temp range-${sensor.color}`">{{ sensor.value | formatVal(sensor.unit) }}</p>
66
</div>
77
</div>
88
</template>
99

1010
<script>
1111
import WidgetMixin from '@/mixins/WidgetMixin';
1212
import GlancesMixin from '@/mixins/GlancesMixin';
13-
import { capitalize, fahrenheitToCelsius } from '@/utils/MiscHelpers';
13+
import { capitalize, celsiusToFahrenheit, fahrenheitToCelsius } from '@/utils/MiscHelpers';
1414
1515
export default {
1616
mixins: [WidgetMixin, GlancesMixin],
@@ -29,18 +29,45 @@ export default {
2929
formatLbl(lbl) {
3030
return capitalize(lbl);
3131
},
32-
formatVal(val, sensorType) {
33-
switch (sensorType) {
34-
case 'rpm':
32+
formatVal(val, unit) {
33+
switch (unit) {
34+
case 'R':
3535
return `${Math.round(val)} rpm`;
36-
case 'percentage':
36+
case '%':
3737
return `${Math.round(val)}%`;
3838
default:
39-
return `${Math.round(val)}°C`;
39+
return `${Math.round(val)}°${unit}`;
4040
}
4141
},
4242
},
4343
methods: {
44+
getDesiredUnits() {
45+
return this.options.units ?? 'C';
46+
},
47+
getDisplayValue(rawValue, units) {
48+
const desiredUnits = this.getDesiredUnits();
49+
if (units === desiredUnits) {
50+
return rawValue;
51+
}
52+
53+
return desiredUnits === 'C'
54+
? fahrenheitToCelsius(rawValue)
55+
: celsiusToFahrenheit(rawValue);
56+
},
57+
getCelsiusValue(rawValue, units) {
58+
if (units !== 'F' && units !== 'C') {
59+
return Number.NaN;
60+
}
61+
62+
return units === 'C' ? rawValue : fahrenheitToCelsius(rawValue);
63+
},
64+
getFahrenheitValue(rawValue, units) {
65+
if (units !== 'F' && units !== 'C') {
66+
return Number.NaN;
67+
}
68+
69+
return units === 'F' ? rawValue : celsiusToFahrenheit(rawValue);
70+
},
4471
getTempColor(temp) {
4572
if (temp <= 50) return 'green';
4673
if (temp > 50 && temp < 75) return 'yellow';
@@ -54,45 +81,53 @@ export default {
5481
return 'green';
5582
},
5683
processData(sensorData) {
57-
const results = [];
58-
sensorData.forEach((sensor) => {
59-
// Start by assuming the sensor's unit is degrees Celsius
60-
let sensorValue = sensor.value;
61-
let color = this.getTempColor(sensorValue);
62-
let sensorType = 'temperature';
63-
64-
// Now, override above if sensor unit is actually of a different type
84+
this.tempData = sensorData.map(sensor => {
6585
switch (sensor.unit) {
6686
case 'F':
67-
sensorValue = fahrenheitToCelsius(sensorValue);
68-
color = fahrenheitToCelsius(sensorValue);
69-
break;
70-
71-
// R is for RPM and is typically for fans
87+
case 'C':
88+
return this.processTemperatureSensor(sensor);
7289
case 'R':
73-
color = 'grey';
74-
sensorType = 'rpm';
75-
break;
76-
77-
// For instance, battery levels
90+
return this.processFanSensor(sensor);
7891
case '%':
79-
sensorType = 'percentage';
80-
color = this.getPercentageColor(sensorValue);
81-
break;
82-
83-
// Nothing to do here, already covered by default values
92+
return this.processBatterySensor(sensor);
8493
default:
85-
break;
94+
// Justification: This is a recoverable error that developers
95+
// should nevertheless be warned about.
96+
// eslint-disable-next-line
97+
console.warn('Unrecognized unit', sensor.unit);
98+
return null;
8699
}
100+
}).filter(Boolean);
101+
},
102+
processBatterySensor({ label, unit, value }) {
103+
const color = this.getPercentageColor(value);
104+
return {
105+
color,
106+
label,
107+
unit,
108+
value,
109+
};
110+
},
111+
processFanSensor({ label, unit, value }) {
112+
return {
113+
color: 'grey',
114+
label,
115+
unit,
116+
value,
117+
};
118+
},
119+
processTemperatureSensor({ label, unit, value: originalValue }) {
120+
const celsiusValue = this.getCelsiusValue(originalValue, unit);
121+
const color = this.getTempColor(celsiusValue);
122+
const displayValue = this.getDisplayValue(originalValue, unit);
123+
const displayUnits = this.getDesiredUnits();
87124
88-
results.push({
89-
label: sensor.label,
90-
value: sensorValue,
91-
color,
92-
sensorType,
93-
});
94-
});
95-
this.tempData = results;
125+
return {
126+
color,
127+
label,
128+
unit: displayUnits,
129+
value: displayValue,
130+
};
96131
},
97132
},
98133
};

src/utils/MiscHelpers.js

+5
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ export const getValueFromCss = (colorVar) => {
165165
return cssProps.getPropertyValue(`--${colorVar}`).trim();
166166
};
167167

168+
/* Given a temperature in Celsius, returns value in Fahrenheit */
169+
export const celsiusToFahrenheit = (celsius) => {
170+
return Math.round((celsius * 1.8) + 32);
171+
};
172+
168173
/* Given a temperature in Fahrenheit, returns value in Celsius */
169174
export const fahrenheitToCelsius = (fahrenheit) => {
170175
return Math.round(((fahrenheit - 32) * 5) / 9);

0 commit comments

Comments
 (0)