Compare commits
3 Commits
9b7a8ae2c8
...
48a227d519
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48a227d519 | ||
|
|
1725c5b0e9 | ||
|
|
d7cb8e1072 |
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user