const CONTROL_COMMANDS = Object.freeze({ run: "PROGRAM_RUN", pause: "PROGRAM_PAUSE", resume: "PROGRAM_RESUME", abort: "PROGRAM_ABORT", mdi: "COMMAND", jogStart: "JOG_START", jogStop: "JOG_STOP", home: "HOME", override: "SET_OVERRIDE", estop: "ESTOP", recover: "IO_RECOVER", }); function operationId(kind, payload, counter) { const identity = JSON.stringify(payload, Object.keys(payload ?? {}).sort()).replace(/[^A-Za-z0-9]+/g, "-").slice(0, 40); return `ui-${kind.toLowerCase()}-${counter}${identity ? `-${identity}` : ""}`; } export class CommandController { constructor(client) { if (!client || typeof client.send !== "function") throw new Error("command_client_required"); this.client = client; this.counter = 0; this.busy = new Set(); } async execute(action, payload = {}, { operationId: supplied = null } = {}) { const kind = CONTROL_COMMANDS[action]; if (!kind) throw new Error("command_action_unknown"); const id = supplied ?? operationId(kind, payload, ++this.counter); if (this.busy.has(id)) throw new Error("command_operation_busy"); this.busy.add(id); try { return await this.client.send(kind, payload, { operationId: id }); } finally { this.busy.delete(id); } } } export { CONTROL_COMMANDS };