working on a stable version

This commit is contained in:
znetsixe
2025-09-23 11:19:22 +02:00
parent ffab553f7e
commit c62071992d
2 changed files with 96 additions and 65 deletions

View File

@@ -61,25 +61,28 @@ class nodeClass {
const mg = this.source; const mg = this.source;
const mode = mg.mode; const mode = mg.mode;
const scaling = mg.scaling; const scaling = mg.scaling;
const totalFlow =
Math.round(
mg.measurements
.type("flow")
.variant("predicted")
.position("downstream")
.getCurrentValue() * 1
) / 1;
const totalPower =
Math.round(
mg.measurements
.type("power")
.variant("predicted")
.position("upstream")
.getCurrentValue() * 1
) / 1;
// Calculate total capacity based on available machines // Add safety checks for measurements
const availableMachines = Object.values(mg.machines).filter((machine) => { const totalFlow = mg.measurements
?.type("flow")
?.variant("predicted")
?.position("downstream")
?.getCurrentValue() || 0;
const totalPower = mg.measurements
?.type("power")
?.variant("predicted")
?.position("upstream")
?.getCurrentValue() || 0;
// Calculate total capacity based on available machines with safety checks
const availableMachines = Object.values(mg.machines || {}).filter((machine) => {
// Safety check: ensure machine and machine.state exist
if (!machine || !machine.state || typeof machine.state.getCurrentState !== 'function') {
console.warn(`Machine missing or invalid:`, machine?.config?.general?.id || 'unknown');
return false;
}
const state = machine.state.getCurrentState(); const state = machine.state.getCurrentState();
const mode = machine.currentMode; const mode = machine.currentMode;
return !( return !(
@@ -89,29 +92,27 @@ class nodeClass {
); );
}); });
const totalCapacity = Math.round(mg.dynamicTotals.flow.max * 1) / 1; const totalCapacity = Math.round((mg.dynamicTotals?.flow?.max || 0) * 1) / 1;
// Determine overall status based on available machines // Determine overall status based on available machines
const status = const status = availableMachines.length > 0
availableMachines.length > 0
? `${availableMachines.length} machine(s) connected` ? `${availableMachines.length} machine(s) connected`
: "No machines"; : "No machines";
let scalingSymbol = ""; let scalingSymbol = "";
switch (scaling.toLowerCase()) { switch ((scaling || "").toLowerCase()) {
case "absolute": case "absolute":
scalingSymbol = "Ⓐ"; // Clear symbol for Absolute mode scalingSymbol = "Ⓐ";
break; break;
case "normalized": case "normalized":
scalingSymbol = "Ⓝ"; // Clear symbol for Normalized mode scalingSymbol = "Ⓝ";
break; break;
default: default:
scalingSymbol = mode; scalingSymbol = mode || "";
break; break;
} }
// Generate status text in a single line const text = ` ${mode || 'Unknown'} | ${scalingSymbol}: 💨=${Math.round(totalFlow)}/${totalCapacity} | ⚡=${Math.round(totalPower)} | ${status}`;
const text = ` ${mode} | ${scalingSymbol}: 💨=${totalFlow}/${totalCapacity} | ⚡=${totalPower} | ${status}`;
return { return {
fill: availableMachines.length > 0 ? "green" : "red", fill: availableMachines.length > 0 ? "green" : "red",
@@ -197,13 +198,25 @@ class nodeClass {
const RED = this.RED; const RED = this.RED;
switch (msg.topic) { switch (msg.topic) {
case "registerChild": case "registerChild":
//console.log(`Registering child in mgc: ${msg.payload}`); console.log(`Registering child in mgc: ${msg.payload}`);
const childId = msg.payload; const childId = msg.payload;
const childObj = RED.nodes.getNode(childId); const childObj = RED.nodes.getNode(childId);
// Debug: Check what we're getting
console.log(`Child object:`, childObj ? 'found' : 'NOT FOUND');
console.log(`Child source:`, childObj?.source ? 'exists' : 'MISSING');
if (childObj?.source) {
console.log(`Child source type:`, childObj.source.constructor.name);
console.log(`Child has state:`, !!childObj.source.state);
}
mg.childRegistrationUtils.registerChild( mg.childRegistrationUtils.registerChild(
childObj.source, childObj.source,
msg.positionVsParent msg.positionVsParent
); );
// Debug: Check machines after registration
console.log(`Total machines after registration:`, Object.keys(mg.machines || {}).length);
break; break;
case "setMode": case "setMode":
@@ -215,6 +228,7 @@ class nodeClass {
case "setScaling": case "setScaling":
const scaling = msg.payload; const scaling = msg.payload;
mg.setScaling(scaling); mg.setScaling(scaling);
break; break;
case "Qd": case "Qd":

View File

@@ -87,6 +87,14 @@ class MachineGroup {
if(softwareType == "machine"){ if(softwareType == "machine"){
// Check if the machine is already registered // Check if the machine is already registered
this.machines[child.config.general.id] === undefined ? this.machines[child.config.general.id] = child : this.logger.warn(`Machine ${child.config.general.id} is already registered.`); this.machines[child.config.general.id] === undefined ? this.machines[child.config.general.id] = child : this.logger.warn(`Machine ${child.config.general.id} is already registered.`);
this.handleChildChange();
/*
// Listen for changes in the child machine
child.emitter.on('stateChange', () => this.handleChildChange());
child.emitter.on('pressureChange', () => this.handlePressureChange());
child.emitter.on('ncogChange', () => this.handleChildChange());
*/
} }
} }
@@ -207,6 +215,7 @@ class MachineGroup {
checkSpecialCases(machines, Qd) { checkSpecialCases(machines, Qd) {
Object.values(machines).forEach(machine => { Object.values(machines).forEach(machine => {
const state = machine.state.getCurrentState(); const state = machine.state.getCurrentState();
const mode = machine.currentMode; const mode = machine.currentMode;
@@ -240,7 +249,7 @@ class MachineGroup {
// Generate all possible subsets of machines (power set) // Generate all possible subsets of machines (power set)
Object.keys(machines).forEach(machineId => { Object.keys(machines).forEach(machineId => {
machineId = parseInt(machineId); //machineId = parseInt(machineId);
const state = machines[machineId].state.getCurrentState(); const state = machines[machineId].state.getCurrentState();
@@ -355,8 +364,9 @@ class MachineGroup {
} }
setScaling(scaling) { setScaling(scaling) {
const scalingSet = new Set(defaultConfig.scaling.current.rules.values.map( (value) => value.value)); const scalingSet = new Set(this.defaultConfig.scaling.current.rules.values.map( (value) => value.value));
scalingSet.has(scaling)? this.scaling = scaling : this.logger.warn(`${scaling} is not a valid scaling option.`); scalingSet.has(scaling)? this.scaling = scaling : this.logger.warn(`${scaling} is not a valid scaling option.`);
this.logger.debug(`Scaling set to: ${scaling}`);
} }
//handle input from parent / user / UI //handle input from parent / user / UI
@@ -894,29 +904,29 @@ class MachineGroup {
module.exports = MachineGroup; module.exports = MachineGroup;
/* /*
const Machine = require('../../../rotatingMachine/dependencies/machine/machine'); const Machine = require('../../rotatingMachine/src/specificClass');
const Measurement = require('../../../measurement/dependencies/measurement/measurement'); const Measurement = require('../../measurement/src/specificClass');
const specs = require('../../../generalFunctions/datasets/assetData/pumps/hydrostal/centrifugal pumps/models.json'); const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json');
const power = require("../../../convert/dependencies/definitions/power"); const { number } = require("../../generalFunctions/src/convert/lodash/lodash._objecttypes");
const { machine } = require("os");
function createBaseMachineConfig(name,specs) { function createBaseMachineConfig(machineNum, name,specs) {
return { return {
general: { general: {
logging: { enabled: true, logLevel: "warn" }, logging: { enabled: true, logLevel: "warn" },
name: name, name: name,
id: machineNum,
unit: "m3/h" unit: "m3/h"
}, },
functionality: { functionality: {
softwareType: "machine", softwareType: "machine",
role: "RotationalDeviceController" role: "rotationaldevicecontroller"
}, },
asset: { asset: {
type: "pump", category: "pump",
subType: "Centrifugal", type: "centrifugal",
model: "TestModel", model: "hidrostal-h05k-s03r",
supplier: "Hydrostal", supplier: "hydrostal",
machineCurve: specs[0].machineCurve machineCurve: specs
}, },
mode: { mode: {
current: "auto", current: "auto",
@@ -947,8 +957,8 @@ function createBaseMachineGroupConfig(name) {
name: name name: name
}, },
functionality: { functionality: {
softwareType: "machineGroup", softwareType: "machinegroup",
role: "GroupController" role: "groupcontroller"
}, },
scaling: { scaling: {
current: "normalized" current: "normalized"
@@ -959,22 +969,26 @@ function createBaseMachineGroupConfig(name) {
}; };
} }
const machineGroupConfig = createBaseMachineGroupConfig("TestMachineGroup"); const machineGroupConfig = createBaseMachineGroupConfig("testmachinegroup");
const machineConfig = createBaseMachineConfig("TestMachine",specs); const machineConfigs = {};
machineConfigs[1]= createBaseMachineConfig(1,"testmachine",specs);
machineConfigs[2] = createBaseMachineConfig(2,"testmachine2",specs);
const ptConfig = { const ptConfig = {
general: { general: {
logging: { enabled: true, logLevel: "debug" }, logging: { enabled: true, logLevel: "debug" },
name: "TestPT", name: "testpt",
id: "0",
unit: "mbar", unit: "mbar",
}, },
functionality: { functionality: {
softwareType: "measurement", softwareType: "measurement",
role: "Sensor" role: "sensor"
}, },
asset: { asset: {
type: "sensor", category: "sensor",
subType: "pressure", type: "pressure",
model: "TestModel", model: "testmodel",
supplier: "vega" supplier: "vega"
}, },
scaling:{ scaling:{
@@ -987,8 +1001,8 @@ async function makeMachines(){
const mg = new MachineGroup(machineGroupConfig); const mg = new MachineGroup(machineGroupConfig);
const pt1 = new Measurement(ptConfig); const pt1 = new Measurement(ptConfig);
const numofMachines = 2; const numofMachines = 2;
for(let i = 0; i < numofMachines; i++){ for(let i = 1; i <= numofMachines; i++){
const machine = new Machine(machineConfig); const machine = new Machine(machineConfigs[i]);
//mg.machines[i] = machine; //mg.machines[i] = machine;
mg.childRegistrationUtils.registerChild(machine, "downstream"); mg.childRegistrationUtils.registerChild(machine, "downstream");
} }
@@ -1029,14 +1043,16 @@ async function makeMachines(){
console.log("------------------------------------"); console.log("------------------------------------");
} }
//*/
*//* /*
for(let demand = 0 ; demand <= 100 ; demand += 1){ for(let demand = 0 ; demand <= 100 ; demand += 1){
//set pressure //set pressure
console.log("------------------------------------"); console.log("------------------------------------");
await mg.handleInput("parent",demand); await mg.handleInput("parent",demand);
console.log(mg.machines[1].state.getCurrentState());
console.log(mg.machines[2].state.getCurrentState());
pt1.calculateInput(1400); pt1.calculateInput(1400);
console.log("Waiting for 0.2 sec "); console.log("Waiting for 0.2 sec ");
//await new Promise(resolve => setTimeout(resolve, 200)); //await new Promise(resolve => setTimeout(resolve, 200));
@@ -1054,4 +1070,5 @@ async function makeMachines(){
makeMachines(); makeMachines();
//*/
*/