first commit

This commit is contained in:
znetsixe
2025-05-14 10:31:50 +02:00
parent 59acfad357
commit a97326af08
6 changed files with 1478 additions and 3 deletions

148
measurement.js Normal file
View File

@@ -0,0 +1,148 @@
module.exports = function (RED) {
function measurement(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 measurement object from source.js
const Measurement = require("./dependencies/measurement/measurement");
const OutputUtils = require("../generalFunctions/helper/outputUtils");
//load user defined config in the node-red UI
const mConfig={
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,
},
scaling:{
enabled: config.scaling,
inputMin: config.i_min,
inputMax: config.i_max,
absMin: config.o_min,
absMax: config.o_max,
offset: config.i_offset
},
smoothing: {
smoothWindow: config.count,
smoothMethod: config.smooth_method,
},
simulation: {
enabled: config.simulator,
},
}
//make new measurement on creation to work with.
const m = new Measurement(mConfig);
// put m on node memory as source
node.source = m;
//load output utils
const output = new OutputUtils();
function updateNodeStatus(val) {
//display status
node.status({ fill: "green", shape: "dot", text: val + " " + mConfig.general.unit });
}
//Update status only on event change
m.emitter.on('mAbs', (val) => {
updateNodeStatus(val);
});
//never ending functions
function tick(){
//kick class ticks for time move
m.tick();
//get output
const classOutput = m.getOutput();
const dbOutput = output.formatMsg(classOutput, m.config, "influxdb");
const pOutput = output.formatMsg(classOutput, m.config, "process");
//only send output on values that changed
let msgs = [];
msgs[0] = pOutput;
msgs[1] = dbOutput;
node.send(msgs);
}
// 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) {
if(msg.topic == "simulator" ){
m.toggleSimulation();
}
if(msg.topic == "outlierDetection" ){
m.toggleOutlierDetection();
}
if(msg.topic == "calibrate" ){
m.calibrate();
}
if(msg.topic == "measurement" && typeof msg.payload == "number"){
//feed input into the measurement node and calculate output
m.inputValue = parseFloat(msg.payload);
}
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("measurement", measurement);
};