|
|
|
|
@@ -1,6 +1,5 @@
|
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
|
const {loadCurve,logger,configUtils,configManager,state, nrmse, MeasurementContainer, predict, interpolation , childRegistrationUtils} = require('generalFunctions');
|
|
|
|
|
const { name } = require('../../generalFunctions/src/convert/lodash/lodash._shimkeys');
|
|
|
|
|
const {loadCurve,gravity,logger,configUtils,configManager,state, nrmse, MeasurementContainer, predict, interpolation , childRegistrationUtils,coolprop} = require('generalFunctions');
|
|
|
|
|
|
|
|
|
|
class Machine {
|
|
|
|
|
|
|
|
|
|
@@ -17,7 +16,7 @@ class Machine {
|
|
|
|
|
|
|
|
|
|
// Load a specific curve
|
|
|
|
|
this.model = machineConfig.asset.model; // Get the model from the machineConfig
|
|
|
|
|
this.curve = this.model ? loadCurve(this.model) : null;
|
|
|
|
|
this.curve = this.model ? loadCurve(this.model) : null; // we need to convert the curve and add units to the curve information
|
|
|
|
|
|
|
|
|
|
//Init config and check if it is valid
|
|
|
|
|
this.config = this.configUtils.initConfig(machineConfig);
|
|
|
|
|
@@ -35,10 +34,8 @@ class Machine {
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
this.hasCurve = true;
|
|
|
|
|
this.config = this.configUtils.updateConfig(this.config, {
|
|
|
|
|
asset: { ...this.config.asset, machineCurve: this.curve }
|
|
|
|
|
});
|
|
|
|
|
machineConfig = { ...machineConfig, asset: { ...machineConfig.asset, machineCurve: this.curve } }; // Merge curve into machineConfig
|
|
|
|
|
this.config = this.configUtils.updateConfig(this.config, { asset: { ...this.config.asset, machineCurve: this.curve } });
|
|
|
|
|
//machineConfig = { ...machineConfig, asset: { ...machineConfig.asset, machineCurve: this.curve } }; // Merge curve into machineConfig
|
|
|
|
|
this.predictFlow = new predict({ curve: this.config.asset.machineCurve.nq }); // load nq (x : ctrl , y : flow relationship)
|
|
|
|
|
this.predictPower = new predict({ curve: this.config.asset.machineCurve.np }); // load np (x : ctrl , y : power relationship)
|
|
|
|
|
this.predictCtrl = new predict({ curve: this.reverseCurve(this.config.asset.machineCurve.nq) }); // load reversed nq (x: flow, y: ctrl relationship)
|
|
|
|
|
@@ -84,16 +81,33 @@ class Machine {
|
|
|
|
|
this._updateState();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//perform init for certain values
|
|
|
|
|
this._init();
|
|
|
|
|
|
|
|
|
|
this.child = {}; // object to hold child information so we know on what to subscribe
|
|
|
|
|
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_init(){
|
|
|
|
|
//assume standard temperature is 20degrees
|
|
|
|
|
this.measurements.type('temperature').variant('measured').position('atEquipment').value(15).unit('C');
|
|
|
|
|
//assume standard atm pressure is at sea level
|
|
|
|
|
this.measurements.type('atmPressure').variant('measured').position('atEquipment').value(101325).unit('Pa');
|
|
|
|
|
//populate min and max
|
|
|
|
|
const flowunit = this.config.general.unit;
|
|
|
|
|
this.measurements.type('flow').variant('predicted').position('max').value(this.predictFlow.currentFxyYMax, Date.now() , flowunit)
|
|
|
|
|
this.measurements.type('flow').variant('predicted').position('min').value(this.predictFlow.currentFxyYMin).unit(this.config.general.unit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_updateState(){
|
|
|
|
|
const isOperational = this._isOperationalState();
|
|
|
|
|
if(!isOperational){
|
|
|
|
|
//overrule the last prediction this should be 0 now
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(0);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(0,Date.now(),this.config.general.unit);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("atEquipment").value(0,Date.now(),this.config.general.unit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -115,7 +129,7 @@ class Machine {
|
|
|
|
|
this.logger.debug(`🔄 ${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(` Emitting... ${eventName} with data:`);
|
|
|
|
|
this.logger.debug(` Emitting... ${eventName} with data:`);
|
|
|
|
|
// Store directly in parent's measurement container
|
|
|
|
|
this.measurements
|
|
|
|
|
.type(measurementType)
|
|
|
|
|
@@ -209,7 +223,6 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
|
|
|
|
|
async handleInput(source, action, parameter) {
|
|
|
|
|
|
|
|
|
|
this.logger.debug("hello");
|
|
|
|
|
//sanitize input
|
|
|
|
|
if( typeof action !== 'string'){this.logger.error(`Action must be string`); return;}
|
|
|
|
|
//convert to lower case to avoid to many mistakes in commands
|
|
|
|
|
@@ -219,8 +232,6 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
if(!this.isValidActionForMode(action,this.currentMode)){return ;}
|
|
|
|
|
if (!this.isValidSourceForMode(source, this.currentMode)) {return ;}
|
|
|
|
|
|
|
|
|
|
this.logger.debug("hello2");
|
|
|
|
|
|
|
|
|
|
this.logger.info(`Handling input from source '${source}' with action '${action}' in mode '${this.currentMode}'.`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
@@ -337,21 +348,23 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
calcFlow(x) {
|
|
|
|
|
if(this.hasCurve) {
|
|
|
|
|
if (!this._isOperationalState()) {
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(0);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(0,Date.now(),this.config.general.unit);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("atEquipment").value(0,Date.now(),this.config.general.unit);
|
|
|
|
|
this.logger.debug(`Machine is not operational. Setting predicted flow to 0.`);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//this.predictFlow.currentX = x; Decrepated
|
|
|
|
|
const cFlow = this.predictFlow.y(x);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(cFlow);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(cFlow,Date.now(),this.config.general.unit);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("atEquipment").value(cFlow,Date.now(),this.config.general.unit);
|
|
|
|
|
//this.logger.debug(`Calculated flow: ${cFlow} for pressure: ${this.getMeasuredPressure()} and position: ${x}`);
|
|
|
|
|
return cFlow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no curve data is available, log a warning and return 0
|
|
|
|
|
this.logger.warn(`No curve data available for flow calculation. Returning 0.`);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(0);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(0, Date.now(),this.config.general.unit);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("atEquipment").value(0, Date.now(),this.config.general.unit);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@@ -416,6 +429,11 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
// returns the best available pressure measurement to use in the prediction calculation
|
|
|
|
|
// this will be either the differential pressure, downstream or upstream pressure
|
|
|
|
|
getMeasuredPressure() {
|
|
|
|
|
if(this.hasCurve === false){
|
|
|
|
|
this.logger.error(`No valid curve available to calculate prediction using last known pressure`);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pressureDiff = this.measurements.type('pressure').variant('measured').difference();
|
|
|
|
|
|
|
|
|
|
// Both upstream & downstream => differential
|
|
|
|
|
@@ -464,6 +482,9 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
const efficiency = this.calcEfficiency(this.predictPower.outputY, this.predictFlow.outputY, "predicted");
|
|
|
|
|
//update the distance from peak
|
|
|
|
|
this.calcDistanceBEP(efficiency,cog,minEfficiency);
|
|
|
|
|
//place min and max flow capabilities in containerthis.predictFlow.currentFxyYMax - this.predictFlow.currentFxyYMin
|
|
|
|
|
this.measurements.type('flow').variant('predicted').position('max').value(this.predictFlow.currentFxyYMax).unit(this.config.general.unit);
|
|
|
|
|
this.measurements.type('flow').variant('predicted').position('min').value(this.predictFlow.currentFxyYMin).unit(this.config.general.unit);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -551,6 +572,7 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
// Update predicted flow if you have prediction capability
|
|
|
|
|
if (this.predictFlow) {
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("downstream").value(this.predictFlow.outputY || 0);
|
|
|
|
|
this.measurements.type("flow").variant("predicted").position("atEquipment").value(this.predictFlow.outputY || 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -582,8 +604,6 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
calcDistanceFromPeak(currentEfficiency,peakEfficiency){
|
|
|
|
|
@@ -623,7 +643,7 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
const {efficiencyCurve, peak, peakIndex, minEfficiency } = this.calcEfficiencyCurve(powerCurve, flowCurve);
|
|
|
|
|
|
|
|
|
|
// Calculate the normalized center of gravity
|
|
|
|
|
const NCog = (flowCurve.y[peakIndex] - this.predictFlow.currentFxyYMin) / (this.predictFlow.currentFxyYMax - this.predictFlow.currentFxyYMin);
|
|
|
|
|
const NCog = (flowCurve.y[peakIndex] - this.predictFlow.currentFxyYMin) / (this.predictFlow.currentFxyYMax - this.predictFlow.currentFxyYMin); //
|
|
|
|
|
|
|
|
|
|
//store in object for later retrieval
|
|
|
|
|
this.currentEfficiencyCurve = efficiencyCurve;
|
|
|
|
|
@@ -675,13 +695,36 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
|
|
|
|
|
calcEfficiency(power,flow,variant) {
|
|
|
|
|
|
|
|
|
|
const pressureDiff = this.measurements.type('pressure').variant('measured').difference('Pa');
|
|
|
|
|
const g = gravity.getStandardGravity();
|
|
|
|
|
const temp = this.measurements.type('temperature').variant('measured').position('atEquipment').getCurrentValue('K');
|
|
|
|
|
const atmPressure = this.measurements.type('atmPressure').variant('measured').position('atEquipment').getCurrentValue('Pa');
|
|
|
|
|
|
|
|
|
|
console.log(`--------------------calc efficiency : Pressure diff:${pressureDiff},${temp}, ${g} `);
|
|
|
|
|
const rho = coolprop.PropsSI('D', 'T', temp, 'P', atmPressure, 'WasteWater');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.logger.debug(`temp: ${temp} atmPressure : ${atmPressure} rho : ${rho} pressureDiff: ${pressureDiff?.value || 0}`);
|
|
|
|
|
const flowM3s = this.measurements.type('flow').variant('predicted').position('atEquipment').getCurrentValue('m3/s');
|
|
|
|
|
const powerWatt = this.measurements.type('power').variant('predicted').position('atEquipment').getCurrentValue('W');
|
|
|
|
|
this.logger.debug(`Flow : ${flowM3s} power: ${powerWatt}`);
|
|
|
|
|
|
|
|
|
|
if (power != 0 && flow != 0) {
|
|
|
|
|
// Calculate efficiency after measurements update
|
|
|
|
|
this.measurements.type("efficiency").variant(variant).position('atEquipment').value((flow / power));
|
|
|
|
|
} else {
|
|
|
|
|
this.measurements.type("efficiency").variant(variant).position('atEquipment').value(null);
|
|
|
|
|
const specificFlow = flow / power;
|
|
|
|
|
const specificEnergyConsumption = power / flow;
|
|
|
|
|
|
|
|
|
|
this.measurements.type("efficiency").variant(variant).position('atEquipment').value(specificFlow);
|
|
|
|
|
this.measurements.type("specificEnergyConsumption").variant(variant).position('atEquipment').value(specificEnergyConsumption);
|
|
|
|
|
|
|
|
|
|
if(pressureDiff?.value != null && flowM3s != null && powerWatt != null){
|
|
|
|
|
const meterPerBar = pressureDiff.value / rho * g;
|
|
|
|
|
const nHydraulicEfficiency = rho * g * flowM3s * (pressureDiff.value * meterPerBar ) / powerWatt;
|
|
|
|
|
this.measurements.type("nHydraulicEfficiency").variant(variant).position('atEquipment').value(nHydraulicEfficiency);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//change this to nhydrefficiency ?
|
|
|
|
|
return this.measurements.type("efficiency").variant(variant).position('atEquipment').getCurrentValue();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@@ -728,15 +771,8 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
getOutput() {
|
|
|
|
|
|
|
|
|
|
// Improved output object generation
|
|
|
|
|
const output = {};
|
|
|
|
|
|
|
|
|
|
Object.entries(this.measurements.measurements).forEach(([type, variants]) => {
|
|
|
|
|
Object.entries(variants).forEach(([variant, positions]) => {
|
|
|
|
|
Object.entries(positions).forEach(([position, measurement]) => {
|
|
|
|
|
output[`${type}.${variant}.${position}`] = measurement.getCurrentValue();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
const output = this.measurements.getFlattenedOutput();
|
|
|
|
|
|
|
|
|
|
//fill in the rest of the output object
|
|
|
|
|
output["state"] = this.state.getCurrentState();
|
|
|
|
|
@@ -771,8 +807,8 @@ _callMeasurementHandler(measurementType, value, position, context) {
|
|
|
|
|
module.exports = Machine;
|
|
|
|
|
|
|
|
|
|
/*------------------- Testing -------------------*/
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
curve = require('C:/Users/zn375/.node-red/public/fallbackData.json');
|
|
|
|
|
|
|
|
|
|
//import a child
|
|
|
|
|
|