UNPKG

node-red-contrib-minelert-universal-io

Version:

Node-RED nodes for Universal IO Gateway and I/O Modules

43 lines (29 loc) 1.28 kB
const { EventBus, logInfo, Utils } = require('@minelert-smartlink/common'); const {toMeters, convertLevelToMilliAmps} = require('./utils/smartlink-utils') module.exports = function(RED) { function LevelToMaNode(config) { RED.nodes.createNode(this, config); const node = this; node.mode = config.mode || 'distance'; // 'distance' or 'level' node.min = parseFloat(config.min) || 0; node.max = parseFloat(config.max) || 1; node.on('input', (msg, send, done) => { const tankHeight = RED.util.evaluateNodeProperty(config.tankHeight, config.tankHeightType || "str", this, msg); const height = parseFloat(tankHeight); const value = msg.payload.value || null const unit = msg.payload.unit || 'm' const result = convertLevelToMilliAmps(value, unit, node.mode, node.min, node.max, height) if ('error' in result) { node.error(result.error) return } node.status({ fill: "green", shape: "dot", text: `${result.mA} mA` }); send({payload: result}); if (done) done(); }); node.on('close', () => { // Optionally perform cleanup }); } RED.nodes.registerType("level-to-ma", LevelToMaNode); };