standaardisation of EVOLV eco
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
<script src="/measurement/menu.js"></script>
|
<script src="/measurement/menu.js"></script> <!-- Load the menu script for dynamic dropdowns -->
|
||||||
|
<script src="/measurement/configData.js"></script> <!-- Load the config script for node information -->
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
RED.nodes.registerType("measurement", {
|
RED.nodes.registerType("measurement", {
|
||||||
|
|||||||
191
measurement.js
191
measurement.js
@@ -1,170 +1,47 @@
|
|||||||
console.log('Loading measurement.js module...');
|
/**
|
||||||
// load helper modules from the generalFunctions folder
|
* Thin wrapper that registers a node with Node-RED and exposes HTTP endpoints. and loads EVOLV in a standard way
|
||||||
const { outputUtils, MenuManager } = require('generalFunctions');
|
*/
|
||||||
|
|
||||||
//internal node-red node dependencies this is the class that will handle the measurement
|
const nodeClass = require('./src/nodeClass.js');
|
||||||
const Measurement = require("./dependencies/measurement/measurement");
|
const { MenuManager, configManager } = require('generalFunctions');
|
||||||
console.log('All dependencies loaded successfully');
|
const nameOfNode = 'measurement';
|
||||||
|
|
||||||
module.exports = function (RED) {
|
module.exports = function(RED) {
|
||||||
function measurement(config) {
|
// Register the node type
|
||||||
//create node
|
RED.nodes.registerType(nameOfNode, function(config) {
|
||||||
|
// Initialize the Node-RED node first
|
||||||
RED.nodes.createNode(this, config);
|
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{
|
// Then create your custom class and attach it
|
||||||
//load user defined config in the node-red UI
|
this.nodeClass = new nodeClass(config, RED, this);
|
||||||
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
|
// Setup admin UIs
|
||||||
function tick(){
|
const menuMgr = new MenuManager();
|
||||||
|
const cfgMgr = new configManager();
|
||||||
|
|
||||||
//kick class ticks for time move
|
console.log(`Loading endpoint for ${nameOfNode} menu...`);
|
||||||
m.tick();
|
// Register the menu for the measurement node
|
||||||
|
RED.httpAdmin.get('/measurement/menu.js', (req, res) => {
|
||||||
//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);
|
|
||||||
|
|
||||||
|
|
||||||
const menuManager = new MenuManager();
|
|
||||||
|
|
||||||
RED.httpAdmin.get("/measurement/menu.js", (req, res) => {
|
|
||||||
try {
|
try {
|
||||||
const script = menuManager.createEndpoint('measurement', ['asset']);
|
const script = menuMgr.createEndpoint(nameOfNode, ['asset']);
|
||||||
res.set('Content-Type', 'application/javascript').send(script);
|
res.type('application/javascript').send(script);
|
||||||
} catch (error) {
|
} catch (err) {
|
||||||
console.error("Error creating measurement menu script:", error);
|
res.status(500).send(`// Error generating menu: ${err.message}`);
|
||||||
res.status(500).send(`// Failed to generate menu data: ${error.message}`);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(`Loading endpoint for ${nameOfNode} config...`);
|
||||||
|
// Endpoint to get the configuration data for the measurement node
|
||||||
|
RED.httpAdmin.get(`/measurement/configData.js`, (req, res) => {
|
||||||
|
try {
|
||||||
|
const script = cfgMgr.createEndpoint(nameOfNode);
|
||||||
|
// Send the configuration data as JSON response
|
||||||
|
res.type('application/javascript').send(script);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).send(`// Error generating configData: ${err.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
console.log('Measurement node and its menu data endpoint are registered.');
|
console.log(`Measurement node '${nameOfNode}' registered.`);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
188
measurementOLD.js
Normal file
188
measurementOLD.js
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
console.log('Loading measurement.js module...');
|
||||||
|
// load helper modules from the generalFunctions folder
|
||||||
|
const { outputUtils, MenuManager, configManager } = require('generalFunctions');
|
||||||
|
|
||||||
|
//internal node-red node dependencies this is the class that will handle the measurement
|
||||||
|
const Measurement = require("./dependencies/measurement/measurement");
|
||||||
|
console.log('All dependencies loaded successfully');
|
||||||
|
const nameOfNode = "measurement";
|
||||||
|
|
||||||
|
module.exports = function (RED) {
|
||||||
|
//load the config manager to get the default configs
|
||||||
|
this.configManager = new configManager();
|
||||||
|
this.defaultConfig = this.configManager.getConfig('measurement');
|
||||||
|
|
||||||
|
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{
|
||||||
|
//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);
|
||||||
|
|
||||||
|
|
||||||
|
const menuManager = new MenuManager();
|
||||||
|
|
||||||
|
// Register the measurement menu handler
|
||||||
|
RED.httpAdmin.get("/measurement/menu.js", (req, res) => {
|
||||||
|
try {
|
||||||
|
const script = menuManager.createEndpoint('measurement', ['asset']);
|
||||||
|
res.set('Content-Type', 'application/javascript').send(script);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error creating measurement menu script:", error);
|
||||||
|
res.status(500).send(`// Failed to generate menu data: ${error.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//register the measurement specific configs so the browser can access them
|
||||||
|
RED.httpAdmin.get("/measurement/configData", (req, res) => {
|
||||||
|
// load the config data from the JSON file and send it
|
||||||
|
try {
|
||||||
|
const configData = require('./dependencies/measurement/measurementConfig.json');
|
||||||
|
res.json(configData);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error loading measurement config data:", error);
|
||||||
|
res.status(500).send(`// Failed to load config data: ${error.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register the measurement node and its menu data endpoint
|
||||||
|
console.log('Measurement node and its menu data endpoint are registered.');
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
167
src/nodeClass.js
Normal file
167
src/nodeClass.js
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
/**
|
||||||
|
* measurement.class.js
|
||||||
|
*
|
||||||
|
* Encapsulates all Measurement node logic in a reusable class.
|
||||||
|
*/
|
||||||
|
const { outputUtils, configManager } = require('generalFunctions');
|
||||||
|
const Measurement = require("../dependencies/measurement/measurement");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class representing a Measurement Node-RED node.
|
||||||
|
*/
|
||||||
|
class MeasurementNode {
|
||||||
|
/**
|
||||||
|
* Create a MeasurementNode.
|
||||||
|
* @param {object} config - Node-RED node configuration.
|
||||||
|
* @param {object} RED - Node-RED runtime API.
|
||||||
|
*/
|
||||||
|
constructor(config, RED, nodeInstance) {
|
||||||
|
|
||||||
|
// Preserve RED reference for HTTP endpoints if needed
|
||||||
|
this.node = nodeInstance;
|
||||||
|
this.RED = RED;
|
||||||
|
|
||||||
|
// Load default & UI config
|
||||||
|
this._loadConfig(config);
|
||||||
|
|
||||||
|
// Instantiate core Measurement class
|
||||||
|
this._setupMeasurementClass();
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
const cfgMgr = new configManager();
|
||||||
|
this.defaultConfig = cfgMgr.getConfig("measurement");
|
||||||
|
|
||||||
|
// Merge UI config over defaults
|
||||||
|
this.config = {
|
||||||
|
general: {
|
||||||
|
name: uiConfig.name,
|
||||||
|
id: this.id,
|
||||||
|
unit: uiConfig.unit,
|
||||||
|
logging: {
|
||||||
|
enabled: uiConfig.enableLog,
|
||||||
|
logLevel: uiConfig.logLevel
|
||||||
|
}
|
||||||
|
},
|
||||||
|
asset: {
|
||||||
|
tagCode: uiConfig.assetTagCode,
|
||||||
|
supplier: uiConfig.supplier,
|
||||||
|
subType: uiConfig.subType,
|
||||||
|
model: uiConfig.model
|
||||||
|
},
|
||||||
|
scaling: {
|
||||||
|
enabled: uiConfig.scaling,
|
||||||
|
inputMin: uiConfig.i_min,
|
||||||
|
inputMax: uiConfig.i_max,
|
||||||
|
absMin: uiConfig.o_min,
|
||||||
|
absMax: uiConfig.o_max,
|
||||||
|
offset: uiConfig.i_offset
|
||||||
|
},
|
||||||
|
smoothing: {
|
||||||
|
smoothWindow: uiConfig.count,
|
||||||
|
smoothMethod: uiConfig.smooth_method
|
||||||
|
},
|
||||||
|
simulation: {
|
||||||
|
enabled: uiConfig.simulator
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Utility for formatting outputs
|
||||||
|
this._output = new outputUtils();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate the core Measurement logic and store as source.
|
||||||
|
*/
|
||||||
|
_setupMeasurementClass() {
|
||||||
|
this.source = new Measurement(this.config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind Measurement events to Node-RED status updates.
|
||||||
|
*/
|
||||||
|
_bindEvents() {
|
||||||
|
this.source.emitter.on('mAbs', (val) => {
|
||||||
|
this.node.status({ fill: 'green', shape: 'dot', text: `${val} ${this.config.general.unit}` });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.id, positionVsParent: 'upstream' }
|
||||||
|
]);
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start the periodic tick loop to drive the Measurement class.
|
||||||
|
*/
|
||||||
|
_startTickLoop() {
|
||||||
|
setTimeout(() => {
|
||||||
|
this._tickInterval = setInterval(() => this._tick(), 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 Measurement class.
|
||||||
|
*/
|
||||||
|
_attachInputHandler() {
|
||||||
|
this.node.on('input', (msg, send, done) => {
|
||||||
|
switch (msg.topic) {
|
||||||
|
case 'simulator': this.source.toggleSimulation(); break;
|
||||||
|
case 'outlierDetection': this.source.toggleOutlierDetection(); break;
|
||||||
|
case 'calibrate': this.source.calibrate(); break;
|
||||||
|
case 'measurement':
|
||||||
|
if (typeof msg.payload === 'number') {
|
||||||
|
this.source.inputValue = parseFloat(msg.payload);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up timers and intervals when Node-RED stops the node.
|
||||||
|
*/
|
||||||
|
_attachCloseHandler() {
|
||||||
|
this.node.on('close', (done) => {
|
||||||
|
clearInterval(this._tickInterval);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = MeasurementNode;
|
||||||
Reference in New Issue
Block a user