UNPKG

node-red-contrib-minelert-universal-io

Version:

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

61 lines (43 loc) 1.41 kB
function toMeters(value, unit) { switch (unit) { case 'cm': return value / 100; case 'mm': return value / 1000; default: return value; // 'm' } } function convertLevelToMilliAmps(value, unit, mode, min, max, height) { let input = parseFloat(value); if (isNaN(input)) { return {error: 'Payload must be a number'}; } let valueForMapping; let mA; let percent; const result = {}; input = toMeters(input, unit); result.readingToMeter = input if (mode === 'distance') { valueForMapping = input; if (valueForMapping < min) valueForMapping = min; if (valueForMapping > max) valueForMapping = max; result.value = valueForMapping; mA = 4 + ((valueForMapping - min) / (max - min)) * 16; percent = (valueForMapping / max) * 100; } else if (mode === 'level') { let level = Math.round((height - input) * 1000) / 1000; if (level < 0) level = 0; if (level > height) level = height; result.value = level; mA = 4 + (level / height) * 16; percent = (level / height) * 100; } else { return {error: 'Invalid mode'};; } result.percent = Math.round(percent * 100) / 100; result.mA = Math.round(mA * 100) / 100; return result; } module.exports = { toMeters, convertLevelToMilliAmps };