From c5c566bd8c334da5638b9447c1d3af46fc19965b Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Sun, 16 Nov 2025 16:45:47 +0100 Subject: [PATCH] extend action handling --- src/index.ts | 2 +- src/listener.ts | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 79e6820..804220f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,4 +28,4 @@ const bootEntries = await efiBootMgrService.listBootEntries(); const mqttClient = await getMqttClient(config.mqtt); runPublishLoop(config, mqttClient, bootEntries); -startListeners(config, mqttClient); +startListeners(config, mqttClient, bootEntries); diff --git a/src/listener.ts b/src/listener.ts index 75d356a..8be8619 100644 --- a/src/listener.ts +++ b/src/listener.ts @@ -1,27 +1,40 @@ import type { MqttClient } from "mqtt"; import type z from "zod"; import type { ConfigSchema } from "./config.schema"; +import type { BootEntries } from "./efibootmgr.schema"; import { logger } from "./logger"; export async function startListeners( config: z.output, mqttClient: MqttClient, + bootEntries: z.output, ) { await mqttClient.subscribeAsync(`efibootmgr-mqtt/${config.device.key}/+`); mqttClient.on("message", async (topic, payload) => { - logger.info(topic, payload); + logger.info(topic, payload.toString()); const pathParts = topic.split("/"); const action = pathParts[pathParts.length - 1]; switch (action) { - /* case "trigger-power": { - await nanoKvmService.triggerPower({}); + case "trigger-reboot": { + const bootEntry = bootEntries.find( + ({ number }) => Number.parseInt(payload.toString(), 10) === number, + ); + + if (!bootEntry) { + logger.fatal( + `Requested boot entry "${payload.toString()}" not found - doing nothing.`, + ); + } + + logger.info(`Rebooting into: ${bootEntry?.label}`); return; } + /* case "trigger-reset": { await nanoKvmService.triggerReset();