Skip to content

Commit

Permalink
fix: node-red crashing by handling client errors (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
inwaar authored Dec 2, 2024
1 parent ba086e2 commit 5e87e70
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions gree-hvac/gree-hvac.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ module.exports = function (RED) {
text: 'connected to ' + client.getDeviceId(),
});
};

const statusConnecting = () =>
node.status({
fill: 'yellow',
Expand All @@ -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) => {
Expand All @@ -57,6 +65,7 @@ module.exports = function (RED) {
]);
});
client.on('success', (updatedProperties, properties) => {
statusConnected();
node.send([
{
topic: 'acknowledged',
Expand All @@ -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);
Expand Down

0 comments on commit 5e87e70

Please sign in to comment.