2023-03-05 14:11:43 -08:00
|
|
|
import useSWR from "swr";
|
|
|
|
import { FaThermometerHalf } from "react-icons/fa";
|
|
|
|
import { useTranslation } from "next-i18next";
|
2023-03-30 14:40:24 -07:00
|
|
|
|
2023-06-03 01:10:15 +01:00
|
|
|
import SingleResource from "../widget/single_resource";
|
|
|
|
import WidgetIcon from "../widget/widget_icon";
|
|
|
|
import ResourceValue from "../widget/resource_value";
|
|
|
|
import ResourceLabel from "../widget/resource_label";
|
|
|
|
import Error from "../widget/error";
|
|
|
|
|
2023-06-10 23:30:44 -07:00
|
|
|
import UsageBar from "./usage-bar";
|
2023-06-03 01:10:15 +01:00
|
|
|
|
2023-03-31 08:44:27 -07:00
|
|
|
function convertToFahrenheit(t) {
|
2023-04-03 21:47:39 -07:00
|
|
|
return t * 9/5 + 32
|
2023-03-31 08:44:27 -07:00
|
|
|
}
|
|
|
|
|
2023-03-05 14:11:43 -08:00
|
|
|
export default function CpuTemp({ expanded, units }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
const { data, error } = useSWR(`/api/widgets/resources?type=cputemp`, {
|
|
|
|
refreshInterval: 1500,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error || data?.error) {
|
2023-06-03 01:10:15 +01:00
|
|
|
return <Error />
|
2023-03-05 14:11:43 -08:00
|
|
|
}
|
|
|
|
|
2023-03-31 08:44:27 -07:00
|
|
|
if (!data || !data.cputemp) {
|
2023-06-03 01:10:15 +01:00
|
|
|
return <SingleResource expanded={expanded}>
|
|
|
|
<WidgetIcon icon={FaThermometerHalf} />
|
|
|
|
<ResourceValue>-</ResourceValue>
|
|
|
|
<ResourceLabel>{t("resources.temp")}</ResourceLabel>
|
|
|
|
<ResourceValue>-</ResourceValue>
|
|
|
|
<ResourceLabel>{t("resources.max")}</ResourceLabel>
|
|
|
|
</SingleResource>
|
2023-03-05 14:11:43 -08:00
|
|
|
}
|
|
|
|
|
2023-03-31 08:44:27 -07:00
|
|
|
let mainTemp = data.cputemp.main;
|
|
|
|
if (data.cputemp.cores?.length) {
|
|
|
|
mainTemp = data.cputemp.cores.reduce((a, b) => a + b) / data.cputemp.cores.length;
|
|
|
|
}
|
2023-03-05 14:11:43 -08:00
|
|
|
const unit = units === "imperial" ? "fahrenheit" : "celsius";
|
2023-03-31 08:44:27 -07:00
|
|
|
mainTemp = (unit === "celsius") ? mainTemp : convertToFahrenheit(mainTemp);
|
|
|
|
const maxTemp = (unit === "celsius") ? data.cputemp.max : convertToFahrenheit(data.cputemp.max);
|
2023-03-05 14:11:43 -08:00
|
|
|
|
2023-06-03 01:10:15 +01:00
|
|
|
return <SingleResource expanded={expanded}>
|
|
|
|
<WidgetIcon icon={FaThermometerHalf} />
|
|
|
|
<ResourceValue>
|
|
|
|
{t("common.number", {
|
|
|
|
value: mainTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
|
|
|
unit
|
|
|
|
})}
|
|
|
|
</ResourceValue>
|
|
|
|
<ResourceLabel>{t("resources.temp")}</ResourceLabel>
|
|
|
|
<ResourceValue>
|
|
|
|
{t("common.number", {
|
|
|
|
value: maxTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
|
|
|
unit
|
|
|
|
})}
|
|
|
|
</ResourceValue>
|
|
|
|
<ResourceLabel>{t("resources.max")}</ResourceLabel>
|
|
|
|
<UsageBar percent={Math.round((mainTemp / maxTemp) * 100)} />
|
|
|
|
</SingleResource>;
|
2023-03-05 14:11:43 -08:00
|
|
|
}
|