Compare commits
7 Commits
9af42bdc4c
...
518262ac98
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
518262ac98 | ||
|
|
a650ca4856 | ||
|
|
fdfb9edf0d | ||
|
|
a369361d99 | ||
|
|
417fad4ec3 | ||
|
|
8ae21ce787 | ||
|
|
5deb22b8da |
156
settler.html
156
settler.html
@@ -1,68 +1,88 @@
|
|||||||
<script src="/reactor/menu.js"></script>
|
<script src="/reactor/menu.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType("settler", {
|
RED.nodes.registerType("settler", {
|
||||||
category: "EVOLV",
|
category: "EVOLV",
|
||||||
color: "#e4a363",
|
color: "#e4a363",
|
||||||
defaults: {
|
defaults: {
|
||||||
name: { value: "" },
|
name: { value: "" },
|
||||||
|
processOutputFormat: { value: "process" },
|
||||||
enableLog: { value: false },
|
dbaseOutputFormat: { value: "influxdb" },
|
||||||
logLevel: { value: "error" },
|
|
||||||
|
enableLog: { value: false },
|
||||||
positionVsParent: { value: "" }
|
logLevel: { value: "error" },
|
||||||
},
|
|
||||||
inputs: 1,
|
positionVsParent: { value: "" }
|
||||||
outputs: 3,
|
},
|
||||||
outputLabels: ["process", "dbase", "parent"],
|
inputs: 1,
|
||||||
icon: "font-awesome/fa-random",
|
outputs: 3,
|
||||||
label: function() {
|
outputLabels: ["process", "dbase", "parent"],
|
||||||
return this.name || "Settling basin";
|
icon: "font-awesome/fa-random",
|
||||||
},
|
label: function() {
|
||||||
oneditprepare: function() {
|
return this.name || "Settling basin";
|
||||||
// wait for the menu scripts to load
|
},
|
||||||
const waitForMenuData = () => {
|
oneditprepare: function() {
|
||||||
if (window.EVOLV?.nodes?.reactor?.initEditor) {
|
// wait for the menu scripts to load
|
||||||
window.EVOLV.nodes.reactor.initEditor(this);
|
const waitForMenuData = () => {
|
||||||
} else {
|
if (window.EVOLV?.nodes?.reactor?.initEditor) {
|
||||||
setTimeout(waitForMenuData, 50);
|
window.EVOLV.nodes.reactor.initEditor(this);
|
||||||
}
|
} else {
|
||||||
};
|
setTimeout(waitForMenuData, 50);
|
||||||
waitForMenuData();
|
}
|
||||||
|
};
|
||||||
$("#node-input-inlet").typedInput({
|
waitForMenuData();
|
||||||
type:"num",
|
|
||||||
types:["num"]
|
$("#node-input-inlet").typedInput({
|
||||||
});
|
type:"num",
|
||||||
},
|
types:["num"]
|
||||||
oneditsave: function() {
|
});
|
||||||
// save logger fields
|
},
|
||||||
if (window.EVOLV?.nodes?.reactor?.loggerMenu?.saveEditor) {
|
oneditsave: function() {
|
||||||
window.EVOLV.nodes.reactor.loggerMenu.saveEditor(this);
|
// save logger fields
|
||||||
}
|
if (window.EVOLV?.nodes?.reactor?.loggerMenu?.saveEditor) {
|
||||||
|
window.EVOLV.nodes.reactor.loggerMenu.saveEditor(this);
|
||||||
// save position field
|
}
|
||||||
if (window.EVOLV?.nodes?.measurement?.positionMenu?.saveEditor) {
|
|
||||||
window.EVOLV.nodes.rotatingMachine.positionMenu.saveEditor(this);
|
// save position field
|
||||||
}
|
if (window.EVOLV?.nodes?.measurement?.positionMenu?.saveEditor) {
|
||||||
}
|
window.EVOLV.nodes.rotatingMachine.positionMenu.saveEditor(this);
|
||||||
});
|
}
|
||||||
</script>
|
}
|
||||||
|
});
|
||||||
<script type="text/html" data-template-name="settler">
|
</script>
|
||||||
<div class="form-row">
|
|
||||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
<script type="text/html" data-template-name="settler">
|
||||||
<input type="text" id="node-input-name" placeholder="Name">
|
<div class="form-row">
|
||||||
</div>
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
<!-- Logger fields injected here -->
|
</div>
|
||||||
<div id="logger-fields-placeholder"></div>
|
|
||||||
|
<h3>Output Formats</h3>
|
||||||
<!-- Position fields will be injected here -->
|
<div class="form-row">
|
||||||
<div id="position-fields-placeholder"></div>
|
<label for="node-input-processOutputFormat"><i class="fa fa-random"></i> Process Output</label>
|
||||||
|
<select id="node-input-processOutputFormat" style="width:60%;">
|
||||||
</script>
|
<option value="process">process</option>
|
||||||
|
<option value="json">json</option>
|
||||||
<script type="text/html" data-help-name="settler">
|
<option value="csv">csv</option>
|
||||||
<p>Settling tank</p>
|
</select>
|
||||||
</script>
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-dbaseOutputFormat"><i class="fa fa-database"></i> Database Output</label>
|
||||||
|
<select id="node-input-dbaseOutputFormat" style="width:60%;">
|
||||||
|
<option value="influxdb">influxdb</option>
|
||||||
|
<option value="json">json</option>
|
||||||
|
<option value="csv">csv</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Logger fields injected here -->
|
||||||
|
<div id="logger-fields-placeholder"></div>
|
||||||
|
|
||||||
|
<!-- Position fields will be injected here -->
|
||||||
|
<div id="position-fields-placeholder"></div>
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/html" data-help-name="settler">
|
||||||
|
<p>Settling tank</p>
|
||||||
|
</script>
|
||||||
|
|||||||
162
src/nodeClass.js
162
src/nodeClass.js
@@ -1,33 +1,34 @@
|
|||||||
const { Settler } = require('./specificClass.js');
|
const { Settler } = require('./specificClass.js');
|
||||||
|
const { configManager } = require('generalFunctions');
|
||||||
|
|
||||||
class nodeClass {
|
|
||||||
/**
|
class nodeClass {
|
||||||
* Node-RED node class for settler.
|
/**
|
||||||
* @param {object} uiConfig - Node-RED node configuration
|
* Node-RED node class for settler.
|
||||||
* @param {object} RED - Node-RED runtime API
|
* @param {object} uiConfig - Node-RED node configuration
|
||||||
* @param {object} nodeInstance - Node-RED node instance
|
* @param {object} RED - Node-RED runtime API
|
||||||
* @param {string} nameOfNode - Name of the node
|
* @param {object} nodeInstance - Node-RED node instance
|
||||||
*/
|
* @param {string} nameOfNode - Name of the node
|
||||||
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
|
*/
|
||||||
// Preserve RED reference for HTTP endpoints if needed
|
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
|
||||||
this.node = nodeInstance;
|
// Preserve RED reference for HTTP endpoints if needed
|
||||||
this.RED = RED;
|
this.node = nodeInstance;
|
||||||
this.name = nameOfNode;
|
this.RED = RED;
|
||||||
this.source = null;
|
this.name = nameOfNode;
|
||||||
|
this.source = null;
|
||||||
this._loadConfig(uiConfig)
|
|
||||||
this._setupClass();
|
this._loadConfig(uiConfig)
|
||||||
|
this._setupClass();
|
||||||
this._attachInputHandler();
|
|
||||||
this._registerChild();
|
this._attachInputHandler();
|
||||||
this._startTickLoop();
|
this._registerChild();
|
||||||
this._attachCloseHandler();
|
this._startTickLoop();
|
||||||
}
|
this._attachCloseHandler();
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Handle node-red input messages
|
/**
|
||||||
*/
|
* Handle node-red input messages
|
||||||
|
*/
|
||||||
_attachInputHandler() {
|
_attachInputHandler() {
|
||||||
this.node.on('input', (msg, send, done) => {
|
this.node.on('input', (msg, send, done) => {
|
||||||
try {
|
try {
|
||||||
@@ -54,62 +55,49 @@ class nodeClass {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse node configuration
|
* Parse node configuration
|
||||||
* @param {object} uiConfig Config set in UI in node-red
|
* @param {object} uiConfig Config set in UI in node-red
|
||||||
*/
|
*/
|
||||||
_loadConfig(uiConfig) {
|
_loadConfig(uiConfig) {
|
||||||
this.config = {
|
const cfgMgr = new configManager();
|
||||||
general: {
|
this.config = cfgMgr.buildConfig('settler', uiConfig, this.node.id);
|
||||||
name: uiConfig.name || this.name,
|
}
|
||||||
id: this.node.id,
|
|
||||||
unit: null,
|
/**
|
||||||
logging: {
|
* Register this node as a child upstream and downstream.
|
||||||
enabled: uiConfig.enableLog,
|
* Delayed to avoid Node-RED startup race conditions.
|
||||||
logLevel: uiConfig.logLevel
|
*/
|
||||||
}
|
_registerChild() {
|
||||||
},
|
setTimeout(() => {
|
||||||
functionality: {
|
this.node.send([
|
||||||
positionVsParent: uiConfig.positionVsParent || 'atEquipment', // Default to 'atEquipment' if not specified
|
null,
|
||||||
softwareType: "settler" // should be set in config manager
|
null,
|
||||||
}
|
{ topic: 'registerChild', payload: this.node.id, positionVsParent: this.config?.functionality?.positionVsParent || 'atEquipment' }
|
||||||
}
|
]);
|
||||||
}
|
}, 100);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Register this node as a child upstream and downstream.
|
/**
|
||||||
* Delayed to avoid Node-RED startup race conditions.
|
* Setup settler class
|
||||||
*/
|
*/
|
||||||
_registerChild() {
|
_setupClass() {
|
||||||
setTimeout(() => {
|
|
||||||
this.node.send([
|
this.source = new Settler(this.config); // protect from reassignment
|
||||||
null,
|
this.node.source = this.source;
|
||||||
null,
|
}
|
||||||
{ topic: 'registerChild', payload: this.node.id, positionVsParent: this.config?.functionality?.positionVsParent || 'atEquipment' }
|
|
||||||
]);
|
_startTickLoop() {
|
||||||
}, 100);
|
setTimeout(() => {
|
||||||
}
|
this._tickInterval = setInterval(() => this._tick(), 1000);
|
||||||
|
}, 1000);
|
||||||
/**
|
}
|
||||||
* Setup settler class
|
|
||||||
*/
|
_tick(){
|
||||||
_setupClass() {
|
this.node.send([this.source.getEffluent, null, null]);
|
||||||
|
}
|
||||||
this.source = new Settler(this.config); // protect from reassignment
|
|
||||||
this.node.source = this.source;
|
|
||||||
}
|
|
||||||
|
|
||||||
_startTickLoop() {
|
|
||||||
setTimeout(() => {
|
|
||||||
this._tickInterval = setInterval(() => this._tick(), 1000);
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
_tick(){
|
|
||||||
this.node.send([this.source.getEffluent, null, null]);
|
|
||||||
}
|
|
||||||
|
|
||||||
_attachCloseHandler() {
|
_attachCloseHandler() {
|
||||||
this.node.on('close', (done) => {
|
this.node.on('close', (done) => {
|
||||||
clearInterval(this._tickInterval);
|
clearInterval(this._tickInterval);
|
||||||
@@ -117,5 +105,5 @@ class nodeClass {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = nodeClass;
|
module.exports = nodeClass;
|
||||||
|
|||||||
@@ -1,157 +1,157 @@
|
|||||||
const { childRegistrationUtils, logger, MeasurementContainer } = require('generalFunctions');
|
const { childRegistrationUtils, logger, MeasurementContainer, POSITIONS } = require('generalFunctions');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
|
||||||
// Compatibility-safe array clone for Node runtimes without global structuredClone.
|
// Compatibility-safe array clone for Node runtimes without global structuredClone.
|
||||||
function cloneArray(values) {
|
function cloneArray(values) {
|
||||||
if (typeof structuredClone === 'function') {
|
if (typeof structuredClone === 'function') {
|
||||||
return structuredClone(values);
|
return structuredClone(values);
|
||||||
}
|
}
|
||||||
return Array.isArray(values) ? [...values] : values;
|
return Array.isArray(values) ? [...values] : values;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Settler domain model.
|
* Settler domain model.
|
||||||
* Splits influent into effluent, sludge and return sludge based on solids balance.
|
* Splits influent into effluent, sludge and return sludge based on solids balance.
|
||||||
*/
|
*/
|
||||||
class Settler {
|
class Settler {
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
// EVOLV stuff
|
// EVOLV stuff
|
||||||
this.logger = new logger(this.config.general.logging.enabled, this.config.general.logging.logLevel, config.general.name);
|
this.logger = new logger(this.config.general.logging.enabled, this.config.general.logging.logLevel, config.general.name);
|
||||||
this.emitter = new EventEmitter();
|
this.emitter = new EventEmitter();
|
||||||
this.measurements = new MeasurementContainer();
|
this.measurements = new MeasurementContainer();
|
||||||
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
|
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
|
||||||
|
|
||||||
this.upstreamReactor = null;
|
this.upstreamReactor = null;
|
||||||
this.returnPump = null;
|
this.returnPump = null;
|
||||||
|
|
||||||
// state variables
|
// state variables
|
||||||
this.F_in = 0; // debit in
|
this.F_in = 0; // debit in
|
||||||
this.Cs_in = new Array(13).fill(0); // Concentrations in
|
this.Cs_in = new Array(13).fill(0); // Concentrations in
|
||||||
this.C_TS = 2500; // Total solids concentration sludge
|
this.C_TS = 2500; // Total solids concentration sludge
|
||||||
}
|
}
|
||||||
|
|
||||||
get getEffluent() {
|
get getEffluent() {
|
||||||
// constrain flow to prevent negatives
|
// constrain flow to prevent negatives
|
||||||
const F_s = Math.min((this.F_in * this.Cs_in[12]) / this.C_TS, this.F_in);
|
const F_s = Math.min((this.F_in * this.Cs_in[12]) / this.C_TS, this.F_in);
|
||||||
const F_eff = this.F_in - F_s;
|
const F_eff = this.F_in - F_s;
|
||||||
|
|
||||||
let F_sr = 0;
|
let F_sr = 0;
|
||||||
if (this.returnPump) {
|
if (this.returnPump) {
|
||||||
F_sr = Math.min(this.returnPump.measurements.type("flow").variant("measured").position("atEquipment").getCurrentValue(), F_s);
|
F_sr = Math.min(this.returnPump.measurements.type("flow").variant("measured").position(POSITIONS.AT_EQUIPMENT).getCurrentValue(), F_s);
|
||||||
}
|
}
|
||||||
const F_so = F_s - F_sr;
|
const F_so = F_s - F_sr;
|
||||||
|
|
||||||
// effluent
|
// effluent
|
||||||
const Cs_eff = cloneArray(this.Cs_in);
|
const Cs_eff = cloneArray(this.Cs_in);
|
||||||
if (F_s > 0) {
|
if (F_s > 0) {
|
||||||
Cs_eff[7] = 0;
|
Cs_eff[7] = 0;
|
||||||
Cs_eff[8] = 0;
|
Cs_eff[8] = 0;
|
||||||
Cs_eff[9] = 0;
|
Cs_eff[9] = 0;
|
||||||
Cs_eff[10] = 0;
|
Cs_eff[10] = 0;
|
||||||
Cs_eff[11] = 0;
|
Cs_eff[11] = 0;
|
||||||
Cs_eff[12] = 0;
|
Cs_eff[12] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sludge
|
// sludge
|
||||||
const Cs_s = cloneArray(this.Cs_in);
|
const Cs_s = cloneArray(this.Cs_in);
|
||||||
if (F_s > 0) {
|
if (F_s > 0) {
|
||||||
Cs_s[7] = this.F_in * this.Cs_in[7] / F_s;
|
Cs_s[7] = this.F_in * this.Cs_in[7] / F_s;
|
||||||
Cs_s[8] = this.F_in * this.Cs_in[8] / F_s;
|
Cs_s[8] = this.F_in * this.Cs_in[8] / F_s;
|
||||||
Cs_s[9] = this.F_in * this.Cs_in[9] / F_s;
|
Cs_s[9] = this.F_in * this.Cs_in[9] / F_s;
|
||||||
Cs_s[10] = this.F_in * this.Cs_in[10] / F_s;
|
Cs_s[10] = this.F_in * this.Cs_in[10] / F_s;
|
||||||
Cs_s[11] = this.F_in * this.Cs_in[11] / F_s;
|
Cs_s[11] = this.F_in * this.Cs_in[11] / F_s;
|
||||||
Cs_s[12] = this.F_in * this.Cs_in[12] / F_s;
|
Cs_s[12] = this.F_in * this.Cs_in[12] / F_s;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{ topic: "Fluent", payload: { inlet: 0, F: F_eff, C: Cs_eff }, timestamp: Date.now() },
|
{ topic: "Fluent", payload: { inlet: 0, F: F_eff, C: Cs_eff }, timestamp: Date.now() },
|
||||||
{ topic: "Fluent", payload: { inlet: 1, F: F_so, C: Cs_s }, timestamp: Date.now() },
|
{ topic: "Fluent", payload: { inlet: 1, F: F_so, C: Cs_s }, timestamp: Date.now() },
|
||||||
{ topic: "Fluent", payload: { inlet: 2, F: F_sr, C: Cs_s }, timestamp: Date.now() }
|
{ topic: "Fluent", payload: { inlet: 2, F: F_sr, C: Cs_s }, timestamp: Date.now() }
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
registerChild(child, softwareType) {
|
registerChild(child, softwareType) {
|
||||||
if(!child) {
|
if(!child) {
|
||||||
this.logger.error(`Invalid ${softwareType} child provided.`);
|
this.logger.error(`Invalid ${softwareType} child provided.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (softwareType) {
|
switch (softwareType) {
|
||||||
case "measurement":
|
case "measurement":
|
||||||
this.logger.debug(`Registering measurement child...`);
|
this.logger.debug(`Registering measurement child...`);
|
||||||
this._connectMeasurement(child);
|
this._connectMeasurement(child);
|
||||||
break;
|
break;
|
||||||
case "reactor":
|
case "reactor":
|
||||||
this.logger.debug(`Registering reactor child...`);
|
this.logger.debug(`Registering reactor child...`);
|
||||||
this._connectReactor(child);
|
this._connectReactor(child);
|
||||||
break;
|
break;
|
||||||
case "machine":
|
case "machine":
|
||||||
this.logger.debug(`Registering machine child...`);
|
this.logger.debug(`Registering machine child...`);
|
||||||
this._connectMachine(child);
|
this._connectMachine(child);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
|
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_connectMeasurement(measurementChild) {
|
_connectMeasurement(measurementChild) {
|
||||||
const position = measurementChild.config.functionality.positionVsParent;
|
const position = measurementChild.config.functionality.positionVsParent;
|
||||||
const measurementType = measurementChild.config.asset.type;
|
const measurementType = measurementChild.config.asset.type;
|
||||||
const eventName = `${measurementType}.measured.${position}`;
|
const eventName = `${measurementType}.measured.${position}`;
|
||||||
|
|
||||||
// Register event listener for measurement updates
|
// Register event listener for measurement updates
|
||||||
measurementChild.measurements.emitter.on(eventName, (eventData) => {
|
measurementChild.measurements.emitter.on(eventName, (eventData) => {
|
||||||
this.logger.debug(`${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
|
this.logger.debug(`${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
|
||||||
|
|
||||||
// Store directly in parent's measurement container
|
// Store directly in parent's measurement container
|
||||||
this.measurements
|
this.measurements
|
||||||
.type(measurementType)
|
.type(measurementType)
|
||||||
.variant("measured")
|
.variant("measured")
|
||||||
.position(position)
|
.position(position)
|
||||||
.value(eventData.value, eventData.timestamp, eventData.unit);
|
.value(eventData.value, eventData.timestamp, eventData.unit);
|
||||||
|
|
||||||
this._updateMeasurement(measurementType, eventData.value, position, eventData);
|
this._updateMeasurement(measurementType, eventData.value, position, eventData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_connectReactor(reactorChild) {
|
_connectReactor(reactorChild) {
|
||||||
if (reactorChild.config.functionality.positionVsParent != "upstream") {
|
if (reactorChild.config.functionality.positionVsParent != POSITIONS.UPSTREAM) {
|
||||||
this.logger.warn("Reactor children of settlers should be upstream.");
|
this.logger.warn("Reactor children of settlers should be upstream.");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.upstreamReactor = reactorChild;
|
this.upstreamReactor = reactorChild;
|
||||||
|
|
||||||
reactorChild.emitter.on("stateChange", (eventData) => {
|
reactorChild.emitter.on("stateChange", (_eventData) => {
|
||||||
this.logger.debug(`State change of upstream reactor detected.`);
|
this.logger.debug(`State change of upstream reactor detected.`);
|
||||||
const raw = this.upstreamReactor.getEffluent;
|
const raw = this.upstreamReactor.getEffluent;
|
||||||
const effluent = Array.isArray(raw) ? raw[0] : raw;
|
const effluent = Array.isArray(raw) ? raw[0] : raw;
|
||||||
this.F_in = effluent.payload.F;
|
this.F_in = effluent.payload.F;
|
||||||
this.Cs_in = effluent.payload.C;
|
this.Cs_in = effluent.payload.C;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_connectMachine(machineChild) {
|
_connectMachine(machineChild) {
|
||||||
if (machineChild.config.functionality.positionVsParent == "downstream") {
|
if (machineChild.config.functionality.positionVsParent == POSITIONS.DOWNSTREAM) {
|
||||||
machineChild.upstreamSource = this;
|
machineChild.upstreamSource = this;
|
||||||
this.returnPump = machineChild;
|
this.returnPump = machineChild;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.logger.warn(`Failed to register machine child.`);
|
this.logger.warn(`Failed to register machine child.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateMeasurement(measurementType, value, position, context) {
|
_updateMeasurement(measurementType, value, _position, _context) {
|
||||||
switch(measurementType) {
|
switch(measurementType) {
|
||||||
case "quantity (tss)":
|
case "quantity (tss)":
|
||||||
this.C_TS = value;
|
this.C_TS = value;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
this.logger.error(`Type '${measurementType}' not recognized for measured update.`);
|
this.logger.error(`Type '${measurementType}' not recognized for measured update.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { Settler };
|
module.exports = { Settler };
|
||||||
|
|||||||
263
test/specificClass.test.js
Normal file
263
test/specificClass.test.js
Normal file
@@ -0,0 +1,263 @@
|
|||||||
|
/**
|
||||||
|
* Tests for settler specificClass (domain logic).
|
||||||
|
*
|
||||||
|
* The Settler class is a simple mass-balance separator:
|
||||||
|
* - Splits influent into effluent (clarified), surplus sludge, and return sludge
|
||||||
|
* - Concentrates particulate species (indices 7-12) into sludge stream
|
||||||
|
* - Removes particulates from effluent stream
|
||||||
|
* - registerChild: connects measurements, upstream reactors, machines
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { Settler } = require('../src/specificClass');
|
||||||
|
|
||||||
|
// --------------- helpers ---------------
|
||||||
|
|
||||||
|
const NUM_SPECIES = 13;
|
||||||
|
|
||||||
|
function makeConfig(overrides = {}) {
|
||||||
|
return {
|
||||||
|
general: {
|
||||||
|
name: 'TestSettler',
|
||||||
|
id: 'settler-test-1',
|
||||||
|
logging: { enabled: false, logLevel: 'error' },
|
||||||
|
},
|
||||||
|
functionality: {
|
||||||
|
softwareType: 'settler',
|
||||||
|
role: 'separator',
|
||||||
|
positionVsParent: 'downstream',
|
||||||
|
},
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------- tests ---------------
|
||||||
|
|
||||||
|
describe('Settler specificClass', () => {
|
||||||
|
|
||||||
|
describe('constructor / initialization', () => {
|
||||||
|
it('should create an instance with default values', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
expect(s).toBeDefined();
|
||||||
|
expect(s.F_in).toBe(0);
|
||||||
|
expect(s.Cs_in).toEqual(new Array(NUM_SPECIES).fill(0));
|
||||||
|
expect(s.C_TS).toBe(2500);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have null upstreamReactor and returnPump initially', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
expect(s.upstreamReactor).toBeNull();
|
||||||
|
expect(s.returnPump).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should initialize an EventEmitter', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
expect(s.emitter).toBeDefined();
|
||||||
|
expect(typeof s.emitter.on).toBe('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should initialize a MeasurementContainer', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
expect(s.measurements).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getEffluent', () => {
|
||||||
|
|
||||||
|
describe('with zero inflow', () => {
|
||||||
|
it('should return three streams with zero flows', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s.F_in = 0;
|
||||||
|
s.Cs_in = new Array(NUM_SPECIES).fill(0);
|
||||||
|
const result = s.getEffluent;
|
||||||
|
expect(result).toHaveLength(3);
|
||||||
|
expect(result[0].payload.F).toBe(0); // effluent
|
||||||
|
expect(result[1].payload.F).toBe(0); // surplus sludge
|
||||||
|
expect(result[2].payload.F).toBe(0); // return sludge
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with normal inflow and particulates', () => {
|
||||||
|
let s;
|
||||||
|
let result;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
s = new Settler(makeConfig());
|
||||||
|
s.F_in = 100;
|
||||||
|
s.C_TS = 5000;
|
||||||
|
// Set concentrations: solubles at indices 0-6, particulates at 7-12
|
||||||
|
const C = new Array(NUM_SPECIES).fill(10);
|
||||||
|
C[12] = 5000; // X_TS
|
||||||
|
s.Cs_in = C;
|
||||||
|
result = s.getEffluent;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 3 output streams', () => {
|
||||||
|
expect(result).toHaveLength(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have effluent topic "Fluent"', () => {
|
||||||
|
expect(result[0].topic).toBe('Fluent');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should calculate sludge flow F_s = min(F_in * X_TS_in / C_TS, F_in)', () => {
|
||||||
|
// F_s = min(100 * 5000 / 5000, 100) = min(100, 100) = 100
|
||||||
|
const F_eff = result[0].payload.F;
|
||||||
|
const F_so = result[1].payload.F;
|
||||||
|
const F_sr = result[2].payload.F;
|
||||||
|
// Total out should equal F_in (mass balance)
|
||||||
|
expect(F_eff + F_so + F_sr).toBeCloseTo(100, 5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set effluent particulate indices to zero when F_s > 0', () => {
|
||||||
|
const Cs_eff = result[0].payload.C;
|
||||||
|
for (let i = 7; i <= 12; i++) {
|
||||||
|
expect(Cs_eff[i]).toBe(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should keep effluent soluble indices unchanged', () => {
|
||||||
|
const Cs_eff = result[0].payload.C;
|
||||||
|
for (let i = 0; i < 7; i++) {
|
||||||
|
expect(Cs_eff[i]).toBe(10);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should concentrate particulates in sludge stream', () => {
|
||||||
|
const Cs_s = result[1].payload.C;
|
||||||
|
const F_s = Math.min((s.F_in * s.Cs_in[12]) / s.C_TS, s.F_in);
|
||||||
|
// Cs_s[i] = F_in * Cs_in[i] / F_s for particulate indices
|
||||||
|
for (let i = 7; i <= 12; i++) {
|
||||||
|
const expected = s.F_in * s.Cs_in[i] / F_s;
|
||||||
|
expect(Cs_s[i]).toBeCloseTo(expected, 5);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with low X_TS and high C_TS (dilute sludge)', () => {
|
||||||
|
it('should produce mostly effluent flow', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s.F_in = 100;
|
||||||
|
s.C_TS = 10000; // high target concentration
|
||||||
|
const C = new Array(NUM_SPECIES).fill(10);
|
||||||
|
C[12] = 100; // low X_TS in
|
||||||
|
s.Cs_in = C;
|
||||||
|
const result = s.getEffluent;
|
||||||
|
// F_s = min(100 * 100 / 10000, 100) = min(1, 100) = 1
|
||||||
|
expect(result[0].payload.F).toBeCloseTo(99, 5); // most flow is effluent
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('mass balance', () => {
|
||||||
|
it('should conserve total flow (F_eff + F_so + F_sr = F_in)', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s.F_in = 200;
|
||||||
|
s.C_TS = 3000;
|
||||||
|
const C = new Array(NUM_SPECIES).fill(5);
|
||||||
|
C[12] = 2000;
|
||||||
|
s.Cs_in = C;
|
||||||
|
const result = s.getEffluent;
|
||||||
|
const totalOut = result[0].payload.F + result[1].payload.F + result[2].payload.F;
|
||||||
|
expect(totalOut).toBeCloseTo(200, 5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not produce negative flows', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s.F_in = 50;
|
||||||
|
s.C_TS = 1000;
|
||||||
|
const C = new Array(NUM_SPECIES).fill(0);
|
||||||
|
C[12] = 500;
|
||||||
|
s.Cs_in = C;
|
||||||
|
const result = s.getEffluent;
|
||||||
|
result.forEach(stream => {
|
||||||
|
expect(stream.payload.F).toBeGreaterThanOrEqual(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('no return pump', () => {
|
||||||
|
it('should have F_sr = 0 when there is no return pump', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s.F_in = 100;
|
||||||
|
s.C_TS = 5000;
|
||||||
|
s.Cs_in = new Array(NUM_SPECIES).fill(10);
|
||||||
|
s.Cs_in[12] = 3000;
|
||||||
|
const result = s.getEffluent;
|
||||||
|
expect(result[2].payload.F).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('edge case: X_TS > C_TS (F_s clamped to F_in)', () => {
|
||||||
|
it('should clamp F_s to F_in when X_TS/C_TS ratio exceeds 1', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s.F_in = 100;
|
||||||
|
s.C_TS = 1000;
|
||||||
|
s.Cs_in = new Array(NUM_SPECIES).fill(10);
|
||||||
|
s.Cs_in[12] = 5000; // X_TS_in > C_TS => F_s = min(500, 100) = 100
|
||||||
|
const result = s.getEffluent;
|
||||||
|
expect(result[0].payload.F).toBe(0); // all flow goes to sludge
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('registerChild()', () => {
|
||||||
|
it('should not throw for null child', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
// null child should trigger the error log but not crash
|
||||||
|
expect(() => s.registerChild(null, 'measurement')).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not throw for unknown software type with valid child', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
const fakeChild = {
|
||||||
|
config: {
|
||||||
|
general: { name: 'fake', id: 'fake-1' },
|
||||||
|
functionality: { positionVsParent: 'upstream' },
|
||||||
|
asset: { type: 'pressure' },
|
||||||
|
},
|
||||||
|
measurements: { emitter: { on: jest.fn() } },
|
||||||
|
};
|
||||||
|
expect(() => s.registerChild(fakeChild, 'unknownType')).not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('_connectMachine()', () => {
|
||||||
|
it('should set returnPump for downstream machine', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
const fakeMachine = {
|
||||||
|
config: {
|
||||||
|
general: { name: 'pump', id: 'pump-1' },
|
||||||
|
functionality: { positionVsParent: 'downstream' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
s._connectMachine(fakeMachine);
|
||||||
|
expect(s.returnPump).toBe(fakeMachine);
|
||||||
|
expect(fakeMachine.upstreamSource).toBe(s);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not set returnPump for non-downstream machine', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
const fakeMachine = {
|
||||||
|
config: {
|
||||||
|
general: { name: 'pump', id: 'pump-2' },
|
||||||
|
functionality: { positionVsParent: 'upstream' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
s._connectMachine(fakeMachine);
|
||||||
|
expect(s.returnPump).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('_updateMeasurement()', () => {
|
||||||
|
it('should update C_TS when measurement type is "quantity (tss)"', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s._updateMeasurement('quantity (tss)', 7000, 'atEquipment', {});
|
||||||
|
expect(s.C_TS).toBe(7000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not change C_TS for unrecognized measurement type', () => {
|
||||||
|
const s = new Settler(makeConfig());
|
||||||
|
s._updateMeasurement('temperature', 25, 'atEquipment', {});
|
||||||
|
expect(s.C_TS).toBe(2500); // unchanged
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user