-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncrementalRecorder.cpp
83 lines (75 loc) · 2.46 KB
/
IncrementalRecorder.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
#define ARDUINOJSON_USE_LONG_LONG 1
#include "IncrementalRecorder.h"
IncrementalRecorder::IncrementalRecorder(String backendUrl, String projectKey, String datasetName, String datasetKey, unsigned long long calcTime){
_backendUrl = backendUrl;
_projectKey = projectKey;
_datasetName = datasetName;
_datasetKey = datasetKey;
_calcTime = calcTime;
ctr = 0;
uploadCounter = 0;
datapoints = new LinkedList<DATAPOINT>();
}
void IncrementalRecorder::uploadData(void *datapoints) {
LinkedList<DATAPOINT> *tmpList = (LinkedList<DATAPOINT>*) datapoints;
http.begin((_backendUrl + ADDDATASETINCREMENT).c_str());
http.addHeader("Content-Type", "application/json");
String jsonString;
DynamicJsonDocument doc(16384);
doc["datasetKey"] = _datasetKey;
JsonArray arr = doc.createNestedArray("data");
for (int i = 0; i < tmpList->size(); i++) {
char strTime[16];
sprintf(strTime, "%lld", tmpList->get(i).time);
String concatData;
concatData.concat(strTime);
concatData.concat(";");
concatData.concat(tmpList->get(i).value);
concatData.concat(";");
concatData.concat(tmpList->get(i).sensorName);
arr.add(concatData);
concatData.~String();
}
serializeJsonPretty(doc, jsonString);
int resCode = http.POST(jsonString);
if (resCode < 0) {
Serial.println(http.errorToString(resCode).c_str());
}
if (!(resCode >= 200 && resCode < 300)) {
Serial.print("Sending data failed: ");
Serial.println(resCode);
}
doc.~BasicJsonDocument();
tmpList->~LinkedList();
jsonString.~String();
uploadCounter--;
vTaskDelete( NULL );
}
void IncrementalRecorder::addDataPoint(unsigned long long dataPointTime, const char* sensorName, double dataPoint) {
datapoints->add(DATAPOINT{dataPoint, dataPointTime, sensorName});
ctr++;
if (ctr > 200) {
uploadCounter++;
xTaskCreate(uploadData, "upload", 10000, datapoints, 2, NULL);
datapoints = new LinkedList<DATAPOINT>();
ctr = 0;
}
}
void IncrementalRecorder::addDataPoint(const char* sensorName, double dataPoint) {
unsigned long long dataPointTime = _calcTime + millis();
addDataPoint(dataPointTime, sensorName, dataPoint);
}
void IncrementalRecorder::onComplete() {
uploadCounter++;
xTaskCreate(uploadData, "upload", 10000, datapoints, 2, NULL);
while(uploadCounter != 0) {
delay(10);
}
uploadCounter = 0;
}
IncrementalRecorder::~IncrementalRecorder() {
datapoints->~LinkedList();
_projectKey.~String();
_datasetKey.~String();
http.~HTTPClient();
}