Merge pull request 'update sjoerd' (#1) from sjoerdfijnje/monster:main into dev-Rene
Reviewed-on: #1
This commit is contained in:
701
dependencies/monster/SpeficicClass.js
vendored
Normal file
701
dependencies/monster/SpeficicClass.js
vendored
Normal file
File diff suppressed because one or more lines are too long
182
monster oud.js
Normal file
182
monster oud.js
Normal 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);
|
||||
|
||||
};
|
||||
328
monster.html
328
monster.html
@@ -1,269 +1,143 @@
|
||||
<script type="module">
|
||||
//import * as menuUtils from "/generalFunctions/helper/menuUtils.js";
|
||||
<!-- Load the dynamic menu & config endpoints -->
|
||||
<script src="/monster/menu.js"></script>
|
||||
<script src="/monster/configData.js"></script>
|
||||
|
||||
RED.nodes.registerType('monster', {
|
||||
|
||||
category: 'wbd typical',
|
||||
color: '#4f8582',
|
||||
|
||||
defaults: {
|
||||
<script>
|
||||
RED.nodes.registerType("monster", {
|
||||
category: "EVOLV",
|
||||
color: "#4f8582",
|
||||
defaults: {
|
||||
|
||||
// Define default properties
|
||||
name: { value: "", required: true },
|
||||
enableLog: { value: false },
|
||||
logLevel: { value: "error" },
|
||||
// Define specific properties
|
||||
samplingtime: { value: 0 },
|
||||
minvolume: { value: 5 },
|
||||
maxweight: { value: 22 },
|
||||
emptyWeightBucket: { value: 3 },
|
||||
aquon_sample_name: { value: "" },
|
||||
|
||||
// Define specific properties
|
||||
samplingtime: { value: 0 },
|
||||
minvolume: { value: 5 },
|
||||
maxweight: { value: 22 },
|
||||
emptyWeightBucket: { value: 3 },
|
||||
aquon_sample_name: { value: "" },
|
||||
//define asset properties
|
||||
uuid: { value: "" },
|
||||
supplier: { value: "" },
|
||||
category: { value: "" },
|
||||
assetType: { value: "" },
|
||||
model: { value: "" },
|
||||
unit: { value: "" },
|
||||
|
||||
//define asset properties
|
||||
supplier: { value: "" },
|
||||
subType: { value: "" },
|
||||
model: { 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,
|
||||
outputs: 4,
|
||||
inputLabels: ["Measurement Input"],
|
||||
outputLabels: ["process", "dbase", "upstreamParent", "downstreamParent"],
|
||||
icon: "font-awesome/fa-exchange",
|
||||
outputs: 3,
|
||||
inputLabels: ["Input"],
|
||||
outputLabels: ["process", "dbase", "parent"],
|
||||
icon: "font-awesome/fa-tachometer",
|
||||
|
||||
// Define label function
|
||||
label: function() {
|
||||
return this.name || "Monsternamekast";
|
||||
label: function () {
|
||||
return this.positionIcon + " " + this.category.slice(0, -1) || "Monster";
|
||||
},
|
||||
|
||||
oneditprepare: function() {
|
||||
const node = this;
|
||||
// wait for the menu scripts to load
|
||||
const waitForMenuData = () => {
|
||||
if (window.EVOLV?.nodes?.monster?.initEditor) {
|
||||
window.EVOLV.nodes.monster.initEditor(this);
|
||||
} else {
|
||||
setTimeout(waitForMenuData, 50);
|
||||
}
|
||||
};
|
||||
waitForMenuData();
|
||||
|
||||
// Define UI html elements
|
||||
const elements = {
|
||||
// Basic fields
|
||||
name: document.getElementById("node-input-name"),
|
||||
// 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);
|
||||
}
|
||||
// your existing project‐settings & 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() {
|
||||
const node = this;
|
||||
|
||||
console.log(`------------ Saving changes to node ------------`);
|
||||
console.log(`${node.uuid}`);
|
||||
const node = this;
|
||||
|
||||
// Save basic properties
|
||||
[ "name", "supplier", "subType", "model" ,"unit" ].forEach(
|
||||
(field) => (node[field] = document.getElementById(`node-input-${field}`).value || "")
|
||||
);
|
||||
|
||||
// Save numeric and boolean properties
|
||||
["enableLog"].forEach(
|
||||
(field) => (node[field] = document.getElementById(`node-input-${field}`).checked)
|
||||
);
|
||||
|
||||
["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");
|
||||
// save asset fields
|
||||
if (window.EVOLV?.nodes?.monster?.assetMenu?.saveEditor) {
|
||||
window.EVOLV.nodes.monster.assetMenu.saveEditor(this);
|
||||
}
|
||||
// save logger fields
|
||||
if (window.EVOLV?.nodes?.monster?.loggerMenu?.saveEditor) {
|
||||
window.EVOLV.nodes.monster.loggerMenu.saveEditor(this);
|
||||
}
|
||||
// save position field
|
||||
if (window.EVOLV?.nodes?.monster?.positionMenu?.saveEditor) {
|
||||
window.EVOLV.nodes.monster.positionMenu.saveEditor(this);
|
||||
}
|
||||
|
||||
if (!node.unit) {
|
||||
RED.notify("Unit selection is required.", "error");
|
||||
}
|
||||
["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;
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
["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>
|
||||
|
||||
<!-- Main UI -->
|
||||
<!-- Main UI Template -->
|
||||
<script type="text/html" data-template-name="monster">
|
||||
|
||||
<!-------------------------------------------INPUT NAME / TYPE ----------------------------------------------->
|
||||
<!-- Node Name -->
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Measurement Name">
|
||||
</div>
|
||||
|
||||
<!-- Sampling Time -->
|
||||
<!-- speficic input -->
|
||||
<div class="form-row">
|
||||
<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>
|
||||
<label for="node-input-samplingtime"><i class="fa fa-clock-o"></i> Sampling time (h)</label>
|
||||
<input type="number" id="node-input-samplingtime" style="width:60%;" />
|
||||
</div>
|
||||
|
||||
<!-- Minimum Volume -->
|
||||
<div class="form-row">
|
||||
<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>
|
||||
<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>
|
||||
|
||||
<!-- Maximum Weight -->
|
||||
<div class="form-row">
|
||||
<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>
|
||||
<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>
|
||||
|
||||
<!-- 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>
|
||||
<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>
|
||||
|
||||
<!-- 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">
|
||||
<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>
|
||||
|
||||
<!-- 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 />
|
||||
<!-- Asset fields injected here -->
|
||||
<div id="asset-fields-placeholder"></div>
|
||||
|
||||
<!-- 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>
|
||||
<!-- Logger fields injected here -->
|
||||
<div id="logger-fields-placeholder"></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>
|
||||
<!-- Position fields injected here -->
|
||||
<div id="position-fields-placeholder"></div>
|
||||
|
||||
</script>
|
||||
|
||||
<script type="text/html" data-help-name="monster">
|
||||
<p><b>Monster Node</b>: Configures and manages monster measurement data.</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>
|
||||
<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>
|
||||
<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>
|
||||
<p><b>Monster node</b>: Configure a monster asset.</p>
|
||||
<ul>
|
||||
|
||||
</ul>
|
||||
</script>
|
||||
201
monster.js
201
monster.js
@@ -1,182 +1,35 @@
|
||||
module.exports = function (RED) {
|
||||
function monster(config) {
|
||||
const nameOfNode = 'monster';
|
||||
const nodeClass = require('./src/nodeClass.js');
|
||||
const { MenuManager, configManager } = require('generalFunctions');
|
||||
|
||||
// create node
|
||||
module.exports = function(RED) {
|
||||
// 1) Register the node type and delegate to your class
|
||||
RED.nodes.registerType(nameOfNode, function(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;
|
||||
// 2) Setup the dynamic menu & config endpoints
|
||||
const menuMgr = new MenuManager();
|
||||
const cfgMgr = new configManager();
|
||||
|
||||
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,
|
||||
},
|
||||
// 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}`);
|
||||
}
|
||||
});
|
||||
|
||||
// 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" };
|
||||
}
|
||||
// Serve /monster/configData.js
|
||||
RED.httpAdmin.get(`/${nameOfNode}/configData.js`, (req, res) => {
|
||||
try {
|
||||
const script = cfgMgr.createEndpoint(nameOfNode);
|
||||
res.type('application/javascript').send(script);
|
||||
} catch (err) {
|
||||
res.status(500).send(`// Error generating configData: ${err.message}`);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
});
|
||||
};
|
||||
220
src/nodeClass.js
Normal file
220
src/nodeClass.js
Normal 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;
|
||||
Reference in New Issue
Block a user