From 9f900788f82232d10a963bc74786762ad9e043b4 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Mon, 20 Oct 2014 10:51:11 +0100 Subject: [PATCH] Added mqlight nodes with share property --- io/mqlight/mqlight.html | 154 ++++++++++++++++++++++++++++++++++++++++ io/mqlight/mqlight.js | 140 ++++++++++++++++++++++++++++++++++++ 2 files changed, 294 insertions(+) create mode 100644 io/mqlight/mqlight.html create mode 100644 io/mqlight/mqlight.js diff --git a/io/mqlight/mqlight.html b/io/mqlight/mqlight.html new file mode 100644 index 00000000..16ff0d0f --- /dev/null +++ b/io/mqlight/mqlight.html @@ -0,0 +1,154 @@ + + + + + + + + + + + + + + + + + diff --git a/io/mqlight/mqlight.js b/io/mqlight/mqlight.js new file mode 100644 index 00000000..4049a1ed --- /dev/null +++ b/io/mqlight/mqlight.js @@ -0,0 +1,140 @@ +/** + * Copyright 2013,2014 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +module.exports = function(RED) { + "use strict"; + var mqlight = require('mqlight'); + var util = require("util"); + + function MQLightServiceNode(n) { + RED.nodes.createNode(this,n); + + var id = "mqlight_" + (n.clientid ? n.clientid : (1+Math.random()*4294967295).toString(16)); + + var opts = { + service: n.service, + id: id + }; + + if (this.credentials) { + opts.user = this.credentials.user; + opts.password = this.credentials.password; + } + + this.client = mqlight.createClient(opts, function(err) { + if (err) { + util.log('[mqlight] ['+id+'] not connected to service '+n.service); + } else { + util.log('[mqlight] ['+id+'] connected to service '+n.service); + } + }); + } + RED.nodes.registerType("mqlight service",MQLightServiceNode,{ + credentials: { + user: {type:"text"}, + password: {type: "password"} + } + }); + + function MQLightIn(n) { + RED.nodes.createNode(this, n); + this.topic = n.topic || ""; + this.share = n.share || null; + this.service = n.service; + this.serviceConfig = RED.nodes.getNode(this.service); + var node = this; + + if (node.serviceConfig) { + if (node.serviceConfig.client) { + var recvClient = node.serviceConfig.client; + recvClient.on("started", function() { + recvClient.on("message", function(data, delivery) { + var msg = { + topic: delivery.message.topic, + payload: data, + _session: { + type: "mqlight", + id: recvClient.id + } + }; + if (delivery.destination.share) { + msg.share = delivery.destination.share; + } + node.send(msg); + }); + var subscribeCallback = function(err) { + if (err) { + node.error("Failed to subscribe: " + err); + } else { + node.log("Subscribed to "+node.topic+(node.share?" ["+node.share+"]":"")); + } + }; + + if (node.share) { + recvClient.subscribe(node.topic, node.share, subscribeCallback); + } else { + recvClient.subscribe(node.topic, subscribeCallback); + } + }); + recvClient.start(); + + node.on("close", function (done) { + recvClient.stop(done); + }); + } + + } + } + RED.nodes.registerType("mqlight in", MQLightIn); + + function MQLightOut(n) { + RED.nodes.createNode(this, n); + this.topic = n.topic || ""; + this.service = n.service; + this.serviceConfig = RED.nodes.getNode(this.service); + var node = this; + + if (node.serviceConfig) { + if (node.serviceConfig.client) { + var sendClient = node.serviceConfig.client; + + sendClient.on("started", function () { + node.on("input", function(msg) { + if (node.topic === "") { + if (msg.topic) { + node.topic = msg.topic; + } else { + node.warn("No topic set in MQ Light out node"); + return; + } + } + sendClient.send(node.topic, msg.payload, function(err) { + if (err) { + node.error(err); + } + }); + }); + }); + sendClient.start(); + + node.on("close", function (done) { + sendClient.stop(done); + }); + } + } + } + RED.nodes.registerType("mqlight out", MQLightOut); +};