diff --git a/src/nodeClass.js b/src/nodeClass.js index 7706508..2d68845 100644 --- a/src/nodeClass.js +++ b/src/nodeClass.js @@ -80,7 +80,7 @@ class nodeClass { const availableMachines = Object.values(mg.machines || {}).filter((machine) => { // Safety check: ensure machine and machine.state exist if (!machine || !machine.state || typeof machine.state.getCurrentState !== 'function') { - console.warn(`Machine missing or invalid:`, machine?.config?.general?.id || 'unknown'); + mg.logger?.warn(`Machine missing or invalid: ${machine?.config?.general?.id || 'unknown'}`); return false; } @@ -195,67 +195,52 @@ class nodeClass { this.node.on( "input", async (msg, send, done) => { - const mg = this.source; - const RED = this.RED; + const mg = this.source; + const RED = this.RED; + try { switch (msg.topic) { - case "registerChild": - //console.log(`Registering child in mgc: ${msg.payload}`); - const childId = msg.payload; - const childObj = RED.nodes.getNode(childId); - - // Debug: Check what we're getting - //console.log(`Child object:`, childObj ? 'found' : 'NOT FOUND'); - //console.log(`Child source:`, childObj?.source ? 'exists' : 'MISSING'); - if (childObj?.source) { - //console.log(`Child source type:`, childObj.source.constructor.name); - //console.log(`Child has state:`, !!childObj.source.state); - } - - mg.childRegistrationUtils.registerChild( - childObj.source, - msg.positionVsParent - ); - - // Debug: Check machines after registration - //console.log(`Total machines after registration:`, Object.keys(mg.machines || {}).length); - break; - + case "registerChild": { + const childId = msg.payload; + const childObj = RED.nodes.getNode(childId); + if (!childObj || !childObj.source) { + mg.logger.warn(`registerChild skipped: missing child/source for id=${childId}`); + break; + } + mg.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent); + break; + } case "setMode": - const mode = msg.payload; - mg.setMode(mode); + mg.setMode(msg.payload); break; - case "setScaling": - const scaling = msg.payload; - mg.setScaling(scaling); - + mg.setScaling(msg.payload); break; - - case "Qd": + case "Qd": { const Qd = parseFloat(msg.payload); const sourceQd = "parent"; - if (isNaN(Qd)) { - return mg.logger.error(`Invalid demand value: ${Qd}`); + mg.logger.error(`Invalid demand value: ${msg.payload}`); + break; } - try { await mg.handleInput(sourceQd, Qd); msg.topic = mg.config.general.name; msg.payload = "done"; send(msg); - } catch (e) { - console.log(e); + } catch (error) { + mg.logger.error(`Failed to process Qd: ${error.message}`); } break; - + } default: - // Handle unknown topics if needed mg.logger.warn(`Unknown topic: ${msg.topic}`); break; } - done(); + } catch (error) { + mg.logger.error(`Input handler failure: ${error.message}`); } + if (typeof done === 'function') done(); + } ); } @@ -266,7 +251,7 @@ class nodeClass { this.node.on("close", (done) => { clearInterval(this._tickInterval); clearInterval(this._statusInterval); - done(); + if (typeof done === 'function') done(); }); } } diff --git a/src/specificClass.js b/src/specificClass.js index f8ae8d1..728f586 100644 --- a/src/specificClass.js +++ b/src/specificClass.js @@ -2,6 +2,10 @@ const EventEmitter = require("events"); const {logger,configUtils,configManager, MeasurementContainer, interpolation , childRegistrationUtils} = require('generalFunctions'); +/** + * Machine group controller domain model. + * Aggregates multiple rotating machines and coordinates group-level optimization/control. + */ class MachineGroup { constructor(machineGroupConfig = {}) { @@ -50,7 +54,8 @@ class MachineGroup { registerChild(child,softwareType) { this.logger.debug('Setting up childs specific for this class'); - const position = child.config.general.positionVsParent; + // Prefer functionality-scoped position metadata; keep general fallback for legacy nodes. + const position = child.config?.functionality?.positionVsParent || child.config?.general?.positionVsParent; if(softwareType == "machine"){ // Check if the machine is already registered @@ -1396,6 +1401,8 @@ async function makeMachines(){ } -makeMachines(); +if (require.main === module) { + makeMachines(); +} //*/