Files
reactor/src/nodeClass.js

115 lines
3.4 KiB
JavaScript
Raw Normal View History

2025-07-04 10:44:54 +02:00
const { Reactor_CSTR, Reactor_PFR } = require('./reactor_class.js');
class nodeClass {
/**
2025-07-04 13:09:20 +02:00
* Construct ReactorNode.
* @param {object} uiConfig - Node-RED node configuration
* @param {object} RED - Node-RED runtime API
* @param {object} nodeInstance - Node-RED node instance
* @param {string} nameOfNode - Name of the node
2025-07-04 10:44:54 +02:00
*/
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
// Preserve RED reference for HTTP endpoints if needed
this.node = nodeInstance;
this.RED = RED;
this.name = nameOfNode;
this._loadConfig(uiConfig)
this._setupClass();
this._attachInputHandler();
}
2025-07-04 13:09:20 +02:00
/**
* Handle node-red input messages
*/
_attachInputHandler() {
this.node.on('input', (msg, send, done) => {
let toggleUpdate = false;
switch (msg.topic) {
case "clock":
toggleUpdate = true;
break;
case "Fluent":
this.reactor.setInfluent = msg;
if (msg.payload.inlet == 0) {
toggleUpdate = true;
}
break;
case "OTR":
this.reactor.setOTR = msg;
break;
case "Dispersion":
this.reactor.setDispersion = msg;
break;
default:
console.log("Unknown topic: " + msg.topic);
}
if (toggleUpdate) {
this.reactor.updateState(msg.timestamp);
send(this.reactor.getEffluent);
}
if (done) {
done();
}
});
}
2025-07-04 13:09:20 +02:00
/**
* Parse node configuration
* @param {object} uiConfig Config set in UI in node-red
*/
_loadConfig(uiConfig) {
this.config = {
reactor_type: uiConfig.reactor_type,
volume: parseFloat(uiConfig.volume),
length: parseFloat(uiConfig.length),
resolution_L: parseInt(uiConfig.resolution_L),
n_inlets: parseInt(uiConfig.n_inlets),
kla: parseFloat(uiConfig.kla),
initialState: [
parseFloat(uiConfig.S_O_init),
parseFloat(uiConfig.S_I_init),
parseFloat(uiConfig.S_S_init),
parseFloat(uiConfig.S_NH_init),
parseFloat(uiConfig.S_N2_init),
parseFloat(uiConfig.S_NO_init),
parseFloat(uiConfig.S_HCO_init),
parseFloat(uiConfig.X_I_init),
parseFloat(uiConfig.X_S_init),
parseFloat(uiConfig.X_H_init),
parseFloat(uiConfig.X_STO_init),
parseFloat(uiConfig.X_A_init),
parseFloat(uiConfig.X_TS_init)
]
}
}
2025-07-04 13:09:20 +02:00
/**
* Setup reactor class based on config
*/
_setupClass() {
2025-07-04 10:44:54 +02:00
let new_reactor;
switch (this.config.reactor_type) {
2025-07-04 10:44:54 +02:00
case "CSTR":
new_reactor = new Reactor_CSTR(this.config);
2025-07-04 10:44:54 +02:00
break;
case "PFR":
new_reactor = new Reactor_PFR(this.config);
2025-07-04 10:44:54 +02:00
break;
default:
console.warn("Unknown reactor type: " + uiConfig.reactor_type);
}
this.reactor = new_reactor; // protect from reassignment
2025-07-04 10:44:54 +02:00
}
}
module.exports = nodeClass;