2014-10-20 11:51:11 +02:00
|
|
|
/**
|
2015-02-13 11:32:43 +01:00
|
|
|
* Copyright 2013, 2015 IBM Corp.
|
2014-10-20 11:51:11 +02:00
|
|
|
*
|
|
|
|
* 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) {
|
2015-04-17 22:09:47 +02:00
|
|
|
var recvClient = node.serviceConfig.client;
|
2014-10-20 11:51:11 +02:00
|
|
|
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);
|
|
|
|
});
|
2015-04-17 22:09:47 +02:00
|
|
|
recvClient.on("error", function(err) {
|
|
|
|
if (err) {
|
|
|
|
node.error(err.toString());
|
|
|
|
}
|
|
|
|
});
|
2014-10-20 11:51:11 +02:00
|
|
|
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) {
|
2015-02-13 11:32:43 +01:00
|
|
|
var topic = node.topic;
|
|
|
|
if (topic === "") {
|
2014-10-20 11:51:11 +02:00
|
|
|
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) {
|
2015-04-17 22:14:20 +02:00
|
|
|
node.error(err,msg);
|
2014-10-20 11:51:11 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-04-17 22:09:47 +02:00
|
|
|
sendClient.on("error", function(err) {
|
|
|
|
if (err) {
|
|
|
|
node.error(err.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-10-20 11:51:11 +02:00
|
|
|
sendClient.start();
|
|
|
|
|
|
|
|
node.on("close", function (done) {
|
|
|
|
sendClient.stop(done);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("mqlight out", MQLightOut);
|
|
|
|
};
|