Compare commits

..

11 Commits

4 changed files with 103 additions and 33 deletions

2
package-lock.json generated
View File

@@ -14,7 +14,7 @@
}, },
"node_modules/generalFunctions": { "node_modules/generalFunctions": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git#f13ee68938ea9d4b3a17ad90618c72634769c777", "resolved": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git#a41f053d5d69a7228d685b39024bb8719818ff16",
"license": "SEE LICENSE" "license": "SEE LICENSE"
} }
} }

View File

@@ -1,8 +0,0 @@
class liquidFlowHandler {
constructor(config) {
this.config = config;
}
}
module.exports = { liquidFlowHandler };

View File

@@ -1,11 +1,11 @@
const Specific = require("./specificClass.js");
/** /**
* node class.js * node class.js
* *
* Encapsulates all node logic in a reusable class. In future updates we can split this into multiple generic classes and use the config to specifiy which ones to use. * Encapsulates all node logic in a reusable class. In future updates we can split this into multiple generic classes and use the config to specifiy which ones to use.
* This allows us to keep the Node-RED node clean and focused on wiring up the UI and event handlers. * This allows us to keep the Node-RED node clean and focused on wiring up the UI and event handlers.
*/ */
const Specific = require("./specificClass.js");
class nodeClass { class nodeClass {
/** /**
* Create a Node. * Create a Node.
@@ -28,9 +28,8 @@ class nodeClass {
this._setupSpecificClass(); this._setupSpecificClass();
// Wire up event and lifecycle handlers // Wire up event and lifecycle handlers
// this._registerChild();
this._startTickLoop(); this._startTickLoop();
// this._attachInputHandler(); this._attachInputHandler();
this._attachCloseHandler(); this._attachCloseHandler();
} }
@@ -43,16 +42,16 @@ class nodeClass {
// Merge UI config over defaults // Merge UI config over defaults
this.config = { this.config = {
general: { general: {
name: uiConfig.name, name: uiConfig.name || this.name,
id: this.node.id, // node.id is for the child registration process id: this.node.id, // node.id is for the child registration process
unit: uiConfig.unit, // add converter options later to convert to default units (need like a model that defines this which units we are going to use and then conver to those standards) unit: null,
logging: { logging: {
enabled: uiConfig.enableLog, enabled: uiConfig.enableLog,
logLevel: uiConfig.logLevel logLevel: uiConfig.logLevel
} }
}, },
functionality: { functionality: {
positionVsParent: uiConfig.positionVsParent || 'atEquipment', // Default to 'atEquipment' if not specified positionVsParent: uiConfig.positionVsParent || 'atEquipment' // Default to 'atEquipment' if not specified
} }
}; };
} }
@@ -68,20 +67,6 @@ class nodeClass {
this.node.source = this.source; // Store the source in the node instance for easy access this.node.source = this.source; // Store the source in the node instance for easy access
} }
/**
* Register this node as a child upstream and downstream.
* Delayed to avoid Node-RED startup race conditions.
*/
_registerChild() {
setTimeout(() => {
this.node.send([
null,
null,
{ topic: 'registerChild', payload: this.config.general.id, positionVsParent: this.config?.functionality?.positionVsParent || 'atEquipment' },
]);
}, 100);
}
/** /**
* Start the periodic tick loop. * Start the periodic tick loop.
*/ */
@@ -95,8 +80,8 @@ class nodeClass {
* Execute a single tick and send outputs. * Execute a single tick and send outputs.
*/ */
_tick() { _tick() {
processMsg = null; let processMsg = this.source.getOutput();
influxMsg = null; let influxMsg = null;
// Send only updated outputs on ports 0 & 1 // Send only updated outputs on ports 0 & 1
this.node.send([processMsg, influxMsg, null]); this.node.send([processMsg, influxMsg, null]);
@@ -113,7 +98,7 @@ class nodeClass {
// Register this node as a child of the parent node // Register this node as a child of the parent node
const childId = msg.payload; const childId = msg.payload;
const childObj = this.RED.nodes.getNode(childId); const childObj = this.RED.nodes.getNode(childId);
this.source.childRegistrationUtils.registerChild(childObj.source ,msg.positionVsParent); this.source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent);
break; break;
} }
}); });

93
src/specificClass.js Normal file
View File

@@ -0,0 +1,93 @@
const { childRegistrationUtils, logger } = require('generalFunctions');
class liquidFlowHandler {
constructor(config) {
this.config = config;
this.logger = new logger(this.config.general.logging.enabled, this.config.general.logging.logLevel, config.general.name);
this.reactors = [null, null];
this.pump = null;
this.flow = 0;
this.childRegistrationUtils = new childRegistrationUtils(this);
}
updateState(timeStamp) {
this.updatePumpFlow();
const effluent = this.reactors[1].getEffluent;
effluent.payload.F = this.flow;
effluent.payload.inlet = 1;
this.reactors[0].setInfluent = effluent;
}
updatePumpFlow() {
const measuredFlow = this.pump.measurements.type("flow").variant("measured").position("downstream").getCurrentValue();
if (!measuredFlow) {
this.logger.warn(`Invalid flow value provided: ${measuredFlow}`);
this.flow = 0;
return;
}
this.flow = measuredFlow;
}
getOutput() {
const mainEffluent = this.reactors[1].getEffluent;
const sideStream = structuredClone(mainEffluent);
const F_in = mainEffluent.payload.F;
mainEffluent.payload.F = Math.max(F_in - this.flow, 0);
sideStream.payload.F = F_in < this.flow ? F_in : this.flow;
sideStream.payload.inlet = 1;
return {payload: [mainEffluent.payload, sideStream.payload]};
}
registerChild(child, softwareType) {
switch (softwareType) {
case "machine":
this.logger.debug(`Registering machine child.`);
this._connectMachine(child);
break;
case "reactor":
this.logger.debug(`Registering reactor child.`);
this._connectReactor(child);
break;
default:
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
}
}
_connectMachine(machine) {
if (!machine) {
this.logger.warn("Invalid machine provided.");
return;
}
if (this.pump) {
this.logger.warn("Too many pumps provided.")
return;
}
this.pump = machine;
}
_connectReactor(reactor) {
const position = reactor.config.functionality.positionVsParent;
if (position == "downstream") {
this.reactors[0] = reactor;
}
if (position == "upstream") {
this.reactors[1] = reactor;
}
this.logger.debug("Attached reactor.")
reactor.emitter.on("stateChange", (data) => {
this.logger.debug(`State change of attached reactor detected.`);
this.updateState(data);
});
}
}
module.exports = liquidFlowHandler;