updates
This commit is contained in:
2
mgc.html
2
mgc.html
@@ -39,7 +39,7 @@
|
|||||||
icon: "font-awesome/fa-cogs",
|
icon: "font-awesome/fa-cogs",
|
||||||
|
|
||||||
label: function () {
|
label: function () {
|
||||||
return this.positionIcon + " " + "machineGroup";
|
return (this.positionIcon || "") + " machineGroup";
|
||||||
},
|
},
|
||||||
oneditprepare: function() {
|
oneditprepare: function() {
|
||||||
// Initialize the menu data for the node
|
// 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");
|
const Specific = require("./specificClass");
|
||||||
|
|
||||||
class nodeClass {
|
class nodeClass {
|
||||||
@@ -37,13 +37,14 @@ class nodeClass {
|
|||||||
_loadConfig(uiConfig, node) {
|
_loadConfig(uiConfig, node) {
|
||||||
const cfgMgr = new configManager();
|
const cfgMgr = new configManager();
|
||||||
this.defaultConfig = cfgMgr.getConfig(this.name);
|
this.defaultConfig = cfgMgr.getConfig(this.name);
|
||||||
|
const flowUnit = this._resolveUnitOrFallback(uiConfig.unit, 'volumeFlowRate', 'm3/h', 'flow');
|
||||||
|
|
||||||
// Merge UI config over defaults
|
// Merge UI config over defaults
|
||||||
this.config = {
|
this.config = {
|
||||||
general: {
|
general: {
|
||||||
name: uiConfig.name,
|
name: uiConfig.name,
|
||||||
id: node.id, // node.id is for the child registration process
|
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: {
|
logging: {
|
||||||
enabled: uiConfig.enableLog,
|
enabled: uiConfig.enableLog,
|
||||||
logLevel: uiConfig.logLevel,
|
logLevel: uiConfig.logLevel,
|
||||||
@@ -57,6 +58,24 @@ class nodeClass {
|
|||||||
this._output = new outputUtils();
|
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() {
|
_updateNodeStatus() {
|
||||||
//console.log('Updating node status...');
|
//console.log('Updating node status...');
|
||||||
const mg = this.source;
|
const mg = this.source;
|
||||||
@@ -68,13 +87,13 @@ class nodeClass {
|
|||||||
?.type("flow")
|
?.type("flow")
|
||||||
?.variant("predicted")
|
?.variant("predicted")
|
||||||
?.position("atequipment")
|
?.position("atequipment")
|
||||||
?.getCurrentValue('m3/h') || 0;
|
?.getCurrentValue(mg?.unitPolicy?.output?.flow || 'm3/h') || 0;
|
||||||
|
|
||||||
const totalPower = mg.measurements
|
const totalPower = mg.measurements
|
||||||
?.type("power")
|
?.type("power")
|
||||||
?.variant("predicted")
|
?.variant("predicted")
|
||||||
?.position("atEquipment")
|
?.position("atEquipment")
|
||||||
?.getCurrentValue() || 0;
|
?.getCurrentValue(mg?.unitPolicy?.output?.power || 'kW') || 0;
|
||||||
|
|
||||||
// Calculate total capacity based on available machines with safety checks
|
// Calculate total capacity based on available machines with safety checks
|
||||||
const availableMachines = Object.values(mg.machines || {}).filter((machine) => {
|
const availableMachines = Object.values(mg.machines || {}).filter((machine) => {
|
||||||
|
|||||||
@@ -1,6 +1,20 @@
|
|||||||
//load local dependencies
|
//load local dependencies
|
||||||
const EventEmitter = require("events");
|
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.
|
* Machine group controller domain model.
|
||||||
@@ -14,20 +28,37 @@ class MachineGroup {
|
|||||||
this.defaultConfig = this.configManager.getConfig('machineGroupControl'); // Load default config for rotating machine ( use software type name ? )
|
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.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.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
|
// Init after config is set
|
||||||
this.logger = new logger(this.config.general.logging.enabled,this.config.general.logging.logLevel, this.config.general.name);
|
this.logger = new logger(this.config.general.logging.enabled,this.config.general.logging.logLevel, this.config.general.name);
|
||||||
|
|
||||||
// Initialize measurements
|
// Initialize measurements
|
||||||
this.measurements = new MeasurementContainer({
|
this.measurements = new MeasurementContainer({
|
||||||
autoConvert: true,
|
autoConvert: true,
|
||||||
windowSize: 50,
|
windowSize: 50,
|
||||||
defaultUnits: {
|
defaultUnits: {
|
||||||
pressure: 'mbar',
|
pressure: this.unitPolicy.output.pressure,
|
||||||
flow: 'l/s',
|
flow: this.unitPolicy.output.flow,
|
||||||
power: 'kW',
|
power: this.unitPolicy.output.power,
|
||||||
temperature: 'C'
|
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();
|
this.interpolation = new interpolation();
|
||||||
@@ -165,8 +196,8 @@ class MachineGroup {
|
|||||||
const minPower = machine.predictPower.currentFxyYMin;
|
const minPower = machine.predictPower.currentFxyYMin;
|
||||||
const maxPower = machine.predictPower.currentFxyYMax;
|
const maxPower = machine.predictPower.currentFxyYMax;
|
||||||
|
|
||||||
const actFlow = machine.measurements.type("flow").variant("predicted").position("atequipment").getCurrentValue();
|
const actFlow = this._readChildMeasurement(machine, "flow", "predicted", "atequipment", this.unitPolicy.canonical.flow) || 0;
|
||||||
const actPower = machine.measurements.type("power").variant("predicted").position("atequipment").getCurrentValue();
|
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}`);
|
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() {
|
handlePressureChange() {
|
||||||
this.logger.info("---------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>Pressure change detected.");
|
this.logger.debug("Pressure change detected.");
|
||||||
// Recalculate totals
|
// Recalculate totals
|
||||||
const { flow, power } = this.calcDynamicTotals();
|
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.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._writeMeasurement("flow", "predicted", "atequipment", flow.act, this.unitPolicy.canonical.flow);
|
||||||
this.measurements.type("power").variant("predicted").position("atequipment").value(power.act);
|
this._writeMeasurement("power", "predicted", "atequipment", power.act, this.unitPolicy.canonical.power);
|
||||||
|
|
||||||
const { maxEfficiency, lowestEfficiency } = this.calcGroupEfficiency(this.machines);
|
const { maxEfficiency, lowestEfficiency } = this.calcGroupEfficiency(this.machines);
|
||||||
const efficiency = this.measurements.type("efficiency").variant("predicted").position("atequipment").getCurrentValue();
|
const efficiency = this.measurements.type("efficiency").variant("predicted").position("atequipment").getCurrentValue();
|
||||||
@@ -260,11 +291,13 @@ class MachineGroup {
|
|||||||
//add special cases
|
//add special cases
|
||||||
if( state === "operational" && ( mode == "virtualControl" || mode === "fysicalControl") ){
|
if( state === "operational" && ( mode == "virtualControl" || mode === "fysicalControl") ){
|
||||||
let flow = 0;
|
let flow = 0;
|
||||||
if(machine.measurements.type("flow").variant("measured").position("downstream").getCurrentValue()){
|
const measuredFlow = this._readChildMeasurement(machine, "flow", "measured", "downstream", this.unitPolicy.canonical.flow);
|
||||||
flow = machine.measurements.type("flow").variant("measured").position("downstream").getCurrentValue();
|
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()){
|
else if (Number.isFinite(predictedFlow) && predictedFlow !== 0) {
|
||||||
flow = machine.measurements.type("flow").variant("predicted").position("atequipment").getCurrentValue();
|
flow = predictedFlow;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
this.logger.error("Dont perform calculation at all seeing that there is a machine working but we dont know the flow its producing");
|
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
|
// this is to ensure a correct evaluation of the flow and power consumption
|
||||||
const pressures = Object.entries(this.machines).map(([machineId, machine]) => {
|
const pressures = Object.entries(this.machines).map(([machineId, machine]) => {
|
||||||
return {
|
return {
|
||||||
downstream: machine.measurements.type("pressure").variant("measured").position("downstream").getCurrentValue(),
|
downstream: this._readChildMeasurement(machine, "pressure", "measured", "downstream", this.unitPolicy.canonical.pressure),
|
||||||
upstream: machine.measurements.type("pressure").variant("measured").position("upstream").getCurrentValue()
|
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"){
|
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
|
//Equilize pressures over all machines so we can make a proper calculation
|
||||||
machine.measurements.type("pressure").variant("measured").position("downstream").value(maxDownstream);
|
this._writeChildMeasurement(machine, "pressure", "measured", "downstream", maxDownstream, this.unitPolicy.canonical.pressure);
|
||||||
machine.measurements.type("pressure").variant("measured").position("upstream").value(minUpstream);
|
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
|
// 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
|
// 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)}`);
|
this.logger.debug(`Moving to demand: ${Qd.toFixed(2)} -> Pumps: [${debugInfo}] => Total Power: ${bestResult.bestPower.toFixed(2)}`);
|
||||||
|
|
||||||
//store the total delivered power
|
//store the total delivered power
|
||||||
this.measurements.type("power").variant("predicted").position("atequipment").value(bestResult.bestPower);
|
this._writeMeasurement("power", "predicted", "atequipment", bestResult.bestPower, this.unitPolicy.canonical.power);
|
||||||
this.measurements.type("flow").variant("predicted").position("atequipment").value(bestResult.bestFlow);
|
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("efficiency").variant("predicted").position("atequipment").value(bestResult.bestFlow / bestResult.bestPower);
|
||||||
this.measurements.type("Ncog").variant("predicted").position("atequipment").value(bestResult.bestCog);
|
this.measurements.type("Ncog").variant("predicted").position("atequipment").value(bestResult.bestCog);
|
||||||
|
|
||||||
@@ -736,8 +769,8 @@ class MachineGroup {
|
|||||||
// Get current pressures from all machines
|
// Get current pressures from all machines
|
||||||
const pressures = Object.entries(this.machines).map(([machineId, machine]) => {
|
const pressures = Object.entries(this.machines).map(([machineId, machine]) => {
|
||||||
return {
|
return {
|
||||||
downstream: machine.measurements.type("pressure").variant("measured").position("downstream").getCurrentValue(),
|
downstream: this._readChildMeasurement(machine, "pressure", "measured", "downstream", this.unitPolicy.canonical.pressure),
|
||||||
upstream: machine.measurements.type("pressure").variant("measured").position("upstream").getCurrentValue()
|
upstream: this._readChildMeasurement(machine, "pressure", "measured", "upstream", this.unitPolicy.canonical.pressure)
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -748,8 +781,8 @@ class MachineGroup {
|
|||||||
// Set consistent pressures across machines
|
// Set consistent pressures across machines
|
||||||
Object.entries(this.machines).forEach(([machineId, machine]) => {
|
Object.entries(this.machines).forEach(([machineId, machine]) => {
|
||||||
if(!this.isMachineActive(machineId)){
|
if(!this.isMachineActive(machineId)){
|
||||||
machine.measurements.type("pressure").variant("measured").position("downstream").value(maxDownstream);
|
this._writeChildMeasurement(machine, "pressure", "measured", "downstream", maxDownstream, this.unitPolicy.canonical.pressure);
|
||||||
machine.measurements.type("pressure").variant("measured").position("upstream").value(minUpstream);
|
this._writeChildMeasurement(machine, "pressure", "measured", "upstream", minUpstream, this.unitPolicy.canonical.pressure);
|
||||||
// Update the measured pressure value
|
// Update the measured pressure value
|
||||||
const pressure = machine.getMeasuredPressure();
|
const pressure = machine.getMeasuredPressure();
|
||||||
this.logger.debug(`Setting pressure for machine ${machineId} to ${pressure}`);
|
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)}`);
|
this.logger.debug(`Priority control for demand: ${totalFlow.toFixed(2)} -> Active pumps: [${debugInfo}] => Total Power: ${totalPower.toFixed(2)}`);
|
||||||
|
|
||||||
// Store measurements
|
// Store measurements
|
||||||
this.measurements.type("power").variant("predicted").position("atequipment").value(totalPower);
|
this._writeMeasurement("power", "predicted", "atequipment", totalPower, this.unitPolicy.canonical.power);
|
||||||
this.measurements.type("flow").variant("predicted").position("atequipment").value(totalFlow);
|
this._writeMeasurement("flow", "predicted", "atequipment", totalFlow, this.unitPolicy.canonical.flow);
|
||||||
this.measurements.type("efficiency").variant("predicted").position("atequipment").value(totalFlow / totalPower);
|
this.measurements.type("efficiency").variant("predicted").position("atequipment").value(totalFlow / totalPower);
|
||||||
this.measurements.type("Ncog").variant("predicted").position("atequipment").value(totalCog);
|
this.measurements.type("Ncog").variant("predicted").position("atequipment").value(totalCog);
|
||||||
|
|
||||||
@@ -1040,8 +1073,8 @@ class MachineGroup {
|
|||||||
// fetch and store measurements
|
// fetch and store measurements
|
||||||
Object.entries(this.machines).forEach(([machineId, machine]) => {
|
Object.entries(this.machines).forEach(([machineId, machine]) => {
|
||||||
|
|
||||||
const powerValue = machine.measurements.type("power").variant("predicted").position("atequipment").getCurrentValue();
|
const powerValue = this._readChildMeasurement(machine, "power", "predicted", "atequipment", this.unitPolicy.canonical.power);
|
||||||
const flowValue = machine.measurements.type("flow").variant("predicted").position("atequipment").getCurrentValue();
|
const flowValue = this._readChildMeasurement(machine, "flow", "predicted", "atequipment", this.unitPolicy.canonical.flow);
|
||||||
|
|
||||||
if (powerValue !== null) {
|
if (powerValue !== null) {
|
||||||
totalPower.push(powerValue);
|
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._writeMeasurement("power", "predicted", "atequipment", totalPower.reduce((a, b) => a + b, 0), this.unitPolicy.canonical.power);
|
||||||
this.measurements.type("flow").variant("predicted").position("atequipment").value(totalFlow.reduce((a, b) => a + b, 0));
|
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){
|
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));
|
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) {
|
setMode(mode) {
|
||||||
this.mode = mode;
|
this.mode = mode;
|
||||||
}
|
}
|
||||||
@@ -1173,27 +1306,32 @@ class MachineGroup {
|
|||||||
const output = {};
|
const output = {};
|
||||||
|
|
||||||
//build the output object
|
//build the output object
|
||||||
this.measurements.getTypes().forEach(type => {
|
Object.entries(this.measurements.measurements || {}).forEach(([type, variants]) => {
|
||||||
this.measurements.getVariants(type).forEach(variant => {
|
Object.keys(variants || {}).forEach((variant) => {
|
||||||
|
const unit = this._outputUnitForType(type);
|
||||||
const downstreamVal = this.measurements.type(type).variant(variant).position("downstream").getCurrentValue();
|
const downstreamVal = this._readMeasurement(type, variant, "downstream", unit);
|
||||||
const atEquipmentVal = this.measurements.type(type).variant(variant).position("atequipment").getCurrentValue();
|
const atEquipmentVal = this._readMeasurement(type, variant, "atequipment", unit);
|
||||||
const upstreamVal = this.measurements.type(type).variant(variant).position("upstream").getCurrentValue();
|
const upstreamVal = this._readMeasurement(type, variant, "upstream", unit);
|
||||||
|
|
||||||
if (downstreamVal != null) {
|
if (downstreamVal != null) {
|
||||||
output[`downstream_${variant}_${type}`] = downstreamVal;
|
output[`downstream_${variant}_${type}`] = downstreamVal;
|
||||||
}
|
}
|
||||||
if (upstreamVal != null) {
|
if (upstreamVal != null) {
|
||||||
output[`upstream_${variant}_${type}`] = upstreamVal;
|
output[`upstream_${variant}_${type}`] = upstreamVal;
|
||||||
}
|
}
|
||||||
if (atEquipmentVal != null) {
|
if (atEquipmentVal != null) {
|
||||||
output[`atequipment${variant}_${type}`] = atEquipmentVal;
|
output[`atequipment${variant}_${type}`] = atEquipmentVal;
|
||||||
}
|
}
|
||||||
if (downstreamVal != null && upstreamVal != null) {
|
if (downstreamVal != null && upstreamVal != null) {
|
||||||
const diffVal = this.measurements.type(type).variant(variant).difference().value;
|
const diff = this.measurements
|
||||||
output[`differential_${variant}_${type}`] = diffVal;
|
.type(type)
|
||||||
}
|
.variant(variant)
|
||||||
});
|
.difference({ from: 'downstream', to: 'upstream', unit });
|
||||||
|
if (diff?.value != null) {
|
||||||
|
output[`differential_${variant}_${type}`] = diff.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//fill in the rest of the output object
|
//fill in the rest of the output object
|
||||||
|
|||||||
Reference in New Issue
Block a user