Refactor child registration and connection methods to handle invalid inputs and improve readability
This commit is contained in:
@@ -14,6 +14,7 @@ const S_O_INDEX = 0;
|
||||
const NUM_SPECIES = 13;
|
||||
const BC_PADDING = 2;
|
||||
const DEBUG = false;
|
||||
const DAY2MS = 1000 * 60 * 60 * 24;
|
||||
|
||||
class Reactor {
|
||||
/**
|
||||
@@ -28,7 +29,6 @@ class Reactor {
|
||||
this.measurements = new MeasurementContainer();
|
||||
this.upstreamReactor = null;
|
||||
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
|
||||
this.parent = []; // Gets assigned via child registration
|
||||
|
||||
this.upstreamReactor = null;
|
||||
this.downstreamReactor = null;
|
||||
@@ -111,6 +111,11 @@ class Reactor {
|
||||
}
|
||||
|
||||
registerChild(child, softwareType) {
|
||||
if(!child) {
|
||||
this.logger.error(`Invalid ${softwareType} child provided.`);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (softwareType) {
|
||||
case "measurement":
|
||||
this.logger.debug(`Registering measurement child...`);
|
||||
@@ -131,11 +136,6 @@ class Reactor {
|
||||
}
|
||||
|
||||
_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}`;
|
||||
@@ -157,11 +157,6 @@ class Reactor {
|
||||
|
||||
|
||||
_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.");
|
||||
}
|
||||
@@ -181,11 +176,6 @@ class Reactor {
|
||||
}
|
||||
|
||||
_connectMachine(machineChild) {
|
||||
if (!machineChild) {
|
||||
this.logger.error("Invalid rotating machine provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (machineChild.config.functionality.positionVsParent == "downstream") {
|
||||
machineChild.upstreamReactor = this;
|
||||
this.returnPump = machineChild;
|
||||
@@ -211,20 +201,18 @@ class Reactor {
|
||||
* @param {number} newTime - New time to update reactor state to, in milliseconds since epoch.
|
||||
*/
|
||||
updateState(newTime = Date.now()) { // expect update with timestamp
|
||||
const day2ms = 1000 * 60 * 60 * 24;
|
||||
|
||||
if (this.upstreamReactor) {
|
||||
this.setInfluent = this.upstreamReactor.getEffluent[0]; // grab main effluent upstream reactor
|
||||
}
|
||||
|
||||
let n_iter = Math.floor(this.speedUpFactor * (newTime-this.currentTime) / (this.timeStep*day2ms));
|
||||
let n_iter = Math.floor(this.speedUpFactor * (newTime-this.currentTime) / (this.timeStep*DAY2MS));
|
||||
if (n_iter) {
|
||||
let n = 0;
|
||||
while (n < n_iter) {
|
||||
this.tick(this.timeStep);
|
||||
n += 1;
|
||||
}
|
||||
this.currentTime += n_iter * this.timeStep * day2ms / this.speedUpFactor;
|
||||
this.currentTime += n_iter * this.timeStep * DAY2MS / this.speedUpFactor;
|
||||
this.emitter.emit("stateChange", this.currentTime);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user