From 5bb66ed7d406f45a50b268a9d0b66e85e1a1b7bd Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Thu, 1 Dec 2022 13:08:48 +0000 Subject: [PATCH] fix single subscription mqtt node status --- .../@node-red/nodes/core/network/10-mqtt.js | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js index bfed09c23..3e789be7f 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js @@ -366,6 +366,16 @@ module.exports = function(RED) { } } + function updateStatus(node, allNodes) { + let setStatus = setStatusDisconnected + if(node.connecting) { + setStatus = setStatusConnecting + } else if(node.connected) { + setStatus = setStatusConnected + } + setStatus(node, allNodes) + } + function setStatusDisconnected(node, allNodes) { if(allNodes) { for (var id in node.users) { @@ -697,13 +707,17 @@ module.exports = function(RED) { if (Object.keys(node.users).length === 1) { if(node.autoConnect) { node.connect(); + //update nodes status + setTimeout(function() { + updateStatus(node, true) + }, 1) } } }; - node.deregister = function(mqttNode,done) { + node.deregister = function(mqttNode, done, autoDisconnect) { delete node.users[mqttNode.id]; - if (!node.closing && node.connected && Object.keys(node.users).length === 0) { + if (autoDisconnect && !node.closing && node.connected && Object.keys(node.users).length === 0) { node.disconnect(); } done(); @@ -1220,7 +1234,7 @@ module.exports = function(RED) { } else { node.brokerConn.unsubscribe(node.topic,node.id, removed); } - node.brokerConn.deregister(node, done); + node.brokerConn.deregister(node, done, removed); node.brokerConn = null; } else { done(); @@ -1283,9 +1297,9 @@ module.exports = function(RED) { node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); } node.brokerConn.register(node); - node.on('close', function(done) { + node.on('close', function(removed, done) { if (node.brokerConn) { - node.brokerConn.deregister(node,done); + node.brokerConn.deregister(node, done, removed) node.brokerConn = null; } else { done();