From 36bba1a3b72525363d021cf2a29ff1393fa0fbb2 Mon Sep 17 00:00:00 2001 From: Igor Starovierov Date: Mon, 2 Dec 2024 23:00:11 +0000 Subject: [PATCH] Fix node-red crashing by handling client errors --- gree-hvac/gree-hvac.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/gree-hvac/gree-hvac.js b/gree-hvac/gree-hvac.js index 5c0bd71..bc46152 100644 --- a/gree-hvac/gree-hvac.js +++ b/gree-hvac/gree-hvac.js @@ -26,7 +26,6 @@ module.exports = function (RED) { text: 'connected to ' + client.getDeviceId(), }); }; - const statusConnecting = () => node.status({ fill: 'yellow', @@ -39,8 +38,17 @@ module.exports = function (RED) { shape: 'dot', text: 'no response...', }); + const statusError = error => + node.status({ fill: 'red', shape: 'dot', text: error }); - statusConnecting(); + const onError = error => { + if (config.debug) { + console.dir(error); + } + + node.error(error); + statusError(error); + }; client.on('connect', statusConnected); client.on('update', (updatedProperties, properties) => { @@ -57,6 +65,7 @@ module.exports = function (RED) { ]); }); client.on('success', (updatedProperties, properties) => { + statusConnected(); node.send([ { topic: 'acknowledged', @@ -70,17 +79,22 @@ module.exports = function (RED) { }); client.on('disconnect', statusConnecting); client.on('no_response', statusNoResponse); + client.on('error', onError); this.on('input', function (msg) { - if (typeof msg.payload === 'object') - client.setProperties(msg.payload); - else client.setProperty(msg.topic, msg.payload); + if (typeof msg.payload === 'object') { + client.setProperties(msg.payload).catch(onError); + } else { + client.setProperty(msg.topic, msg.payload).catch(onError); + } }); this.on('close', function () { - client.disconnect(); + client.disconnect().catch(onError); client = null; }); + + statusConnecting(); } RED.nodes.registerType('gree-hvac', GreeHvacNode);