const { childRegistrationUtils, logger } = require('generalFunctions'); class liquidFlowHandler { constructor(config) { this.config = config; this.logger = new logger(this.config.general.logging.enabled, this.config.general.logging.logLevel, config.general.name); this.reactors = [null, null]; this.pump = null; this.flow = 0; this.childRegistrationUtils = new childRegistrationUtils(this); } updateState(timeStamp) { this.updatePumpFlow(); const effluent = this.reactors[1].getEffluent; effluent.payload.F = this.flow; effluent.payload.inlet = 1; this.reactors[0].setInfluent = effluent; } updatePumpFlow() { this.flow = this.pump.measurements.type("flow").variant("measured").position("downstream").getCurrentValue() || 0; } getOutput() { const mainEffluent = this.reactors[1].getEffluent; const sideStream = structuredClone(mainEffluent); const F_in = mainEffluent.payload.F; mainEffluent.payload.F = Math.max(F_in - this.flow, 0); sideStream.payload.F = F_in < this.flow ? F_in : this.flow; sideStream.payload.inlet = 1; return {payload: [mainEffluent.payload, sideStream.payload]}; } registerChild(child, softwareType) { switch (softwareType) { case "machine": this.logger.debug(`Registering machine child.`); this._connectMachine(child); break; case "reactor": this.logger.debug(`Registering reactor child.`); this._connectReactor(child); break; default: this.logger.error(`Unrecognized softwareType: ${softwareType}`); } } _connectMachine(machine) { if (!machine) { this.logger.warn("Invalid machine provided."); return; } if (this.pump) { this.logger.warn("Too many pumps provided.") return; } this.pump = machine; } _connectReactor(reactor) { const position = reactor.config.functionality.positionVsParent; if (position == "downstream") { this.reactors[0] = reactor; } if (position == "upstream") { this.reactors[1] = reactor; } this.logger.debug("Attached reactor.") reactor.emitter.on("stateChange", (data) => { this.logger.debug(`State change of attached reactor detected.`); this.updateState(data); }); } } module.exports = liquidFlowHandler;