33 lines
1.7 KiB
JavaScript
Executable File
33 lines
1.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
// Decoder simulator from the command line (the same one as Settings → Decoder simulator):
|
|
// npm run sim [-- --lap 18 --spread 1.5 --port 9000 --app http://127.0.0.1:8081]
|
|
// Port 9000 is the app's default decoder address, so the app connects without any change.
|
|
import { createRequire } from "node:module";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const { createDecoderSimulator } = require("../server/decoder_sim.js");
|
|
|
|
const args = Object.fromEntries(process.argv.slice(2).reduce((pairs, arg, index, all) => {
|
|
if (arg.startsWith("--")) pairs.push([arg.slice(2), all[index + 1] && !all[index + 1].startsWith("--") ? all[index + 1] : "true"]);
|
|
return pairs;
|
|
}, []));
|
|
const app = String(args.app || `http://127.0.0.1:${process.env.PORT || 8081}`);
|
|
const fixed = args.tp ? String(args.tp).split(",").map((value) => value.trim()).filter(Boolean) : null;
|
|
|
|
const sim = createDecoderSimulator({
|
|
// --tp: these transponders; otherwise the active heat of the running app.
|
|
getState: async () => {
|
|
if (fixed) return { drivers: fixed.map((transponder, index) => ({ id: `tp${index}`, transponder })), sessions: [] };
|
|
const response = await fetch(`${app}/api/state`);
|
|
return (await response.json())?.state || null;
|
|
},
|
|
log: (line) => console.log(line),
|
|
});
|
|
|
|
sim.start({ port: Number(args.port || 9000), host: "127.0.0.1", lapSec: Number(args.lap || 18), spreadSec: Number(args.spread ?? 1.5) })
|
|
.then(() => console.log("Ctrl+C stops."))
|
|
.catch((error) => {
|
|
console.error(`${error.message} - probably AMMC or the simulator started from Settings. Try: npm run sim -- --port 9100 and set the decoder address to ws://127.0.0.1:9100.`);
|
|
process.exit(1);
|
|
});
|