/** * 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;