UNPKG

node-red-contrib-motechat

Version:

ultranet topic and payload communication

49 lines (43 loc) 1.73 kB
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 let isReg = require('../lib/mcClient.js').isRegist function call(config) { RED.nodes.createNode(this, config) let node = this node.on('input', (msg, send, done) => { send = send || function () { node.send.apply(node, arguments) } let [DDN, topic, func, name] = [ RED.util.evaluateNodeProperty(config.DDN, config.DDNType, node, msg), RED.util.evaluateNodeProperty(config.topic, config.topicType, node, msg), RED.util.evaluateNodeProperty(config.func, config.funcType, node, msg), config.name ] let { t1, t2 } = msg if (!name) { debug('name should not be empty !!!') return } if (isReg()){ caller.call(topic, DDN, func, { t1, t2 }, 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() }) } else{ send([null, "Motechat hasn't registered!"]) debug("Motechat hasn't registered!") } }) } RED.nodes.registerType("call", call) }