Compare commits

...

3 Commits

Author SHA1 Message Date
Rene De ren
48a227d519 Merge branch 'main' into dev-Rene 2025-10-24 15:22:08 +02:00
znetsixe
1725c5b0e9 bug fixes for measurement container lagged retrieval-> unit conversion and sample output 2025-10-23 09:51:27 +02:00
znetsixe
d7cb8e1072 latest version 2025-10-21 12:45:06 +02:00
2 changed files with 75 additions and 6 deletions

View File

@@ -169,6 +169,50 @@ class MeasurementContainer {
return this;
}
/**
* Check whether a measurement series exists.
*
* You can rely on the current chain (type/variant/position already set via
* type().variant().position()), or pass them explicitly via the options.
*
* @param {object} options
* @param {string} [options.type] Override the current type
* @param {string} [options.variant] Override the current variant
* @param {string} [options.position] Override the current position
* @param {boolean} [options.requireValues=false]
* When true, the series must contain at least one stored value.
*
* @returns {boolean}
*/
exists({ type, variant, position, requireValues = false } = {}) {
const typeKey = type ?? this._currentType;
if (!typeKey) return false;
const variantKey = variant ?? this._currentVariant;
if (!variantKey) return false;
const positionKey = position ?? this._currentPosition;
const typeBucket = this.measurements[typeKey];
if (!typeBucket) return false;
const variantBucket = typeBucket[variantKey];
if (!variantBucket) return false;
if (!positionKey) {
// No specific position requested just check the variant bucket.
return requireValues
? Object.values(variantBucket).some(m => m?.values?.length > 0)
: Object.keys(variantBucket).length > 0;
}
const measurement = variantBucket[positionKey];
if (!measurement) return false;
return requireValues ? measurement.values?.length > 0 : true;
}
unit(unitName) {
if (!this._ensureChainIsValid()) return this;
@@ -252,8 +296,9 @@ class MeasurementContainer {
const measurement = this.get();
if (!measurement) return null;
const value = measurement.getLaggedSample(lag);
if (value === null) return null;
let sample = measurement.getLaggedSample(lag);
if (sample === null) return null;
const value = sample.value;
// Return as-is if no unit conversion requested
if (!requestedUnit) {
@@ -263,12 +308,17 @@ class MeasurementContainer {
// Convert if needed
if (measurement.unit && requestedUnit !== measurement.unit) {
try {
return convertModule(value).from(measurement.unit).to(requestedUnit);
const convertedValue = convertModule(value).from(measurement.unit).to(requestedUnit);
//replace old value in sample and return obj
sample.value = convertedValue ;
sample.unit = requestedUnit;
return sample;
} catch (error) {
if (this.logger) {
this.logger.error(`Unit conversion failed: ${error.message}`);
}
return value; // Return original value if conversion fails
return sample; // Return original value if conversion fails
}
}

View File

@@ -175,6 +175,25 @@ const downstreamData = basicContainer
.position('downstream')
.get();
//check wether a serie exists
const hasSeries = measurements
.type("flow")
.variant("measured")
.exists(); // true if any position exists
const hasUpstreamValues = measurements
.type("flow")
.variant("measured")
.exists({ position: "upstream", requireValues: true });
// Passing everything explicitly
const hasPercent = measurements.exists({
type: "volume",
variant: "percent",
position: "atEquipment",
});
console.log(`Downstream: ${downstreamVal} ${downstreamData.unit} at ${downstreamData.distance}m\n`);
// ====================================
@@ -255,8 +274,8 @@ 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
const prevSample = stats.getLaggedValue(2); // new helper
const prevPrevSample = stats.getLaggedValue(3); // optional
if (prevSample) {
const delta = latest - prevSample.value;