monster update
This commit is contained in:
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);
|
||||
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user