Compare commits
8 Commits
f2c9134b64
...
067017f2ea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
067017f2ea | ||
|
|
52f1cf73b4 | ||
|
|
a81733c492 | ||
|
|
555d4d865b | ||
|
|
db85100c4d | ||
|
|
b884faf402 | ||
|
|
2c43d28f76 | ||
|
|
d52a1827e3 |
256
src/configs/monster.json
Normal file
256
src/configs/monster.json
Normal file
@@ -0,0 +1,256 @@
|
|||||||
|
{
|
||||||
|
"general": {
|
||||||
|
"name": {
|
||||||
|
"default": "Monster Configuration",
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "A human-readable name or label for this configuration."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"default": null,
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"nullable": true,
|
||||||
|
"description": "A unique identifier for this configuration. If not provided, defaults to null."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"unit": {
|
||||||
|
"default": "unitless",
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The unit for this configuration (e.g., 'meters', 'seconds', 'unitless')."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"logging": {
|
||||||
|
"logLevel": {
|
||||||
|
"default": "info",
|
||||||
|
"rules": {
|
||||||
|
"type": "enum",
|
||||||
|
"values": [
|
||||||
|
{
|
||||||
|
"value": "debug",
|
||||||
|
"description": "Log messages are printed for debugging purposes."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value": "info",
|
||||||
|
"description": "Informational messages are printed."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value": "warn",
|
||||||
|
"description": "Warning messages are printed."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value": "error",
|
||||||
|
"description": "Error messages are printed."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"enabled": {
|
||||||
|
"default": true,
|
||||||
|
"rules": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Indicates whether logging is active. If true, log messages will be generated."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"functionality": {
|
||||||
|
"softwareType": {
|
||||||
|
"default": "monster",
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Specified software type for this configuration."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"role": {
|
||||||
|
"default": "samplingCabinet",
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Indicates the role this configuration plays (e.g., sensor, controller, etc.)."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"asset": {
|
||||||
|
"uuid": {
|
||||||
|
"default": null,
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"nullable": true,
|
||||||
|
"description": "Asset tag number which is a universally unique identifier for this asset. May be null if not assigned."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"geoLocation": {
|
||||||
|
"default": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"rules": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "An object representing the asset's physical coordinates or location.",
|
||||||
|
"schema": {
|
||||||
|
"x": {
|
||||||
|
"default": 0,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "X coordinate of the asset's location."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"y": {
|
||||||
|
"default": 0,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Y coordinate of the asset's location."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"z": {
|
||||||
|
"default": 0,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Z coordinate of the asset's location."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"supplier": {
|
||||||
|
"default": "Unknown",
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The supplier or manufacturer of the asset."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"default": "sensor",
|
||||||
|
"rules": {
|
||||||
|
"type": "enum",
|
||||||
|
"values": [
|
||||||
|
{
|
||||||
|
"value": "sensor",
|
||||||
|
"description": "A device that detects or measures a physical property and responds to it (e.g. temperature sensor)."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"subType": {
|
||||||
|
"default": "pressure",
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "A more specific classification within 'type'. For example, 'pressure' for a pressure sensor."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"model": {
|
||||||
|
"default": "Unknown",
|
||||||
|
"rules": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "A user-defined or manufacturer-defined model identifier for the asset."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"emptyWeightBucket": {
|
||||||
|
"default": 3,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "The weight of the empty bucket in kilograms."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"constraints": {
|
||||||
|
"samplingtime": {
|
||||||
|
"default": 0,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "The time interval between sampling events (in seconds) if not using a flow meter."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"samplingperiod": {
|
||||||
|
"default": 24,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "The fixed period in hours in which a composite sample is collected."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"minVolume": {
|
||||||
|
"default": 5,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"min": 5,
|
||||||
|
"description": "The minimum volume in liters."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"maxWeight": {
|
||||||
|
"default": 23,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"max": 23,
|
||||||
|
"description": "The maximum weight in kilograms."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"subSampleVolume": {
|
||||||
|
"default": 50,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"min": 50,
|
||||||
|
"max": 50,
|
||||||
|
"description": "The volume of each sub-sample in milliliters."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"storageTemperature": {
|
||||||
|
"default": {
|
||||||
|
"min": 1,
|
||||||
|
"max": 5
|
||||||
|
},
|
||||||
|
"rules": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "Acceptable storage temperature range for samples in degrees Celsius.",
|
||||||
|
"schema": {
|
||||||
|
"min": {
|
||||||
|
"default": 1,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"min": 1,
|
||||||
|
"description": "Minimum acceptable storage temperature in degrees Celsius."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"max": {
|
||||||
|
"default": 5,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"max": 5,
|
||||||
|
"description": "Maximum acceptable storage temperature in degrees Celsius."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flowmeter": {
|
||||||
|
"default": true,
|
||||||
|
"rules": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Indicates whether a flow meter is used for proportional sampling."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"closedSystem": {
|
||||||
|
"default": false,
|
||||||
|
"rules": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Indicates if the sampling system is closed (true) or open (false)."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"intakeSpeed": {
|
||||||
|
"default": 0.3,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Minimum intake speed in meters per second."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"intakeDiameter": {
|
||||||
|
"default": 12,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Minimum inner diameter of the intake tubing in millimeters."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -299,6 +299,23 @@
|
|||||||
"description": "Reference height to use to identify the height vs other basins with. This will say something more about the expected pressure loss in m head"
|
"description": "Reference height to use to identify the height vs other basins with. This will say something more about the expected pressure loss in m head"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"minHeightBasedOn": {
|
||||||
|
"default": "outlet",
|
||||||
|
"rules": {
|
||||||
|
"type": "enum",
|
||||||
|
"values": [
|
||||||
|
{
|
||||||
|
"value": "inlet",
|
||||||
|
"description": "Minimum height is based on inlet elevation."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value": "outlet",
|
||||||
|
"description": "Minimum height is based on outlet elevation."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Basis for minimum height check: inlet or outlet."
|
||||||
|
}
|
||||||
|
},
|
||||||
"staticHead": {
|
"staticHead": {
|
||||||
"default": 12,
|
"default": 12,
|
||||||
"rules": {
|
"rules": {
|
||||||
@@ -401,19 +418,36 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"levelbased": {
|
"levelbased": {
|
||||||
"thresholds": {
|
"startLevel": {
|
||||||
"default": [30,40,50,60,70,80,90],
|
"default": 1,
|
||||||
"rules": {
|
|
||||||
"type": "array",
|
|
||||||
"description": "Each time a threshold is overwritten a new pump can start or kick into higher gear. Volume thresholds (%) in ascending order used for level-based control."
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"timeThresholdSeconds": {
|
|
||||||
"default": 5,
|
|
||||||
"rules": {
|
"rules": {
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"min": 0,
|
"min": 0,
|
||||||
"description": "Duration the volume condition must persist before triggering pump actions (seconds)."
|
"description": "start of pump / group when level reaches this in meters starting from bottom."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"stopLevel": {
|
||||||
|
"default": 1,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"min": 0,
|
||||||
|
"description": "stop of pump / group when level reaches this in meters starting from bottom"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"minFlowLevel": {
|
||||||
|
"default": 1,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"min": 0,
|
||||||
|
"description": "min level to scale the flow lineair"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"maxFlowLevel": {
|
||||||
|
"default": 4,
|
||||||
|
"rules": {
|
||||||
|
"type": "number",
|
||||||
|
"min": 0,
|
||||||
|
"description": "max level to scale the flow lineair"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -552,7 +586,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"timeleftToFullOrEmptyThresholdSeconds": {
|
"timeleftToFullOrEmptyThresholdSeconds": {
|
||||||
"default": 120,
|
"default": 0,
|
||||||
"rules": {
|
"rules": {
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"min": 0,
|
"min": 0,
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"unit": {
|
"unit": {
|
||||||
"default": "m3/h",
|
"default": "l/s",
|
||||||
"rules": {
|
"rules": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "The default measurement unit for this configuration (e.g., 'meters', 'seconds', 'unitless')."
|
"description": "The default measurement unit for this configuration (e.g., 'meters', 'seconds', 'unitless')."
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class MeasurementContainer {
|
|||||||
this.windowSize = options.windowSize || 10; // Default window size
|
this.windowSize = options.windowSize || 10; // Default window size
|
||||||
|
|
||||||
// For chaining context
|
// For chaining context
|
||||||
|
this._currentChildId = null;
|
||||||
this._currentType = null;
|
this._currentType = null;
|
||||||
this._currentVariant = null;
|
this._currentVariant = null;
|
||||||
this._currentPosition = null;
|
this._currentPosition = null;
|
||||||
@@ -49,6 +50,11 @@ class MeasurementContainer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
child(childId) {
|
||||||
|
this._currentChildId = childId || 'default';
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
setChildName(childName) {
|
setChildName(childName) {
|
||||||
this.childName = childName;
|
this.childName = childName;
|
||||||
return this;
|
return this;
|
||||||
@@ -72,11 +78,19 @@ class MeasurementContainer {
|
|||||||
null;
|
null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getUnit(type) {
|
||||||
|
if (!type) return null;
|
||||||
|
if (this.preferredUnits && this.preferredUnits[type]) return this.preferredUnits[type];
|
||||||
|
if (this.defaultUnits && this.defaultUnits[type]) return this.defaultUnits[type];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Chainable methods
|
// Chainable methods
|
||||||
type(typeName) {
|
type(typeName) {
|
||||||
this._currentType = typeName;
|
this._currentType = typeName;
|
||||||
this._currentVariant = null;
|
this._currentVariant = null;
|
||||||
this._currentPosition = null;
|
this._currentPosition = null;
|
||||||
|
this._currentChildId = null;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +100,7 @@ class MeasurementContainer {
|
|||||||
}
|
}
|
||||||
this._currentVariant = variantName;
|
this._currentVariant = variantName;
|
||||||
this._currentPosition = null;
|
this._currentPosition = null;
|
||||||
|
this._currentChildId = null;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,8 +227,6 @@ class MeasurementContainer {
|
|||||||
return requireValues ? measurement.values?.length > 0 : true;
|
return requireValues ? measurement.values?.length > 0 : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unit(unitName) {
|
unit(unitName) {
|
||||||
if (!this._ensureChainIsValid()) return this;
|
if (!this._ensureChainIsValid()) return this;
|
||||||
|
|
||||||
@@ -226,35 +239,46 @@ class MeasurementContainer {
|
|||||||
// Terminal operations - get data out
|
// Terminal operations - get data out
|
||||||
get() {
|
get() {
|
||||||
if (!this._ensureChainIsValid()) return null;
|
if (!this._ensureChainIsValid()) return null;
|
||||||
return this._getOrCreateMeasurement();
|
const variantBucket = this.measurements[this._currentType]?.[this._currentVariant];
|
||||||
|
if (!variantBucket) return null;
|
||||||
|
const posBucket = variantBucket[this._currentPosition];
|
||||||
|
if (!posBucket) return null;
|
||||||
|
|
||||||
|
// Legacy single measurement
|
||||||
|
if (posBucket?.getCurrentValue) return posBucket;
|
||||||
|
|
||||||
|
// Child-aware: pick requested child, otherwise fall back to default, otherwise first available
|
||||||
|
if (posBucket && typeof posBucket === 'object') {
|
||||||
|
const requestedKey = this._currentChildId || this.childId;
|
||||||
|
const keys = Object.keys(posBucket);
|
||||||
|
if (!keys.length) return null;
|
||||||
|
const measurement =
|
||||||
|
(requestedKey && posBucket[requestedKey]) ||
|
||||||
|
posBucket.default ||
|
||||||
|
posBucket[keys[0]];
|
||||||
|
return measurement || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
getCurrentValue(requestedUnit = null) {
|
getCurrentValue(requestedUnit = null) {
|
||||||
const measurement = this.get();
|
const measurement = this.get();
|
||||||
if (!measurement) return null;
|
if (!measurement) return null;
|
||||||
|
|
||||||
const value = measurement.getCurrentValue();
|
const value = measurement.getCurrentValue();
|
||||||
if (value === null) return null;
|
if (value === null) return null;
|
||||||
|
if (!requestedUnit || !measurement.unit || requestedUnit === measurement.unit) {
|
||||||
// Return as-is if no unit conversion requested
|
|
||||||
if (!requestedUnit) {
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert if needed
|
|
||||||
if (measurement.unit && requestedUnit !== measurement.unit) {
|
|
||||||
try {
|
try {
|
||||||
return convertModule(value).from(measurement.unit).to(requestedUnit);
|
return convertModule(value).from(measurement.unit).to(requestedUnit);
|
||||||
} 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 value;
|
return value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getAverage(requestedUnit = null) {
|
getAverage(requestedUnit = null) {
|
||||||
const measurement = this.get();
|
const measurement = this.get();
|
||||||
@@ -357,6 +381,50 @@ class MeasurementContainer {
|
|||||||
return sample;
|
return sample;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sum(type, variant, positions = [], targetUnit = null) {
|
||||||
|
const bucket = this.measurements?.[type]?.[variant];
|
||||||
|
if (!bucket) return 0;
|
||||||
|
return positions
|
||||||
|
.map((pos) => {
|
||||||
|
const posBucket = bucket[pos];
|
||||||
|
if (!posBucket) return 0;
|
||||||
|
return Object.values(posBucket)
|
||||||
|
.map((m) => {
|
||||||
|
if (!m?.getCurrentValue) return 0;
|
||||||
|
const val = m.getCurrentValue();
|
||||||
|
if (val == null) return 0;
|
||||||
|
const fromUnit = m.unit || targetUnit;
|
||||||
|
if (!targetUnit || !fromUnit || fromUnit === targetUnit) return val;
|
||||||
|
try { return convertModule(val).from(fromUnit).to(targetUnit); } catch { return val; }
|
||||||
|
})
|
||||||
|
.reduce((acc, v) => acc + (Number.isFinite(v) ? v : 0), 0);
|
||||||
|
})
|
||||||
|
.reduce((acc, v) => acc + v, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
getFlattenedOutput() {
|
||||||
|
const out = {};
|
||||||
|
Object.entries(this.measurements).forEach(([type, variants]) => {
|
||||||
|
Object.entries(variants).forEach(([variant, positions]) => {
|
||||||
|
Object.entries(positions).forEach(([position, entry]) => {
|
||||||
|
// Legacy single series
|
||||||
|
if (entry?.getCurrentValue) {
|
||||||
|
out[`${type}.${variant}.${position}`] = entry.getCurrentValue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Child-bucketed series
|
||||||
|
if (entry && typeof entry === 'object') {
|
||||||
|
Object.entries(entry).forEach(([childId, m]) => {
|
||||||
|
if (m?.getCurrentValue) {
|
||||||
|
out[`${type}.${variant}.${position}.${childId}`] = m.getCurrentValue();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
// Difference calculations between positions
|
// Difference calculations between positions
|
||||||
difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
||||||
@@ -364,12 +432,21 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
|||||||
throw new Error("Type and variant must be specified for difference calculation");
|
throw new Error("Type and variant must be specified for difference calculation");
|
||||||
}
|
}
|
||||||
|
|
||||||
const get = pos =>
|
const get = pos => {
|
||||||
this.measurements?.[this._currentType]?.[this._currentVariant]?.[pos] || null;
|
const bucket = this.measurements?.[this._currentType]?.[this._currentVariant]?.[pos];
|
||||||
|
if (!bucket) return null;
|
||||||
|
// child-aware bucket: pick current childId/default or first available
|
||||||
|
if (bucket && typeof bucket === 'object' && !bucket.getCurrentValue) {
|
||||||
|
const childKey = this._currentChildId || this.childId || Object.keys(bucket)[0];
|
||||||
|
return bucket?.[childKey] || null;
|
||||||
|
}
|
||||||
|
// legacy single measurement
|
||||||
|
return bucket;
|
||||||
|
};
|
||||||
|
|
||||||
const a = get(from);
|
const a = get(from);
|
||||||
const b = get(to);
|
const b = get(to);
|
||||||
if (!a || !b || a.values.length === 0 || b.values.length === 0) {
|
if (!a || !b || !a.values || !b.values || a.values.length === 0 || b.values.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,13 +457,7 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
|||||||
const aAvg = this._convertValueToUnit(a.getAverage(), a.unit, targetUnit);
|
const aAvg = this._convertValueToUnit(a.getAverage(), a.unit, targetUnit);
|
||||||
const bAvg = this._convertValueToUnit(b.getAverage(), b.unit, targetUnit);
|
const bAvg = this._convertValueToUnit(b.getAverage(), b.unit, targetUnit);
|
||||||
|
|
||||||
return {
|
return { value: aVal - bVal, avgDiff: aAvg - bAvg, unit: targetUnit, from, to };
|
||||||
value: aVal - bVal,
|
|
||||||
avgDiff: aAvg - bAvg,
|
|
||||||
unit: targetUnit,
|
|
||||||
from,
|
|
||||||
to,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper methods
|
// Helper methods
|
||||||
@@ -410,18 +481,26 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
|||||||
this.measurements[this._currentType][this._currentVariant] = {};
|
this.measurements[this._currentType][this._currentVariant] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.measurements[this._currentType][this._currentVariant][this._currentPosition]) {
|
const positionKey = this._currentPosition;
|
||||||
this.measurements[this._currentType][this._currentVariant][this._currentPosition] =
|
const childKey = this._currentChildId || this.childId || 'default';
|
||||||
new MeasurementBuilder()
|
|
||||||
|
if (!this.measurements[this._currentType][this._currentVariant][positionKey]) {
|
||||||
|
this.measurements[this._currentType][this._currentVariant][positionKey] = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const bucket = this.measurements[this._currentType][this._currentVariant][positionKey];
|
||||||
|
|
||||||
|
if (!bucket[childKey]) {
|
||||||
|
bucket[childKey] = new MeasurementBuilder()
|
||||||
.setType(this._currentType)
|
.setType(this._currentType)
|
||||||
.setVariant(this._currentVariant)
|
.setVariant(this._currentVariant)
|
||||||
.setPosition(this._currentPosition)
|
.setPosition(positionKey)
|
||||||
.setWindowSize(this.windowSize)
|
.setWindowSize(this.windowSize)
|
||||||
.setDistance(this._currentDistance)
|
.setDistance(this._currentDistance)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.measurements[this._currentType][this._currentVariant][this._currentPosition];
|
return bucket[childKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Additional utility methods
|
// Additional utility methods
|
||||||
|
|||||||
@@ -177,18 +177,18 @@ const downstreamData = basicContainer
|
|||||||
.get();
|
.get();
|
||||||
|
|
||||||
//check wether a serie exists
|
//check wether a serie exists
|
||||||
const hasSeries = measurements
|
const hasSeries = basicContainer
|
||||||
.type("flow")
|
.type("flow")
|
||||||
.variant("measured")
|
.variant("measured")
|
||||||
.exists(); // true if any position exists
|
.exists(); // true if any position exists
|
||||||
|
|
||||||
const hasUpstreamValues = measurements
|
const hasUpstreamValues = basicContainer
|
||||||
.type("flow")
|
.type("flow")
|
||||||
.variant("measured")
|
.variant("measured")
|
||||||
.exists({ position: "upstream", requireValues: true });
|
.exists({ position: "upstream", requireValues: true });
|
||||||
|
|
||||||
// Passing everything explicitly
|
// Passing everything explicitly
|
||||||
const hasPercent = measurements.exists({
|
const hasPercent = basicContainer.exists({
|
||||||
type: "volume",
|
type: "volume",
|
||||||
variant: "percent",
|
variant: "percent",
|
||||||
position: "atEquipment",
|
position: "atEquipment",
|
||||||
@@ -274,14 +274,14 @@ console.log(` History: [${allValues.values.join(', ')}]\n`);
|
|||||||
|
|
||||||
console.log('--- Lagged sample comparison ---');
|
console.log('--- Lagged sample comparison ---');
|
||||||
|
|
||||||
const latest = stats.getCurrentValue(); // existing helper
|
const latestSample = stats.getLaggedSample(0); // newest sample object
|
||||||
const prevSample = stats.getLaggedValue(1); // new helper
|
const prevSample = stats.getLaggedSample(1);
|
||||||
const prevPrevSample = stats.getLaggedValue(2); // optional
|
const prevPrevSample = stats.getLaggedSample(2);
|
||||||
|
|
||||||
if (prevSample) {
|
if (prevSample) {
|
||||||
const delta = latest - prevSample.value;
|
const delta = (latestSample?.value ?? 0) - (prevSample.value ?? 0);
|
||||||
console.log(
|
console.log(
|
||||||
`Current vs previous: ${latest} ${statsData.unit} (t=${stats.get().getLatestTimestamp()}) vs ` +
|
`Current vs previous: ${latestSample?.value} ${statsData.unit} (t=${latestSample?.timestamp}) vs ` +
|
||||||
`${prevSample.value} ${prevSample.unit} (t=${prevSample.timestamp})`
|
`${prevSample.value} ${prevSample.unit} (t=${prevSample.timestamp})`
|
||||||
);
|
);
|
||||||
console.log(`Δ = ${delta.toFixed(2)} ${statsData.unit}`);
|
console.log(`Δ = ${delta.toFixed(2)} ${statsData.unit}`);
|
||||||
@@ -345,6 +345,68 @@ basicContainer.getTypes().forEach(type => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// --- Child Aggregation -----------------------------------------------------
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// ====================================
|
||||||
|
// AGGREGATION WITH CHILD SERIES (sum)
|
||||||
|
// ====================================
|
||||||
|
console.log();
|
||||||
|
console.log('--- Example X: Aggregation with sum() and child series ---');
|
||||||
|
|
||||||
|
// Container where flow is stored internally in m3/h
|
||||||
|
const aggContainer = new MeasurementContainer({
|
||||||
|
windowSize: 10,
|
||||||
|
defaultUnits: {
|
||||||
|
flow: 'm3/h',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Two pumps both feeding the same inlet position
|
||||||
|
aggContainer
|
||||||
|
.child('pumpA')
|
||||||
|
.type('flow')
|
||||||
|
.variant('measured')
|
||||||
|
.position('inlet')
|
||||||
|
.value(10, Date.now(), 'm3/h'); // 10 m3/h
|
||||||
|
|
||||||
|
aggContainer
|
||||||
|
.child('pumpB')
|
||||||
|
.type('flow')
|
||||||
|
.variant('measured')
|
||||||
|
.position('inlet')
|
||||||
|
.value(15, Date.now(), 'm3/h'); // 15 m3/h
|
||||||
|
|
||||||
|
// Another position, e.g. outlet, also with two pumps
|
||||||
|
aggContainer
|
||||||
|
.child('pumpA')
|
||||||
|
.type('flow')
|
||||||
|
.variant('measured')
|
||||||
|
.position('outlet')
|
||||||
|
.value(8, Date.now(), 'm3/h'); // 8 m3/h
|
||||||
|
|
||||||
|
aggContainer
|
||||||
|
.child('pumpB')
|
||||||
|
.type('flow')
|
||||||
|
.variant('measured')
|
||||||
|
.position('outlet')
|
||||||
|
.value(11, Date.now(), 'm3/h'); // 11 m3/h
|
||||||
|
|
||||||
|
|
||||||
|
// 1) Sum only inlet position (children pumpA + pumpB)
|
||||||
|
const inletTotal = aggContainer.sum('flow', 'measured', ['inlet']);
|
||||||
|
console.log(`Total inlet flow: ${inletTotal} m3/h (expected 25 m3/h)`);
|
||||||
|
|
||||||
|
// 2) Sum inlet + outlet positions together
|
||||||
|
const totalAll = aggContainer.sum('flow', 'measured', ['inlet', 'outlet']);
|
||||||
|
console.log(`Total inlet+outlet flow: ${totalAll} m3/h (expected 44 m3/h)`);
|
||||||
|
|
||||||
|
// 3) Same sum but explicitly ask for a target unit (e.g. l/s)
|
||||||
|
// This will use convertModule(...) internally.
|
||||||
|
// If conversion is not supported, it will fall back to the raw value.
|
||||||
|
const totalAllLps = aggContainer.sum('flow', 'measured', ['inlet', 'outlet'], 'l/s');
|
||||||
|
console.log(`Total inlet+outlet flow in l/s: ${totalAllLps} l/s (converted from m3/h)\n`);
|
||||||
|
|
||||||
|
|
||||||
console.log('\n✅ All examples complete!\n');
|
console.log('\n✅ All examples complete!\n');
|
||||||
|
|||||||
@@ -81,11 +81,8 @@ class movementManager {
|
|||||||
const direction = targetPosition > this.currentPosition ? 1 : -1;
|
const direction = targetPosition > this.currentPosition ? 1 : -1;
|
||||||
const distance = Math.abs(targetPosition - this.currentPosition);
|
const distance = Math.abs(targetPosition - this.currentPosition);
|
||||||
|
|
||||||
// Speed is a fraction [0,1] of full-range per second
|
const velocity = this.getVelocity(); // units per second
|
||||||
this.speed = Math.min(Math.max(this.speed, 0), 1);
|
if (velocity <= 0) {
|
||||||
const fullRange = this.maxPosition - this.minPosition;
|
|
||||||
const velocity = this.speed * fullRange; // units per second
|
|
||||||
if (velocity === 0) {
|
|
||||||
return reject(new Error("Movement aborted: zero speed"));
|
return reject(new Error("Movement aborted: zero speed"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,11 +151,11 @@ class movementManager {
|
|||||||
const direction = targetPosition > this.currentPosition ? 1 : -1;
|
const direction = targetPosition > this.currentPosition ? 1 : -1;
|
||||||
const distance = Math.abs(targetPosition - this.currentPosition);
|
const distance = Math.abs(targetPosition - this.currentPosition);
|
||||||
|
|
||||||
// Ensure speed is a percentage [0, 1]
|
const velocity = this.getVelocity();
|
||||||
this.speed = Math.min(Math.max(this.speed, 0), 1);
|
if (velocity <= 0) {
|
||||||
|
return reject(new Error("Movement aborted: zero speed"));
|
||||||
// Calculate duration based on percentage of distance per second
|
}
|
||||||
const duration = 1 / this.speed; // 1 second for 100% of the distance
|
const duration = distance / velocity;
|
||||||
|
|
||||||
this.timeleft = duration; //set this so other classes can use it
|
this.timeleft = duration; //set this so other classes can use it
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
@@ -217,13 +214,16 @@ class movementManager {
|
|||||||
const direction = targetPosition > this.currentPosition ? 1 : -1;
|
const direction = targetPosition > this.currentPosition ? 1 : -1;
|
||||||
const totalDistance = Math.abs(targetPosition - this.currentPosition);
|
const totalDistance = Math.abs(targetPosition - this.currentPosition);
|
||||||
const startPosition = this.currentPosition;
|
const startPosition = this.currentPosition;
|
||||||
this.speed = Math.min(Math.max(this.speed, 0), 1);
|
const velocity = this.getVelocity();
|
||||||
|
if (velocity <= 0) {
|
||||||
|
return reject(new Error("Movement aborted: zero speed"));
|
||||||
|
}
|
||||||
|
|
||||||
const easeFunction = (t) =>
|
const easeFunction = (t) =>
|
||||||
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
||||||
|
|
||||||
let elapsedTime = 0;
|
let elapsedTime = 0;
|
||||||
const duration = totalDistance / this.speed;
|
const duration = totalDistance / velocity;
|
||||||
this.timeleft = duration;
|
this.timeleft = duration;
|
||||||
const interval = this.interval;
|
const interval = this.interval;
|
||||||
|
|
||||||
@@ -273,6 +273,20 @@ class movementManager {
|
|||||||
constrain(value) {
|
constrain(value) {
|
||||||
return Math.min(Math.max(value, this.minPosition), this.maxPosition);
|
return Math.min(Math.max(value, this.minPosition), this.maxPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getNormalizedSpeed() {
|
||||||
|
const rawSpeed = Number.isFinite(this.speed) ? this.speed : 0;
|
||||||
|
const clampedSpeed = Math.max(0, rawSpeed);
|
||||||
|
const hasMax = Number.isFinite(this.maxSpeed) && this.maxSpeed > 0;
|
||||||
|
const effectiveSpeed = hasMax ? Math.min(clampedSpeed, this.maxSpeed) : clampedSpeed;
|
||||||
|
return effectiveSpeed / 100; // convert %/s -> fraction of range per second
|
||||||
|
}
|
||||||
|
|
||||||
|
getVelocity() {
|
||||||
|
const normalizedSpeed = this.getNormalizedSpeed();
|
||||||
|
const fullRange = this.maxPosition - this.minPosition;
|
||||||
|
return normalizedSpeed * fullRange;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = movementManager;
|
module.exports = movementManager;
|
||||||
|
|||||||
@@ -127,7 +127,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"maxSpeed": {
|
"maxSpeed": {
|
||||||
"default": 10,
|
"default": 1000,
|
||||||
"rules": {
|
"rules": {
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"description": "Maximum speed setting."
|
"description": "Maximum speed setting."
|
||||||
|
|||||||
Reference in New Issue
Block a user