need to further update measurement emit function

This commit is contained in:
Rene De ren
2025-10-03 15:37:08 +02:00
parent 44033da15d
commit d99561fa80
2 changed files with 89 additions and 86 deletions

View File

@@ -7,7 +7,7 @@ class MeasurementContainer {
this.emitter = new EventEmitter();
this.measurements = {};
this.windowSize = options.windowSize || 10; // Default window size
// For chaining context
this._currentType = null;
this._currentVariant = null;
@@ -24,11 +24,11 @@ class MeasurementContainer {
length: 'm',
...options.defaultUnits // Allow override
};
// Auto-conversion settings
this.autoConvert = options.autoConvert !== false; // Default to true
this.preferredUnits = options.preferredUnits || {}; // Per-measurement overrides
// For chaining context
this._currentType = null;
this._currentVariant = null;
@@ -39,23 +39,23 @@ class MeasurementContainer {
this.childId = null;
this.childName = null;
this.parentRef = null;
}
// NEW: Methods to set child context
setChildId(childId) {
this.childId = childId;
return this;
this.childId = childId;
return this;
}
setChildName(childName) {
this.childName = childName;
return this;
this.childName = childName;
return this;
}
setParentRef(parent) {
this.parentRef = parent;
return this;
this.parentRef = parent;
return this;
}
// New method to set preferred units
@@ -66,9 +66,9 @@ class MeasurementContainer {
// Get the target unit for a measurement type
_getTargetUnit(measurementType) {
return this.preferredUnits[measurementType] ||
this.defaultUnits[measurementType] ||
null;
return this.preferredUnits[measurementType] ||
this.defaultUnits[measurementType] ||
null;
}
// Chainable methods
@@ -99,71 +99,72 @@ class MeasurementContainer {
}
this._currentPosition = positionValue;
return this;
}
// ENHANCED: Update your existing value method
value(val, timestamp = Date.now(), sourceUnit = null) {
if (!this._ensureChainIsValid()) return this;
const measurement = this._getOrCreateMeasurement();
const targetUnit = this._getTargetUnit(this._currentType);
let convertedValue = val;
let finalUnit = sourceUnit || targetUnit;
if (!this._ensureChainIsValid()) return this;
// Auto-convert if enabled and units are specified
if (this.autoConvert && sourceUnit && targetUnit && sourceUnit !== targetUnit) {
try {
convertedValue = convertModule(val).from(sourceUnit).to(targetUnit);
finalUnit = targetUnit;
if (this.logger) {
this.logger.debug(`Auto-converted ${val} ${sourceUnit} to ${convertedValue} ${targetUnit}`);
}
} catch (error) {
if (this.logger) {
this.logger.warn(`Auto-conversion failed from ${sourceUnit} to ${targetUnit}: ${error.message}`);
}
convertedValue = val;
finalUnit = sourceUnit;
const measurement = this._getOrCreateMeasurement();
const targetUnit = this._getTargetUnit(this._currentType);
let convertedValue = val;
let finalUnit = sourceUnit || targetUnit;
// Auto-convert if enabled and units are specified
if (this.autoConvert && sourceUnit && targetUnit && sourceUnit !== targetUnit) {
try {
convertedValue = convertModule(val).from(sourceUnit).to(targetUnit);
finalUnit = targetUnit;
if (this.logger) {
this.logger.debug(`Auto-converted ${val} ${sourceUnit} to ${convertedValue} ${targetUnit}`);
}
} catch (error) {
if (this.logger) {
this.logger.warn(`Auto-conversion failed from ${sourceUnit} to ${targetUnit}: ${error.message}`);
}
convertedValue = val;
finalUnit = sourceUnit;
}
}
measurement.setValue(convertedValue, timestamp);
if (finalUnit && !measurement.unit) {
measurement.setUnit(finalUnit);
}
measurement.setValue(convertedValue, timestamp);
// ENHANCED: Emit event with rich context
const eventData = {
value: convertedValue,
originalValue: val,
unit: finalUnit,
sourceUnit: sourceUnit,
timestamp,
position: this._currentPosition,
variant: this._currentVariant,
type: this._currentType,
// NEW: Enhanced context
childId: this.childId,
childName: this.childName,
parentRef: this.parentRef
};
if (finalUnit && !measurement.unit) {
measurement.setUnit(finalUnit);
}
// Emit the exact event your parent expects
this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData);
//console.log(`Emitted event: ${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData);
// ENHANCED: Emit event with rich context
const eventData = {
value: convertedValue,
originalValue: val,
unit: finalUnit,
sourceUnit: sourceUnit,
timestamp,
position: this._currentPosition,
variant: this._currentVariant,
type: this._currentType,
// NEW: Enhanced context
childId: this.childId,
childName: this.childName,
parentRef: this.parentRef
};
return this;
// Emit the exact event your parent expects
this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._convertPositionNum2Str(this._currentPosition)}`, eventData);
this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData);
console.log(`Emitted event: ${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData);
return this;
}
unit(unitName) {
if (!this._ensureChainIsValid()) return this;
const measurement = this._getOrCreateMeasurement();
measurement.setUnit(unitName);
this._unit = unitName;
@@ -179,7 +180,7 @@ class MeasurementContainer {
getCurrentValue(requestedUnit = null) {
const measurement = this.get();
if (!measurement) return null;
const value = measurement.getCurrentValue();
if (value === null) return null;
@@ -206,7 +207,7 @@ class MeasurementContainer {
getAverage(requestedUnit = null) {
const measurement = this.get();
if (!measurement) return null;
const avgValue = measurement.getAverage();
if (avgValue === null) return null;
@@ -246,31 +247,31 @@ class MeasurementContainer {
if (!this._currentType || !this._currentVariant) {
throw new Error('Type and variant must be specified for difference calculation');
}
const savedPosition = this._currentPosition;
// Get upstream and downstream measurements
const positions = this.getPositions();
this._currentPosition = Math.min(...positions);
const upstream = this.get();
this._currentPosition = Math.max(...positions);
const downstream = this.get();
this._currentPosition = savedPosition;
if (!upstream || !downstream || upstream.values.length === 0 || downstream.values.length === 0) {
return null;
}
// Get target unit for conversion
const targetUnit = requestedUnit || upstream.unit || downstream.unit;
// Get values in the same unit
const upstreamValue = this._convertValueToUnit(upstream.getCurrentValue(), upstream.unit, targetUnit);
const downstreamValue = this._convertValueToUnit(downstream.getCurrentValue(), downstream.unit, targetUnit);
const upstreamAvg = this._convertValueToUnit(upstream.getAverage(), upstream.unit, targetUnit);
const downstreamAvg = this._convertValueToUnit(downstream.getAverage(), downstream.unit, targetUnit);
@@ -297,13 +298,13 @@ class MeasurementContainer {
if (!this.measurements[this._currentType]) {
this.measurements[this._currentType] = {};
}
if (!this.measurements[this._currentType][this._currentVariant]) {
this.measurements[this._currentType][this._currentVariant] = {};
}
if (!this.measurements[this._currentType][this._currentVariant][this._currentPosition]) {
this.measurements[this._currentType][this._currentVariant][this._currentPosition] =
this.measurements[this._currentType][this._currentVariant][this._currentPosition] =
new MeasurementBuilder()
.setType(this._currentType)
.setVariant(this._currentVariant)
@@ -311,7 +312,7 @@ class MeasurementContainer {
.setWindowSize(this.windowSize)
.build();
}
return this.measurements[this._currentType][this._currentVariant][this._currentPosition];
}
@@ -324,7 +325,7 @@ class MeasurementContainer {
if (!this._currentType) {
throw new Error('Type must be specified before listing variants');
}
return this.measurements[this._currentType] ?
return this.measurements[this._currentType] ?
Object.keys(this.measurements[this._currentType]) : [];
}
@@ -332,9 +333,9 @@ class MeasurementContainer {
if (!this._currentType || !this._currentVariant) {
throw new Error('Type and variant must be specified before listing positions');
}
if (!this.measurements[this._currentType] ||
!this.measurements[this._currentType][this._currentVariant]) {
if (!this.measurements[this._currentType] ||
!this.measurements[this._currentType][this._currentVariant]) {
return [];
}
@@ -407,7 +408,7 @@ class MeasurementContainer {
const best = convertModule(currentValue)
.from(measurement.unit)
.toBest({ exclude: excludeUnits });
return best;
} catch (error) {
if (this.logger) {
@@ -418,14 +419,16 @@ class MeasurementContainer {
}
_convertPositionStr2Num(positionString) {
switch(positionString) {
switch (positionString) {
case "atEquipment":
return 0;
case "upstream":
return Number.POSITIVE_INFINITY;
case "downstream":
return Number.NEGATIVE_INFINITY;
default:
if (this.logger) {
this.logger.error(`Invalid positionVsParent provided: ${positionString}`);