Compare commits

..

6 Commits

Author SHA1 Message Date
znetsixe
3971b4e328 adjusted test for new model 2025-11-25 16:19:09 +01:00
77adb043b0 Merge pull request 'update sjoerd' (#1) from sjoerdfijnje/monster:main into dev-Rene
Reviewed-on: #1
2025-11-25 14:30:18 +00:00
2576625f0a merge update 2025-11-25 15:29:43 +01:00
df935b2868 monster update 2025-11-25 15:03:51 +01:00
znetsixe
7e683792d4 removed deprecated statement 2025-11-13 19:38:35 +01:00
4213b18139 adjusted html to new standard 2025-10-13 10:06:08 +02:00
8 changed files with 1282 additions and 661 deletions

706
dependencies/monster/SpeficicClass.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,11 +1,14 @@
const tf = require('@tensorflow/tfjs'); const tf = require('@tensorflow/tfjs');
const fs = require('fs');
const path = require('path');
class ModelLoader { class ModelLoader {
constructor(logger) { constructor(logger) {
this.logger = logger || console; this.logger = logger || console;
this.model = null; this.model = null;
} }
/*
async loadModel(modelUrl, inputShape = [null, 24, 166]) { async loadModel(modelUrl, inputShape = [null, 24, 166]) {
try { try {
this.logger.debug(`Fetching model JSON from: ${modelUrl}`); this.logger.debug(`Fetching model JSON from: ${modelUrl}`);
@@ -63,6 +66,34 @@ this.model = await tf.loadLayersModel(tf.io.fromMemory(artifacts));
throw error; throw error;
} }
} }
*/
async loadModelPath(modelPath, inputShape = [1,48,6]) {
try {
const resolvedModelPath = path.resolve(modelPath);
this.logger.debug(`Loading model JSON from: ${resolvedModelPath}`);
const modelJSON = JSON.parse(fs.readFileSync(resolvedModelPath, 'utf-8'));
this.configureInputLayer(modelJSON, inputShape);
const baseDir = path.dirname(resolvedModelPath);
const weightFile = path.resolve(baseDir, modelJSON.weightsManifest[0].paths[0]);
const weightBuffer = fs.readFileSync(weightFile).buffer;
const artifacts = {
modelTopology: modelJSON.modelTopology,
weightSpecs: modelJSON.weightsManifest[0].weights,
weightData: weightBuffer
};
this.model = await tf.loadLayersModel(tf.io.fromMemory(artifacts));
this.logger.debug('Model loaded successfully');
return this.model;
} catch (error) {
this.logger.error(`Failed to load model: ${error.message}`);
throw error;
}
}
@@ -101,11 +132,22 @@ this.model = await tf.loadLayersModel(tf.io.fromMemory(artifacts));
const modelLoader = new ModelLoader(); const modelLoader = new ModelLoader();
//example!!
/*
(async () => { (async () => {
try { try {
const localURL = "http://localhost:1880/generalFunctions/datasets/lstmData/tfjs_model/model.json"; //const localURL = "http://localhost:1880/generalFunctions/datasets/lstmData/tfjs_model/model.json";
const model = await modelLoader.loadModel(localURL); const localPath = path.join(
__dirname,
'../../../generalFunctions/datasets/lstmData/tfjs_model/model.json'
);
const model = await modelLoader.loadModelPath(localPath);
//const model = await modelLoader.loadModel(localURL); USES URL
console.log('Model loaded successfully'); console.log('Model loaded successfully');
const denseLayer = model.getLayer('dense_8'); const denseLayer = model.getLayer('dense_8');
@@ -118,5 +160,5 @@ const modelLoader = new ModelLoader();
} }
})(); })();
//*/
module.exports = ModelLoader; module.exports = ModelLoader;

View File

@@ -1,256 +0,0 @@
{
"general": {
"name": {
"default": "Monster Configuration",
"rules": {
"type": "string",
"description": "A human-readable name or label for this configuration."
}
},
"id": {
"default": null,
"rules": {
"type": "string",
"nullable": true,
"description": "A unique identifier for this configuration. If not provided, defaults to null."
}
},
"unit": {
"default": "unitless",
"rules": {
"type": "string",
"description": "The unit for this configuration (e.g., 'meters', 'seconds', 'unitless')."
}
},
"logging": {
"logLevel": {
"default": "info",
"rules": {
"type": "enum",
"values": [
{
"value": "debug",
"description": "Log messages are printed for debugging purposes."
},
{
"value": "info",
"description": "Informational messages are printed."
},
{
"value": "warn",
"description": "Warning messages are printed."
},
{
"value": "error",
"description": "Error messages are printed."
}
]
}
},
"enabled": {
"default": true,
"rules": {
"type": "boolean",
"description": "Indicates whether logging is active. If true, log messages will be generated."
}
}
}
},
"functionality": {
"softwareType": {
"default": "monster",
"rules": {
"type": "string",
"description": "Specified software type for this configuration."
}
},
"role": {
"default": "samplingCabinet",
"rules": {
"type": "string",
"description": "Indicates the role this configuration plays (e.g., sensor, controller, etc.)."
}
}
},
"asset": {
"uuid": {
"default": null,
"rules": {
"type": "string",
"nullable": true,
"description": "Asset tag number which is a universally unique identifier for this asset. May be null if not assigned."
}
},
"geoLocation": {
"default": {
"x": 0,
"y": 0,
"z": 0
},
"rules": {
"type": "object",
"description": "An object representing the asset's physical coordinates or location.",
"schema": {
"x": {
"default": 0,
"rules": {
"type": "number",
"description": "X coordinate of the asset's location."
}
},
"y": {
"default": 0,
"rules": {
"type": "number",
"description": "Y coordinate of the asset's location."
}
},
"z": {
"default": 0,
"rules": {
"type": "number",
"description": "Z coordinate of the asset's location."
}
}
}
}
},
"supplier": {
"default": "Unknown",
"rules": {
"type": "string",
"description": "The supplier or manufacturer of the asset."
}
},
"type": {
"default": "sensor",
"rules": {
"type": "enum",
"values": [
{
"value": "sensor",
"description": "A device that detects or measures a physical property and responds to it (e.g. temperature sensor)."
}
]
}
},
"subType": {
"default": "pressure",
"rules": {
"type": "string",
"description": "A more specific classification within 'type'. For example, 'pressure' for a pressure sensor."
}
},
"model": {
"default": "Unknown",
"rules": {
"type": "string",
"description": "A user-defined or manufacturer-defined model identifier for the asset."
}
},
"emptyWeightBucket": {
"default": 3,
"rules": {
"type": "number",
"description": "The weight of the empty bucket in kilograms."
}
}
},
"constraints": {
"samplingtime": {
"default": 0,
"rules": {
"type": "number",
"description": "The time interval between sampling events (in seconds) if not using a flow meter."
}
},
"samplingperiod": {
"default": 24,
"rules": {
"type": "number",
"description": "The fixed period in hours in which a composite sample is collected."
}
},
"minVolume": {
"default": 5,
"rules": {
"type": "number",
"min": 5,
"description": "The minimum volume in liters."
}
},
"maxWeight": {
"default": 23,
"rules": {
"type": "number",
"max": 23,
"description": "The maximum weight in kilograms."
}
},
"subSampleVolume": {
"default": 50,
"rules": {
"type": "number",
"min": 50,
"max": 50,
"description": "The volume of each sub-sample in milliliters."
}
},
"storageTemperature": {
"default": {
"min": 1,
"max": 5
},
"rules": {
"type": "object",
"description": "Acceptable storage temperature range for samples in degrees Celsius.",
"schema": {
"min": {
"default": 1,
"rules": {
"type": "number",
"min": 1,
"description": "Minimum acceptable storage temperature in degrees Celsius."
}
},
"max": {
"default": 5,
"rules": {
"type": "number",
"max": 5,
"description": "Maximum acceptable storage temperature in degrees Celsius."
}
}
}
}
},
"flowmeter": {
"default": true,
"rules": {
"type": "boolean",
"description": "Indicates whether a flow meter is used for proportional sampling."
}
},
"closedSystem": {
"default": false,
"rules": {
"type": "boolean",
"description": "Indicates if the sampling system is closed (true) or open (false)."
}
},
"intakeSpeed": {
"default": 0.3,
"rules": {
"type": "number",
"description": "Minimum intake speed in meters per second."
}
},
"intakeDiameter": {
"default": 12,
"rules": {
"type": "number",
"description": "Minimum inner diameter of the intake tubing in millimeters."
}
}
}
}

182
monster oud.js Normal file
View File

@@ -0,0 +1,182 @@
module.exports = function (RED) {
function monster(config) {
// create node
RED.nodes.createNode(this, config);
// call this => node so whenver you want to call a node function type node and the function behind it
var node = this;
try{
// fetch monster object from monster.js
const Monster = require("./dependencies/monster/monster_class");
const OutputUtils = require("../generalFunctions/helper/outputUtils");
const mConfig={
general: {
name: config.name,
id: node.id,
unit: config.unit,
logging:{
logLevel: config.logLevel,
enabled: config.enableLog,
},
},
asset: {
supplier: config.supplier,
subType: config.subType,
model: config.model,
emptyWeightBucket: config.emptyWeightBucket,
},
constraints: {
minVolume: config.minVolume,
maxWeight: config.maxWeight,
samplingtime: config.samplingtime,
},
}
// make new monster on creation to work with.
const m = new Monster(mConfig);
// put m on node memory as source
node.source = m;
//load output utils
const output = new OutputUtils();
//internal vars
this.interval_id = null;
//updating node state
function updateNodeStatus() {
try{
const bucketVol = m.bucketVol;
const maxVolume = m.maxVolume;
const state = m.running;
const mode = "AI" ; //m.mode;
let status;
switch (state) {
case false:
status = { fill: "red", shape: "dot", text: `${mode}: OFF` };
break;
case true:
status = { fill: "green", shape: "dot", text: `${mode}: ON => ${bucketVol} | ${maxVolume}` };
break;
}
return status;
} catch (error) {
node.error("Error in updateNodeStatus: " + error);
return { fill: "red", shape: "ring", text: "Status Error" };
}
}
function tick(){
try{
// load status node
const status = updateNodeStatus();
// kick time based function in node
m.tick();
//show node status
node.status(status);
} catch (error) {
node.error("Error in tick function: " + error);
node.status({ fill: "red", shape: "ring", text: "Tick Error" });
}
}
// register child on first output this timeout is needed because of node - red stuff
setTimeout(
() => {
/*---execute code on first start----*/
let msgs = [];
msgs[2] = { topic : "registerChild" , payload: node.id, positionVsParent: "upstream" };
msgs[3] = { topic : "registerChild" , payload: node.id, positionVsParent: "downstream" };
//send msg
this.send(msgs);
},
100
);
//declare refresh interval internal node
setTimeout(
() => {
/*---execute code on first start----*/
this.interval_id = setInterval(function(){ tick() },1000)
},
1000
);
node.on('input', function (msg,send,done) {
try{
switch(msg.topic) {
case 'registerChild':
const childId = msg.payload;
const childObj = RED.nodes.getNode(childId);
m.childRegistrationUtils.registerChild(childObj.source ,msg.positionVsParent);
break;
case 'setMode':
m.setMode(msg.payload);
break;
case 'start':
m.i_start = true;
break;
}
} catch (error) {
node.error("Error in input function: " + error);
node.status({ fill: "red", shape: "ring", text: "Input Error" });
}
if(msg.topic == "i_flow"){
monster.q = parseFloat(msg.payload);
}
if(msg.topic == "i_start"){
monster.i_start = true;
}
if(msg.topic == "model_prediction"){
let var1 = msg.payload.dagvoorheen;
let var2 = msg.payload.dagnadien;
monster.get_model_prediction(var1, var2);
}
if(msg.topic == "aquon_monsternametijden"){
monster.monsternametijden = msg.payload;
}
if(msg.topic == "rain_data"){
monster.rain_data = msg.payload;
}
//register child classes
if(msg.topic == "registerChild"){
let child = msg.payload;
monster.registerChild(child);
}
done();
});
// tidy up any async code here - shutdown connections and so on.
node.on('close', function() {
clearTimeout(this.interval_id);
});
} catch (error) {
node.error("Error in monster function: " + error);
node.status({ fill: "red", shape: "ring", text: "Monster Error" });
}
}
RED.nodes.registerType("monster", monster);
};

View File

@@ -1,18 +1,13 @@
<script type="module"> <!-- Load the dynamic menu & config endpoints -->
import * as menuUtils from "/generalFunctions/helper/menuUtils.js"; <script src="/monster/menu.js"></script>
<script src="/monster/configData.js"></script>
RED.nodes.registerType('monster', {
category: 'wbd typical',
color: '#4f8582',
<script>
RED.nodes.registerType("monster", {
category: "EVOLV",
color: "#4f8582",
defaults: { defaults: {
// Define default properties
name: { value: "", required: true },
enableLog: { value: false },
logLevel: { value: "error" },
// Define specific properties // Define specific properties
samplingtime: { value: 0 }, samplingtime: { value: 0 },
minvolume: { value: 5 }, minvolume: { value: 5 },
@@ -21,249 +16,128 @@ RED.nodes.registerType('monster', {
aquon_sample_name: { value: "" }, aquon_sample_name: { value: "" },
//define asset properties //define asset properties
uuid: { value: "" },
supplier: { value: "" }, supplier: { value: "" },
subType: { value: "" }, category: { value: "" },
assetType: { value: "" },
model: { value: "" }, model: { value: "" },
unit: { value: "" }, unit: { value: "" },
//logger properties
enableLog: { value: false },
logLevel: { value: "error" },
//physicalAspect
positionVsParent: { value: "" },
positionIcon: { value: "" },
hasDistance: { value: false },
distance: { value: 0 },
distanceUnit: { value: "m" },
distanceDescription: { value: "" }
}, },
inputs: 1, inputs: 1,
outputs: 4, outputs: 3,
inputLabels: ["Measurement Input"], inputLabels: ["Input"],
outputLabels: ["process", "dbase", "upstreamParent", "downstreamParent"], outputLabels: ["process", "dbase", "parent"],
icon: "font-awesome/fa-exchange", icon: "font-awesome/fa-tachometer",
// Define label function
label: function () { label: function () {
return this.name || "Monsternamekast"; return this.positionIcon + " " + this.category.slice(0, -1) || "Monster";
}, },
oneditprepare: function() { oneditprepare: function() {
const node = this; // wait for the menu scripts to load
const waitForMenuData = () => {
// Define UI html elements if (window.EVOLV?.nodes?.monster?.initEditor) {
const elements = { window.EVOLV.nodes.monster.initEditor(this);
// Basic fields } else {
name: document.getElementById("node-input-name"), setTimeout(waitForMenuData, 50);
// specific fields
samplingtime: document.getElementById("node-input-samplingtime"),
minvolume: document.getElementById("node-input-minvolume"),
maxweight: document.getElementById("node-input-maxweight"),
emptyWeightBucket: document.getElementById("node-input-emptyWeightBucket"),
aquon_sample_name: document.getElementById("node-input-aquon_sample_name"),
// Logging fields
logCheckbox: document.getElementById("node-input-enableLog"),
logLevelSelect: document.getElementById("node-input-logLevel"),
rowLogLevel: document.getElementById("row-logLevel"),
// Asset fields
supplier: document.getElementById("node-input-supplier"),
subType: document.getElementById("node-input-subType"),
model: document.getElementById("node-input-model"),
unit: document.getElementById("node-input-unit"),
};
//this needs to live somewhere and for now we add it to every node file for simplicity
const projecSettingstURL = "http://localhost:1880/generalFunctions/settings/projectSettings.json";
try{
// Fetch project settings
menuUtils.fetchProjectData(projecSettingstURL)
.then((projectSettings) => {
//assign to node vars
node.configUrls = projectSettings.configUrls;
const { cloudConfigURL, localConfigURL } = menuUtils.getSpecificConfigUrl("monster",node.configUrls.cloud.taggcodeAPI);
node.configUrls.cloud.config = cloudConfigURL; // first call
node.configUrls.local.config = localConfigURL; // backup call
node.locationId = projectSettings.locationId;
node.uuid = projectSettings.uuid;
// Gets the ID of the active workspace (Flow)
const activeFlowId = RED.workspaces.active(); //fetches active flow id
node.processId = activeFlowId;
// UI elements across all nodes
menuUtils.fetchAndPopulateDropdowns(node.configUrls, elements, node); // function for all assets
menuUtils.initBasicToggles(elements);
})
}catch(e){
console.log("Error fetching project settings", e);
} }
};
waitForMenuData();
// your existing projectsettings & asset dropdown logic can remain here
document.getElementById("node-input-samplingtime");
document.getElementById("node-input-minvolume");
document.getElementById("node-input-maxweight");
document.getElementById("node-input-emptyWeightBucket");
document.getElementById("node-input-aquon_sample_name");
}, },
oneditsave: function() { oneditsave: function() {
const node = this; const node = this;
console.log(`------------ Saving changes to node ------------`); // save asset fields
console.log(`${node.uuid}`); if (window.EVOLV?.nodes?.monster?.assetMenu?.saveEditor) {
window.EVOLV.nodes.monster.assetMenu.saveEditor(this);
// Save basic properties }
[ "name", "supplier", "subType", "model" ,"unit" ].forEach( // save logger fields
(field) => (node[field] = document.getElementById(`node-input-${field}`).value || "") if (window.EVOLV?.nodes?.monster?.loggerMenu?.saveEditor) {
); window.EVOLV.nodes.monster.loggerMenu.saveEditor(this);
}
// Save numeric and boolean properties // save position field
["enableLog"].forEach( if (window.EVOLV?.nodes?.monster?.positionMenu?.saveEditor) {
(field) => (node[field] = document.getElementById(`node-input-${field}`).checked) window.EVOLV.nodes.monster.positionMenu.saveEditor(this);
);
["samplingtime","minvolume","maxweight","emptyWeightBucket","aquon_sample_name"].forEach(
(field) => (node[field] = parseFloat(document.getElementById(`node-input-${field}`).value) || 0)
);
node.logLevel = document.getElementById("node-input-logLevel").value || "info";
// Validation checks
if (node.scaling && (isNaN(node.i_min) || isNaN(node.i_max))) {
RED.notify("Scaling enabled, but input range is incomplete!", "error");
}
if (!node.unit) {
RED.notify("Unit selection is required.", "error");
}
if (node.subType && !node.unit) {
RED.notify("Unit must be set when specifying a subtype.", "error");
}
console.log("stored node modelData", node.modelMetadata);
console.log("------------ Changes saved to measurement node preparing to save to API ------------");
try{
// Fetch project settings
menuUtils.apiCall(node,node.configUrls)
.then((response) => {
//save response to node information
node.assetId = response.asset_id;
node.assetTagCode = response.asset_tag_number;
})
.catch((error) => {
console.log("Error during API call", error);
});
}catch(e){
console.log("Error saving assetID and tagnumber", e);
}
} }
["samplingtime", "minvolume", "maxweight", "emptyWeightBucket"].forEach((field) => {
const element = document.getElementById(`node-input-${field}`);
const value = parseFloat(element?.value) || 0;
console.log(`----------------> Saving ${field}: ${value}`);
node[field] = value;
}); });
["aquon_sample_name"].forEach((field) => {
const element = document.getElementById(`node-input-${field}`);
const value = element?.value || "";
console.log(`----------------> Saving ${field}: ${value}`);
node[field] = value;
});
}
});
</script> </script>
<!-- Main UI --> <!-- Main UI Template -->
<script type="text/html" data-template-name="monster"> <script type="text/html" data-template-name="monster">
<!-------------------------------------------INPUT NAME / TYPE -----------------------------------------------> <!-- speficic input -->
<!-- Node Name -->
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-samplingtime"><i class="fa fa-clock-o"></i> Sampling time (h)</label>
<input type="text" id="node-input-name" placeholder="Measurement Name"> <input type="number" id="node-input-samplingtime" style="width:60%;" />
</div>
<div class="form-row">
<label for="node-input-minvolume"><i class="fa fa-clock-o"></i> Min volume (L)</label>
<input type="number" id="node-input-minvolume" style="width:60%;" />
</div>
<div class="form-row">
<label for="node-input-maxweight"><i class="fa fa-clock-o"></i> Max weight (kg)</label>
<input type="number" id="node-input-maxweight" style="width:60%;" />
</div>
<div class="form-row">
<label for="node-input-emptyWeightBucket"><i class="fa fa-clock-o"></i> Empty weight of bucket (kg)</label>
<input type="number" id="node-input-emptyWeightBucket" style="width:60%;" />
</div>
<div class="form-row">
<label for="node-input-aquon_sample_name"><i class="fa fa-clock-o"></i> Aquon sample name</label>
<input type="text" id="node-input-aquon_sample_name" style="width:60%;" />
</div> </div>
<!-- Sampling Time --> <!-- Asset fields injected here -->
<div class="form-row"> <div id="asset-fields-placeholder"></div>
<label for="node-input-samplingtime"><i class="fa fa-clock-o"></i> Sampling Time (hours)</label>
<input type="number" id="node-input-samplingtime" placeholder="Enter sampling time in hours" min="0" required>
</div>
<!-- Minimum Volume --> <!-- Logger fields injected here -->
<div class="form-row"> <div id="logger-fields-placeholder"></div>
<label for="node-input-minvolume"><i class="fa fa-tint"></i> Minimum Volume (liters)</label>
<input type="number" id="node-input-minvolume" placeholder="Enter minimum volume in liters" min="0" required>
</div>
<!-- Maximum Weight --> <!-- Position fields injected here -->
<div class="form-row"> <div id="position-fields-placeholder"></div>
<label for="node-input-maxweight"><i class="fa fa-balance-scale"></i> Maximum Weight (kg)</label>
<input type="number" id="node-input-maxweight" placeholder="Enter maximum weight in kg" min="0" required>
</div>
<!-- Empty Bucket Weight -->
<div class="form-row">
<label for="node-input-emptyWeightBucket"><i class="fa fa-bucket"></i> Empty Bucket Weight (kg)</label>
<input type="number" id="node-input-emptyWeightBucket" placeholder="Enter empty bucket weight in kg" min="0" required>
</div>
<!-- Aquon Sample Name -->
<div class="form-row">
<label for="node-input-aquon_sample_name"><i class="fa fa-flask"></i> Aquon Sample Name</label>
<input type="text" id="node-input-aquon_sample_name" placeholder="Enter Aquon sample name">
</div>
<!-- Optional Extended Fields: supplier, type, subType, model -->
<hr />
<div class="form-row">
<label for="node-input-supplier"
><i class="fa fa-industry"></i> Supplier</label>
<select id="node-input-supplier" style="width:60%;">
<option value="">(optional)</option>
</select>
</div>
<div class="form-row">
<label for="node-input-subType"
><i class="fa fa-puzzle-piece"></i> SubType</label>
<select id="node-input-subType" style="width:60%;">
<option value="">(optional)</option>
</select>
</div>
<div class="form-row">
<label for="node-input-model"><i class="fa fa-wrench"></i> Model</label>
<select id="node-input-model" style="width:60%;">
<option value="">(optional)</option>
</select>
</div>
<div class="form-row">
<label for="node-input-unit"><i class="fa fa-balance-scale"></i> Unit</label>
<select id="node-input-unit" style="width:60%;"></select>
</div>
<hr />
<!-- loglevel checkbox -->
<div class="form-row">
<label for="node-input-enableLog"
><i class="fa fa-cog"></i> Enable Log</label>
<input
type="checkbox"
id="node-input-enableLog"
style="width:20px; vertical-align:baseline;"
/>
<span>Enable logging</span>
</div>
<div class="form-row" id="row-logLevel">
<label for="node-input-logLevel"><i class="fa fa-cog"></i> Log Level</label>
<select id="node-input-logLevel" style="width:60%;">
<option value="info">Info</option>
<option value="debug">Debug</option>
<option value="warn">Warn</option>
<option value="error">Error</option>
</select>
</div>
</script> </script>
<script type="text/html" data-help-name="monster"> <script type="text/html" data-help-name="monster">
<p><b>Monster Node</b>: Configures and manages monster measurement data.</p> <p><b>Monster node</b>: Configure a monster asset.</p>
<p>Use this node to configure and manage monster measurement data. The node can be configured to handle various measurement parameters and asset properties.</p> <ul>
<li><b>Supplier:</b> Select a supplier to populate machine options.</li>
<li><b>SubType:</b> Select a subtype if applicable to further categorize the asset.</li> </ul>
<li><b>Model:</b> Define the specific model for more granular asset configuration.</li>
<li><b>Unit:</b> Assign a unit to standardize measurements or operations.</li>
<li><b>Sampling Time:</b> Define the sampling time in hours for measurements.</li>
<li><b>Minimum Volume:</b> Specify the minimum volume in liters.</li>
<li><b>Maximum Weight:</b> Specify the maximum weight in kilograms.</li>
<li><b>Empty Bucket Weight:</b> Define the weight of the empty bucket in kilograms.</li>
<li><b>Aquon Sample Name:</b> Provide the name for the Aquon sample.</li>
<li><b>Enable Log:</b> Enable or disable logging for this node.</li>
<li><b>Log Level:</b> Select the log level (Info, Debug, Warn, Error) for logging messages.</li>
</script> </script>

View File

@@ -1,182 +1,35 @@
const nameOfNode = 'monster';
const nodeClass = require('./src/nodeClass.js');
const { MenuManager, configManager } = require('generalFunctions');
module.exports = function(RED) { module.exports = function(RED) {
function monster(config) { // 1) Register the node type and delegate to your class
RED.nodes.registerType(nameOfNode, function(config) {
// create node
RED.nodes.createNode(this, config); RED.nodes.createNode(this, config);
this.nodeClass = new nodeClass(config, RED, this, nameOfNode);
// call this => node so whenver you want to call a node function type node and the function behind it
var node = this;
try{
// fetch monster object from monster.js
const Monster = require("./dependencies/monster/monster_class");
const OutputUtils = require("../generalFunctions/helper/outputUtils");
const mConfig={
general: {
name: config.name,
id: node.id,
unit: config.unit,
logging:{
logLevel: config.logLevel,
enabled: config.enableLog,
},
},
asset: {
supplier: config.supplier,
subType: config.subType,
model: config.model,
emptyWeightBucket: config.emptyWeightBucket,
},
constraints: {
minVolume: config.minVolume,
maxWeight: config.maxWeight,
samplingtime: config.samplingtime,
},
}
// make new monster on creation to work with.
const m = new Monster(mConfig);
// put m on node memory as source
node.source = m;
//load output utils
const output = new OutputUtils();
//internal vars
this.interval_id = null;
//updating node state
function updateNodeStatus() {
try{
const bucketVol = m.bucketVol;
const maxVolume = m.maxVolume;
const state = m.running;
const mode = "AI" ; //m.mode;
let status;
switch (state) {
case false:
status = { fill: "red", shape: "dot", text: `${mode}: OFF` };
break;
case true:
status = { fill: "green", shape: "dot", text: `${mode}: ON => ${bucketVol} | ${maxVolume}` };
break;
}
return status;
} catch (error) {
node.error("Error in updateNodeStatus: " + error);
return { fill: "red", shape: "ring", text: "Status Error" };
}
}
function tick(){
try{
// load status node
const status = updateNodeStatus();
// kick time based function in node
m.tick();
//show node status
node.status(status);
} catch (error) {
node.error("Error in tick function: " + error);
node.status({ fill: "red", shape: "ring", text: "Tick Error" });
}
}
// register child on first output this timeout is needed because of node - red stuff
setTimeout(
() => {
/*---execute code on first start----*/
let msgs = [];
msgs[2] = { topic : "registerChild" , payload: node.id, positionVsParent: "upstream" };
msgs[3] = { topic : "registerChild" , payload: node.id, positionVsParent: "downstream" };
//send msg
this.send(msgs);
},
100
);
//declare refresh interval internal node
setTimeout(
() => {
/*---execute code on first start----*/
this.interval_id = setInterval(function(){ tick() },1000)
},
1000
);
node.on('input', function (msg,send,done) {
try{
switch(msg.topic) {
case 'registerChild':
const childId = msg.payload;
const childObj = RED.nodes.getNode(childId);
m.childRegistrationUtils.registerChild(childObj.source ,msg.positionVsParent);
break;
case 'setMode':
m.setMode(msg.payload);
break;
case 'start':
m.i_start = true;
break;
}
} catch (error) {
node.error("Error in input function: " + error);
node.status({ fill: "red", shape: "ring", text: "Input Error" });
}
if(msg.topic == "i_flow"){
monster.q = parseFloat(msg.payload);
}
if(msg.topic == "i_start"){
monster.i_start = true;
}
if(msg.topic == "model_prediction"){
let var1 = msg.payload.dagvoorheen;
let var2 = msg.payload.dagnadien;
monster.get_model_prediction(var1, var2);
}
if(msg.topic == "aquon_monsternametijden"){
monster.monsternametijden = msg.payload;
}
if(msg.topic == "rain_data"){
monster.rain_data = msg.payload;
}
//register child classes
if(msg.topic == "registerChild"){
let child = msg.payload;
monster.registerChild(child);
}
done();
}); });
// tidy up any async code here - shutdown connections and so on. // 2) Setup the dynamic menu & config endpoints
node.on('close', function() { const menuMgr = new MenuManager();
clearTimeout(this.interval_id); const cfgMgr = new configManager();
// Serve /monster/menu.js
RED.httpAdmin.get(`/${nameOfNode}/menu.js`, (req, res) => {
try {
const script = menuMgr.createEndpoint(nameOfNode, ['logger','position']);
res.type('application/javascript').send(script);
} catch (err) {
res.status(500).send(`// Error generating menu: ${err.message}`);
}
}); });
} catch (error) { // Serve /monster/configData.js
node.error("Error in monster function: " + error); RED.httpAdmin.get(`/${nameOfNode}/configData.js`, (req, res) => {
node.status({ fill: "red", shape: "ring", text: "Monster Error" }); try {
const script = cfgMgr.createEndpoint(nameOfNode);
res.type('application/javascript').send(script);
} catch (err) {
res.status(500).send(`// Error generating configData: ${err.message}`);
} }
} });
RED.nodes.registerType("monster", monster);
}; };

220
src/nodeClass.js Normal file
View File

@@ -0,0 +1,220 @@
/**
* 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 { outputUtils, configManager } = require('generalFunctions');
const Specific = require("./specificClass");
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,this.node);
// Instantiate core class
this._setupSpecificClass(uiConfig);
// Wire up event and lifecycle handlers
this._bindEvents();
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,node) {
// Merge UI config over defaults
this.config = {
general: {
id: 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
}
},
asset: {
uuid: uiConfig.assetUuid, //need to add this later to the asset model
tagCode: uiConfig.assetTagCode, //need to add this later to the asset model
supplier: uiConfig.supplier,
category: uiConfig.category, //add later to define as the software type
type: uiConfig.assetType,
model: uiConfig.model,
unit: uiConfig.unit
},
functionality: {
positionVsParent: uiConfig.positionVsParent
}
};
// Utility for formatting outputs
this._output = new outputUtils();
}
/**
* Instantiate the core Measurement logic and store as source.
*/
_setupSpecificClass(uiConfig) {
const monsterConfig = this.config;
this.source = new Specific(monsterConfig);
//store in node
this.node.source = this.source; // Store the source in the node instance for easy access
}
/**
* Bind events to Node-RED status updates. Using internal emitter. --> REMOVE LATER WE NEED ONLY COMPLETE CHILDS AND THEN CHECK FOR UPDATES
*/
_bindEvents() {
}
_updateNodeStatus() {
const m = this.source;
try{
const bucketVol = m.bucketVol;
const maxVolume = m.maxVolume;
const state = m.running;
const mode = "AI" ; //m.mode;
let status;
switch (state) {
case false:
status = { fill: "red", shape: "dot", text: `${mode}: OFF` };
break;
case true:
status = { fill: "green", shape: "dot", text: `${mode}: ON => ${bucketVol} | ${maxVolume}` };
break;
}
return status;
} catch (error) {
node.error("Error in updateNodeStatus: " + error);
return { fill: "red", shape: "ring", text: "Status Error" };
}
}
/**
* 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);
// Update node status on nodered screen every second ( this is not the best way to do this, but it works for now)
this._statusInterval = setInterval(() => {
const status = this._updateNodeStatus();
this.node.status(status);
}, 1000);
}, 1000);
}
/**
* Execute a single tick: update measurement, format and send outputs.
*/
_tick() {
this.source.tick();
const raw = this.source.getOutput();
const processMsg = this._output.formatMsg(raw, this.config, 'process');
const influxMsg = this._output.formatMsg(raw, this.config, 'influxdb');
// Send only updated outputs on ports 0 & 1
this.node.send([processMsg, influxMsg]);
}
/**
* 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 */
const m = this.source;
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);
m.childRegistrationUtils.registerChild(childObj.source ,msg.positionVsParent);
break;
case 'setMode':
m.setMode(msg.payload);
break;
case 'execSequence':
const { source, action, parameter } = msg.payload;
m.handleInput(source, action, parameter);
break;
case 'execMovement':
const { source: mvSource, action: mvAction, setpoint } = msg.payload;
m.handleInput(mvSource, mvAction, Number(setpoint));
break;
case 'flowMovement':
const { source: fmSource, action: fmAction, setpoint: fmSetpoint } = msg.payload;
m.handleInput(fmSource, fmAction, Number(fmSetpoint));
break;
case 'emergencystop':
const { source: esSource, action: esAction } = msg.payload;
m.handleInput(esSource, esAction);
break;
case 'showWorkingCurves':
m.showWorkingCurves();
send({ topic : "Showing curve" , payload: m.showWorkingCurves() });
break;
case 'CoG':
m.showCoG();
send({ topic : "Showing CoG" , payload: m.showCoG() });
break;
}
});
}
/**
* Clean up timers and intervals when Node-RED stops the node.
*/
_attachCloseHandler() {
this.node.on('close', (done) => {
clearInterval(this._tickInterval);
clearInterval(this._statusInterval);
done();
});
}
}
module.exports = nodeClass;