UNPKG

node-red-contrib-plcs

Version:

Mentor Monitor Project Plcs Modbus Controllers

422 lines (363 loc) 15.9 kB
var numeral = require('numbro'); module.exports = function(RED) { "use strict"; var log = RED.log; function digitalOutput(n) { RED.nodes.createNode(this,n); var node = this; var context = this.context().flow; //numeral.defaultFormat('0.00'); numeral.setDefaults({ thousandSeparated: false, mantissa: 2 }); this.on('input', function(msg) { // Create new Buffer based on array bytes const buf = Buffer.from(msg.payload.buffer); var vars = getMsg(node, buf, msg); msg.payload = {"time": msg.topic.time, "value": vars, "code":msg.topic.device} var value = context.get(msg.topic.device); if (value.time != msg.topic.time || (value.timeout -1) != msg.topic.try) { return; } // Reduce the number of inputs value.n--; // Store the data in msg.payload for all operations as they complete value.data[value.n] = msg.payload; if (value.n === 0 ) { value.timeout = 0; for (var i = 0; i < value.data.length; i++) { msg.payload.value = Object.assign(value.data[i].value, msg.payload.value); } msg.payload = JSON.stringify({"code": msg.topic.device, "time": msg.payload.time, "head": Object.keys(msg.payload.value), "data": Object.values(msg.payload.value)}); node.send(msg); } context.set(msg.topic.device, value); }); } function getMsg(node, buf, msg) { switch(msg.topic.devicetype) { case "PM850": { var value = getValuesPM850(buf,msg); break; } case "PM5100": { var value = getValuesPM5100(buf,msg); break; } case "PM700": { var value = getValuesPM700(buf,msg); break; } case "CVMC10": { var value = getValuesCVMC10(buf,msg); break; } default: var value = null; break; } return value; } function getValuesCVMC10(buf, msg) { if (msg.topic.group == 1) { var Current_A = numeral(buf.readUInt32BE(2*2,4)).format(); var Current_B = numeral(buf.readUInt32BE(18*2,4)).format(); var Current_C = numeral(buf.readUInt32BE(34*2,4)).format(); var Real_Power_Total_kW = numeral(buf.readUInt32BE(48*2,4)/100).format(); var Reactive_Power_Total_kVAR = numeral(buf.readUInt32BE(50*2,4)).format(); var Apparent_Power_Total_kVA = numeral(buf.readUInt32BE(54*2,4)/100).format(); var Power_Factor = numeral(buf.readUInt32BE(56*2,4)).format(); } if (msg.topic.group == 2) { var Frequency = numeral(buf.readUInt32BE((60-60)*2,4)/100).format(); var Voltage_AB = numeral(buf.readUInt32BE((62-60)*2,4)/10).format(); var Voltage_BC = numeral(buf.readUInt32BE((64-60)*2,4)/10).format(); var Voltage_CA = numeral(buf.readUInt32BE((66-60)*2,4)/10).format(); var THD_Current_A_ = numeral(buf.readUInt32BE((76-60)*2,4)).format(); var THD_Current_B_ = numeral(buf.readUInt32BE((78-60)*2,4)).format(); var THD_Current_C_ = numeral(buf.readUInt32BE((80-60)*2,4)).format(); } if (msg.topic.group == 3) { var Real_Energy_kWh = numeral(buf.readUInt32BE((220-220)*2,4)).format(); var Reactive_Energy_kVARh = numeral(buf.readUInt32BE((224-220)*2,4)).format(); var Apparent_Energy_kVAh = numeral(buf.readUInt32BE((230-220)*2,4)).format(); } // save the value if (msg.topic.group == 1) { var value = { "Current_A" : Current_A, "Current_B" : Current_B, "Current_C" : Current_C, "Real_Power_Total_kW" : Real_Power_Total_kW, "Reactive_Power_Total_kVAR" : Reactive_Power_Total_kVAR, "Apparent_Power_Total_kVA" : Apparent_Power_Total_kVA, "Power_Factor" : Power_Factor, }; } if (msg.topic.group == 2) { var value = { "Frequency" : Frequency, "Voltage_AB" : Voltage_AB, "Voltage_BC" : Voltage_BC, "Voltage_CA" : Voltage_CA, "THD_Current_A_" : THD_Current_A_, "THD_Current_B_" : THD_Current_B_, "THD_Current_C_" : THD_Current_C_, }; } if (msg.topic.group == 3) { var value = { "Real_Energy_kWh" : Real_Energy_kWh, "Reactive_Energy_kVARh" : Reactive_Energy_kVARh, "Apparent_Energy_kVAh" : Apparent_Energy_kVAh, } } return value; } function getValuesPM850(buf, msg) { if (msg.topic.group == 1) { var Current_A = numeral(buf.readUInt16BE((100-100)*2,2)).format(); var Current_B = numeral(buf.readUInt16BE((101-100)*2,2)).format(); var Current_C = numeral(buf.readUInt16BE((102-100)*2,2)).format(); var Voltage_AB = numeral(buf.readUInt16BE((120-100)*2,2)).format(); var Voltage_BC = numeral(buf.readUInt16BE((121-100)*2,2)).format(); var Voltage_CA = numeral(buf.readUInt16BE((122-100)*2,2)).format(); var Real_Power_Total_kW = numeral(buf.readUInt16BE((143-100)*2,2)).format(); var Reactive_Power_Total_kVAR = numeral(buf.readUInt16BE((147-100)*2,2)).format(); var Apparent_Power_Total_kVA = numeral(buf.readUInt16BE((151-100)*2,2)).format(); var Power_Factor = buf.readUInt16BE((163-100)*2,2)/1000; var Frequency = buf.readUInt16BE((180-100)*2,2)/100; } if (msg.topic.group == 2) { var THD_Current_A_ = buf.readUInt16BE((200-200)*2,2)/10; var THD_Current_B_ = buf.readUInt16BE((201-200)*2,2)/10; var THD_Current_C_ = buf.readUInt16BE((202-200)*2,2)/10; } if (msg.topic.group == 3) { var Real_Energy_A_kWh = buf.readUInt16BE((716-716)*2,2); var Real_Energy_B_kWh = buf.readUInt16BE((717-716)*2,2); var Real_Energy_C_kWh = buf.readUInt16BE((718-716)*2,2); var Real_Energy_D_kWh = buf.readUInt16BE((719-716)*2,2); var Real_Energy_kWh = (Real_Energy_D_kWh * 1000000000000 + Real_Energy_C_kWh * 100000000 + Real_Energy_B_kWh * 10000 + Real_Energy_A_kWh)/1000; var Reactive_Energy_A_kVARh = buf.readUInt16BE((720-716)*2,2); var Reactive_Energy_B_kVARh = buf.readUInt16BE((721-716)*2,2); var Reactive_Energy_C_kVARh = buf.readUInt16BE((722-716)*2,2); var Reactive_Energy_D_kVARh = buf.readUInt16BE((723-716)*2,2); var Reactive_Energy_kVARh = (Reactive_Energy_D_kVARh * 1000000000000 + Reactive_Energy_C_kVARh*100000000 + Reactive_Energy_B_kVARh*10000 + Reactive_Energy_A_kVARh)/1000; var Apparent_Energy_A_kVAh = buf.readUInt16BE((724-716)*2,2); var Apparent_Energy_B_kVAh = buf.readUInt16BE((725-716)*2,2); var Apparent_Energy_C_kVAh = buf.readUInt16BE((726-716)*2,2); var Apparent_Energy_D_kVAh = buf.readUInt16BE((727-716)*2,2); var Apparent_Energy_kVAh = (Apparent_Energy_D_kVAh * 1000000000000 + Apparent_Energy_C_kVAh * 100000000 + Apparent_Energy_B_kVAh*10000 + Apparent_Energy_A_kVAh)/1000; } // save the value if (msg.topic.group == 1) { var value = { "Current_A" : Current_A, "Current_B" : Current_B, "Current_C" : Current_C, "Voltage_AB" : Voltage_AB, "Voltage_BC" : Voltage_BC, "Voltage_CA" : Voltage_CA, "Real_Power_Total_kW" : Real_Power_Total_kW, "Reactive_Power_Total_kVAR" : Reactive_Power_Total_kVAR, "Apparent_Power_Total_kVA" : Apparent_Power_Total_kVA, "Power_Factor" : Power_Factor, "Frequency" : Frequency, }; } if (msg.topic.group == 2) { var value = { "THD_Current_A_" : THD_Current_A_, "THD_Current_B_" : THD_Current_B_, "THD_Current_C_" : THD_Current_C_, }; } if (msg.topic.group == 3) { var value = { /* "Real_Energy_A_kWh" : Real_Energy_A_kWh, "Real_Energy_B_kWh" : Real_Energy_B_kWh, "Real_Energy_C_kWh" : Real_Energy_C_kWh, "Real_Energy_D_kWh" : Real_Energy_D_kWh, */ "Real_Energy_kWh" : Real_Energy_kWh, /* "Reactive_Energy_A_kVARh" : Reactive_Energy_A_kVARh, "Reactive_Energy_B_kVARh" : Reactive_Energy_B_kVARh, "Reactive_Energy_C_kVARh" : Reactive_Energy_C_kVARh, "Reactive_Energy_D_kVARh" : Reactive_Energy_D_kVARh, */ "Reactive_Energy_kVARh" : Reactive_Energy_kVARh, /* "Apparent_Energy_A_VAh" : Apparent_Energy_A_kVAh, "Apparent_Energy_B_VAh" : Apparent_Energy_B_kVAh, "Apparent_Energy_C_VAh" : Apparent_Energy_C_kVAh, "Apparent_Energy_D_VAh" : Apparent_Energy_D_kVAh, */ "Apparent_Energy_kVAh" : Apparent_Energy_kVAh, } } return value; } function getValuesPM700(buf, msg) { if (msg.topic.group == 1) { //var Real_Energy_kWh = numeral(buf.readFloatBE(0)).format(); //var Reactive_Energy_kVARh = numeral(buf.readFloatBE(4*2)).format(); var Real_Energy_kWh = numeral(buf.readFloatBE(0)).format("0.000"); var Reactive_Energy_kVARh = numeral(buf.readFloatBE(4*2)).format("0.000"); var Apparent_Energy_kVAh = numeral(buf.readFloatBE(2*2)).format("0.000"); var Real_Power_Total_kW = numeral(buf.readFloatBE(6*2)).format(); var Apparent_Power_Total_kVA = numeral(buf.readFloatBE(8*2)).format(); var Reactive_Power_Total_kVAR = numeral(buf.readFloatBE(10*2)).format(); var Power_Factor = numeral(buf.readFloatBE(12*2)).format(); } if (msg.topic.group == 2) { var Frequency = numeral(buf.readFloatBE((20-20)*2)).format(); var Current_A = numeral(buf.readFloatBE((34-20)*2)).format(); var Current_B = numeral(buf.readFloatBE((36-20)*2)).format(); var Current_C = numeral(buf.readFloatBE((38-20)*2)).format(); var Voltage_AB = numeral(buf.readFloatBE((54-20)*2,4)).format(); var Voltage_BC = numeral(buf.readFloatBE((56-20)*2,4)).format(); var Voltage_CA = numeral(buf.readFloatBE((58-20)*2,4)).format(); } if (msg.topic.group == 3) { var THD_Current_A_ = numeral(buf.readFloatBE(0)).format(); var THD_Current_B_ = numeral(buf.readFloatBE(2*2)).format(); var THD_Current_C_ = numeral(buf.readFloatBE(4*2)).format(); } // save the value if (msg.topic.group == 1) { var value = { "Real_Power_Total_kW" : Real_Power_Total_kW, "Reactive_Power_Total_kVAR" : Reactive_Power_Total_kVAR, "Apparent_Power_Total_kVA" : Apparent_Power_Total_kVA, "Power_Factor" : Power_Factor, "Real_Energy_kWh" : Real_Energy_kWh, "Reactive_Energy_kVARh" : Reactive_Energy_kVARh, "Apparent_Energy_kVAh" : Apparent_Energy_kVAh, }; } if (msg.topic.group == 2) { var value = { "Frequency" : Frequency, "Current_A" : Current_A, "Current_B" : Current_B, "Current_C" : Current_C, "Voltage_AB" : Voltage_AB, "Voltage_BC" : Voltage_BC, "Voltage_CA" : Voltage_CA, }; } if (msg.topic.group == 3) { var value = { "THD_Current_A_" : THD_Current_A_, "THD_Current_B_" : THD_Current_B_, "THD_Current_C_" : THD_Current_C_, } } return value; } function getValuesPM5100(buf, msg) { if (msg.topic.group == 1) { var Current_A = numeral(buf.readFloatBE(0*2)).format(); var Current_B = numeral(buf.readFloatBE(2*2)).format(); var Current_C = numeral(buf.readFloatBE(4*2)).format(); var Voltage_AB = numeral(buf.readFloatBE(20*2,4)).format(); var Voltage_BC = numeral(buf.readFloatBE(22*2,4)).format(); var Voltage_CA = numeral(buf.readFloatBE(24*2,4)).format(); } if (msg.topic.group == 2) { var Real_Power_Total_kW = numeral(buf.readFloatBE((60-60)*2)).format(); var Reactive_Power_Total_kVAR = numeral(buf.readFloatBE((68-60)*2)).format(); var Apparent_Power_Total_kVA = numeral(buf.readFloatBE((76-60)*2)).format(); var Power_Factor = numeral(buf.readFloatBE((84-60)*2)).format(); var Frequency = numeral(buf.readFloatBE((110-60)*2)).format(); } if (msg.topic.group == 3) { var THD_Current_A_ = numeral(buf.readFloatBE(0)).format(); var THD_Current_B_ = numeral(buf.readFloatBE(2*2)).format(); var THD_Current_C_ = numeral(buf.readFloatBE(4*2)).format(); var Apparent_Energy_kVAh = numeral(buf.readFloatBE(4*2)).format(); } if (msg.topic.group == 4) { var Real_Energy_kWh = numeral(buf.readUInt32BE(6*2)/1000).format("0.000"); var Reactive_Energy_kVARh = numeral(buf.readUInt32BE(22*2)/1000).format("0.000"); } // save the value if (msg.topic.group == 1) { var value = { "Current_A" : Current_A, "Current_B" : Current_B, "Current_C" : Current_C, "Voltage_AB" : Voltage_AB, "Voltage_BC" : Voltage_BC, "Voltage_CA" : Voltage_CA, }; } if (msg.topic.group == 2) { var value = { "Real_Power_Total_kW" : Real_Power_Total_kW, "Reactive_Power_Total_kVAR" : Reactive_Power_Total_kVAR, "Apparent_Power_Total_kVA" : Apparent_Power_Total_kVA, "Power_Factor" : Power_Factor, "Frequency" : Frequency, }; } if (msg.topic.group == 4) { var value = { "Real_Energy_kWh" : Real_Energy_kWh, "Reactive_Energy_kVARh" : Reactive_Energy_kVARh, }; } if (msg.topic.group == 3) { var value = { "THD_Current_A_" : THD_Current_A_, "THD_Current_B_" : THD_Current_B_, "THD_Current_C_" : THD_Current_C_, "Apparent_Energy_kVAh" : Apparent_Energy_kVAh, } } return value; } RED.nodes.registerType("Legacy Output",digitalOutput); };