node-red-contrib-motechat
Version:
ultranet topic and payload communication
52 lines (41 loc) • 1.22 kB
JavaScript
module.exports = function (RED) {
"use strict"
let caller = {
call: require('../../lib/mcClient.js').call,
}
let getDDN = require('../../lib/mcClient.js').getDDN
let debug = require('../lib/mcClient.js').debug
function CachedStorage(config) {
RED.nodes.createNode(this, config)
var node = this
node.on('input', (msg, send, done) => {
try {
send = send || function () { node.send.apply(node, arguments) }
let func = config.func
let name = config.name
let DDN = "sys/"
let topic = 'xs://cached';
if (!name) {
debug('name should not be empty !!!')
return
}
if (!func) {
debug('func should not be empty !!!')
return
}
caller.call(topic, DDN, func, msg.payload).then(reply => {
let newMsg = Object.assign(msg, {
hostDDN: getDDN(),
name: name,
payload: reply
})
send([newMsg, null])
}).catch(err => send([null, err]))
.finally(() => { if (done) done() })
} catch (err) {
send([null, err])
}
})
}
RED.nodes.registerType("cached-storage", CachedStorage)
}