-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_node
More file actions
101 lines (91 loc) · 3.57 KB
/
function_node
File metadata and controls
101 lines (91 loc) · 3.57 KB
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
// Function node to parse VE.Direct data received from Node-RED serial node
// Input: msg.payload contains the array of bytes
let data = msg.payload;
function parseVEData(data) {
let parsed = {};
let dataStr = '';
// Convert byte array to string for easier processing
for (let i = 0; i < data.length; i++) {
dataStr += String.fromCharCode(data[i]);
}
// Split the raw data string into lines
let lines = dataStr.split('\r\n');
// Variables to hold intermediate values
let panelVoltage = 0;
let panelPower = 0;
// Iterate through each line and extract key-value pairs
lines.forEach(line => {
let [key, value] = line.split('\t');
if (key && value) {
key = key.trim();
value = value.trim();
// Parse specific fields based on VE.Direct protocol
switch (key) {
case 'PID':
parsed.VictronProductId = value;
break;
case 'FWE':
parsed.VictronFirmwareVersion = value;
break;
case 'SER#':
parsed.VictronSerialNumber = value;
break;
case 'V':
parsed.VictronBatteryVoltage = parseInt(value) / 1000.0;
break;
case 'I':
parsed.VictronBatteryCurrent = parseInt(value) / 1000.0;
break;
case 'VPV':
panelVoltage = parseInt(value) / 1000.0;
parsed.VictronSolarVoltage = panelVoltage;
break;
case 'PPV':
panelPower = parseInt(value);
parsed.VictronSolarPower = panelPower;
break;
case 'MPPT':
parsed.VictronTrackingMode = parseInt(value);
break;
case 'CS':
parsed.VictronChargingMode = parseInt(value);
break;
case 'ERR':
parsed.VictronError = parseInt(value);
break;
case 'H19':
parsed.VictronYieldTotal = parseInt(value) * 10; // Adjust based on actual units
break;
case 'H20':
parsed.VictronYieldToday = parseInt(value) * 10; // Adjust based on actual units
break;
case 'H21':
parsed.VictronMaxPowerToday = parseInt(value);
break;
case 'H22':
parsed.VictronYieldYesterday = parseInt(value) * 10; // Adjust based on actual units
break;
case 'H23':
parsed.VictronMaxPowerYesterday = parseInt(value);
break;
case 'HSDS':
parsed.VictronDayNumber = parseInt(value);
// Calculate solar current
parsed.VictronSolarCurrent = (panelVoltage > 0 && panelPower > 0) ? panelPower / panelVoltage : 0;
break;
// Add more cases as needed
}
}
});
// Generate separate messages for each key-value pair
let messages = [];
for (let key in parsed) {
if (parsed.hasOwnProperty(key)) {
let message = { topic: key, payload: parsed[key] };
node.send(message); // Send each message separately
}
}
return null; // No need to return a single object
}
// Parse the data
parseVEData(data);