Add the reusable RMS core application (server, web UI, plugins, tests, tools) with generic defaults, GPL licensing, and maintainer context documentation so deployments can consume this repo as software source independent of station-specific overlays.
57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
async function createPlugin(ctx) {
|
|
return {
|
|
async execute(action) {
|
|
if (action !== "getTxState") {
|
|
throw new Error(`Unknown action: ${action}`);
|
|
}
|
|
return readTxState(ctx);
|
|
},
|
|
async getStatus() {
|
|
return readTxState(ctx);
|
|
},
|
|
async health() {
|
|
return { ok: true };
|
|
}
|
|
};
|
|
}
|
|
|
|
function readTxState(ctx) {
|
|
const stateFilePath = resolvePath(String(ctx.getSetting("stateFilePath", ctx.env.TX_STATE_PATH || "./data/tx-state.json")));
|
|
const fallback = {
|
|
txActive: false,
|
|
source: "tx-state-file",
|
|
updatedAt: null,
|
|
path: stateFilePath
|
|
};
|
|
if (!stateFilePath || !fs.existsSync(stateFilePath)) {
|
|
return fallback;
|
|
}
|
|
try {
|
|
const raw = fs.readFileSync(stateFilePath, "utf8");
|
|
const parsed = JSON.parse(raw);
|
|
return {
|
|
txActive: Boolean(parsed && parsed.txActive),
|
|
source: "tx-state-file",
|
|
updatedAt: parsed && parsed.updatedAt ? parsed.updatedAt : null,
|
|
path: stateFilePath,
|
|
details: parsed && typeof parsed === "object" ? parsed : null
|
|
};
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
function resolvePath(value) {
|
|
const v = String(value || "").trim();
|
|
if (!v) return "";
|
|
if (path.isAbsolute(v)) return v;
|
|
return path.resolve(process.cwd(), v);
|
|
}
|
|
|
|
module.exports = {
|
|
createPlugin
|
|
};
|