Refactor child registration to use dedicated connection methods for measurement and reactor types

This commit is contained in:
HorriblePerson555
2025-10-16 16:32:20 +02:00
parent 5a14f44fdd
commit 37e6523c55

View File

@@ -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 -------------//