Add upstream and downstream reactor handling; improve error logging

This commit is contained in:
2025-10-22 14:41:35 +02:00
parent d7cc6a4a8b
commit a8fb56bfb8

View File

@@ -68,6 +68,10 @@ class Machine {
this.updatePosition(); this.updatePosition();
}); });
// used for holding the source and sink unit operations or other object with setInfluent / getEffluent method for e.g. recirculation.
this.upstreamReactor = null;
this.downstreamReactor = null;
this.child = {}; // object to hold child information so we know on what to subscribe this.child = {}; // object to hold child information so we know on what to subscribe
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
@@ -92,7 +96,7 @@ class Machine {
_connectMeasurement(measurementChild) { _connectMeasurement(measurementChild) {
if (!measurementChild) { if (!measurementChild) {
this.logger.warn("Invalid measurement provided."); this.logger.error("Invalid measurement provided.");
return; return;
} }
@@ -107,14 +111,12 @@ class Machine {
measurementChild.measurements.emitter.on(eventName, (eventData) => { measurementChild.measurements.emitter.on(eventName, (eventData) => {
this.logger.debug(`🔄 ${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`); 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 // Store directly in parent's measurement container
this.measurements this.measurements
.type(measurementType) .type(measurementType)
.variant("measured") .variant("measured")
.position(position) .position(position)
.value(eventData.value, eventData.timestamp, eventData.unit); .value(eventData.value, eventData.timestamp, eventData.unit);
// Call the appropriate handler // Call the appropriate handler
switch (measurementType) { switch (measurementType) {
@@ -135,26 +137,31 @@ class Machine {
} }
_connectReactor(reactorChild) { _connectReactor(reactorChild) {
this.logger.error("Reactor child not implemented yet."); if (!reactorChild) {
this.logger.error("Invalid measurement provided.");
return;
}
this.downstreamReactor = reactorChild; // downstream from the pumps perpective
} }
//---------------- END child stuff -------------// //---------------- END child stuff -------------//
// Method to assess drift using errorMetrics // Method to assess drift using errorMetrics
assessDrift(measurement, processMin, processMax) { assessDrift(measurement, processMin, processMax) {
this.logger.debug(`Assessing drift for measurement: ${measurement} processMin: ${processMin} processMax: ${processMax}`); this.logger.debug(`Assessing drift for measurement: ${measurement} processMin: ${processMin} processMax: ${processMax}`);
const predictedMeasurement = this.measurements.type(measurement).variant("predicted").position("downstream").getAllValues().values; const predictedMeasurement = this.measurements.type(measurement).variant("predicted").position("downstream").getAllValues().values;
const measuredMeasurement = this.measurements.type(measurement).variant("measured").position("downstream").getAllValues().values; const measuredMeasurement = this.measurements.type(measurement).variant("measured").position("downstream").getAllValues().values;
if (!predictedMeasurement || !measuredMeasurement) return null; if (!predictedMeasurement || !measuredMeasurement) return null;
return this.errorMetrics.assessDrift( return this.errorMetrics.assessDrift(
predictedMeasurement, predictedMeasurement,
measuredMeasurement, measuredMeasurement,
processMin, processMin,
processMax processMax
); );
} }
reverseCurve(curve) { reverseCurve(curve) {
const reversedCurve = {}; const reversedCurve = {};
@@ -499,12 +506,17 @@ class Machine {
// NEW: Flow handler // NEW: Flow handler
updateMeasuredFlow(value, position, context = {}) { updateMeasuredFlow(value, position, context = {}) {
if (!this._isOperationalState()) { if (!this._isOperationalState()) {
this.logger.warn(`Machine not operational, skipping flow update from ${context.childName || 'unknown'}`); this.logger.warn(`Machine not operational, skipping flow update from ${context.childName || 'unknown'}`);
return; return;
} }
this.logger.debug(`Flow update: ${value} at ${position} from ${context.childName || 'child'}`); this.logger.debug(`Flow update: ${value} at ${position} from ${context.childName || 'child'}`);
if (this.upstreamReactor && this.downstreamReactor){
this._updateConnectedReactor();
}
// Store in parent's measurement container // Store in parent's measurement container
this.measurements.type("flow").variant("measured").position(position).value(value, context.timestamp, context.unit); this.measurements.type("flow").variant("measured").position(position).value(value, context.timestamp, context.unit);
@@ -515,6 +527,10 @@ class Machine {
} }
} }
_updateConnectedReactor() {
this.downstreamReactor.setInfluent = this.upstreamReactor.getEffluent[1];
}
// Helper method for operational state check // Helper method for operational state check
_isOperationalState() { _isOperationalState() {
const state = this.state.getCurrentState(); const state = this.state.getCurrentState();