license update and enhancements to measurement functionality + child parent relationship

This commit is contained in:
znetsixe
2025-08-07 13:52:06 +02:00
parent fa7c59fcab
commit d9c2699566
2 changed files with 213 additions and 81 deletions

View File

@@ -67,7 +67,7 @@ class Machine {
this.config = this.configUtils.initConfig(machineConfig);
if (!this.model || !this.curve) {
this.logger.warning(`${!this.model ? 'Model not specified' : 'Curve not found for model ' + this.model} in machineConfig. Cannot make predictions.`);
this.logger.warn(`${!this.model ? 'Model not specified' : 'Curve not found for model ' + this.model} in machineConfig. Cannot make predictions.`);
// Set prediction objects to null to prevent method calls
this.predictFlow = null;
this.predictPower = null;
@@ -91,7 +91,7 @@ class Machine {
// Initialize measurements
this.measurements = new MeasurementContainer();
this.interpolation = new interpolation();
this.child = {}; // object to hold child information so we know on what to subscribe
this.flowDrift = null;
@@ -104,15 +104,72 @@ class Machine {
this.absDistFromPeak = 0;
this.relDistFromPeak = 0;
// When position state changes, update position
this.state.emitter.on("positionChange", (data) => {
this.logger.debug(`Position change detected: ${data}`);
this.updatePosition();
});
this.child = {}; // object to hold child information so we know on what to subscribe
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
}
/*------------------- Register child events -------------------*/
registerOnChildEvents() {
this.logger.debug('Setting up child event listeners');
// ✅ Use utility to get sensors (but keep your manual control)
const sensors = this.childRegistrationUtils.getChildrenOfType('measurement', 'sensor');
if (sensors.length === 0) {
this.logger.warn('No measurement sensors found to register events for');
return;
}
this.logger.debug(`Found ${sensors.length} sensors to register events for`);
// ✅ PRESSURE - Your existing logic but enhanced
sensors.forEach(sensor => {
// ✅ Enhanced upstream pressure handling
sensor.measurements.emitter.on('pressure.measured.upstream', (eventData) => {
this.logger.debug(`🔄 Upstream pressure from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
// ✅ Your existing logic - but with enhanced context
this.updateMeasuredPressure(eventData.value, 'upstream', eventData);
});
// ✅ Enhanced downstream pressure handling
sensor.measurements.emitter.on('pressure.measured.downstream', (eventData) => {
this.logger.debug(`🔄 Downstream pressure from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
// ✅ Your existing logic - but with enhanced context
this.updateMeasuredPressure(eventData.value, 'downstream', eventData);
});
this.logger.debug(`✅ Pressure events registered for sensor: ${sensor.config.general.name}`);
});
// ✅ FLOW - Add your flow handling (following same pattern)
sensors.forEach(sensor => {
sensor.measurements.emitter.on('flow.measured.upstream', (eventData) => {
this.logger.debug(`🔄 Upstream flow from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
this.updateMeasuredFlow(eventData.value, 'upstream', eventData);
});
sensor.measurements.emitter.on('flow.measured.downstream', (eventData) => {
this.logger.debug(`🔄 Downstream flow from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
this.updateMeasuredFlow(eventData.value, 'downstream', eventData);
});
});
// ✅ Log registration summary
this.logger.info(`✅ Event listeners registered for ${sensors.length} measurement sensors`);
// ✅ Optional: Debug child structure
this.childRegistrationUtils.logChildStructure();
}
// Method to assess drift using errorMetrics
assessDrift(measurement, processMin, processMax) {
this.logger.debug(`Assessing drift for measurement: ${measurement} processMin: ${processMin} processMax: ${processMax}`);
@@ -260,9 +317,7 @@ class Machine {
// Calculate flow based on current pressure and position
calcFlow(x) {
if(this.hasCurve) {
const state = this.state.getCurrentState();
if (!["operational", "accelerating", "decelerating"].includes(state)) {
if (!this._isOperationalState()) {
this.measurements.type("flow").variant("predicted").position("downstream").value(0);
this.logger.debug(`Machine is not operational. Setting predicted flow to 0.`);
return 0;
@@ -285,8 +340,7 @@ class Machine {
// Calculate power based on current pressure and position
calcPower(x) {
if(this.hasCurve) {
const state = this.state.getCurrentState();
if (!["operational", "accelerating", "decelerating"].includes(state)) {
if (!this._isOperationalState()) {
this.measurements.type("power").variant("predicted").position('upstream').value(0);
this.logger.debug(`Machine is not operational. Setting predicted power to 0.`);
return 0;
@@ -340,7 +394,8 @@ class Machine {
}
// this function returns the pressure for calculations
// returns the best available pressure measurement to use in the prediction calculation
// this will be either the differential pressure, downstream or upstream pressure
getMeasuredPressure() {
const pressureDiff = this.measurements.type('pressure').variant('measured').difference();
@@ -435,7 +490,7 @@ class Machine {
}
handleMeasuredPower() {
const power = this.measurements.type("power").variant("measured").position("upstream").getCurrentValue();
const power = this.measurements.type("power").variant("measured").position("atEquipment").getCurrentValue();
// If your system calls it "upstream" or just a single "value", adjust accordingly
if (power != null) {
@@ -447,75 +502,55 @@ class Machine {
}
}
updatePressure(variant,value,position) {
// ENHANCED: Your existing handler but with rich context
updateMeasuredPressure(value, position, context = {}) {
// Your existing operational check
if (!this._isOperationalState()) {
this.logger.warn(`Machine not operational, skipping pressure update from ${context.childName || 'unknown'}`);
return;
}
switch (variant) {
case ("measured"):
//only update when machine is in a state where it can be used
if (this.state.getCurrentState() == "operational" || this.state.getCurrentState() == "accelerating" || this.state.getCurrentState() == "decelerating") {
// put value in measurements
this.measurements.type("pressure").variant("measured").position(position).value(value);
this.logger.debug(`Updated measured pressure: ${value} at position: ${position}`);
//when measured pressure gets updated we need some logic to fetch the relevant value which could be downstream or differential pressure
const pressure = this.getMeasuredPressure();
//update the flow power and cog
this.updatePosition();
this.logger.debug(`Measured pressure: ${pressure}`);
}
break;
// Enhanced logging with child context
this.logger.debug(`Pressure update: ${value} at ${position} from ${context.childName || 'child'} (${context.childId || 'unknown-id'})`);
// Store in parent's measurement container (your existing logic)
this.measurements.type("pressure").variant("measured").position(position).value(value, context.timestamp, context.unit);
// Your existing logic
const pressure = this.getMeasuredPressure();
this.updatePosition();
this.logger.debug(`Using pressure: ${pressure} for calculations`);
}
default:
this.logger.warn(`Unrecognized variant '${variant}' for pressure update.`);
break;
// NEW: Flow handler (following your pattern)
updateMeasuredFlow(value, position, context = {}) {
if (!this._isOperationalState()) {
this.logger.warn(`Machine not operational, skipping flow update from ${context.childName || 'unknown'}`);
return;
}
this.logger.debug(`Flow update: ${value} at ${position} from ${context.childName || 'child'}`);
// Store in parent's measurement container
this.measurements.type("flow").variant("measured").position(position).value(value, context.timestamp, context.unit);
// Update predicted flow if you have prediction capability
if (this.predictFlow) {
this.measurements.type("flow").variant("predicted").position("atEquipment").value(this.predictFlow.outputY || 0);
}
}
updateFlow(variant,value,position) {
switch (variant) {
case ("measured"):
// put value in measurements
this.measurements.type("flow").variant("measured").position(position).value(value);
//when measured flow gets updated we need to push the last known value in the prediction measurements to keep them synced
this.measurements.type("flow").variant("predicted").position("downstream").value(this.predictFlow.outputY);
break;
case ("predicted"):
this.logger.debug('not doing anythin yet');
break;
default:
this.logger.warn(`Unrecognized variant '${variant}' for flow update.`);
break;
}
}
updateMeasurement(variant, subType, value, position) {
this.logger.debug(`---------------------- updating ${subType} ------------------ `);
switch (subType) {
case "pressure":
// Update pressure measurement
this.updatePressure(variant,value,position);
break;
case "flow":
this.updateFlow(variant,value,position);
// Update flow measurement
this.flowDrift = this.assessDrift("flow", this.predictFlow.currentFxyYMin , this.predictFlow.currentFxyYMax);
this.logger.debug(`---------------------------------------- `);
break;
case "power":
// Update power measurement
break;
default:
this.logger.error(`Type '${subType}' not recognized for measured update.`);
return;
}
// Helper method for operational state check
_isOperationalState() {
const state = this.state.getCurrentState();
return ["operational", "accelerating", "decelerating"].includes(state);
}
//what is the internal functions that need updating when something changes that has influence on this.
updatePosition() {
if (this.state.getCurrentState() == "operational" || this.state.getCurrentState() == "accelerating" || this.state.getCurrentState() == "decelerating") {
if (this._isOperationalState()) {
const currentPosition = this.state.getCurrentPosition();
// Update the predicted values based on the new position
@@ -729,12 +764,12 @@ class Machine {
module.exports = Machine;
/*------------------- Testing -------------------*/
/*
curve = require('C:/Users/zn375/.node-red/public/fallbackData.json');
//import a child
const Child = require('../../../measurement/dependencies/measurement/measurement');
const Child = require('../../measurement/src/specificClass');
console.log(`Creating child...`);
const PT1 = new Child(config={
@@ -747,11 +782,16 @@ const PT1 = new Child(config={
},
functionality:{
softwareType:"measurement",
positionVsParent:"upstream",
},
asset:{
type:"sensor",
subType:"pressure",
supplier:"Vega",
category:"sensor",
type:"pressure",
model:"Vegabar 82",
unit: "mbar"
},
});
const PT2 = new Child(config={
@@ -764,10 +804,14 @@ const PT2 = new Child(config={
},
functionality:{
softwareType:"measurement",
positionVsParent:"upstream",
},
asset:{
type:"sensor",
subType:"pressure",
supplier:"Vega",
category:"sensor",
type:"pressure",
model:"Vegabar 82",
unit: "mbar"
},
});
@@ -785,7 +829,7 @@ const machineConfig = {
asset: {
supplier: "Hydrostal",
type: "pump",
subType: "centrifugal",
category: "centrifugal",
model: "H05K-S03R+HGM1X-X280KO", // Ensure this field is present.
machineCurve: curve["machineCurves"]["Hydrostal"]["H05K-S03R+HGM1X-X280KO"],
}