-
Notifications
You must be signed in to change notification settings - Fork 118
/
parsed_message.h
207 lines (182 loc) · 7.45 KB
/
parsed_message.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef PARSED_MESSAGE_H
#define PARSED_MESSAGE_H
class ParsedMessage {
public:
double cumulativeActiveImport;
double cumulativeActiveExport;
double cumulativeReactiveImport;
double cumulativeReactiveExport;
double momentaryActiveImport;
double momentaryActiveExport;
double momentaryReactiveImport;
double momentaryReactiveExport;
double momentaryActiveImportL1;
double momentaryActiveExportL1;
double momentaryActiveImportL2;
double momentaryActiveExportL2;
double momentaryActiveImportL3;
double momentaryActiveExportL3;
double momentaryReactiveImportL1;
double momentaryReactiveExportL1;
double momentaryReactiveImportL2;
double momentaryReactiveExportL2;
double momentaryReactiveImportL3;
double momentaryReactiveExportL3;
double voltageL1;
double voltageL2;
double voltageL3;
double currentL1;
double currentL2;
double currentL3;
uint16_t crc;
bool telegramComplete;
bool crcOk;
bool sendBatchOne;
void parseRow(const char* obisCode, const char* value) {
double obisValue = simpleatof(value);
parseRow(obisCode, obisValue);
}
void parseRow(const char* obisCode, double obisValue) {
int obisCodeLen = strnlen(obisCode, 7);
if (obisCode[obisCodeLen-1] == '0' &&
obisCode[obisCodeLen-2] == '.' &&
obisCode[obisCodeLen-4] == '.') {
switch (obisCodeLen) {
case 5: switch(obisCode[obisCodeLen-3]) {
case '7': switch (obisCode[0]) {
case '1': momentaryActiveImport = obisValue;
break;
case '2': momentaryActiveExport = obisValue;
break;
case '3': momentaryReactiveImport = obisValue;
break;
case '4': momentaryReactiveExport = obisValue;
break;
default: break;
}
break;
case '8': switch (obisCode[0]) {
case '1': cumulativeActiveImport = obisValue;
break;
case '2': cumulativeActiveExport = obisValue;
break;
case '3': cumulativeReactiveImport = obisValue;
break;
case '4': cumulativeReactiveExport = obisValue;
break;
default: break;
}
break;
default: break;
}
break;
case 6: if (obisCode[obisCodeLen-3] == '7') {
switch(obisCode[1]) {
case '1': switch (obisCode[0]) {
case '2': momentaryActiveImportL1 = obisValue;
break;
case '3': currentL1 = obisValue;
break;
case '4': momentaryActiveImportL2 = obisValue;
break;
case '5': currentL2 = obisValue;
break;
case '6': momentaryActiveImportL3 = obisValue;
break;
case '7': currentL3 = obisValue;
break;
default: break;
}
break;
case '2': switch (obisCode[0]) {
case '2': momentaryActiveExportL1 = obisValue;
break;
case '3': voltageL1 = obisValue;
break;
case '4': momentaryActiveExportL2 = obisValue;
break;
case '5': voltageL2 = obisValue;
break;
case '6': momentaryActiveExportL3 = obisValue;
break;
case '7': voltageL3 = obisValue;
break;
default: break;
}
break;
case '3': switch (obisCode[0]) {
case '2': momentaryReactiveImportL1 = obisValue;
break;
case '4': momentaryReactiveImportL2 = obisValue;
break;
case '6': momentaryReactiveImportL3 = obisValue;
break;
default: break;
}
break;
case '4': switch (obisCode[0]) {
case '2': momentaryReactiveExportL1 = obisValue;
break;
case '4': momentaryReactiveExportL2 = obisValue;
break;
case '6': momentaryReactiveExportL3 = obisValue;
break;
default: break;
}
break;
default: break;
}
}
break;
default: break;
}
}
}
// Limitations:
// Numbers larger than 2G will fail (but spec only goes to 99999999.999 so ok)
// And numbers have no more than 3 decimals in the spec.
// spec == Swedish spec for H1
double simpleatof(const char* value) {
double decFactors[10] = {10.0, 100.0, 1000.0, 100000.0,
1000000.0, 10000000.0, 100000000.0,
1000000000.0, 10000000000.0,
100000000000.0};
int idx = 0;
int intPart = 0;
while (value[idx] != '.')
{
intPart = intPart*10 + (value[idx]-'0');
idx++;
}
int len = strlen(value), startIdx = ++idx;
int decPart = 0;
while (idx < len)
{
decPart = decPart*10 + (value[idx]-'0');
idx++;
}
return intPart + decPart/decFactors[len-startIdx-1];
}
void initNewTelegram() {
crc = 0x0000;
telegramComplete = false;
crcOk = false;
sendBatchOne = true;
}
void updateCrc16(uint8_t a) {
int i;
crc ^= a;
for (i = 0; i < 8; ++i) {
if (crc & 1) {
crc = (crc >> 1) ^ 0xA001;
} else {
crc = (crc >> 1);
}
}
}
void checkCrc(uint16_t crcFromMessage) {
crcOk = crc == crcFromMessage;
telegramComplete = true;
}
};
#endif