import { publishActionReply } from '../../actionsHelper.js' import { isValidUuid } from '../../simRepository.js' export const methods = { /* Event-Rx: { "action": "STARTSIMULATION", "reqid": "6az5e4r6a", "sender": "", "roles": ["*"], "payload": { "simulationUuid": "...", "keyframeId": "...", "infraId": "..." } } */ async action_STARTSIMULATION(action, payload, reqid, sender, roles) { const replyOpts = { action, reqid, sender, replyChannel: this.config.maestro.maestroActionsReply, } if(!this.accessRights.canDo(roles, action)) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: 'Unauthorized action !', } }) return } if(!sender || !isValidUuid(sender)) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: 'Missing or invalid sender (user UUID)', } }) return } if(!payload?.simulationUuid || !payload?.keyframeId) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: 'Missing simulationUuid or keyframeId', } }) return } try { const result = await this.maestroSrv.startSimulation(sender, payload) if(!result.ok) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: result.err, } }) return } publishActionReply(this, { ...replyOpts, reply: { success: true, payload: { simulationId: result.simulationId, keyframeId: result.keyframeId, infraId: result.infraId, agentIds: result.agentIds, }, } }) } catch(err) { console.error(`[${this.redisId}] STARTSIMULATION failed:`, err) publishActionReply(this, { ...replyOpts, reply: { success: false, err: err.message ?? 'STARTSIMULATION failed', } }) } }, /* Event-Rx: { "action": "STOPSIMULATION", "reqid": "6az5e4r6a", "sender": "", "payload": { "simulationUuid": "..." } } */ async action_STOPSIMULATION(action, payload, reqid, sender, roles) { const replyOpts = { action, reqid, sender, replyChannel: this.config.maestro.maestroActionsReply, } if(!this.accessRights.canDo(roles, action)) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: 'Unauthorized action !', } }) return } if(!sender || !isValidUuid(sender)) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: 'Missing or invalid sender (user UUID)', } }) return } if(!payload?.simulationUuid) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: 'Missing simulationUuid', } }) return } try { const result = await this.maestroSrv.stopSimulation(sender, payload) if(!result.ok) { publishActionReply(this, { ...replyOpts, reply: { success: false, err: result.err, } }) return } publishActionReply(this, { ...replyOpts, reply: { success: true, payload: { simulationId: result.simulationId }, } }) } catch(err) { console.error(`[${this.redisId}] STOPSIMULATION failed:`, err) publishActionReply(this, { ...replyOpts, reply: { success: false, err: err.message ?? 'STOPSIMULATION failed', } }) } }, }