29 lines
1.0 KiB
JavaScript
29 lines
1.0 KiB
JavaScript
|
|
export function dispatchMessage(redisCnx, msg, chan) {
|
|
const gps = redisCnx.config.gps
|
|
if(!gps?.gpsActionsChannel) return
|
|
|
|
const actionsChan = redisCnx.fullChan(gps.gpsActionsChannel)
|
|
if(chan != actionsChan) return
|
|
|
|
const action = msg.action
|
|
if(!action || typeof(action) !== 'string') {
|
|
console.warn(`[${redisCnx.redisId}] Ignoring message without action on ${chan}`)
|
|
return
|
|
}
|
|
|
|
const handler = redisCnx['action_'+action]
|
|
if(typeof(handler) != 'function') {
|
|
if(redisCnx.debug) console.warn(`[${redisCnx.redisId}] Unknown action ${action} on ${chan}`)
|
|
return
|
|
}
|
|
|
|
const payload = ('payload' in msg) ? msg.payload : null
|
|
const reqid = ('reqid' in msg) ? msg.reqid.substr(0, 50) : null
|
|
const sender = msg.sender || null
|
|
const roles = Array.isArray(msg.roles) ? msg.roles : ['*']
|
|
|
|
if(redisCnx.debug) console.log(`[${redisCnx.redisId}] Dispatching action ${action} from ${sender}`)
|
|
handler.call(redisCnx, action, payload, reqid, sender, roles)
|
|
}
|