Files
heatExchanger/heatExchanger.js
znetsixe 09f1c37125 changes
2025-05-26 17:24:42 +02:00

147 lines
4.3 KiB
JavaScript

module.exports = function (RED) {
function heatExchanger(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 heatExchanger object from source.js
const heatExchanger = require("./dependencies/heatExchanger/heatExchanger");
const OutputUtils = require("../generalFunctions/helper/outputUtils");
//load user defined config in the node-red UI
const heConfig={
general: {
name: config.name,
id: node.id,
unit: config.unit,
logging:{
logLevel: config.logLevel,
enabled: config.enableLog,
},
},
asset: {
tagCode: config.assetTagCode,
supplier: config.supplier,
subType: config.subType,
model: config.model,
}
}
//make new heatExchanger on creation to work with.
const he = new heatExchanger(heConfig);
// put m on node memory as source
node.source = he;
//load output utils
const output = new OutputUtils();
function updateNodeStatus(val) {
//display status
node.status({ fill: "green", shape: "dot", text: val + " " + heConfig.general.unit });
}
function tick() {
try {
//const status = updateNodeStatus();
//node.status(status);
//get output
const classOutput = he.getOutput();
const dbOutput = output.formatMsg(classOutput, he.config, "influxdb");
const pOutput = output.formatMsg(classOutput, he.config, "process");
//only send output on values that changed
let msgs = [];
msgs[0] = pOutput;
msgs[1] = dbOutput;
node.send(msgs);
} 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.intervalId = setInterval(function(){ tick() },1000)
},
1000
);
//-------------------------------------------------------------------->>what to do on input
node.on("input", function (msg,send,done) {
// augment this later with child connections
if(msg.topic == "TempHotInput")
{
if(msg.payload.mAbs){
he.setHotInput(msg.payload.mAbs);
console.log("HotInput: " + msg.payload);
}
}
if(msg.topic == "TempColdInput")
{
if(msg.payload.mAbs){
he.setColdInput(msg.payload.mAbs);
console.log("ColdInput: " + msg.payload);
}
}
if(msg.topic == "FlowRateHot"){
if(msg.payload.downstream_predicted_flow){
he.setHotFlowRate(msg.payload.downstream_predicted_flow);
console.log("FlowRateHot: " + msg.payload.downstream_predicted_flow);
}
}
if(msg.topic == "FlowRateCold"){
if(msg.payload.downstream_predicted_flow){
he.setColdFlowRate(msg.payload.downstream_predicted_flow);
console.log("FlowRateCold: " + msg.payload.downstream_predicted_flow);
}
}
done();
});
// tidy up any async code here - shutdown connections and so on.
node.on('close', function(done) {
if (node.intervalId) clearTimeout(node.intervalId);
if (node.tickInterval) clearInterval(node.tickInterval);
if (done) done();
});
}catch(e){
console.log(e);
}
}
RED.nodes.registerType("heatExchanger", heatExchanger);
};