From 37e6523c5527e3bfd9d9dbb4f021e018f45be3cf Mon Sep 17 00:00:00 2001 From: HorriblePerson555 <47578455+HorriblePerson555@users.noreply.github.com> Date: Thu, 16 Oct 2025 16:32:20 +0200 Subject: [PATCH] Refactor child registration to use dedicated connection methods for measurement and reactor types --- src/specificClass.js | 90 ++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/src/specificClass.js b/src/specificClass.js index 245946b..a0f3853 100644 --- a/src/specificClass.js +++ b/src/specificClass.js @@ -77,52 +77,62 @@ class Machine { registerChild(child, softwareType) { this.logger.debug('Setting up child event for softwaretype ' + softwareType); - if(softwareType === "measurement"){ - const position = child.config.functionality.positionVsParent; - const distance = child.config.functionality.distanceVsParent || 0; - const measurementType = child.config.asset.type; - const key = `${measurementType}_${position}`; - //rebuild to measurementype.variant no position and then switch based on values not strings or names. - const eventName = `${measurementType}.measured.${position}`; - - this.logger.debug(`Setting up listener for ${eventName} from child ${child.config.general.name}`); - // Register event listener for measurement updates - child.measurements.emitter.on(eventName, (eventData) => { - this.logger.debug(`🔄 ${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`); + switch (softwareType) { + case "measurement": + this._connectMeasurement(child); + break; + case "reactor": + this._connectReactor(child); + break; - - console.log(` Emitting... ${eventName} with data:`); - // Store directly in parent's measurement container - this.measurements - .type(measurementType) - .variant("measured") - .position(position) - .value(eventData.value, eventData.timestamp, eventData.unit); - - // Call the appropriate handler - this._callMeasurementHandler(measurementType, eventData.value, position, eventData); - }); + default: + this.logger.error(`Unrecognized softwareType: ${softwareType}`); } } -// Centralized handler dispatcher -_callMeasurementHandler(measurementType, value, position, context) { - switch (measurementType) { - case 'pressure': - this.updateMeasuredPressure(value, position, context); - break; + _connectMeasurement(measurementChild) { + const position = measurementChild.config.functionality.positionVsParent; + const distance = measurementChild.config.functionality.distanceVsParent || 0; + const measurementType = measurementChild.config.asset.type; + const key = `${measurementType}_${position}`; + //rebuild to measurementype.variant no position and then switch based on values not strings or names. + const eventName = `${measurementType}.measured.${position}`; + + this.logger.debug(`Setting up listener for ${eventName} from child ${measurementChild.config.general.name}`); + // Register event listener for measurement updates + measurementChild.measurements.emitter.on(eventName, (eventData) => { + this.logger.debug(`🔄 ${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`); + + + console.log(` Emitting... ${eventName} with data:`); + // Store directly in parent's measurement container + this.measurements + .type(measurementType) + .variant("measured") + .position(position) + .value(eventData.value, eventData.timestamp, eventData.unit); - case 'flow': - this.updateMeasuredFlow(value, position, context); - break; - - default: - this.logger.warn(`No handler for measurement type: ${measurementType}`); - // Generic handler - just update position - this.updatePosition(); - break; + // Call the appropriate handler + switch (measurementType) { + case 'pressure': + this.updateMeasuredPressure(eventData.value, position, eventData); + break; + + case 'flow': + this.updateMeasuredFlow(eventData.value, position, eventData); + break; + + default: + this.logger.warn(`No handler for measurement type: ${measurementType}`); + // Generic handler - just update position + this.updatePosition(); + } + }); + } + + _connectReactor(reactorChild) { + this.logger.error("Reactor child not implemented yet."); } -} //---------------- END child stuff -------------//