bug fixes for measurement container lagged retrieval-> unit conversion and sample output
This commit is contained in:
@@ -169,6 +169,50 @@ class MeasurementContainer {
|
|||||||
return this;
|
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) {
|
unit(unitName) {
|
||||||
if (!this._ensureChainIsValid()) return this;
|
if (!this._ensureChainIsValid()) return this;
|
||||||
@@ -252,8 +296,9 @@ class MeasurementContainer {
|
|||||||
const measurement = this.get();
|
const measurement = this.get();
|
||||||
if (!measurement) return null;
|
if (!measurement) return null;
|
||||||
|
|
||||||
const value = measurement.getLaggedSample(lag);
|
let sample = measurement.getLaggedSample(lag);
|
||||||
if (value === null) return null;
|
if (sample === null) return null;
|
||||||
|
const value = sample.value;
|
||||||
|
|
||||||
// Return as-is if no unit conversion requested
|
// Return as-is if no unit conversion requested
|
||||||
if (!requestedUnit) {
|
if (!requestedUnit) {
|
||||||
@@ -263,12 +308,17 @@ class MeasurementContainer {
|
|||||||
// Convert if needed
|
// Convert if needed
|
||||||
if (measurement.unit && requestedUnit !== measurement.unit) {
|
if (measurement.unit && requestedUnit !== measurement.unit) {
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
if (this.logger) {
|
if (this.logger) {
|
||||||
this.logger.error(`Unit conversion failed: ${error.message}`);
|
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')
|
.position('downstream')
|
||||||
.get();
|
.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`);
|
console.log(`Downstream: ${downstreamVal} ${downstreamData.unit} at ${downstreamData.distance}m\n`);
|
||||||
|
|
||||||
// ====================================
|
// ====================================
|
||||||
|
|||||||
Reference in New Issue
Block a user