-
Notifications
You must be signed in to change notification settings - Fork 3
/
bp-modbus-display.js
executable file
·153 lines (139 loc) · 4.13 KB
/
bp-modbus-display.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
MIT License Copyright 2021, 2022 - Bitpool Pty Ltd
*/
module.exports = function (RED) {
function bpModbusDisplay(config) {
var node = this;
RED.nodes.createNode(node, config);
node.bpModbusDisplayName = config.bpModbusDisplayName;
node.bpDeviceRegister = config.bpDeviceRegister;
node.bpChkShowDebugWarnings = config.bpChkShowDebugWarnings;
this.status({});
node.on("input", function (msg) {
try {
var nodeRegister = parseInt(node.bpDeviceRegister);
if (nodeRegister && isValidInputMsg(msg)) {
let regAddress = msg.modbusSource.regAddress;
if (nodeRegister == regAddress) {
let regUnitName = msg.modbusSource.regUnitName;
let regUnitScale = msg.modbusSource.regUnitScale;
let regValue = msg.payload;
let prefix = "";
let suffix = "";
switch (regUnitScale) {
case "MICRO":
prefix = "µ";
break;
case "MILLI":
prefix = "m";
break;
case "KILO":
prefix = "k";
break;
case "MEGA":
prefix = "M";
break;
case "GIGA":
prefix = "G";
break;
}
switch (regUnitName) {
case "VOLTAGE_V":
suffix = "volts";
break;
case "CURRENT_A":
suffix = "amps";
break;
case "POWER_ACTIVE_P":
suffix = "W";
break;
case "POWER_REACTIVE_Q":
suffix = "var";
break;
case "POWER_APPARENT_S":
suffix = "VA";
break;
case "ENERGY_ACTIVE_P":
suffix = "Wh";
break;
case "ENERGY_REACTIVE_Q":
suffix = "varh";
break;
case "ENERGY_APPARENT_S":
suffix = "VAh";
break;
case "FREQUENCY_HZ":
suffix = "hz";
break;
case "TEMPERATURE_C":
suffix = "°C";
break;
case "TEMPERATURE_F":
suffix = "°F";
break;
case "PERCENT":
suffix = "%";
break;
}
node.status({
fill: "green",
shape: "dot",
text: String(regValue).trim() + " " + prefix + suffix,
});
}
} else {
if (this.bpChkShowDebugWarnings) {
this.warn(
"This module requires specific input parameters to the node, please refer the documentation."
);
}
}
} catch (err) {
if (this.bpChkShowDebugWarnings) {
this.warn(err);
}
} finally {
node.send(msg);
}
});
}
function isValidInputMsg(msg) {
if (
msg.hasOwnProperty("modbusSource") &&
msg.hasOwnProperty("payload") &&
msg.modbusSource.hasOwnProperty("topic") &&
msg.modbusSource.hasOwnProperty("regUnitName") &&
msg.modbusSource.hasOwnProperty("regUnitScale")
) {
return true;
}
return false;
}
RED.nodes.registerType("bp-display", bpModbusDisplay);
RED.httpAdmin.get('/bpModbusDevices', function(req, res) {
let nodeNames = []
RED.nodes.eachNode((val) =>{
if(val.type == 'bp-device'){
let obj = {
id: val.id,
bpModbusId: val.bpModbusId,
bpUnitName: val.bpUnitName
}
nodeNames.push(obj)
}
})
res.send(JSON.stringify(nodeNames));
});
RED.httpAdmin.get('/bpModbusDeviceRegisterMap/:nodeId', function(req, res) {
let srcNode = RED.nodes.getNode(req.params.nodeId)
let regMap = []
for (const [addr, value] of Object.entries(srcNode.registerMap)) {
let reg = {
regAddress: parseInt(addr),
regName: value.modbusSource.regName,
}
regMap.push(reg)
}
res.send(JSON.stringify(regMap));
});
};