Files
settler/src/specificClass.js

102 lines
3.1 KiB
JavaScript
Raw Normal View History

const { childRegistrationUtils, logger, MeasurementContainer } = require('generalFunctions');
const EventEmitter = require('events');
class Settler {
constructor(config) {
this.config = config;
// EVOLV stuff
this.logger = new logger(this.config.general.logging.enabled, this.config.general.logging.logLevel, config.general.name);
this.emitter = new EventEmitter();
this.measurements = new MeasurementContainer();
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
this.upstreamReactor = null;
this.returnPump = null;
// state variables
this.F_in = 0;
}
get getEffluent() {
return;
}
registerChild(child, softwareType) {
switch (softwareType) {
case "measurement":
this.logger.debug(`Registering measurement child...`);
this._connectMeasurement(child);
break;
case "reactor":
this.logger.debug(`Registering reactor child...`);
this._connectReactor(child);
break;
case "machine":
this.logger.debug(`Registering machine child...`);
this._connectMachine(child);
break;
default:
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
}
}
_connectMeasurement(measurementChild) {
if (!measurementChild) {
this.logger.error("Invalid measurement provided.");
return;
}
const position = measurementChild.config.functionality.positionVsParent;
const measurementType = measurementChild.config.asset.type;
const eventName = `${measurementType}.measured.${position}`;
// Register event listener for measurement updates
measurementChild.measurements.emitter.on(eventName, (eventData) => {
this.logger.debug(`${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
// Store directly in parent's measurement container
this.measurements
.type(measurementType)
.variant("measured")
.position(position)
.value(eventData.value, eventData.timestamp, eventData.unit);
this._updateMeasurement(measurementType, eventData.value, position, eventData);
});
}
_connectReactor(reactorChild) {
if (!reactorChild) {
this.logger.error("Invalid reactor provided.");
return;
}
if (reactorChild.config.functionality.positionVsParent != "upstream") {
this.logger.warn("Reactor children of reactors should always be upstream.");
}
this.upstreamReactor = reactorChild;
reactorChild.emitter.on("stateChange", (eventData) => {
this.logger.debug(`State change of upstream reactor detected.`);
const effluent = this.upstreamReactor.getEffluent[0];
this.F_in = effluent.payload.F;
this.Cs_in = effluent.payload.C;
});
}
_connectMachine(machineChild) {
if (!machineChild) {
this.logger.error("Invalid rotating machine provided.");
return;
}
if (machineChild.config.functionality.positionVsParent == "downstream") {
machineChild.upstreamReactor = this;
this.returnPump = machineChild;
}
}
}
module.exports = { Settler };