Add temperature module with dynamic temperature updates and critical state handling

This commit is contained in:
Supertiger
2025-08-02 10:31:50 +01:00
parent a99594b318
commit 5f455921fd
2 changed files with 44 additions and 0 deletions

View File

@@ -10,4 +10,8 @@ export const modules = {
import("./modules/battery").then( import("./modules/battery").then(
({ createBatteryModule }) => createBatteryModule ({ createBatteryModule }) => createBatteryModule
), ),
temperature: () =>
import("./modules/temperature").then(
({ createTemperatureModule }) => createTemperatureModule
),
}; };

View File

@@ -0,0 +1,40 @@
import type { WaybarConfig } from "../configParser";
import type { Module } from "../createModule";
import { createLabel } from "../Label";
export const createTemperatureModule = (
module: Module,
config: WaybarConfig["temperature"]
) => {
const label = createLabel({
interval: 1000,
update: () => update(),
config: config!,
module,
});
module.element.appendChild(label.element);
const update = async () => {
const temperatureC = Math.round(Math.random() * 90);
const temperatureF = Math.round(temperatureC * (9 / 5) + 32);
const criticalThreshold = config["critical-threshold"];
if (temperatureC >= criticalThreshold) {
module.element.classList.add("critical");
} else {
module.element.classList.remove("critical");
}
const maxTemp = criticalThreshold !== undefined ? criticalThreshold : 0;
label.set({
temperatureC,
temperatureF,
icon: label.getIcon(temperatureC, "", maxTemp),
});
};
update();
};