153 lines
4.3 KiB
JavaScript
153 lines
4.3 KiB
JavaScript
import { publishActionReply, parseSimTime } from '../../actionsHelper.js'
|
|
|
|
export const methods = {
|
|
|
|
/* Event-Rx:
|
|
{
|
|
"action": "GETAGENTPOSITION",
|
|
"reqid": "6az5e4r6a",
|
|
"payload": {
|
|
"agentId": "agent42",
|
|
"t": 12.5
|
|
}
|
|
}
|
|
Event-Tx:
|
|
{
|
|
"action": "GETAGENTPOSITION",
|
|
"success": true,
|
|
"reqid": "6az5e4r6a",
|
|
"payload": {
|
|
"agent": {
|
|
"id": "agent42",
|
|
"position": { "x": 1, "y": 2, "z": 3 },
|
|
"vector": { "x": 0, "y": 0, "z": 0 },
|
|
"since": 0,
|
|
"generation": 2,
|
|
"t": 12.5
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
async action_GETAGENTPOSITION(action, payload, reqid, sender, roles) {
|
|
const replyOpts = {
|
|
action,
|
|
reqid,
|
|
sender,
|
|
replyChannel: this.config.gps.gpsActionsReply,
|
|
}
|
|
if(!this.accessRights.canDo(roles, action)) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Unauthorized action !',
|
|
} })
|
|
return
|
|
}
|
|
|
|
if(!this.gpsSrv.isLive()) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Simulation not live',
|
|
} })
|
|
return
|
|
}
|
|
|
|
const agentId = payload?.agentId
|
|
if(!agentId || typeof(agentId) !== 'string') {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Missing or invalid agentId',
|
|
} })
|
|
return
|
|
}
|
|
|
|
const at = parseSimTime(payload, () => this.gpsSrv.now())
|
|
if(at === null) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Invalid simulation time',
|
|
} })
|
|
return
|
|
}
|
|
|
|
const agent = this.gpsSrv.getAgentPosition(agentId, at)
|
|
if(!agent) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: `Unknown agent: ${agentId}`,
|
|
} })
|
|
return
|
|
}
|
|
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: true,
|
|
payload: { agent },
|
|
} })
|
|
},
|
|
|
|
/* Event-Rx:
|
|
{
|
|
"action": "GETAGENTSINPRISM",
|
|
"reqid": "6az5e4r6a",
|
|
"payload": {
|
|
"prism": {
|
|
"xMin": -10, "xMax": 10,
|
|
"yMin": -10, "yMax": 10,
|
|
"zMin": 0, "zMax": 5
|
|
},
|
|
"t": 0
|
|
}
|
|
}
|
|
*/
|
|
async action_GETAGENTSINPRISM(action, payload, reqid, sender, roles) {
|
|
const replyOpts = {
|
|
action,
|
|
reqid,
|
|
sender,
|
|
replyChannel: this.config.gps.gpsActionsReply,
|
|
}
|
|
if(!this.accessRights.canDo(roles, action)) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Unauthorized action !',
|
|
} })
|
|
return
|
|
}
|
|
|
|
if(!this.gpsSrv.isLive()) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Simulation not live',
|
|
} })
|
|
return
|
|
}
|
|
|
|
const prism = payload?.prism
|
|
if(!this.gpsSrv.isValidPrism(prism)) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Missing or invalid prism bounds',
|
|
} })
|
|
return
|
|
}
|
|
|
|
const at = parseSimTime(payload, () => this.gpsSrv.now())
|
|
if(at === null) {
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: false,
|
|
err: 'Invalid simulation time',
|
|
} })
|
|
return
|
|
}
|
|
|
|
const agents = this.gpsSrv.getAgentsInPrism(prism, at)
|
|
publishActionReply(this, { ...replyOpts, reply: {
|
|
success: true,
|
|
payload: {
|
|
agents,
|
|
t: at,
|
|
},
|
|
} })
|
|
},
|
|
|
|
}
|