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

This commit is contained in:
znetsixe
2025-10-16 14:37:42 +02:00
parent f9d1348fd0
commit c99d24e4c6
3 changed files with 69 additions and 4 deletions

View File

@@ -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
// ====================================