From f46addf20a7983c9843801b5284238e6df307a74 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 5 Mar 2023 14:11:43 -0800 Subject: [PATCH] Add cputemp to resources widget --- package.json | 1 + pnpm-lock.yaml | 9 +++ public/locales/en/common.json | 4 +- src/components/widgets/resources/cputemp.jsx | 79 +++++++++++++++++++ .../widgets/resources/resources.jsx | 4 +- src/pages/api/widgets/resources.js | 6 ++ 6 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/components/widgets/resources/cputemp.jsx diff --git a/package.json b/package.json index f6e86adbc..ee6e7af6d 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "minecraft-ping-js": "^1.0.2", "next": "^12.3.1", "next-i18next": "^12.0.1", + "osx-temperature-sensor": "^1.0.8", "pretty-bytes": "^6.0.0", "raw-body": "^2.5.1", "react": "^18.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 94afa5916..fabcb86ab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25,6 +25,7 @@ specifiers: minecraft-ping-js: ^1.0.2 next: ^12.3.1 next-i18next: ^12.0.1 + osx-temperature-sensor: ^1.0.8 postcss: ^8.4.16 prettier: ^2.7.1 pretty-bytes: ^6.0.0 @@ -57,6 +58,7 @@ dependencies: minecraft-ping-js: 1.0.2 next: 12.3.1_biqbaboplfbrettd7655fr4n2y next-i18next: 12.0.1_azq6kxkn3od7qdylwkyksrwopy + osx-temperature-sensor: 1.0.8 pretty-bytes: 6.0.0 raw-body: 2.5.1 react: 18.2.0 @@ -2442,6 +2444,13 @@ packages: word-wrap: 1.2.3 dev: true + /osx-temperature-sensor/1.0.8: + resolution: {integrity: sha512-Gl3b+bn7+oDDnqPa+4v/cg3yg9lnE8ppS7ivL3opBZh4i7h99JNmkm6zWmo0m2a83UUJu+C9D7lGP0OS8IlehA==} + engines: {node: '>=4.0.0'} + os: [darwin] + requiresBuild: true + dev: false + /p-limit/3.1.0: resolution: {integrity: sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==} engines: {node: '>=10'} diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 92f429264..8f9fc1fbf 100755 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -36,7 +36,9 @@ "total": "Total", "free": "Free", "used": "Used", - "load": "Load" + "load": "Load", + "temp": "TEMP", + "max": "Max" }, "unifi": { "users": "Users", diff --git a/src/components/widgets/resources/cputemp.jsx b/src/components/widgets/resources/cputemp.jsx new file mode 100644 index 000000000..d5cb61009 --- /dev/null +++ b/src/components/widgets/resources/cputemp.jsx @@ -0,0 +1,79 @@ +import useSWR from "swr"; +import { FaThermometerHalf } from "react-icons/fa"; +import { BiError } from "react-icons/bi"; +import { useTranslation } from "next-i18next"; + +export default function CpuTemp({ expanded, units }) { + const { t } = useTranslation(); + + const { data, error } = useSWR(`/api/widgets/resources?type=cputemp`, { + refreshInterval: 1500, + }); + + if (error || data?.error) { + return ( +
+ +
+ {t("widget.api_error")} +
+
+ ); + } + + if (!data) { + return ( +
+ +
+ +
-
+
{t("resources.temp")}
+
+ {expanded && ( + +
-
+
{t("resources.max")}
+
+ )} +
+
+ ); + } + + const unit = units === "imperial" ? "fahrenheit" : "celsius"; + const mainTemp = (unit === "celsius") ? data.cputemp.main : data.cputemp.main * 5/9 + 32; + const maxTemp = (unit === "celsius") ? data.cputemp.max : data.cputemp.max * 5/9 + 32; + + return ( +
+ +
+ +
+ {t("common.number", { + value: mainTemp, + maximumFractionDigits: 1, + style: "unit", + unit + })} +
+
{t("resources.temp")}
+
+ {expanded && ( + +
+ {t("common.number", { + value: maxTemp, + maximumFractionDigits: 1, + style: "unit", + unit + })} +
+
{t("resources.max")}
+
+ )} +
+
+ ); +} diff --git a/src/components/widgets/resources/resources.jsx b/src/components/widgets/resources/resources.jsx index 0524e39af..2238d2cc4 100644 --- a/src/components/widgets/resources/resources.jsx +++ b/src/components/widgets/resources/resources.jsx @@ -1,9 +1,10 @@ import Disk from "./disk"; import Cpu from "./cpu"; import Memory from "./memory"; +import CpuTemp from "./cputemp"; export default function Resources({ options }) { - const { expanded } = options; + const { expanded, units } = options; return (
@@ -12,6 +13,7 @@ export default function Resources({ options }) { {Array.isArray(options.disk) ? options.disk.map((disk) => ) : options.disk && } + {options.cputemp && }
{options.label && (
{options.label}
diff --git a/src/pages/api/widgets/resources.js b/src/pages/api/widgets/resources.js index 69bec041a..31257ff09 100644 --- a/src/pages/api/widgets/resources.js +++ b/src/pages/api/widgets/resources.js @@ -35,6 +35,12 @@ export default async function handler(req, res) { }); } + if (type === "cputemp") { + return res.status(200).json({ + cputemp: await si.cpuTemperature(), + }); + } + return res.status(400).json({ error: "invalid type", });