UNPKG

node-red-contrib-ibm-mq

Version:

IBM MQ read/write nodes

148 lines (144 loc) 6.14 kB
module.exports = function (RED) { "use strict"; var EventBus = require('vertx3-eventbus-client'); function IBMMQReadNode(config) { RED.nodes.createNode(this, config); var node = this; var timeout; var _msg; this.url = config.url || 'http://localhost:8080/eventbus/'; this.eb = new EventBus(this.url); this.host = config.host; this.port = config.port; this.channel = config.channel; this.user = config.user; this.pass = config.pass; this.qmname = config.qmname; this.qname = config.qname; config.count = parseInt(config.count) ? parseInt(config.count) : 1000; this.count = config.count; this.active = config.active; /*this.timer = setInterval(function () { if (node.active) { node.send({payload: "hello world2"}); } }.bind(this), this.interval);*/ this.eb.onopen = function () { //console.log("handeler for nodered."+node.id) node.eb.registerHandler("nodered." + node.id, function (err, msg) { //console.dir(msg); //if (node.active) { var body = msg.body; if (body.count > 0) { node.send({ payload: msg.body.msgs, count: msg.body.count, id: node.id, name: node.name, type: node.type, timestamp: Date.now() }); msg.reply(); } else { _msg = msg; timeout = setTimeout(function () { _msg.reply(); timeout = null; }, 3000); } //} }); config.cmd = "deploy"; node.eb.send("vertx.cmd", JSON.stringify(config), function (ar_err, ar) { if (ar_err == null) { if (node.active) node.status({fill: "green", shape: "dot", text: "connected"}); else node.status({fill: "green", shape: "ring", text: "deployed"}); } else { node.status({fill: "red", shape: "ring", text: "error!"}); node.error({ payload: "Received deploy reply: " + JSON.stringify(ar_err), Type: node.type, ID: node.id }); } }); }; this.on('close', function () { config.cmd = "undeploy"; node.eb.send("vertx.cmd", JSON.stringify(config)); if (timeout) {//Do not make server pending for reply if there is. clearTimeout(timeout); timeout = null; _msg.reply(); } node.eb.close(); node.eb = null; }); this.eb.onclose = function () { //console.log('close................'); }; } RED.nodes.registerType("ibm-mq-read", IBMMQReadNode); RED.httpAdmin.post("/ibm-mq/:id/:state", RED.auth.needsPermission("ibm-mq-read.write"), function (req, res) { var node = RED.nodes.getNode(req.params.id); var state = req.params.state; if (node !== null && typeof node !== "undefined") { if (state === "enable") { //node.active = true; node.eb.send('vertx.' + node.id, { cmd: "start", type: "mq-read", nodeID: node.id }, function (ar_err, ar) { if (ar_err == null) { node.log({ payload: "Received start reply: " + JSON.stringify(ar.body), Type: node.type, ID: node.id }); node.active = true; node.status({fill: "green", shape: "dot", text: "connected"}); res.sendStatus(200); } else { node.error({ payload: "Received start reply: " + JSON.stringify(ar_err), Type: node.type, ID: node.id }); res.sendStatus(404); } }); } else if (state === "disable") { //node.active = false; node.eb.send('vertx.' + node.id, { cmd: "stop", type: "mq-read", nodeID: node.id }, function (ar_err, ar) { if (ar_err == null) { node.log({ payload: "Received stop reply: " + JSON.stringify(ar.body), Type: node.type, ID: node.id }); node.status({fill: "red", shape: "dot", text: "disconnected"}); node.active = false; res.sendStatus(201); } else { node.error({ payload: "Received stop reply: " + JSON.stringify(ar_err), Type: node.type, ID: node.id }); res.sendStatus(404); } }); } else { res.sendStatus(404); } } else { res.sendStatus(404); } }); };