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

@@ -13,7 +13,7 @@ class ChildRegistrationUtils {
// Enhanced child setup
child.parent = this.mainClass;
child.positionVsParent = positionVsParent;
//child.positionVsParent = positionVsParent;
// Enhanced measurement container with rich context
if (child.measurements) {
@@ -33,10 +33,10 @@ class ChildRegistrationUtils {
registeredAt: Date.now()
});
// IMPORTANT: Only call parent registration - no automatic handling and if parent has this function then try to register this child
if (typeof this.mainClass.registerChild === 'function') {
this.logger.debug(`Child ${name} stored under type ${softwareType}`);
this.logger.debug(`Spitting out mainclass: ${this.mainClass.child} `);
this.mainClass.registerChild(child, softwareType);
}
this.logger.info(`✅ Child ${name} registered successfully`);
}

View File

@@ -44,18 +44,18 @@ class MeasurementContainer {
// 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
@@ -67,8 +67,8 @@ class MeasurementContainer {
// Get the target unit for a measurement type
_getTargetUnit(measurementType) {
return this.preferredUnits[measurementType] ||
this.defaultUnits[measurementType] ||
null;
this.defaultUnits[measurementType] ||
null;
}
// Chainable methods
@@ -105,59 +105,60 @@ class MeasurementContainer {
// ENHANCED: Update your existing value method
value(val, timestamp = Date.now(), sourceUnit = null) {
if (!this._ensureChainIsValid()) return this;
if (!this._ensureChainIsValid()) return this;
const measurement = this._getOrCreateMeasurement();
const targetUnit = this._getTargetUnit(this._currentType);
const measurement = this._getOrCreateMeasurement();
const targetUnit = this._getTargetUnit(this._currentType);
let convertedValue = val;
let finalUnit = sourceUnit || targetUnit;
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;
// 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;
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);
measurement.setValue(convertedValue, timestamp);
if (finalUnit && !measurement.unit) {
measurement.setUnit(finalUnit);
}
if (finalUnit && !measurement.unit) {
measurement.setUnit(finalUnit);
}
// 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
};
// 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
};
// 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);
// 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;
return this;
}
@@ -334,7 +335,7 @@ class MeasurementContainer {
}
if (!this.measurements[this._currentType] ||
!this.measurements[this._currentType][this._currentVariant]) {
!this.measurements[this._currentType][this._currentVariant]) {
return [];
}
@@ -418,7 +419,9 @@ class MeasurementContainer {
}
_convertPositionStr2Num(positionString) {
switch(positionString) {
switch (positionString) {
case "atEquipment":
return 0;
case "upstream":