-
Notifications
You must be signed in to change notification settings - Fork 1
/
OMI-processing.cpp
330 lines (266 loc) · 11.1 KB
/
OMI-processing.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "Config.h"
#include "MyCertificates.h"
#include "KMEGLib/K1Building.h" // constants and metadata
#include "SensorNet.h" // yggdrasil protocol handling
#include "HttpsCertificateExtension.h"
#include <ESP8266WiFi.h> // For mac address name wifi bridge object
//enum TrySendRet {
// Success,
// NoWifi,
// Fail
//};
// templates (format string) for sprintf to insert the value
// It has placeholder for id, name and value
static const char writeHeader[] =
"<omi:omiEnvelope xmlns:omi=\"omi.xsd\" version=\"1.0\" ttl=\"20\">"
"<omi:write msgformat=\"odf\">"
"<omi:msg>"
"<Objects xmlns=\"odf.xsd\">";
static const char objectStartTemplate[] =
"<Object>"
"<id>%s</id>";
//"%%s" // single printf-escaped %
static const char objectEnd[] = "</Object>";
static const char itemTemplate[] =
"<InfoItem name=\"%s\">"
"<value>%s</value>"
"</InfoItem>";
static const char writeFooter[] =
"</Objects>"
"</omi:msg>"
"</omi:write>"
"</omi:omiEnvelope>";
static const uint16_t MaxItemSize = sizeof(itemTemplate) + MAX_II_NAME_VALUE_SIZE;
// tmp buffer for InfoItem
//static char itemStr[MaxItemSize]; // extra size for name and value
static const uint16_t OMIHttpBufferSize =
sizeof(writeHeader) + sizeof(writeFooter)
+ (sizeof(objectStartTemplate) + sizeof(objectEnd) + MAX_II_NAME_VALUE_SIZE) * MAX_OBJECTS
+ MaxItemSize * MAX_ITEMS_PER_WRITE;
// tmp buffer for http payload
static char omiHttpBuffer[OMIHttpBufferSize];
uint8_t openObjects = 0; // GLOBAL: current number of non-closed <Object>
void omiHeader(){
DBGSTREAM.printf(FS("[OMI-processing] New O-MI message, Free memory left: %u\r\n"), ESP.getFreeHeap(), OMIHttpBufferSize);
strcpy(omiHttpBuffer, writeHeader); // replaces the old
openObjects = 0;
}
bool omiAddObject(const char * objectName) {
int16_t len = strlen(omiHttpBuffer);
++openObjects;
return snprintf(omiHttpBuffer + len, OMIHttpBufferSize - len, objectStartTemplate, objectName)
< (OMIHttpBufferSize - len);
}
// Must be used on a buffer that has %s for InfoItem name and value
bool omiAddInfoItem(const char * itemName, const char * valueStr) {
int16_t len = strlen(omiHttpBuffer);
return snprintf(omiHttpBuffer + len, OMIHttpBufferSize - len, itemTemplate, itemName, valueStr)
< (OMIHttpBufferSize - len);
}
bool omiCloseObject(){
uint16_t bufLen = strlen(omiHttpBuffer);
if (bufLen + sizeof(objectEnd) < OMIHttpBufferSize) {
--openObjects;
strcpy(omiHttpBuffer + bufLen, objectEnd);
return true;
}
return false;
}
bool omiFooter(){
while (openObjects) if(!omiCloseObject()) return false; // Auto-close open <Object>
uint16_t bufLen = strlen(omiHttpBuffer);
if (bufLen + sizeof(writeFooter) < OMIHttpBufferSize) {
strcpy(omiHttpBuffer + bufLen, writeFooter);
return true;
}
return false;
}
bool createBridgeBootOMI() {
char valueStr[VALUE_LEN];
omiHeader();
omiAddObject("K1");
omiAddObject(FS("Gateways"));
// bridges by mac address
WiFi.macAddress().toCharArray(valueStr, VALUE_LEN);
omiAddObject(valueStr);
omiAddInfoItem(FS("HackVersion"), _BuildInfo.src_version);
omiAddInfoItem(FS("HackBuildTime"), _BuildInfo.time);
omiAddInfoItem(FS("ESPVersion"), _BuildInfo.env_version);
String(ESP.getFreeSketchSpace()).toCharArray(valueStr, VALUE_LEN);
omiAddInfoItem(FS("FreeFlashMemory"), valueStr);
String(ESP.getFreeHeap()).toCharArray(valueStr, VALUE_LEN);
omiAddInfoItem(FS("FreeRAM"), valueStr);
//DBGSTREAM.printf(FS("version: %s\r\n Arduino/ESP version: %s\r\n Build time: %s %s\r\n"),
// _BuildInfo.src_version, _BuildInfo.env_version, _BuildInfo.date, _BuildInfo.time);
//DBGSTREAM.printf(
// FS("[SETUP] Free heap RAM: %u Program flash: Free (for updating): %u Used: %u \r\n")
// , ESP.getFreeHeap(), ESP.getFreeSketchSpace(), ESP.getSketchSize());
return omiFooter();
}
bool createOMI(NodeStr * packetData, uint8_t len) {
char valueStr[VALUE_LEN];
{
yield();
omiHeader();
omiAddObject("K1");
/*
if (packetData.Type == FAIL_OSCILLOSCOPE || packetData.packetLost) {
omiAddObject(FS("Gateways"));
WiFi.macAddress().toCharArray(valueStr, VALUE_LEN);
omiAddObject(valueStr);
String(packetData.Id).toCharArray(valueStr, VALUE_LEN);
char* info = FS("CRC error, from: ");
strcat(info, valueStr);
omiAddInfoItem("PacketLoss", info); //buffer might be used for info, not FS available
return omiFooter();
}
*/ //TODO re-add Gateways object
}
for(uint8_t i = 0; i < len; i++)
{
DBGSTREAM.printf(FS("[OMI-processing] Creating Object. getNodeName(%i).\r\n"), packetData[i].Id);
omiAddObject(getNodeName(packetData[i].Id));
if(packetData[i].threeCount > 0){ //check if we have any values
DBGSTREAM.printf(FS("[OMI-processing] temp,humi,light InfoItem.\r\n"));
DBGSTREAM.printf(FS("[OMI-processing] threeCount: %u \r\n"), packetData[i].threeCount);
for(uint8_t idx = 0; idx < 3; idx++){ //loop temp humi illu values
//do sensor value conversations here
//conversions according to datasheets of Sht2x
int32_t threeValue = 0;
switch(idx){
case TEMPERATURE_I:{
DBGSTREAM.printf(FS("[OMI-processing] temp.\r\n"));
threeValue = 17572; //constant from the datasheets
threeValue *= packetData[i].intValues[idx];
threeValue >>= 14; //divide by 2^14
threeValue -= (4685 * packetData[i].threeCount);
break;
}
case HUMIDITY_I:{
DBGSTREAM.printf(FS("[OMI-processing] humi.\r\n"));
threeValue = 12500; //constant from the datasheets
threeValue *= packetData[i].intValues[idx];
threeValue >>= 12; //divide by 2^14
threeValue -= (600 * packetData[i].threeCount);
break;
}
case LIGHT_I:{
DBGSTREAM.printf(FS("[OMI-processing] light.\r\n"));
threeValue = 6250; //unsure about this constant
threeValue *= packetData[i].intValues[idx];
threeValue >>= 12;
threeValue = threeValue * 1.5 + 0.5; //rounding fix;
break;
}
}
DBGSTREAM.printf(FS("[OMI-processing] intValue: %u\r\n"), packetData[i].intValues[idx]);
if(packetData[i].threeCount == 0) continue;
String(((float) threeValue * 0.01) / packetData[i].threeCount ).toCharArray(valueStr,VALUE_LEN);
omiAddInfoItem(getTypeName(TH20_OSCILLOSCOPE, idx), valueStr);
}
}
if(packetData[i].co2Count > 0){
DBGSTREAM.printf(FS("[OMI-processing] CO2 InfoItem.\r\n"));
String(packetData[i].intValues[3] / packetData[i].co2Count).toCharArray(valueStr,VALUE_LEN);
omiAddInfoItem(getTypeName(CO2S100_OSCILLOSCOPE), valueStr);
}
if(packetData[i].intValues[4] <= 30){
DBGSTREAM.printf(FS("[OMI-processing] PIR InfoItem.\r\n"));
String(packetData[i].intValues[4]).toCharArray(valueStr,VALUE_LEN);
omiAddInfoItem(getTypeName(PIR_OSCILLOSCOPE), valueStr);
}
if(packetData[i].RssiCount > 0){
DBGSTREAM.printf(FS("[OMI-processing] RSSI InfoItem.\r\n"));
String(packetData[i].Rssi / packetData[i].RssiCount).toCharArray(valueStr, VALUE_LEN);
omiAddInfoItem("Rssi", valueStr);
}
omiCloseObject();
}
yield();
return omiFooter(); // should return false if buffer overflowed
}
/*
// Parse the values TODO
{switch (packetData.Type) {
case TH20_OSCILLOSCOPE: {
DBGSTREAM.printf(FS("[OMI-processing] temp,humi,light InfoItem.\r\n"));
if (packetData.Last_seq != 0){
String(packetData.Last_seq).toCharArray(valueStr,VALUE_LEN);
omiAddInfoItem(FS("THI_PacketNo"), valueStr);
}
for (uint8_t idx = 0; idx < 3; ++idx) { // loop temp,humi,illu
// convert to human readable (*100)
String((float)packetData.intValues[idx] * 0.01).toCharArray(valueStr,VALUE_LEN);
omiAddInfoItem(getTypeName(packetData.Type, idx), valueStr); // TODO: select the data
}
break;
}
case PIR_OSCILLOSCOPE: // break;
case CO2S100_OSCILLOSCOPE: // break;
{
if (packetData.Last_seq != 0){
String(packetData.Last_seq).toCharArray(valueStr,VALUE_LEN);
if (packetData.Type == PIR_OSCILLOSCOPE)
omiAddInfoItem(FS("PIR_PacketNo"), valueStr);
else if (packetData.Type == CO2S100_OSCILLOSCOPE)
omiAddInfoItem(FS("CO2_PacketNo"), valueStr);
}
}
default:
{
DBGSTREAM.printf(FS("[OMI-processing] Default InfoItem processing. getTypeName(%i)\r\n"),
packetData.Type);
//XXX TODO FIXME VVVVVVVV
String(packetData.intValues[4]).toCharArray(valueStr,VALUE_LEN);
omiAddInfoItem(getTypeName(packetData.Type), valueStr);
}
}}
}
// Extra data
if (packetData.Rssi != 0) {
String((unsigned int)packetData.Rssi).toCharArray(valueStr,VALUE_LEN);
omiAddInfoItem(FS("Rssi"), valueStr);
}
*/
#ifdef OMI_CERT
bool trySend(HttpsCertificateClient& http) {
#else
bool trySend(HTTPClient& http) {
#endif
yield();
bool isSuccess=false;
{
DBGSTREAM.println(F("[HTTP] begin..."));
// configure traged server and url
// There is support for http basic auth, but it's not very safe without https
#ifdef OMI_CERT
http.begin(OMI_URL, OMI_CERT_FINGERPRINT, client_crt, client_crt_len, client_key, client_key_len);
#else
#warning("Client certificate is disabled!")
http.begin(OMI_URL, OMI_CERT_FINGERPRINT);
#endif
//if(!createOMI(packetData))
// DBGSTREAM.printf(FS("[OMI-processing] Buffer overflowed, O-MI request larger than %u"),
// OMIHttpBufferSize);
DBGSTREAM.printf(FS("[HTTP] POST... with payload: %s\r\n"), omiHttpBuffer);
http.addHeader(FS("Content-Type"), "text/xml");
}
int httpCode = 0;
// start connection and send HTTP header
httpCode = http.POST((uint8_t*)omiHttpBuffer, strlen(omiHttpBuffer));
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
DBGSTREAM.printf(FS("[HTTP] POST... code: %d\r\n"), httpCode);
// Success
if(httpCode == HTTP_CODE_OK) isSuccess=true;
// print result into serial
String payload = http.getString();
DBGSTREAM.println(payload);
} else {
DBGSTREAM.print(F("[HTTP] POST... failed, error: "));
DBGSTREAM.printf(FS("%s\r\n"), http.errorToString(httpCode).c_str());
}
http.end();
return isSuccess;
}