1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add use strict to mqtt node, allow will parm to be passed.

This commit is contained in:
Dave C-J 2014-05-29 09:00:28 +01:00
parent 1d5e8de6f6
commit 7ad28de52a
2 changed files with 20 additions and 21 deletions

View File

@ -15,10 +15,10 @@
**/ **/
module.exports = function(RED) { module.exports = function(RED) {
"use strict";
var connectionPool = require("./lib/mqttConnectionPool"); var connectionPool = require("./lib/mqttConnectionPool");
var util = require("util"); var util = require("util");
function MQTTBrokerNode(n) { function MQTTBrokerNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.broker = n.broker; this.broker = n.broker;
@ -28,12 +28,12 @@ module.exports = function(RED) {
if (credentials) { if (credentials) {
this.username = credentials.user; this.username = credentials.user;
this.password = credentials.password; this.password = credentials.password;
} }
} }
RED.nodes.registerType("mqtt-broker",MQTTBrokerNode); RED.nodes.registerType("mqtt-broker",MQTTBrokerNode);
var querystring = require('querystring'); var querystring = require('querystring');
RED.httpAdmin.get('/mqtt-broker/:id',function(req,res) { RED.httpAdmin.get('/mqtt-broker/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id); var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) { if (credentials) {
@ -42,12 +42,12 @@ module.exports = function(RED) {
res.send(JSON.stringify({})); res.send(JSON.stringify({}));
} }
}); });
RED.httpAdmin.delete('/mqtt-broker/:id',function(req,res) { RED.httpAdmin.delete('/mqtt-broker/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id); RED.nodes.deleteCredentials(req.params.id);
res.send(200); res.send(200);
}); });
RED.httpAdmin.post('/mqtt-broker/:id',function(req,res) { RED.httpAdmin.post('/mqtt-broker/:id',function(req,res) {
var body = ""; var body = "";
req.on('data', function(chunk) { req.on('data', function(chunk) {
@ -70,8 +70,8 @@ module.exports = function(RED) {
res.send(200); res.send(200);
}); });
}); });
function MQTTInNode(n) { function MQTTInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.topic = n.topic; this.topic = n.topic;
@ -100,25 +100,25 @@ module.exports = function(RED) {
this.error("missing broker configuration"); this.error("missing broker configuration");
} }
} }
RED.nodes.registerType("mqtt in",MQTTInNode); RED.nodes.registerType("mqtt in",MQTTInNode);
MQTTInNode.prototype.close = function() { MQTTInNode.prototype.close = function() {
if (this.client) { if (this.client) {
this.client.disconnect(); this.client.disconnect();
} }
} }
function MQTTOutNode(n) { function MQTTOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.topic = n.topic; this.topic = n.topic;
this.broker = n.broker; this.broker = n.broker;
this.brokerConfig = RED.nodes.getNode(this.broker); this.brokerConfig = RED.nodes.getNode(this.broker);
var node = this; var node = this;
if (this.brokerConfig) { if (this.brokerConfig) {
this.status({fill:"red",shape:"ring",text:"disconnected"},true); this.status({fill:"red",shape:"ring",text:"disconnected"},true);
this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password); this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password);
@ -142,13 +142,12 @@ module.exports = function(RED) {
this.error("missing broker configuration"); this.error("missing broker configuration");
} }
} }
RED.nodes.registerType("mqtt out",MQTTOutNode); RED.nodes.registerType("mqtt out",MQTTOutNode);
MQTTOutNode.prototype.close = function() { MQTTOutNode.prototype.close = function() {
if (this.client) { if (this.client) {
this.client.disconnect(); this.client.disconnect();
} }
} }
} }

View File

@ -28,7 +28,7 @@ function matchTopic(ts,t) {
} }
module.exports = { module.exports = {
get: function(broker,port,clientid,username,password) { get: function(broker,port,clientid,username,password,will) {
var id = "["+(username||"")+":"+(password||"")+"]["+(clientid||"")+"]@"+broker+":"+port; var id = "["+(username||"")+":"+(password||"")+"]["+(clientid||"")+"]@"+broker+":"+port;
if (!connections[id]) { if (!connections[id]) {
connections[id] = function() { connections[id] = function() {
@ -40,6 +40,7 @@ module.exports = {
options.clientId = clientid || 'mqtt_' + (1+Math.random()*4294967295).toString(16); options.clientId = clientid || 'mqtt_' + (1+Math.random()*4294967295).toString(16);
options.username = username; options.username = username;
options.password = password; options.password = password;
options.will = will;
var queue = []; var queue = [];
var subscriptions = []; var subscriptions = [];
var connecting = false; var connecting = false;
@ -125,4 +126,3 @@ module.exports = {
return connections[id]; return connections[id];
} }
}; };