Skip to content

Commit

Permalink
Enhancement: configurable CPU temp scale (gethomepage#3332)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: shamoon <[email protected]>
  • Loading branch information
daviddavid and shamoon authored Apr 19, 2024
1 parent 068e664 commit c95837f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/widgets/info/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ _Note: unfortunately, the package used for getting CPU temp ([systeminformation]
memory: true
disk: /disk/mount/path
cputemp: true
tempmin: 0 # optional, minimum cpu temp
tempmax: 100 # optional, maximum cpu temp
uptime: true
units: imperial # only used by cpu temp
refresh: 3000 # optional, in ms
Expand Down
11 changes: 8 additions & 3 deletions src/components/widgets/resources/cputemp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function convertToFahrenheit(t) {
return (t * 9) / 5 + 32;
}

export default function CpuTemp({ expanded, units, refresh = 1500 }) {
export default function CpuTemp({ expanded, units, refresh = 1500, tempmin = 0, tempmax = -1 }) {
const { t } = useTranslation();

const { data, error } = useSWR(`/api/widgets/resources?type=cputemp`, {
Expand Down Expand Up @@ -39,7 +39,12 @@ export default function CpuTemp({ expanded, units, refresh = 1500 }) {
}
const unit = units === "imperial" ? "fahrenheit" : "celsius";
mainTemp = unit === "celsius" ? mainTemp : convertToFahrenheit(mainTemp);
const maxTemp = unit === "celsius" ? data.cputemp.max : convertToFahrenheit(data.cputemp.max);

const minTemp = tempmin < mainTemp ? tempmin : mainTemp;
let maxTemp = tempmax;
if (maxTemp < minTemp) {
maxTemp = unit === "celsius" ? data.cputemp.max : convertToFahrenheit(data.cputemp.max);
}

return (
<Resource
Expand All @@ -58,7 +63,7 @@ export default function CpuTemp({ expanded, units, refresh = 1500 }) {
unit,
})}
expandedLabel={t("resources.max")}
percentage={Math.round((mainTemp / maxTemp) * 100)}
percentage={Math.round(((mainTemp - minTemp) / (maxTemp - minTemp)) * 100)}
expanded={expanded}
/>
);
Expand Down
6 changes: 4 additions & 2 deletions src/components/widgets/resources/resources.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CpuTemp from "./cputemp";
import Uptime from "./uptime";

export default function Resources({ options }) {
const { expanded, units, diskUnits } = options;
const { expanded, units, diskUnits, tempmin, tempmax } = options;
let { refresh } = options;
if (!refresh) refresh = 1500;
refresh = Math.max(refresh, 1000);
Expand All @@ -23,7 +23,9 @@ export default function Resources({ options }) {
<Disk key={disk} options={{ disk }} expanded={expanded} diskUnits={diskUnits} refresh={refresh} />
))
: options.disk && <Disk options={options} expanded={expanded} diskUnits={diskUnits} refresh={refresh} />}
{options.cputemp && <CpuTemp expanded={expanded} units={units} refresh={refresh} />}
{options.cputemp && (
<CpuTemp expanded={expanded} units={units} refresh={refresh} tempmin={tempmin} tempmax={tempmax} />
)}
{options.uptime && <Uptime refresh={refresh} />}
</div>
{options.label && (
Expand Down

0 comments on commit c95837f

Please sign in to comment.