93 lines
2.7 KiB
JavaScript
93 lines
2.7 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
const { MeasurementContainer } = require('generalFunctions');
|
||
|
|
|
||
|
|
/**
|
||
|
|
* VirtualPressureChildren — builds two dashboard-sim children backed
|
||
|
|
* by their own MeasurementContainer (upstream + downstream). Children
|
||
|
|
* are signed as belonging to a parent machine via `setParentRef`.
|
||
|
|
*
|
||
|
|
* Extracted from rotatingMachine specificClass._initVirtualPressureChildren.
|
||
|
|
*/
|
||
|
|
|
||
|
|
const DEFAULT_IDS = {
|
||
|
|
upstream: 'dashboard-sim-upstream',
|
||
|
|
downstream: 'dashboard-sim-downstream',
|
||
|
|
};
|
||
|
|
|
||
|
|
class VirtualPressureChildren {
|
||
|
|
/**
|
||
|
|
* @param {object} opts
|
||
|
|
* - logger: pass-through to MeasurementContainer
|
||
|
|
* - unitPolicy: { canonical, output }
|
||
|
|
* - parentRef: object to use as parent for setParentRef (optional)
|
||
|
|
* - ids: override the default { upstream, downstream } id pair (optional)
|
||
|
|
*/
|
||
|
|
constructor({ logger, unitPolicy, parentRef = null, ids = DEFAULT_IDS } = {}) {
|
||
|
|
this.logger = logger || { warn() {}, debug() {} };
|
||
|
|
this.unitPolicy = unitPolicy;
|
||
|
|
this.parentRef = parentRef;
|
||
|
|
this.ids = { ...DEFAULT_IDS, ...(ids || {}) };
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @returns {{ upstream: VirtualChild, downstream: VirtualChild }}
|
||
|
|
* Each child = { config: { general, functionality, asset }, measurements }.
|
||
|
|
*/
|
||
|
|
build() {
|
||
|
|
return {
|
||
|
|
upstream: this._createChild('upstream'),
|
||
|
|
downstream: this._createChild('downstream'),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
_createChild(position) {
|
||
|
|
const id = this.ids[position];
|
||
|
|
const name = `dashboard-sim-${position}`;
|
||
|
|
const measurements = new MeasurementContainer({
|
||
|
|
autoConvert: true,
|
||
|
|
defaultUnits: this._unitMap('output'),
|
||
|
|
preferredUnits: this._unitMap('output'),
|
||
|
|
canonicalUnits: this.unitPolicy?.canonical,
|
||
|
|
storeCanonical: true,
|
||
|
|
strictUnitValidation: true,
|
||
|
|
throwOnInvalidUnit: true,
|
||
|
|
requireUnitForTypes: ['pressure'],
|
||
|
|
}, this.logger);
|
||
|
|
|
||
|
|
if (typeof measurements.setChildId === 'function') measurements.setChildId(id);
|
||
|
|
if (typeof measurements.setChildName === 'function') measurements.setChildName(name);
|
||
|
|
if (this.parentRef && typeof measurements.setParentRef === 'function') {
|
||
|
|
measurements.setParentRef(this.parentRef);
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
config: {
|
||
|
|
general: { id, name },
|
||
|
|
functionality: {
|
||
|
|
softwareType: 'measurement',
|
||
|
|
positionVsParent: position,
|
||
|
|
},
|
||
|
|
asset: {
|
||
|
|
type: 'pressure',
|
||
|
|
unit: this.unitPolicy?.output?.pressure,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
measurements,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
_unitMap(section) {
|
||
|
|
const src = this.unitPolicy?.[section] || {};
|
||
|
|
return {
|
||
|
|
pressure: src.pressure,
|
||
|
|
flow: src.flow,
|
||
|
|
power: src.power,
|
||
|
|
temperature: src.temperature,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
VirtualPressureChildren.DEFAULT_IDS = DEFAULT_IDS;
|
||
|
|
module.exports = VirtualPressureChildren;
|