From 7ad28de52a878932c3a396882652c65a5f752f7c Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Thu, 29 May 2014 09:00:28 +0100 Subject: [PATCH] Add use strict to mqtt node, allow will parm to be passed. --- nodes/core/io/10-mqtt.js | 37 ++++++++++++------------- nodes/core/io/lib/mqttConnectionPool.js | 4 +-- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/nodes/core/io/10-mqtt.js b/nodes/core/io/10-mqtt.js index 3b9c9befa..3ecbb86e9 100644 --- a/nodes/core/io/10-mqtt.js +++ b/nodes/core/io/10-mqtt.js @@ -15,10 +15,10 @@ **/ module.exports = function(RED) { - + "use strict"; var connectionPool = require("./lib/mqttConnectionPool"); var util = require("util"); - + function MQTTBrokerNode(n) { RED.nodes.createNode(this,n); this.broker = n.broker; @@ -28,12 +28,12 @@ module.exports = function(RED) { if (credentials) { this.username = credentials.user; this.password = credentials.password; - } + } } RED.nodes.registerType("mqtt-broker",MQTTBrokerNode); - + var querystring = require('querystring'); - + RED.httpAdmin.get('/mqtt-broker/:id',function(req,res) { var credentials = RED.nodes.getCredentials(req.params.id); if (credentials) { @@ -42,12 +42,12 @@ module.exports = function(RED) { res.send(JSON.stringify({})); } }); - + RED.httpAdmin.delete('/mqtt-broker/:id',function(req,res) { RED.nodes.deleteCredentials(req.params.id); res.send(200); }); - + RED.httpAdmin.post('/mqtt-broker/:id',function(req,res) { var body = ""; req.on('data', function(chunk) { @@ -70,8 +70,8 @@ module.exports = function(RED) { res.send(200); }); }); - - + + function MQTTInNode(n) { RED.nodes.createNode(this,n); this.topic = n.topic; @@ -100,25 +100,25 @@ module.exports = function(RED) { this.error("missing broker configuration"); } } - + RED.nodes.registerType("mqtt in",MQTTInNode); - + MQTTInNode.prototype.close = function() { if (this.client) { this.client.disconnect(); } } - - + + function MQTTOutNode(n) { RED.nodes.createNode(this,n); - + this.topic = n.topic; this.broker = n.broker; - + this.brokerConfig = RED.nodes.getNode(this.broker); var node = this; - + if (this.brokerConfig) { 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); @@ -142,13 +142,12 @@ module.exports = function(RED) { this.error("missing broker configuration"); } } - + RED.nodes.registerType("mqtt out",MQTTOutNode); - + MQTTOutNode.prototype.close = function() { if (this.client) { this.client.disconnect(); } } } - diff --git a/nodes/core/io/lib/mqttConnectionPool.js b/nodes/core/io/lib/mqttConnectionPool.js index ebd3dc672..d15f0fc7c 100644 --- a/nodes/core/io/lib/mqttConnectionPool.js +++ b/nodes/core/io/lib/mqttConnectionPool.js @@ -28,7 +28,7 @@ function matchTopic(ts,t) { } module.exports = { - get: function(broker,port,clientid,username,password) { + get: function(broker,port,clientid,username,password,will) { var id = "["+(username||"")+":"+(password||"")+"]["+(clientid||"")+"]@"+broker+":"+port; if (!connections[id]) { connections[id] = function() { @@ -40,6 +40,7 @@ module.exports = { options.clientId = clientid || 'mqtt_' + (1+Math.random()*4294967295).toString(16); options.username = username; options.password = password; + options.will = will; var queue = []; var subscriptions = []; var connecting = false; @@ -125,4 +126,3 @@ module.exports = { return connections[id]; } }; -