From c99d24e4c63331d002c38f366b089782e1dbe98f Mon Sep 17 00:00:00 2001 From: znetsixe <73483679+znetsixe@users.noreply.github.com> Date: Thu, 16 Oct 2025 14:37:42 +0200 Subject: [PATCH] added lagged value functionality for retrieving values further down in memory; Converted position always to lower case strings to avoid problems with caps sensitivity names; added examples for use in examples.js --- src/measurements/Measurement.js | 17 ++++++++++++++ src/measurements/MeasurementContainer.js | 27 ++++++++++++++++++++-- src/measurements/examples.js | 29 ++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/measurements/Measurement.js b/src/measurements/Measurement.js index f175546..761432b 100644 --- a/src/measurements/Measurement.js +++ b/src/measurements/Measurement.js @@ -67,6 +67,23 @@ class Measurement { if (this.values.length === 0) return null; return this.values[this.values.length - 1]; } + + getLaggedValue(lag){ + if(this.values.length <= lag) return null; + return this.values[this.values.length - lag]; + } + + getLaggedSample(lag){ + if (lag < 0) throw new Error('lag must be >= 0'); + const index = this.values.length - 1 - lag; + if (index < 0) return null; + + return { + value: this.values[index], + timestamp: this.timestamps[index], + unit: this.unit, + }; + } getAverage() { if (this.values.length === 0) return null; diff --git a/src/measurements/MeasurementContainer.js b/src/measurements/MeasurementContainer.js index ea95e46..cc09839 100644 --- a/src/measurements/MeasurementContainer.js +++ b/src/measurements/MeasurementContainer.js @@ -94,7 +94,8 @@ class MeasurementContainer { throw new Error('Variant must be specified before position'); } - this._currentPosition = positionValue; + + this._currentPosition = positionValue.toString().toLowerCase();; return this; } @@ -247,9 +248,31 @@ class MeasurementContainer { return measurement ? measurement.getAllValues() : null; } + getLaggedValue(lag = 1,requestedUnit = null ){ + const measurement = this.get(); + if (!measurement) return null; + + const value = measurement.getLaggedSample(lag); + if (value === null) return null; - differenceUpDown(){ + // Return as-is if no unit conversion requested + if (!requestedUnit) { + return value; + } + // Convert if needed + if (measurement.unit && requestedUnit !== measurement.unit) { + try { + return convertModule(value).from(measurement.unit).to(requestedUnit); + } catch (error) { + if (this.logger) { + this.logger.error(`Unit conversion failed: ${error.message}`); + } + return value; // Return original value if conversion fails + } + } + + return value; } diff --git a/src/measurements/examples.js b/src/measurements/examples.js index 4f66759..9521a7a 100644 --- a/src/measurements/examples.js +++ b/src/measurements/examples.js @@ -214,8 +214,8 @@ const pressureDiff = basicContainer console.log(`Pressure difference: ${pressureDiff.value} ${pressureDiff.unit}\n`); //reversable difference -const deltaP = measurements.type("pressure").variant("measured").difference(); // defaults to downstream - upstream -const netFlow = measurements.type("flow").variant("measured").difference({ from: "upstream", to: "downstream" }); +const deltaP = basicContainer.type("pressure").variant("measured").difference(); // defaults to downstream - upstream +const netFlow = basicContainer.type("flow").variant("measured").difference({ from: "upstream", to: "downstream" }); // ==================================== // ADVANCED STATISTICS & HISTORY @@ -252,6 +252,28 @@ const allValues = stats.getAllValues(); console.log(` Samples: ${allValues.values.length}`); console.log(` History: [${allValues.values.join(', ')}]\n`); +console.log('--- Lagged sample comparison ---'); + +const latest = stats.getCurrentValue(); // existing helper +const prevSample = stats.getLaggedValue(1); // new helper +const prevPrevSample = stats.getLaggedValue(2); // optional + +if (prevSample) { + const delta = latest - prevSample.value; + console.log( + `Current vs previous: ${latest} ${statsData.unit} (t=${stats.get().getLatestTimestamp()}) vs ` + + `${prevSample.value} ${prevSample.unit} (t=${prevSample.timestamp})` + ); + console.log(`Δ = ${delta.toFixed(2)} ${statsData.unit}`); +} + +if (prevPrevSample) { + console.log( + `Previous vs 2-steps-back timestamps: ${new Date(prevSample.timestamp).toISOString()} vs ` + + `${new Date(prevPrevSample.timestamp).toISOString()}` + ); +} + // ==================================== // DYNAMIC UNIT MANAGEMENT // ==================================== @@ -303,8 +325,11 @@ basicContainer.getTypes().forEach(type => { } }); + + console.log('\n✅ All examples complete!\n'); + // ==================================== // BEST PRACTICES // ====================================