const EventEmitter = require('events'); const {logger,configUtils,configManager,MeasurementContainer,coolprop} = require('generalFunctions'); class pumpingStation { constructor(config={}) { this.emitter = new EventEmitter(); // Own EventEmitter this.configManager = new configManager(); this.defaultConfig = this.configManager.getConfig('pumpingStation'); this.configUtils = new configUtils(this.defaultConfig); this.config = this.configUtils.initConfig(config); // Init after config is set this.logger = new logger(this.config.general.logging.enabled,this.config.general.logging.logLevel, this.config.general.name); // General properties this.measurements = new MeasurementContainer({ autoConvert: true, windowSize: this.config.smoothing.smoothWindow }); // init basin object in pumping station this.basin = { volumeWater : null,// Total volume of water in the basin, calculated from water level and basin di emptyVolume : null,// Volume in the basin when empty (at level of outlet pipe) fullVolume : null,// Volume in the basin when at level of overflow point crossSectionalArea: null,// Cross-sectional area of the basin, used to calculate volume from water level }; // pumping station specifics this.calculatedFlowrate = null,// Function to calculate flow rate based on water level rise or fall NO MEASUREMENT this is the predicted value which should match a flowrate if we have it and we have to check mass balance ? Look at the pumps connected to the group controller or directly to this node and check incoming vs outgoing? this.timeBeforeOverflow = null,// Time before the basin overflows at current inflow rate at level of heightOutlet this.timeBeforeEmpty = null,// Time before the basin empties at current outflow rate at level of heightInlet // Initialize basin-specific properties and calculate used parameters this.initBasinProperties(); } /*------------------- Register child events -------------------*/ 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}`); 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); }); } } _callMeasurementHandler(measurementType, value, position, context) { switch (measurementType) { case 'pressure': this.updateMeasuredPressure(value, position, context); break; case 'flow': this.updateMeasuredFlow(value, position, context); break; case 'temperature': this.updateMeasuredTemperature(value, position, context); break; case 'level': this.updateMeasuredLevel(value, position, context); break; default: this.logger.warn(`No handler for measurement type: ${measurementType}`); // Generic handler - just update position this.updatePosition(); break; } } // context handler for pressure updates updateMeasuredPressure(value, position, context = {}) { // init temp let kelvinTemp = null; //pressure updates come from pressure boxes inside the basin they get converted to a level and stored as level measured at position inlet or outlet this.logger.debug(`Pressure update: ${value} at ${position} from ${context.childName || 'child'} (${context.childId || 'unknown-id'})`); // Store in parent's measurement container for the first time this.measurements.type("pressure").variant("measured").position(position).value(value, context.timestamp, context.unit); //convert pressure to level based on density of water and height of pressure sensor const mTemp = this.measurements.type("temperature").variant("measured").position("atEquipment").getCurrentValue('K'); //default to 20C if no temperature measurement //prefer measured temp but otherwise assume nominal temp for wastewater if(mTemp === null){ this.logger.warn(`No temperature measurement available, defaulting to 15C for pressure to level conversion.`); this.measurements.type("temperature").variant("assumed").position("atEquipment").value(15, Date.now(), "C"); kelvinTemp = this.measurements.type('temperature').variant('assumed').position('atEquipment').getCurrentValue('K'); } else { kelvinTemp = mTemp; } this.logger.debug(`Using temperature: ${kelvinTemp} K for calculations`); const density = coolprop.PropsSI('D','T',kelvinTemp,'P',101325,'Water'); //density in kg/m3 at temp and surface pressure const g = 9.80665; const pressure_Pa = this.measurements.type("pressure").variant("measured").position(position).getCurrentValue('Pa'); const level = pressure_Pa / density * g; this.measurements.type("level").variant("predicted").position(position).value(level); //calculate how muc flow went in or out based on pressure difference this.logger.debug(`Using pressure: ${value} for calculations`); } initBasinProperties() { // Load and calc basic params const volEmptyBasin = this.config.basin.volume; const heightBasin = this.config.basin.height; const heightInlet = this.config.basin.heightInlet; const heightOutlet = this.config.basin.heightOutlet; const heightOverflow = this.config.basin.heightOverflow; //calculated params const surfaceArea = volEmptyBasin / heightBasin; const maxVol = heightBasin * surfaceArea; // if Basin where to ever fill up completely this is the water volume const maxVolOverflow = heightOverflow * surfaceArea ; // Max water volume before you start loosing water to overflow const minVol = heightInlet * surfaceArea; const minVolOut = heightOutlet * surfaceArea ; // this will indicate if its an open end or a closed end. this.basin.volEmptyBasin = volEmptyBasin ; this.basin.heightBasin = heightBasin ; this.basin.heightInlet = heightInlet ; this.basin.heightOutlet = heightOutlet ; this.basin.heightOverflow = heightOverflow ; this.basin.surfaceArea = surfaceArea ; this.basin.maxVol = maxVol ; this.basin.maxVolOverflow = maxVolOverflow; this.basin.minVol = minVol ; this.basin.minVolOut = minVolOut ; this.logger.debug( `Basin initialized | area=${surfaceArea.toFixed(2)} m², max=${maxVol.toFixed(2)} m³, overflow=${maxVolOverflow.toFixed(2)} m³` ); } _calcVolumeFromLevel(level) { const surfaceArea = this.basin.surfaceArea; return Math.max(level, 0) * surfaceArea; } getOutput() { return { volume: this.volume, }; } } module.exports = pumpingStation; /* // */ (async () => { const PropsSI = await coolprop.getPropsSI(); // 👇 replace these with your real inputs const tC_input = 25; // °C const pPa_input = 101325; // Pa // Sanitize & convert const T = Number(tC_input) + 273.15; // K const P = Number(pPa_input); // Pa const fluid = 'Water'; // Preconditions if (!Number.isFinite(T) || !Number.isFinite(P)) { throw new Error(`Bad inputs: T=${T} K, P=${P} Pa`); } if (T <= 0) throw new Error(`Temperature must be in Kelvin (>0). Got ${T}.`); if (P <= 0) throw new Error(`Pressure must be >0 Pa. Got ${P}.`); // Try T,P order let rho = PropsSI('D', 'T', T, 'P', P, fluid); // Fallback: P,T order (should be equivalent) if (!Number.isFinite(rho)) rho = PropsSI('D', 'P', P, 'T', T, fluid); console.log({ T, P, rho }); if (!Number.isFinite(rho)) { console.error('Still Infinity. Extra checks:'); console.error('typeof T:', typeof T, 'typeof P:', typeof P); console.error('Example known-good call:', PropsSI('D', 'T', 298.15, 'P', 101325, 'Water')); } })();