-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdgeML.cpp
55 lines (49 loc) · 1.52 KB
/
EdgeML.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
#include "EdgeML.h"
Recorder::Recorder(String backendUrl, String deviceApiKey) {
_backendUrl = backendUrl;
_deviceApiKey = deviceApiKey;
}
IncrementalRecorder* Recorder::getIncrementalRecorder(String datasetName){
unsigned long long currentTime;
unsigned long long calcTime;
currentTime = getTime();
calcTime = (unsigned long long) currentTime - millis();
HTTPClient http;
String reqAddr = _backendUrl + INITDATASETINCREMENT;
http.begin(reqAddr.c_str());
http.addHeader("Content-Type", "application/json");
DynamicJsonDocument reqObj(1024);
reqObj["deviceApiKey"] = _deviceApiKey;
reqObj["name"] = datasetName;
String sendObj;
serializeJson(reqObj, sendObj);
http.POST(sendObj);
String res = http.getString();
DynamicJsonDocument resObj(1024);
deserializeJson(resObj, res);
const char* datasetKey = (const char*) resObj["datasetKey"];
IncrementalRecorder* incRec = new IncrementalRecorder(_backendUrl, _deviceApiKey, datasetName, datasetKey, calcTime);
http.~HTTPClient();
sendObj.~String();
reqAddr.~String();
res.~String();
// Will lead to heap corruption error
//reqObj.~BasicJsonDocument();
//resObj.~BasicJsonDocument();
return incRec;
}
unsigned long long Recorder::getTime() {
configTime(0, 0, "pool.ntp.org");
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return(0);
}
time(&now);
return (unsigned long long) now * 1000;
}
Recorder::~Recorder() {
_backendUrl.~String();
_deviceApiKey.~String();
INITDATASETINCREMENT.~String();
}