From d7cb8e1072500994dedf59ed7ef94b2c48754f6a Mon Sep 17 00:00:00 2001 From: znetsixe <73483679+znetsixe@users.noreply.github.com> Date: Tue, 21 Oct 2025 12:45:06 +0200 Subject: [PATCH 1/2] latest version --- src/measurements/examples.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/measurements/examples.js b/src/measurements/examples.js index 9521a7a..63e6c3b 100644 --- a/src/measurements/examples.js +++ b/src/measurements/examples.js @@ -255,8 +255,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; From 1725c5b0e9ae4cf8101aa63fa55527e2d8539682 Mon Sep 17 00:00:00 2001 From: znetsixe <73483679+znetsixe@users.noreply.github.com> Date: Thu, 23 Oct 2025 09:51:27 +0200 Subject: [PATCH 2/2] bug fixes for measurement container lagged retrieval-> unit conversion and sample output --- src/measurements/MeasurementContainer.js | 58 ++++++++++++++++++++++-- src/measurements/examples.js | 19 ++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/measurements/MeasurementContainer.js b/src/measurements/MeasurementContainer.js index cc09839..1a3cab0 100644 --- a/src/measurements/MeasurementContainer.js +++ b/src/measurements/MeasurementContainer.js @@ -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 } } diff --git a/src/measurements/examples.js b/src/measurements/examples.js index 63e6c3b..254e61e 100644 --- a/src/measurements/examples.js +++ b/src/measurements/examples.js @@ -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`); // ====================================