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

Tidy up mqtt nodes - linting and done handling

Closes #935
This commit is contained in:
Nick O'Leary 2016-07-26 21:33:00 +01:00
parent 0136ebd2b4
commit d944298dd7
2 changed files with 18 additions and 14 deletions

View File

@ -85,13 +85,13 @@ module.exports = function(RED) {
} }
// Create the URL to pass in to the MQTT.js library // Create the URL to pass in to the MQTT.js library
if (this.brokerurl == "") { if (this.brokerurl === "") {
if (this.usetls) { if (this.usetls) {
this.brokerurl="mqtts://"; this.brokerurl="mqtts://";
} else { } else {
this.brokerurl="mqtt://"; this.brokerurl="mqtt://";
} }
if (this.broker != "") { if (this.broker !== "") {
this.brokerurl = this.brokerurl+this.broker+":"+this.port; this.brokerurl = this.brokerurl+this.broker+":"+this.port;
} else { } else {
this.brokerurl = this.brokerurl+"localhost:1883"; this.brokerurl = this.brokerurl+"localhost:1883";
@ -157,7 +157,7 @@ module.exports = function(RED) {
return node.client.end(done); return node.client.end(done);
} else { } else {
node.client.end(); node.client.end();
done(); return done();
} }
} }
done(); done();
@ -183,14 +183,18 @@ module.exports = function(RED) {
// Re-subscribe to stored topics // Re-subscribe to stored topics
for (var s in node.subscriptions) { for (var s in node.subscriptions) {
var topic = s; if (node.subscriptions.hasOwnProperty(s)) {
var qos = 0; var topic = s;
for (var r in node.subscriptions[s]) { var qos = 0;
qos = Math.max(qos,node.subscriptions[s][r].qos); for (var r in node.subscriptions[s]) {
node.client.on('message',node.subscriptions[s][r].handler); 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 // Send any birth message
@ -260,7 +264,7 @@ module.exports = function(RED) {
node.client.removeListener('message',sub[ref].handler); node.client.removeListener('message',sub[ref].handler);
delete sub[ref]; delete sub[ref];
} }
if (Object.keys(sub).length == 0) { if (Object.keys(sub).length === 0) {
delete node.subscriptions[topic]; delete node.subscriptions[topic];
if (node.connected){ if (node.connected){
node.client.unsubscribe(topic); node.client.unsubscribe(topic);
@ -294,7 +298,7 @@ module.exports = function(RED) {
done(); done();
}); });
this.client.end(); this.client.end();
} if (this.connecting) { } else if (this.connecting) {
node.client.end(); node.client.end();
done(); done();
} else { } else {

View File

@ -87,7 +87,7 @@ MQTTClient.prototype.connect = function(options) {
} }
}); });
client.on('connack',function(packet) { client.on('connack',function(packet) {
if (packet.returnCode == 0) { if (packet.returnCode === 0) {
self.watchdog = setInterval(function(self) { self.watchdog = setInterval(function(self) {
var now = (new Date()).getTime(); var now = (new Date()).getTime();
@ -230,7 +230,7 @@ MQTTClient.prototype.publish = function(topic,payload,qos,retain) {
qos: qos||0, qos: qos||0,
retain:retain||false retain:retain||false
}; };
if (options.qos != 0) { if (options.qos !== 0) {
options.messageId = self._nextMessageId(); options.messageId = self._nextMessageId();
} }
this.lastOutbound = (new Date()).getTime() this.lastOutbound = (new Date()).getTime()