From d944298dd7c2fb4e979f28c04d5cce3e9a537b89 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 26 Jul 2016 21:33:00 +0100 Subject: [PATCH] Tidy up mqtt nodes - linting and done handling Closes #935 --- nodes/core/io/10-mqtt.js | 28 ++++++++++++++++------------ nodes/core/io/lib/mqtt.js | 4 ++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/nodes/core/io/10-mqtt.js b/nodes/core/io/10-mqtt.js index 791571e56..118bfc78c 100644 --- a/nodes/core/io/10-mqtt.js +++ b/nodes/core/io/10-mqtt.js @@ -85,13 +85,13 @@ module.exports = function(RED) { } // Create the URL to pass in to the MQTT.js library - if (this.brokerurl == "") { + if (this.brokerurl === "") { if (this.usetls) { this.brokerurl="mqtts://"; } else { this.brokerurl="mqtt://"; } - if (this.broker != "") { + if (this.broker !== "") { this.brokerurl = this.brokerurl+this.broker+":"+this.port; } else { this.brokerurl = this.brokerurl+"localhost:1883"; @@ -157,7 +157,7 @@ module.exports = function(RED) { return node.client.end(done); } else { node.client.end(); - done(); + return done(); } } done(); @@ -183,14 +183,18 @@ module.exports = function(RED) { // Re-subscribe to stored topics for (var s in node.subscriptions) { - var topic = s; - var qos = 0; - for (var r in node.subscriptions[s]) { - qos = Math.max(qos,node.subscriptions[s][r].qos); - node.client.on('message',node.subscriptions[s][r].handler); + if (node.subscriptions.hasOwnProperty(s)) { + var topic = s; + var qos = 0; + for (var r in node.subscriptions[s]) { + if (node.subscriptions[s].hasOwnProperty(r)) { + qos = Math.max(qos,node.subscriptions[s][r].qos); + node.client.on('message',node.subscriptions[s][r].handler); + } + } + var options = {qos: qos}; + node.client.subscribe(topic, options); } - var options = {qos: qos}; - node.client.subscribe(topic, options); } // Send any birth message @@ -260,7 +264,7 @@ module.exports = function(RED) { node.client.removeListener('message',sub[ref].handler); delete sub[ref]; } - if (Object.keys(sub).length == 0) { + if (Object.keys(sub).length === 0) { delete node.subscriptions[topic]; if (node.connected){ node.client.unsubscribe(topic); @@ -294,7 +298,7 @@ module.exports = function(RED) { done(); }); this.client.end(); - } if (this.connecting) { + } else if (this.connecting) { node.client.end(); done(); } else { diff --git a/nodes/core/io/lib/mqtt.js b/nodes/core/io/lib/mqtt.js index d2f3a6761..3f6efd312 100644 --- a/nodes/core/io/lib/mqtt.js +++ b/nodes/core/io/lib/mqtt.js @@ -87,7 +87,7 @@ MQTTClient.prototype.connect = function(options) { } }); client.on('connack',function(packet) { - if (packet.returnCode == 0) { + if (packet.returnCode === 0) { self.watchdog = setInterval(function(self) { var now = (new Date()).getTime(); @@ -230,7 +230,7 @@ MQTTClient.prototype.publish = function(topic,payload,qos,retain) { qos: qos||0, retain:retain||false }; - if (options.qos != 0) { + if (options.qos !== 0) { options.messageId = self._nextMessageId(); } this.lastOutbound = (new Date()).getTime()