extend action handling
This commit is contained in:
@@ -28,4 +28,4 @@ const bootEntries = await efiBootMgrService.listBootEntries();
|
|||||||
const mqttClient = await getMqttClient(config.mqtt);
|
const mqttClient = await getMqttClient(config.mqtt);
|
||||||
|
|
||||||
runPublishLoop(config, mqttClient, bootEntries);
|
runPublishLoop(config, mqttClient, bootEntries);
|
||||||
startListeners(config, mqttClient);
|
startListeners(config, mqttClient, bootEntries);
|
||||||
|
|||||||
@@ -1,27 +1,40 @@
|
|||||||
import type { MqttClient } from "mqtt";
|
import type { MqttClient } from "mqtt";
|
||||||
import type z from "zod";
|
import type z from "zod";
|
||||||
import type { ConfigSchema } from "./config.schema";
|
import type { ConfigSchema } from "./config.schema";
|
||||||
|
import type { BootEntries } from "./efibootmgr.schema";
|
||||||
import { logger } from "./logger";
|
import { logger } from "./logger";
|
||||||
|
|
||||||
export async function startListeners(
|
export async function startListeners(
|
||||||
config: z.output<typeof ConfigSchema>,
|
config: z.output<typeof ConfigSchema>,
|
||||||
mqttClient: MqttClient,
|
mqttClient: MqttClient,
|
||||||
|
bootEntries: z.output<typeof BootEntries>,
|
||||||
) {
|
) {
|
||||||
await mqttClient.subscribeAsync(`efibootmgr-mqtt/${config.device.key}/+`);
|
await mqttClient.subscribeAsync(`efibootmgr-mqtt/${config.device.key}/+`);
|
||||||
|
|
||||||
mqttClient.on("message", async (topic, payload) => {
|
mqttClient.on("message", async (topic, payload) => {
|
||||||
logger.info(topic, payload);
|
logger.info(topic, payload.toString());
|
||||||
|
|
||||||
const pathParts = topic.split("/");
|
const pathParts = topic.split("/");
|
||||||
|
|
||||||
const action = pathParts[pathParts.length - 1];
|
const action = pathParts[pathParts.length - 1];
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
/* case "trigger-power": {
|
case "trigger-reboot": {
|
||||||
await nanoKvmService.triggerPower({});
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
case "trigger-reset": {
|
case "trigger-reset": {
|
||||||
await nanoKvmService.triggerReset();
|
await nanoKvmService.triggerReset();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user