const fs = require("fs"); async function createPlugin(ctx) { return { async execute(action) { if (action === "runCheck") { return runCheck(ctx); } throw new Error(`Unknown action: ${action}`); }, async getStatus() { return readStatus(ctx); }, async health() { return { ok: true }; } }; } async function runCheck(ctx) { const command = String(ctx.getSetting("checkCommand", ctx.env.VSWR_CHECK_CMD || "")).trim(); if (!command) { return { ok: true, skipped: true, message: "VSWR_CHECK_CMD nicht gesetzt" }; } const simulate = typeof ctx.simulateHardware === "boolean" ? ctx.simulateHardware : (process.platform !== "linux" && String(ctx.env.ALLOW_NON_LINUX_CMDS || "false") !== "true"); if (simulate) { return { ok: true, skipped: true, message: `VSWR Check simuliert (${ctx.execMode || "dev"})`, status: readStatus(ctx) }; } const result = await ctx.commandRunner(command, { timeoutMs: Number(ctx.getSetting("timeoutMs", ctx.env.VSWR_CHECK_TIMEOUT_MS || 240000)) }); if (!result.ok) { throw new Error(result.stderr || result.error || "VSWR check failed"); } return { ok: true, message: "VSWR check abgeschlossen", status: readStatus(ctx) }; } function readStatus(ctx) { const metadataPath = String(ctx.getSetting("metadataPath", ctx.env.VSWR_METADATA_PATH || "")).trim(); if (!metadataPath || !fs.existsSync(metadataPath)) { return { status: "UNKNOWN", path: metadataPath || null }; } const raw = fs.readFileSync(metadataPath, "utf8").trim(); return { status: raw || "UNKNOWN", path: metadataPath }; } module.exports = { createPlugin };