diff --git a/src/groupcontrol.test.js b/src/groupcontrol.test.js index ee14089..796876d 100644 --- a/src/groupcontrol.test.js +++ b/src/groupcontrol.test.js @@ -270,9 +270,6 @@ async function testNormalizedScaling(mg, pt1) { console.log("\n🧪 testing normalized scaling (0-100%)..."); mg.setScaling("normalized"); - //first set pressure: - pt1.inputValue = 1400; - //fetch ranges const maxflow = mg.dynamicTotals.flow.max; console.log(`max group flow capacity: ${maxflow.toFixed(2)} m3/h`); @@ -280,20 +277,47 @@ async function testNormalizedScaling(mg, pt1) { console.log(`min group flow capacity: ${minFlow.toFixed(2)} m3/h`); const testPoints = [0, 10, 25, 50, 75, 90, 100]; + const testPressurePoints = [800, 1200, 1600, 2000]; - for (const demand of testPoints) { + for (const pressure of testPressurePoints) { try { - console.log(`\n--- normalized demand: ${demand}% ---`); - await mg.handleInput("parent", demand); - - - logMachineStates(mg, `normalized ${demand}%`); - - } catch (err) { - console.error(`❌ error at ${demand}%:`, err.message); + console.log(`\n--- testing at ${pressure} mbar ---`); + pt1.calculateInput(pressure); + logMachineStates(mg, `${pressure} mbar, before demand tests`); + + + for (const demand of testPoints) { + try { + console.log(`\n--- normalized demand: ${demand}% ---`); + await mg.handleInput("parent", demand); + + + logMachineStates(mg, `normalized ${demand}%`); + + //check if total flow is within expected range + const totalFlow = mg.measurements?.type("flow")?.variant("predicted")?.position("downstream")?.getCurrentValue() || 0; + const expectedFlow = minFlow + (demand / 100) * (maxflow - minFlow); + const percentTolerance = 0.1 ; // % tolerance of expected flow + const tolerance = (expectedFlow * percentTolerance) / 100; + + if (totalFlow < expectedFlow - tolerance || totalFlow > expectedFlow + tolerance) { + console.warn(`⚠️ Total flow (${totalFlow.toFixed(2)} m3/h) is outside expected range (${(expectedFlow - tolerance).toFixed(2)} - ${(expectedFlow + tolerance).toFixed(2)} m3/h)`); + } + else { + console.log( `Difference between expected and actual flow: ${(totalFlow - expectedFlow).toFixed(2)} m3/h`); + console.log(`✅ Total flow (${totalFlow.toFixed(2)} m3/h) is within expected range (${(expectedFlow - tolerance).toFixed(2)} - ${(expectedFlow + tolerance).toFixed(2)} m3/h)`); + } + + } catch (err) { + console.error(`❌ error at ${demand}%:`, err.message); + } } + + } catch (err) { + console.error(`❌ error setting pressure to ${pressure}:`, err.message); } } +} async function testAbsoluteScaling(mg, pt1) { console.log("\n🧪 testing absolute scaling..."); diff --git a/src/specificClass.js b/src/specificClass.js index 30402b9..3d0c017 100644 --- a/src/specificClass.js +++ b/src/specificClass.js @@ -63,17 +63,21 @@ class MachineGroup { if(softwareType == "machine"){ // 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.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()); - */ + + //listen for machine pressure changes + this.logger.debug(`Listening for pressure changes from machine ${child.config.general.id}`); + + + child.measurements.emitter.on("pressure.measured.differential", (eventData) => { + this.logger.debug(`Pressure update from ${child.config.general.id}: ${eventData.value} ${eventData.unit}`); + this.handleChildChange(); + }); } } + + calcAbsoluteTotals() { const absoluteTotals = { flow: { min: Infinity, max: 0 }, power: { min: Infinity, max: 0 } }; @@ -112,7 +116,10 @@ class MachineGroup { const dynamicTotals = { flow: { min: Infinity, max: 0 }, power: { min: Infinity, max: 0 }, NCog : 0 }; + this.logger.debug(`\n --------- Calculating dynamic totals for ${Object.keys(this.machines).length} machines. @ current pressure settings : ----------`); Object.values(this.machines).forEach(machine => { + this.logger.debug(`Processing machine with id: ${machine.config.general.id}`); + this.logger.debug(`Current pressure settings: ${JSON.stringify(machine.predictFlow.currentF)}`); //fetch min flow ever seen over all machines const minFlow = machine.predictFlow.currentFxyYMin; const maxFlow = machine.predictFlow.currentFxyYMax;