diff --git a/src/nodeClass.js b/src/nodeClass.js index c7e730a..8e919a9 100644 --- a/src/nodeClass.js +++ b/src/nodeClass.js @@ -167,39 +167,42 @@ class nodeClass { this.node.on( "input", async (msg, send, done) => { - const vg = this.source; - const RED = this.RED; + const vg = this.source; + const RED = this.RED; + try { switch (msg.topic) { - case "registerChild": - //console.log(`Registering child in mgc: ${msg.payload}`); + case "registerChild": { const childId = msg.payload; const childObj = RED.nodes.getNode(childId); - vg.childRegistrationUtils.registerChild( - childObj.source, - msg.positionVsParent - ); + if (!childObj || !childObj.source) { + vg.logger.warn(`registerChild skipped: missing child/source for id=${childId}`); + break; + } + vg.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent); break; - + } case 'setMode': vg.setMode(msg.payload); break; - case 'execSequence': + case 'execSequence': { const { source: seqSource, action: seqAction, parameter } = msg.payload; vg.handleInput(seqSource, seqAction, parameter); break; - - case 'totalFlowChange': // een van valves is van stand veranderd waardoor total flow is veranderd + } + case 'totalFlowChange': { const { source: tfcSource, action: tfcAction, q} = msg.payload; - vg.handleInput(tfcSource, tfcAction, Number(q)); + vg.handleInput(tfcSource, tfcAction, Number(q)); break; - + } default: - // Handle unknown topics if needed vg.logger.warn(`Unknown topic: ${msg.topic}`); break; } - done(); + } catch (error) { + vg.logger.error(`Input handler failure: ${error.message}`); } + if (typeof done === 'function') done(); + } ); } @@ -210,7 +213,7 @@ class nodeClass { this.node.on("close", (done) => { clearInterval(this._tickInterval); clearInterval(this._statusInterval); - done(); + if (typeof done === 'function') done(); }); } }