From f3ec238a2c2bdfdd3499482a87932b521502eeeb Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:47:39 -0700 Subject: [PATCH] Fix glances cpu temp detection & fahrenheit conversion --- src/components/widgets/glances/glances.jsx | 25 ++++++++++++++++---- src/components/widgets/resources/cputemp.jsx | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/components/widgets/glances/glances.jsx b/src/components/widgets/glances/glances.jsx index e34c49339..a6f4dfa8c 100644 --- a/src/components/widgets/glances/glances.jsx +++ b/src/components/widgets/glances/glances.jsx @@ -6,6 +6,12 @@ import { useTranslation } from "next-i18next"; import UsageBar from "../resources/usage-bar"; +const cpuSensorLabels = ["cpu_thermal", "Core"]; + +function convertToFahrenheit(t) { + return t * 9/5 + 32 +} + export default function Widget({ options }) { const { t, i18n } = useTranslation(); @@ -65,11 +71,20 @@ export default function Widget({ options }) { } const unit = options.units === "imperial" ? "fahrenheit" : "celsius"; - let mainTemp; + let mainTemp = 0; let maxTemp = 80; - if (options.cputemp && data.sensors) { - mainTemp = unit === "celsius" ? data.sensors.find(s => s.label.includes("cpu_thermal")).value : data.sensors.find(s => s.label.includes("cpu_thermal")).value * 5/9 + 32; - if (data.sensors.warning) maxTemp = data.sensors.warning; + const cpuSensors = data.sensors?.filter(s => cpuSensorLabels.some(label => s.label.startsWith(label)) && s.type === "temperature_core"); + if (options.cputemp && cpuSensors) { + try { + mainTemp = cpuSensors.reduce((acc, s) => acc + s.value, 0) / cpuSensors.length; + maxTemp = Math.max(cpuSensors.reduce((acc, s) => acc + s.warning, 0) / cpuSensors.length, maxTemp); + if (unit === "fahrenheit") { + mainTemp = convertToFahrenheit(mainTemp); + maxTemp = convertToFahrenheit(maxTemp); + } + } catch (e) { + // cpu sensor retrieval failed + } } const tempPercent = Math.round((mainTemp / maxTemp) * 100); @@ -110,7 +125,7 @@ export default function Widget({ options }) { - {options.cputemp && mainTemp && + {options.cputemp && mainTemp > 0 && (
diff --git a/src/components/widgets/resources/cputemp.jsx b/src/components/widgets/resources/cputemp.jsx index 22eecb068..92c684e58 100644 --- a/src/components/widgets/resources/cputemp.jsx +++ b/src/components/widgets/resources/cputemp.jsx @@ -6,7 +6,7 @@ import { useTranslation } from "next-i18next"; import UsageBar from "./usage-bar"; function convertToFahrenheit(t) { - return t * 5/9 + 32 + return t * 9/5 + 32 } export default function CpuTemp({ expanded, units }) {