need to further update measurement emit function
This commit is contained in:
@@ -13,7 +13,7 @@ class ChildRegistrationUtils {
|
|||||||
|
|
||||||
// Enhanced child setup
|
// Enhanced child setup
|
||||||
child.parent = this.mainClass;
|
child.parent = this.mainClass;
|
||||||
child.positionVsParent = positionVsParent;
|
//child.positionVsParent = positionVsParent;
|
||||||
|
|
||||||
// Enhanced measurement container with rich context
|
// Enhanced measurement container with rich context
|
||||||
if (child.measurements) {
|
if (child.measurements) {
|
||||||
@@ -33,10 +33,10 @@ class ChildRegistrationUtils {
|
|||||||
registeredAt: Date.now()
|
registeredAt: Date.now()
|
||||||
});
|
});
|
||||||
|
|
||||||
// IMPORTANT: Only call parent registration - no automatic handling and if parent has this function then try to register this child
|
this.logger.debug(`Child ${name} stored under type ${softwareType}`);
|
||||||
if (typeof this.mainClass.registerChild === 'function') {
|
this.logger.debug(`Spitting out mainclass: ${this.mainClass.child} `);
|
||||||
|
|
||||||
this.mainClass.registerChild(child, softwareType);
|
this.mainClass.registerChild(child, softwareType);
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.info(`✅ Child ${name} registered successfully`);
|
this.logger.info(`✅ Child ${name} registered successfully`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,18 +44,18 @@ class MeasurementContainer {
|
|||||||
|
|
||||||
// NEW: Methods to set child context
|
// NEW: Methods to set child context
|
||||||
setChildId(childId) {
|
setChildId(childId) {
|
||||||
this.childId = childId;
|
this.childId = childId;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
setChildName(childName) {
|
setChildName(childName) {
|
||||||
this.childName = childName;
|
this.childName = childName;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
setParentRef(parent) {
|
setParentRef(parent) {
|
||||||
this.parentRef = parent;
|
this.parentRef = parent;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// New method to set preferred units
|
// New method to set preferred units
|
||||||
@@ -67,8 +67,8 @@ class MeasurementContainer {
|
|||||||
// Get the target unit for a measurement type
|
// Get the target unit for a measurement type
|
||||||
_getTargetUnit(measurementType) {
|
_getTargetUnit(measurementType) {
|
||||||
return this.preferredUnits[measurementType] ||
|
return this.preferredUnits[measurementType] ||
|
||||||
this.defaultUnits[measurementType] ||
|
this.defaultUnits[measurementType] ||
|
||||||
null;
|
null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chainable methods
|
// Chainable methods
|
||||||
@@ -105,59 +105,60 @@ class MeasurementContainer {
|
|||||||
|
|
||||||
// ENHANCED: Update your existing value method
|
// ENHANCED: Update your existing value method
|
||||||
value(val, timestamp = Date.now(), sourceUnit = null) {
|
value(val, timestamp = Date.now(), sourceUnit = null) {
|
||||||
if (!this._ensureChainIsValid()) return this;
|
if (!this._ensureChainIsValid()) return this;
|
||||||
|
|
||||||
const measurement = this._getOrCreateMeasurement();
|
const measurement = this._getOrCreateMeasurement();
|
||||||
const targetUnit = this._getTargetUnit(this._currentType);
|
const targetUnit = this._getTargetUnit(this._currentType);
|
||||||
|
|
||||||
let convertedValue = val;
|
let convertedValue = val;
|
||||||
let finalUnit = sourceUnit || targetUnit;
|
let finalUnit = sourceUnit || targetUnit;
|
||||||
|
|
||||||
// Auto-convert if enabled and units are specified
|
// Auto-convert if enabled and units are specified
|
||||||
if (this.autoConvert && sourceUnit && targetUnit && sourceUnit !== targetUnit) {
|
if (this.autoConvert && sourceUnit && targetUnit && sourceUnit !== targetUnit) {
|
||||||
try {
|
try {
|
||||||
convertedValue = convertModule(val).from(sourceUnit).to(targetUnit);
|
convertedValue = convertModule(val).from(sourceUnit).to(targetUnit);
|
||||||
finalUnit = targetUnit;
|
finalUnit = targetUnit;
|
||||||
|
|
||||||
if (this.logger) {
|
if (this.logger) {
|
||||||
this.logger.debug(`Auto-converted ${val} ${sourceUnit} to ${convertedValue} ${targetUnit}`);
|
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;
|
|
||||||
}
|
}
|
||||||
|
} 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) {
|
if (finalUnit && !measurement.unit) {
|
||||||
measurement.setUnit(finalUnit);
|
measurement.setUnit(finalUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ENHANCED: Emit event with rich context
|
// ENHANCED: Emit event with rich context
|
||||||
const eventData = {
|
const eventData = {
|
||||||
value: convertedValue,
|
value: convertedValue,
|
||||||
originalValue: val,
|
originalValue: val,
|
||||||
unit: finalUnit,
|
unit: finalUnit,
|
||||||
sourceUnit: sourceUnit,
|
sourceUnit: sourceUnit,
|
||||||
timestamp,
|
timestamp,
|
||||||
position: this._currentPosition,
|
position: this._currentPosition,
|
||||||
variant: this._currentVariant,
|
variant: this._currentVariant,
|
||||||
type: this._currentType,
|
type: this._currentType,
|
||||||
// NEW: Enhanced context
|
// NEW: Enhanced context
|
||||||
childId: this.childId,
|
childId: this.childId,
|
||||||
childName: this.childName,
|
childName: this.childName,
|
||||||
parentRef: this.parentRef
|
parentRef: this.parentRef
|
||||||
};
|
};
|
||||||
|
|
||||||
// Emit the exact event your parent expects
|
// Emit the exact event your parent expects
|
||||||
this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData);
|
this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._convertPositionNum2Str(this._currentPosition)}`, eventData);
|
||||||
//console.log(`Emitted event: ${this._currentType}.${this._currentVariant}.${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] ||
|
if (!this.measurements[this._currentType] ||
|
||||||
!this.measurements[this._currentType][this._currentVariant]) {
|
!this.measurements[this._currentType][this._currentVariant]) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -418,7 +419,9 @@ class MeasurementContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_convertPositionStr2Num(positionString) {
|
_convertPositionStr2Num(positionString) {
|
||||||
switch(positionString) {
|
|
||||||
|
switch (positionString) {
|
||||||
|
|
||||||
case "atEquipment":
|
case "atEquipment":
|
||||||
return 0;
|
return 0;
|
||||||
case "upstream":
|
case "upstream":
|
||||||
|
|||||||
Reference in New Issue
Block a user