license update and enhancements to measurement functionality + child parent relationship
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
const MeasurementBuilder = require('./MeasurementBuilder');
|
||||
const EventEmitter = require('events');
|
||||
const convertModule = require('../convert/index');
|
||||
|
||||
class MeasurementContainer {
|
||||
constructor(options = {}, logger) {
|
||||
this.logger = logger;
|
||||
constructor(options = {}) {
|
||||
this.emitter = new EventEmitter();
|
||||
this.measurements = {};
|
||||
this.windowSize = options.windowSize || 10; // Default window size
|
||||
|
||||
@@ -10,6 +12,63 @@ class MeasurementContainer {
|
||||
this._currentType = null;
|
||||
this._currentVariant = null;
|
||||
this._currentPosition = null;
|
||||
this._unit = null;
|
||||
|
||||
// Default units for each measurement type
|
||||
this.defaultUnits = {
|
||||
pressure: 'mbar',
|
||||
flow: 'm3/h',
|
||||
power: 'kW',
|
||||
temperature: 'C',
|
||||
volume: 'm3',
|
||||
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;
|
||||
this._currentPosition = null;
|
||||
this._unit = null;
|
||||
|
||||
// NEW: Enhanced child identification
|
||||
this.childId = null;
|
||||
this.childName = null;
|
||||
this.parentRef = null;
|
||||
|
||||
}
|
||||
|
||||
// NEW: Methods to set child context
|
||||
setChildId(childId) {
|
||||
this.childId = childId;
|
||||
return this;
|
||||
}
|
||||
|
||||
setChildName(childName) {
|
||||
this.childName = childName;
|
||||
return this;
|
||||
}
|
||||
|
||||
setParentRef(parent) {
|
||||
this.parentRef = parent;
|
||||
return this;
|
||||
}
|
||||
|
||||
// New method to set preferred units
|
||||
setPreferredUnit(measurementType, unit) {
|
||||
this.preferredUnits[measurementType] = unit;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Get the target unit for a measurement type
|
||||
_getTargetUnit(measurementType) {
|
||||
return this.preferredUnits[measurementType] ||
|
||||
this.defaultUnits[measurementType] ||
|
||||
null;
|
||||
}
|
||||
|
||||
// Chainable methods
|
||||
@@ -37,20 +96,69 @@ class MeasurementContainer {
|
||||
return this;
|
||||
}
|
||||
|
||||
// Core methods that complete the chain
|
||||
value(val, timestamp = Date.now()) {
|
||||
if (!this._ensureChainIsValid()) return this;
|
||||
|
||||
const measurement = this._getOrCreateMeasurement();
|
||||
measurement.setValue(val, timestamp);
|
||||
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;
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// ✅ 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);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
unit(unitName) {
|
||||
if (!this._ensureChainIsValid()) return this;
|
||||
|
||||
const measurement = this._getOrCreateMeasurement();
|
||||
measurement.setUnit(unitName);
|
||||
this._unit = unitName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -60,14 +168,52 @@ class MeasurementContainer {
|
||||
return this._getOrCreateMeasurement();
|
||||
}
|
||||
|
||||
getCurrentValue() {
|
||||
getCurrentValue(requestedUnit = null) {
|
||||
const measurement = this.get();
|
||||
return measurement ? measurement.getCurrentValue() : null;
|
||||
if (!measurement) return null;
|
||||
|
||||
const value = measurement.getCurrentValue();
|
||||
if (value === null) return null;
|
||||
|
||||
// Return as-is if no unit conversion requested
|
||||
if (!requestedUnit) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Convert if needed
|
||||
if (measurement.unit && requestedUnit !== measurement.unit) {
|
||||
try {
|
||||
return convertModule(value).from(measurement.unit).to(requestedUnit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`Unit conversion failed: ${error.message}`);
|
||||
}
|
||||
return value; // Return original value if conversion fails
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
getAverage() {
|
||||
getAverage(requestedUnit = null) {
|
||||
const measurement = this.get();
|
||||
return measurement ? measurement.getAverage() : null;
|
||||
if (!measurement) return null;
|
||||
|
||||
const avgValue = measurement.getAverage();
|
||||
if (avgValue === null) return null;
|
||||
|
||||
if (!requestedUnit || !measurement.unit || requestedUnit === measurement.unit) {
|
||||
return avgValue;
|
||||
}
|
||||
|
||||
try {
|
||||
return convertModule(avgValue).from(measurement.unit).to(requestedUnit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`Unit conversion failed: ${error.message}`);
|
||||
}
|
||||
return avgValue;
|
||||
}
|
||||
}
|
||||
|
||||
getMin() {
|
||||
@@ -85,47 +231,43 @@ class MeasurementContainer {
|
||||
return measurement ? measurement.getAllValues() : null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Difference calculations between positions
|
||||
difference() {
|
||||
difference(requestedUnit = null) {
|
||||
if (!this._currentType || !this._currentVariant) {
|
||||
throw new Error('Type and variant must be specified for difference calculation');
|
||||
}
|
||||
|
||||
// Save position to restore chain state after operation
|
||||
const savedPosition = this._currentPosition;
|
||||
|
||||
// Get upstream measurement
|
||||
// Get upstream and downstream measurements
|
||||
this._currentPosition = 'upstream';
|
||||
const upstream = this.get();
|
||||
|
||||
// Get downstream measurement
|
||||
this._currentPosition = 'downstream';
|
||||
const downstream = this.get();
|
||||
|
||||
// Restore chain state
|
||||
this._currentPosition = savedPosition;
|
||||
|
||||
if (!upstream || !downstream || upstream.values.length === 0 || downstream.values.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Ensure units match
|
||||
let downstreamForCalc = downstream;
|
||||
if (upstream.unit && downstream.unit && upstream.unit !== downstream.unit) {
|
||||
try {
|
||||
downstreamForCalc = downstream.convertTo(upstream.unit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`Unit conversion failed: ${error.message}`);
|
||||
}
|
||||
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);
|
||||
|
||||
return {
|
||||
value: downstreamForCalc.getCurrentValue() - upstream.getCurrentValue() ,
|
||||
avgDiff: downstreamForCalc.getAverage() - upstream.getAverage() ,
|
||||
unit: upstream.unit
|
||||
value: downstreamValue - upstreamValue,
|
||||
avgDiff: downstreamAvg - upstreamAvg,
|
||||
unit: targetUnit
|
||||
};
|
||||
}
|
||||
|
||||
@@ -195,6 +337,72 @@ class MeasurementContainer {
|
||||
this._currentVariant = null;
|
||||
this._currentPosition = null;
|
||||
}
|
||||
|
||||
// Helper method for value conversion
|
||||
_convertValueToUnit(value, fromUnit, toUnit) {
|
||||
if (!value || !fromUnit || !toUnit || fromUnit === toUnit) {
|
||||
return value;
|
||||
}
|
||||
|
||||
try {
|
||||
return convertModule(value).from(fromUnit).to(toUnit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.warn(`Conversion failed from ${fromUnit} to ${toUnit}: ${error.message}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
// Get available units for a measurement type
|
||||
getAvailableUnits(measurementType = null) {
|
||||
const type = measurementType || this._currentType;
|
||||
if (!type) return [];
|
||||
|
||||
// Map measurement types to convert module measures
|
||||
const measureMap = {
|
||||
pressure: 'pressure',
|
||||
flow: 'volumeFlowRate',
|
||||
power: 'power',
|
||||
temperature: 'temperature',
|
||||
volume: 'volume',
|
||||
length: 'length',
|
||||
mass: 'mass',
|
||||
energy: 'energy'
|
||||
};
|
||||
|
||||
const convertMeasure = measureMap[type];
|
||||
if (!convertMeasure) return [];
|
||||
|
||||
try {
|
||||
return convertModule().possibilities(convertMeasure);
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Get best unit for current value
|
||||
getBestUnit(excludeUnits = []) {
|
||||
const measurement = this.get();
|
||||
if (!measurement || !measurement.unit) return null;
|
||||
|
||||
const currentValue = measurement.getCurrentValue();
|
||||
if (currentValue === null) return null;
|
||||
|
||||
try {
|
||||
const best = convertModule(currentValue)
|
||||
.from(measurement.unit)
|
||||
.toBest({ exclude: excludeUnits });
|
||||
|
||||
return best;
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`getBestUnit failed: ${error.message}`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = MeasurementContainer;
|
||||
|
||||
Reference in New Issue
Block a user