Basic EVOLV setup

This commit is contained in:
2025-07-23 14:46:03 +02:00
parent 79e0d0678a
commit f70fa15ef5
5 changed files with 228 additions and 23 deletions

View File

@@ -1,3 +1,5 @@
<script src="/liquidFlowHandler/menu.js"></script>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType("liquidFlowHandler", { RED.nodes.registerType("liquidFlowHandler", {
category: "WWTP", category: "WWTP",
@@ -12,6 +14,23 @@
icon: "font-awesome/fa-random", icon: "font-awesome/fa-random",
label: function() { label: function() {
return this.name || "Liquid Flow Handler"; return this.name || "Liquid Flow Handler";
},
oneditprepare: function() {
// wait for the menu scripts to load
const waitForMenuData = () => {
if (window.EVOLV?.nodes?.liquidFlowHandler?.initEditor) {
window.EVOLV.nodes.liquidFlowHandler.initEditor(this);
} else {
setTimeout(waitForMenuData, 50);
}
};
waitForMenuData();
},
oneditsave: function() {
// save logger fields
if (window.EVOLV?.nodes?.liquidFlowHandler?.loggerMenu?.saveEditor) {
window.EVOLV.nodes.liquidFlowHandler.loggerMenu.saveEditor(this);
}
} }
}); });
</script> </script>
@@ -21,6 +40,10 @@
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<!-- Logger fields injected here -->
<div id="logger-fields-placeholder"></div>
</script> </script>
<script type="text/html" data-help-name="liquidFlowHandler"> <script type="text/html" data-help-name="liquidFlowHandler">

View File

@@ -1,6 +1,26 @@
const nameOfNode = "liquidFlowHandler";
const nodeClass = require('./src/nodeClass.js');
const { MenuManager } = require('generalFunctions');
module.exports = function(RED) { module.exports = function(RED) {
function liquidFlowHandler(config) { // 1) Register the node type and delegate to your class
RED.nodes.registerType(nameOfNode, function(config) {
RED.nodes.createNode(this, config); RED.nodes.createNode(this, config);
this.nodeClass = new nodeClass(config, RED, this, nameOfNode);
});
// 2) Setup the dynamic menu endpoint
const menuMgr = new MenuManager();
// Serve /liquidFlowHandler/menu.js
RED.httpAdmin.get(`/${nameOfNode}/menu.js`, (req, res) => {
try {
const script = menuMgr.createEndpoint(nameOfNode, ['logger']);
res.type('application/javascript').send(script);
} catch (err) {
res.status(500).send(`// Error generating menu: ${err.message}`);
} }
RED.nodes.registerType("liquidFlowHandler", liquidFlowHandler); });
}; };

21
package-lock.json generated Normal file
View File

@@ -0,0 +1,21 @@
{
"name": "liquidflowhandler",
"version": "0.0.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "liquidflowhandler",
"version": "0.0.1",
"license": "SEE LICENSE",
"dependencies": {
"generalFunctions": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git"
}
},
"node_modules/generalFunctions": {
"version": "1.0.0",
"resolved": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git#f13ee68938ea9d4b3a17ad90618c72634769c777",
"license": "SEE LICENSE"
}
}
}

8
src/SpecificClass.js Normal file
View File

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

133
src/nodeClass.js Normal file
View File

@@ -0,0 +1,133 @@
/**
* 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.
* 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 {
/**
* Create a Node.
* @param {object} uiConfig - Node-RED node configuration.
* @param {object} RED - Node-RED runtime API.
*/
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
// Preserve RED reference for HTTP endpoints if needed
this.node = nodeInstance; // This is the Node-RED node instance, we can use this to send messages and update status
this.RED = RED; // This is the Node-RED runtime API, we can use this to create endpoints if needed
this.name = nameOfNode; // This is the name of the node, it should match the file name and the node type in Node-RED
this.source = null; // Will hold the specific class instance
this.config = null; // Will hold the merged configuration
// Load default & UI config
this._loadConfig(uiConfig);
// Instantiate core class
this._setupSpecificClass();
// Wire up event and lifecycle handlers
// this._registerChild();
this._startTickLoop();
// this._attachInputHandler();
this._attachCloseHandler();
}
/**
* Load and merge default config with user-defined settings.
* @param {object} uiConfig - Raw config from Node-RED UI.
*/
_loadConfig(uiConfig) {
// Merge UI config over defaults
this.config = {
general: {
name: uiConfig.name,
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)
logging: {
enabled: uiConfig.enableLog,
logLevel: uiConfig.logLevel
}
},
functionality: {
positionVsParent: uiConfig.positionVsParent || 'atEquipment', // Default to 'atEquipment' if not specified
}
};
}
/**
* Instantiate the specific class and store as source.
*/
_setupSpecificClass() {
this.source = new Specific(this.config);
//store in node
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.
*/
_startTickLoop() {
setTimeout(() => {
this._tickInterval = setInterval(() => this._tick(), 1000);
}, 1000);
}
/**
* Execute a single tick and send outputs.
*/
_tick() {
processMsg = null;
influxMsg = null;
// Send only updated outputs on ports 0 & 1
this.node.send([processMsg, influxMsg, null]);
}
/**
* Attach the node's input handler, routing control messages to the class.
*/
_attachInputHandler() {
this.node.on('input', (msg, send, done) => {
/* Update to complete event based node by putting the tick function after an input event */
switch(msg.topic) {
case 'registerChild':
// Register this node as a child of the parent node
const childId = msg.payload;
const childObj = this.RED.nodes.getNode(childId);
this.source.childRegistrationUtils.registerChild(childObj.source ,msg.positionVsParent);
break;
}
});
}
/**
* Clean up timers and intervals when Node-RED stops the node.
*/
_attachCloseHandler() {
this.node.on('close', (done) => {
clearInterval(this._tickInterval);
done();
});
}
}
module.exports = nodeClass;