updates
This commit is contained in:
2
mgc.html
2
mgc.html
@@ -39,7 +39,7 @@
|
||||
icon: "font-awesome/fa-cogs",
|
||||
|
||||
label: function () {
|
||||
return this.positionIcon + " " + "machineGroup";
|
||||
return (this.positionIcon || "") + " machineGroup";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
// Initialize the menu data for the node
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const { outputUtils, configManager } = require("generalFunctions");
|
||||
const { outputUtils, configManager, convert } = require("generalFunctions");
|
||||
const Specific = require("./specificClass");
|
||||
|
||||
class nodeClass {
|
||||
@@ -37,13 +37,14 @@ class nodeClass {
|
||||
_loadConfig(uiConfig, node) {
|
||||
const cfgMgr = new configManager();
|
||||
this.defaultConfig = cfgMgr.getConfig(this.name);
|
||||
const flowUnit = this._resolveUnitOrFallback(uiConfig.unit, 'volumeFlowRate', 'm3/h', 'flow');
|
||||
|
||||
// Merge UI config over defaults
|
||||
this.config = {
|
||||
general: {
|
||||
name: uiConfig.name,
|
||||
id: node.id, // node.id is for the child registration process
|
||||
unit: uiConfig.unit, // add converter options later to convert to default units (need like a model that defines this which units we are going to use and then conver to those standards)
|
||||
unit: flowUnit,
|
||||
logging: {
|
||||
enabled: uiConfig.enableLog,
|
||||
logLevel: uiConfig.logLevel,
|
||||
@@ -57,6 +58,24 @@ class nodeClass {
|
||||
this._output = new outputUtils();
|
||||
}
|
||||
|
||||
_resolveUnitOrFallback(candidate, expectedMeasure, fallbackUnit, label) {
|
||||
const raw = typeof candidate === "string" ? candidate.trim() : "";
|
||||
const fallback = String(fallbackUnit || "").trim();
|
||||
if (!raw) {
|
||||
return fallback;
|
||||
}
|
||||
try {
|
||||
const desc = convert().describe(raw);
|
||||
if (expectedMeasure && desc.measure !== expectedMeasure) {
|
||||
throw new Error(`expected '${expectedMeasure}' but got '${desc.measure}'`);
|
||||
}
|
||||
return raw;
|
||||
} catch (error) {
|
||||
this.node?.warn?.(`Invalid ${label} unit '${raw}' (${error.message}). Falling back to '${fallback}'.`);
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
_updateNodeStatus() {
|
||||
//console.log('Updating node status...');
|
||||
const mg = this.source;
|
||||
@@ -68,13 +87,13 @@ class nodeClass {
|
||||
?.type("flow")
|
||||
?.variant("predicted")
|
||||
?.position("atequipment")
|
||||
?.getCurrentValue('m3/h') || 0;
|
||||
?.getCurrentValue(mg?.unitPolicy?.output?.flow || 'm3/h') || 0;
|
||||
|
||||
const totalPower = mg.measurements
|
||||
?.type("power")
|
||||
?.variant("predicted")
|
||||
?.position("atEquipment")
|
||||
?.getCurrentValue() || 0;
|
||||
?.getCurrentValue(mg?.unitPolicy?.output?.power || 'kW') || 0;
|
||||
|
||||
// Calculate total capacity based on available machines with safety checks
|
||||
const availableMachines = Object.values(mg.machines || {}).filter((machine) => {
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
//load local dependencies
|
||||
const EventEmitter = require("events");
|
||||
const {logger,configUtils,configManager, MeasurementContainer, interpolation , childRegistrationUtils} = require('generalFunctions');
|
||||
const {logger,configUtils,configManager, MeasurementContainer, interpolation , childRegistrationUtils, convert} = require('generalFunctions');
|
||||
|
||||
const CANONICAL_UNITS = Object.freeze({
|
||||
pressure: 'Pa',
|
||||
flow: 'm3/s',
|
||||
power: 'W',
|
||||
temperature: 'K',
|
||||
});
|
||||
|
||||
const DEFAULT_IO_UNITS = Object.freeze({
|
||||
pressure: 'mbar',
|
||||
flow: 'm3/h',
|
||||
power: 'kW',
|
||||
temperature: 'C',
|
||||
});
|
||||
|
||||
/**
|
||||
* Machine group controller domain model.
|
||||
@@ -14,6 +28,12 @@ class MachineGroup {
|
||||
this.defaultConfig = this.configManager.getConfig('machineGroupControl'); // Load default config for rotating machine ( use software type name ? )
|
||||
this.configUtils = new configUtils(this.defaultConfig);// this will handle the config endpoints so we can load them dynamically
|
||||
this.config = this.configUtils.initConfig(machineGroupConfig); // verify and set the config for the machine group
|
||||
this.unitPolicy = this._buildUnitPolicy(this.config);
|
||||
this.config = this.configUtils.updateConfig(this.config, {
|
||||
general: {
|
||||
unit: this.unitPolicy.output.flow,
|
||||
}
|
||||
});
|
||||
|
||||
// Init after config is set
|
||||
this.logger = new logger(this.config.general.logging.enabled,this.config.general.logging.logLevel, this.config.general.name);
|
||||
@@ -23,11 +43,22 @@ class MachineGroup {
|
||||
autoConvert: true,
|
||||
windowSize: 50,
|
||||
defaultUnits: {
|
||||
pressure: 'mbar',
|
||||
flow: 'l/s',
|
||||
power: 'kW',
|
||||
temperature: 'C'
|
||||
}
|
||||
pressure: this.unitPolicy.output.pressure,
|
||||
flow: this.unitPolicy.output.flow,
|
||||
power: this.unitPolicy.output.power,
|
||||
temperature: this.unitPolicy.output.temperature
|
||||
},
|
||||
preferredUnits: {
|
||||
pressure: this.unitPolicy.output.pressure,
|
||||
flow: this.unitPolicy.output.flow,
|
||||
power: this.unitPolicy.output.power,
|
||||
temperature: this.unitPolicy.output.temperature
|
||||
},
|
||||
canonicalUnits: this.unitPolicy.canonical,
|
||||
storeCanonical: true,
|
||||
strictUnitValidation: true,
|
||||
throwOnInvalidUnit: true,
|
||||
requireUnitForTypes: ['pressure', 'flow', 'power', 'temperature']
|
||||
});
|
||||
|
||||
this.interpolation = new interpolation();
|
||||
@@ -165,8 +196,8 @@ class MachineGroup {
|
||||
const minPower = machine.predictPower.currentFxyYMin;
|
||||
const maxPower = machine.predictPower.currentFxyYMax;
|
||||
|
||||
const actFlow = machine.measurements.type("flow").variant("predicted").position("atequipment").getCurrentValue();
|
||||
const actPower = machine.measurements.type("power").variant("predicted").position("atequipment").getCurrentValue();
|
||||
const actFlow = this._readChildMeasurement(machine, "flow", "predicted", "atequipment", this.unitPolicy.canonical.flow) || 0;
|
||||
const actPower = this._readChildMeasurement(machine, "power", "predicted", "atequipment", this.unitPolicy.canonical.power) || 0;
|
||||
|
||||
this.logger.debug(`Machine ${machine.config.general.id} - Min Flow: ${minFlow}, Max Flow: ${maxFlow}, Min Power: ${minPower}, Max Power: ${maxPower}, NCog: ${machine.NCog}`);
|
||||
|
||||
@@ -215,13 +246,13 @@ class MachineGroup {
|
||||
}
|
||||
|
||||
handlePressureChange() {
|
||||
this.logger.info("---------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>Pressure change detected.");
|
||||
this.logger.debug("Pressure change detected.");
|
||||
// Recalculate totals
|
||||
const { flow, power } = this.calcDynamicTotals();
|
||||
|
||||
this.logger.debug(`Dynamic Totals after pressure change - Flow: Min ${flow.min}, Max ${flow.max}, Act ${flow.act} | Power: Min ${power.min}, Max ${power.max}, Act ${power.act}`);
|
||||
this.measurements.type("flow").variant("predicted").position("atequipment").value(flow.act);
|
||||
this.measurements.type("power").variant("predicted").position("atequipment").value(power.act);
|
||||
this._writeMeasurement("flow", "predicted", "atequipment", flow.act, this.unitPolicy.canonical.flow);
|
||||
this._writeMeasurement("power", "predicted", "atequipment", power.act, this.unitPolicy.canonical.power);
|
||||
|
||||
const { maxEfficiency, lowestEfficiency } = this.calcGroupEfficiency(this.machines);
|
||||
const efficiency = this.measurements.type("efficiency").variant("predicted").position("atequipment").getCurrentValue();
|
||||
@@ -260,11 +291,13 @@ class MachineGroup {
|
||||
//add special cases
|
||||
if( state === "operational" && ( mode == "virtualControl" || mode === "fysicalControl") ){
|
||||
let flow = 0;
|
||||
if(machine.measurements.type("flow").variant("measured").position("downstream").getCurrentValue()){
|
||||
flow = machine.measurements.type("flow").variant("measured").position("downstream").getCurrentValue();
|
||||
const measuredFlow = this._readChildMeasurement(machine, "flow", "measured", "downstream", this.unitPolicy.canonical.flow);
|
||||
const predictedFlow = this._readChildMeasurement(machine, "flow", "predicted", "atequipment", this.unitPolicy.canonical.flow);
|
||||
if (Number.isFinite(measuredFlow) && measuredFlow !== 0) {
|
||||
flow = measuredFlow;
|
||||
}
|
||||
else if(machine.measurements.type("flow").variant("predicted").position("atequipment").getCurrentValue()){
|
||||
flow = machine.measurements.type("flow").variant("predicted").position("atequipment").getCurrentValue();
|
||||
else if (Number.isFinite(predictedFlow) && predictedFlow !== 0) {
|
||||
flow = predictedFlow;
|
||||
}
|
||||
else{
|
||||
this.logger.error("Dont perform calculation at all seeing that there is a machine working but we dont know the flow its producing");
|
||||
@@ -616,8 +649,8 @@ class MachineGroup {
|
||||
// this is to ensure a correct evaluation of the flow and power consumption
|
||||
const pressures = Object.entries(this.machines).map(([machineId, machine]) => {
|
||||
return {
|
||||
downstream: machine.measurements.type("pressure").variant("measured").position("downstream").getCurrentValue(),
|
||||
upstream: machine.measurements.type("pressure").variant("measured").position("upstream").getCurrentValue()
|
||||
downstream: this._readChildMeasurement(machine, "pressure", "measured", "downstream", this.unitPolicy.canonical.pressure),
|
||||
upstream: this._readChildMeasurement(machine, "pressure", "measured", "upstream", this.unitPolicy.canonical.pressure)
|
||||
};
|
||||
});
|
||||
|
||||
@@ -631,8 +664,8 @@ class MachineGroup {
|
||||
if(machine.state.getCurrentState() !== "operational" && machine.state.getCurrentState() !== "accelerating" && machine.state.getCurrentState() !== "decelerating"){
|
||||
|
||||
//Equilize pressures over all machines so we can make a proper calculation
|
||||
machine.measurements.type("pressure").variant("measured").position("downstream").value(maxDownstream);
|
||||
machine.measurements.type("pressure").variant("measured").position("upstream").value(minUpstream);
|
||||
this._writeChildMeasurement(machine, "pressure", "measured", "downstream", maxDownstream, this.unitPolicy.canonical.pressure);
|
||||
this._writeChildMeasurement(machine, "pressure", "measured", "upstream", minUpstream, this.unitPolicy.canonical.pressure);
|
||||
|
||||
// after updating the measurement directly we need to force the update of the value OLIFANT this is not so clear now in the code
|
||||
// we need to find a better way to do this but for now it works
|
||||
@@ -695,8 +728,8 @@ class MachineGroup {
|
||||
this.logger.debug(`Moving to demand: ${Qd.toFixed(2)} -> Pumps: [${debugInfo}] => Total Power: ${bestResult.bestPower.toFixed(2)}`);
|
||||
|
||||
//store the total delivered power
|
||||
this.measurements.type("power").variant("predicted").position("atequipment").value(bestResult.bestPower);
|
||||
this.measurements.type("flow").variant("predicted").position("atequipment").value(bestResult.bestFlow);
|
||||
this._writeMeasurement("power", "predicted", "atequipment", bestResult.bestPower, this.unitPolicy.canonical.power);
|
||||
this._writeMeasurement("flow", "predicted", "atequipment", bestResult.bestFlow, this.unitPolicy.canonical.flow);
|
||||
this.measurements.type("efficiency").variant("predicted").position("atequipment").value(bestResult.bestFlow / bestResult.bestPower);
|
||||
this.measurements.type("Ncog").variant("predicted").position("atequipment").value(bestResult.bestCog);
|
||||
|
||||
@@ -736,8 +769,8 @@ class MachineGroup {
|
||||
// Get current pressures from all machines
|
||||
const pressures = Object.entries(this.machines).map(([machineId, machine]) => {
|
||||
return {
|
||||
downstream: machine.measurements.type("pressure").variant("measured").position("downstream").getCurrentValue(),
|
||||
upstream: machine.measurements.type("pressure").variant("measured").position("upstream").getCurrentValue()
|
||||
downstream: this._readChildMeasurement(machine, "pressure", "measured", "downstream", this.unitPolicy.canonical.pressure),
|
||||
upstream: this._readChildMeasurement(machine, "pressure", "measured", "upstream", this.unitPolicy.canonical.pressure)
|
||||
};
|
||||
});
|
||||
|
||||
@@ -748,8 +781,8 @@ class MachineGroup {
|
||||
// Set consistent pressures across machines
|
||||
Object.entries(this.machines).forEach(([machineId, machine]) => {
|
||||
if(!this.isMachineActive(machineId)){
|
||||
machine.measurements.type("pressure").variant("measured").position("downstream").value(maxDownstream);
|
||||
machine.measurements.type("pressure").variant("measured").position("upstream").value(minUpstream);
|
||||
this._writeChildMeasurement(machine, "pressure", "measured", "downstream", maxDownstream, this.unitPolicy.canonical.pressure);
|
||||
this._writeChildMeasurement(machine, "pressure", "measured", "upstream", minUpstream, this.unitPolicy.canonical.pressure);
|
||||
// Update the measured pressure value
|
||||
const pressure = machine.getMeasuredPressure();
|
||||
this.logger.debug(`Setting pressure for machine ${machineId} to ${pressure}`);
|
||||
@@ -925,8 +958,8 @@ class MachineGroup {
|
||||
this.logger.debug(`Priority control for demand: ${totalFlow.toFixed(2)} -> Active pumps: [${debugInfo}] => Total Power: ${totalPower.toFixed(2)}`);
|
||||
|
||||
// Store measurements
|
||||
this.measurements.type("power").variant("predicted").position("atequipment").value(totalPower);
|
||||
this.measurements.type("flow").variant("predicted").position("atequipment").value(totalFlow);
|
||||
this._writeMeasurement("power", "predicted", "atequipment", totalPower, this.unitPolicy.canonical.power);
|
||||
this._writeMeasurement("flow", "predicted", "atequipment", totalFlow, this.unitPolicy.canonical.flow);
|
||||
this.measurements.type("efficiency").variant("predicted").position("atequipment").value(totalFlow / totalPower);
|
||||
this.measurements.type("Ncog").variant("predicted").position("atequipment").value(totalCog);
|
||||
|
||||
@@ -1040,8 +1073,8 @@ class MachineGroup {
|
||||
// fetch and store measurements
|
||||
Object.entries(this.machines).forEach(([machineId, machine]) => {
|
||||
|
||||
const powerValue = machine.measurements.type("power").variant("predicted").position("atequipment").getCurrentValue();
|
||||
const flowValue = machine.measurements.type("flow").variant("predicted").position("atequipment").getCurrentValue();
|
||||
const powerValue = this._readChildMeasurement(machine, "power", "predicted", "atequipment", this.unitPolicy.canonical.power);
|
||||
const flowValue = this._readChildMeasurement(machine, "flow", "predicted", "atequipment", this.unitPolicy.canonical.flow);
|
||||
|
||||
if (powerValue !== null) {
|
||||
totalPower.push(powerValue);
|
||||
@@ -1051,8 +1084,8 @@ class MachineGroup {
|
||||
}
|
||||
});
|
||||
|
||||
this.measurements.type("power").variant("predicted").position("atequipment").value(totalPower.reduce((a, b) => a + b, 0));
|
||||
this.measurements.type("flow").variant("predicted").position("atequipment").value(totalFlow.reduce((a, b) => a + b, 0));
|
||||
this._writeMeasurement("power", "predicted", "atequipment", totalPower.reduce((a, b) => a + b, 0), this.unitPolicy.canonical.power);
|
||||
this._writeMeasurement("flow", "predicted", "atequipment", totalFlow.reduce((a, b) => a + b, 0), this.unitPolicy.canonical.flow);
|
||||
|
||||
if(totalPower.reduce((a, b) => a + b, 0) > 0){
|
||||
this.measurements.type("efficiency").variant("predicted").position("atequipment").value(totalFlow.reduce((a, b) => a + b, 0) / totalPower.reduce((a, b) => a + b, 0));
|
||||
@@ -1163,6 +1196,106 @@ class MachineGroup {
|
||||
}));
|
||||
}
|
||||
|
||||
_buildUnitPolicy(config = {}) {
|
||||
const flowUnit = this._resolveUnitOrFallback(
|
||||
config?.general?.unit,
|
||||
'volumeFlowRate',
|
||||
DEFAULT_IO_UNITS.flow
|
||||
);
|
||||
const pressureUnit = this._resolveUnitOrFallback(
|
||||
config?.general?.pressureUnit,
|
||||
'pressure',
|
||||
DEFAULT_IO_UNITS.pressure
|
||||
);
|
||||
const powerUnit = this._resolveUnitOrFallback(
|
||||
config?.general?.powerUnit,
|
||||
'power',
|
||||
DEFAULT_IO_UNITS.power
|
||||
);
|
||||
|
||||
return {
|
||||
canonical: { ...CANONICAL_UNITS },
|
||||
output: {
|
||||
flow: flowUnit,
|
||||
pressure: pressureUnit,
|
||||
power: powerUnit,
|
||||
temperature: DEFAULT_IO_UNITS.temperature,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
_resolveUnitOrFallback(candidate, expectedMeasure, fallbackUnit) {
|
||||
const fallback = String(fallbackUnit || '').trim();
|
||||
const raw = typeof candidate === 'string' ? candidate.trim() : '';
|
||||
if (!raw) {
|
||||
return fallback;
|
||||
}
|
||||
try {
|
||||
const desc = convert().describe(raw);
|
||||
if (expectedMeasure && desc.measure !== expectedMeasure) {
|
||||
throw new Error(`expected '${expectedMeasure}', got '${desc.measure}'`);
|
||||
}
|
||||
return raw;
|
||||
} catch (error) {
|
||||
this.logger?.warn?.(`Invalid unit '${raw}' (${error.message}); falling back to '${fallback}'.`);
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
_outputUnitForType(type) {
|
||||
switch (String(type || '').toLowerCase()) {
|
||||
case 'flow':
|
||||
return this.unitPolicy.output.flow;
|
||||
case 'power':
|
||||
return this.unitPolicy.output.power;
|
||||
case 'pressure':
|
||||
return this.unitPolicy.output.pressure;
|
||||
case 'temperature':
|
||||
return this.unitPolicy.output.temperature;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
_readMeasurement(type, variant, position, unit = null) {
|
||||
const requestedUnit = unit || this._outputUnitForType(type);
|
||||
return this.measurements
|
||||
.type(type)
|
||||
.variant(variant)
|
||||
.position(position)
|
||||
.getCurrentValue(requestedUnit || undefined);
|
||||
}
|
||||
|
||||
_writeMeasurement(type, variant, position, value, unit = null, timestamp = Date.now()) {
|
||||
if (!Number.isFinite(value)) {
|
||||
return;
|
||||
}
|
||||
this.measurements
|
||||
.type(type)
|
||||
.variant(variant)
|
||||
.position(position)
|
||||
.value(value, timestamp, unit || undefined);
|
||||
}
|
||||
|
||||
_readChildMeasurement(machine, type, variant, position, unit = null) {
|
||||
return machine?.measurements
|
||||
?.type(type)
|
||||
?.variant(variant)
|
||||
?.position(position)
|
||||
?.getCurrentValue(unit || undefined);
|
||||
}
|
||||
|
||||
_writeChildMeasurement(machine, type, variant, position, value, unit = null, timestamp = Date.now()) {
|
||||
if (!machine?.measurements || !Number.isFinite(value)) {
|
||||
return;
|
||||
}
|
||||
machine.measurements
|
||||
.type(type)
|
||||
.variant(variant)
|
||||
.position(position)
|
||||
.value(value, timestamp, unit || undefined);
|
||||
}
|
||||
|
||||
setMode(mode) {
|
||||
this.mode = mode;
|
||||
}
|
||||
@@ -1173,12 +1306,12 @@ class MachineGroup {
|
||||
const output = {};
|
||||
|
||||
//build the output object
|
||||
this.measurements.getTypes().forEach(type => {
|
||||
this.measurements.getVariants(type).forEach(variant => {
|
||||
|
||||
const downstreamVal = this.measurements.type(type).variant(variant).position("downstream").getCurrentValue();
|
||||
const atEquipmentVal = this.measurements.type(type).variant(variant).position("atequipment").getCurrentValue();
|
||||
const upstreamVal = this.measurements.type(type).variant(variant).position("upstream").getCurrentValue();
|
||||
Object.entries(this.measurements.measurements || {}).forEach(([type, variants]) => {
|
||||
Object.keys(variants || {}).forEach((variant) => {
|
||||
const unit = this._outputUnitForType(type);
|
||||
const downstreamVal = this._readMeasurement(type, variant, "downstream", unit);
|
||||
const atEquipmentVal = this._readMeasurement(type, variant, "atequipment", unit);
|
||||
const upstreamVal = this._readMeasurement(type, variant, "upstream", unit);
|
||||
|
||||
if (downstreamVal != null) {
|
||||
output[`downstream_${variant}_${type}`] = downstreamVal;
|
||||
@@ -1190,8 +1323,13 @@ class MachineGroup {
|
||||
output[`atequipment${variant}_${type}`] = atEquipmentVal;
|
||||
}
|
||||
if (downstreamVal != null && upstreamVal != null) {
|
||||
const diffVal = this.measurements.type(type).variant(variant).difference().value;
|
||||
output[`differential_${variant}_${type}`] = diffVal;
|
||||
const diff = this.measurements
|
||||
.type(type)
|
||||
.variant(variant)
|
||||
.difference({ from: 'downstream', to: 'upstream', unit });
|
||||
if (diff?.value != null) {
|
||||
output[`differential_${variant}_${type}`] = diff.value;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user