2026-03-11 15:35:28 +01:00
|
|
|
const { childRegistrationUtils, logger, MeasurementContainer, POSITIONS } = require('generalFunctions');
|
2025-10-22 16:07:42 +02:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
|
|
2026-03-31 16:26:04 +02:00
|
|
|
// Compatibility-safe array clone for Node runtimes without global structuredClone.
|
|
|
|
|
function cloneArray(values) {
|
|
|
|
|
if (typeof structuredClone === 'function') {
|
|
|
|
|
return structuredClone(values);
|
|
|
|
|
}
|
|
|
|
|
return Array.isArray(values) ? [...values] : values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Settler domain model.
|
|
|
|
|
* Splits influent into effluent, sludge and return sludge based on solids balance.
|
|
|
|
|
*/
|
2025-10-22 16:07:42 +02:00
|
|
|
class Settler {
|
|
|
|
|
constructor(config) {
|
|
|
|
|
this.config = config;
|
|
|
|
|
// EVOLV stuff
|
|
|
|
|
this.logger = new logger(this.config.general.logging.enabled, this.config.general.logging.logLevel, config.general.name);
|
|
|
|
|
this.emitter = new EventEmitter();
|
|
|
|
|
this.measurements = new MeasurementContainer();
|
|
|
|
|
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
|
2025-10-23 17:15:41 +02:00
|
|
|
|
|
|
|
|
this.upstreamReactor = null;
|
|
|
|
|
this.returnPump = null;
|
|
|
|
|
|
|
|
|
|
// state variables
|
2025-10-24 15:00:57 +02:00
|
|
|
this.F_in = 0; // debit in
|
|
|
|
|
this.Cs_in = new Array(13).fill(0); // Concentrations in
|
|
|
|
|
this.C_TS = 2500; // Total solids concentration sludge
|
2025-10-23 17:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get getEffluent() {
|
2025-10-24 15:00:57 +02:00
|
|
|
// constrain flow to prevent negatives
|
|
|
|
|
const F_s = Math.min((this.F_in * this.Cs_in[12]) / this.C_TS, this.F_in);
|
|
|
|
|
const F_eff = this.F_in - F_s;
|
2026-03-31 16:26:04 +02:00
|
|
|
|
2025-10-24 15:00:57 +02:00
|
|
|
let F_sr = 0;
|
|
|
|
|
if (this.returnPump) {
|
2026-03-11 15:35:28 +01:00
|
|
|
F_sr = Math.min(this.returnPump.measurements.type("flow").variant("measured").position(POSITIONS.AT_EQUIPMENT).getCurrentValue(), F_s);
|
2025-10-24 15:00:57 +02:00
|
|
|
}
|
|
|
|
|
const F_so = F_s - F_sr;
|
|
|
|
|
|
|
|
|
|
// effluent
|
2026-03-31 16:26:04 +02:00
|
|
|
const Cs_eff = cloneArray(this.Cs_in);
|
2025-10-24 15:00:57 +02:00
|
|
|
if (F_s > 0) {
|
|
|
|
|
Cs_eff[7] = 0;
|
|
|
|
|
Cs_eff[8] = 0;
|
|
|
|
|
Cs_eff[9] = 0;
|
|
|
|
|
Cs_eff[10] = 0;
|
|
|
|
|
Cs_eff[11] = 0;
|
|
|
|
|
Cs_eff[12] = 0;
|
|
|
|
|
}
|
2026-03-31 16:26:04 +02:00
|
|
|
|
2025-10-24 15:00:57 +02:00
|
|
|
// sludge
|
2026-03-31 16:26:04 +02:00
|
|
|
const Cs_s = cloneArray(this.Cs_in);
|
2025-10-24 15:00:57 +02:00
|
|
|
if (F_s > 0) {
|
|
|
|
|
Cs_s[7] = this.F_in * this.Cs_in[7] / F_s;
|
|
|
|
|
Cs_s[8] = this.F_in * this.Cs_in[8] / F_s;
|
|
|
|
|
Cs_s[9] = this.F_in * this.Cs_in[9] / F_s;
|
|
|
|
|
Cs_s[10] = this.F_in * this.Cs_in[10] / F_s;
|
|
|
|
|
Cs_s[11] = this.F_in * this.Cs_in[11] / F_s;
|
|
|
|
|
Cs_s[12] = this.F_in * this.Cs_in[12] / F_s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
{ topic: "Fluent", payload: { inlet: 0, F: F_eff, C: Cs_eff }, timestamp: Date.now() },
|
|
|
|
|
{ topic: "Fluent", payload: { inlet: 1, F: F_so, C: Cs_s }, timestamp: Date.now() },
|
|
|
|
|
{ topic: "Fluent", payload: { inlet: 2, F: F_sr, C: Cs_s }, timestamp: Date.now() }
|
|
|
|
|
];
|
2025-10-22 16:07:42 +02:00
|
|
|
}
|
2025-10-22 16:22:33 +02:00
|
|
|
|
|
|
|
|
registerChild(child, softwareType) {
|
2025-10-31 13:47:19 +01:00
|
|
|
if(!child) {
|
|
|
|
|
this.logger.error(`Invalid ${softwareType} child provided.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 16:22:33 +02:00
|
|
|
switch (softwareType) {
|
2025-10-23 17:15:41 +02:00
|
|
|
case "measurement":
|
|
|
|
|
this.logger.debug(`Registering measurement child...`);
|
|
|
|
|
this._connectMeasurement(child);
|
|
|
|
|
break;
|
|
|
|
|
case "reactor":
|
|
|
|
|
this.logger.debug(`Registering reactor child...`);
|
|
|
|
|
this._connectReactor(child);
|
|
|
|
|
break;
|
|
|
|
|
case "machine":
|
|
|
|
|
this.logger.debug(`Registering machine child...`);
|
|
|
|
|
this._connectMachine(child);
|
|
|
|
|
break;
|
2025-10-22 16:22:33 +02:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-23 17:15:41 +02:00
|
|
|
|
|
|
|
|
_connectMeasurement(measurementChild) {
|
|
|
|
|
const position = measurementChild.config.functionality.positionVsParent;
|
|
|
|
|
const measurementType = measurementChild.config.asset.type;
|
|
|
|
|
const eventName = `${measurementType}.measured.${position}`;
|
|
|
|
|
|
|
|
|
|
// Register event listener for measurement updates
|
|
|
|
|
measurementChild.measurements.emitter.on(eventName, (eventData) => {
|
|
|
|
|
this.logger.debug(`${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
|
|
|
|
|
|
|
|
|
|
// Store directly in parent's measurement container
|
|
|
|
|
this.measurements
|
|
|
|
|
.type(measurementType)
|
|
|
|
|
.variant("measured")
|
|
|
|
|
.position(position)
|
|
|
|
|
.value(eventData.value, eventData.timestamp, eventData.unit);
|
2026-03-31 16:26:04 +02:00
|
|
|
|
2025-10-23 17:15:41 +02:00
|
|
|
this._updateMeasurement(measurementType, eventData.value, position, eventData);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_connectReactor(reactorChild) {
|
2026-03-11 15:35:28 +01:00
|
|
|
if (reactorChild.config.functionality.positionVsParent != POSITIONS.UPSTREAM) {
|
2025-10-24 15:00:57 +02:00
|
|
|
this.logger.warn("Reactor children of settlers should be upstream.");
|
2025-10-23 17:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.upstreamReactor = reactorChild;
|
|
|
|
|
|
2026-03-11 15:35:28 +01:00
|
|
|
reactorChild.emitter.on("stateChange", (_eventData) => {
|
2025-10-23 17:15:41 +02:00
|
|
|
this.logger.debug(`State change of upstream reactor detected.`);
|
2026-03-31 16:26:04 +02:00
|
|
|
const raw = this.upstreamReactor.getEffluent;
|
|
|
|
|
const effluent = Array.isArray(raw) ? raw[0] : raw;
|
2025-10-23 17:15:41 +02:00
|
|
|
this.F_in = effluent.payload.F;
|
|
|
|
|
this.Cs_in = effluent.payload.C;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_connectMachine(machineChild) {
|
2026-03-11 15:35:28 +01:00
|
|
|
if (machineChild.config.functionality.positionVsParent == POSITIONS.DOWNSTREAM) {
|
2025-11-06 14:52:41 +01:00
|
|
|
machineChild.upstreamSource = this;
|
2025-10-23 17:15:41 +02:00
|
|
|
this.returnPump = machineChild;
|
2025-11-06 14:52:41 +01:00
|
|
|
return;
|
2025-10-23 17:15:41 +02:00
|
|
|
}
|
2025-11-06 14:52:41 +01:00
|
|
|
this.logger.warn(`Failed to register machine child.`);
|
2025-10-23 17:15:41 +02:00
|
|
|
}
|
2025-10-24 15:00:57 +02:00
|
|
|
|
2026-03-11 15:35:28 +01:00
|
|
|
_updateMeasurement(measurementType, value, _position, _context) {
|
2025-10-24 15:00:57 +02:00
|
|
|
switch(measurementType) {
|
2025-10-31 13:47:19 +01:00
|
|
|
case "quantity (tss)":
|
2025-10-24 15:00:57 +02:00
|
|
|
this.C_TS = value;
|
|
|
|
|
break;
|
2026-03-31 16:26:04 +02:00
|
|
|
|
2025-10-24 15:00:57 +02:00
|
|
|
default:
|
|
|
|
|
this.logger.error(`Type '${measurementType}' not recognized for measured update.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-22 16:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 16:26:04 +02:00
|
|
|
module.exports = { Settler };
|