-
Notifications
You must be signed in to change notification settings - Fork 2
/
AirCasting.h
228 lines (192 loc) · 7.06 KB
/
AirCasting.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef PM25_NODEMCU_AIRCASTING_H
#define PM25_NODEMCU_AIRCASTING_H
#include "AirCastingConfig.h"
#include "AirCastingSession.h"
#include <ESP8266TrueRandom.h>
#define SERIALIZED_CONTENT(v) \
String __serializedContent; \
v.printTo(__serializedContent); \
return __serializedContent
class AirCasting
{
public:
AirCasting();
String getSessionUUID();
bool push(String date, unsigned int pm2_5);
private:
AirCastingConfig* conf;
AirCastingSession* sess;
String authenticate();
String generateUUID();
String createPayload(String target, String content);
String makeFixedSession(String uuid);
String makeMeasurement(String date, unsigned int pm2_5);
String createFixedSession(String uuid);
String extractJsonField(String field, String httpPayload);
};
AirCasting::AirCasting() {
this->conf = new AirCastingConfig();
this->sess = new AirCastingSession();
}
String AirCasting::extractJsonField(String field, String httpPayload) {
DynamicJsonBuffer jsonReceiveBuffer;
JsonObject& replyRoot = jsonReceiveBuffer.parseObject(httpPayload.c_str());
if (!replyRoot.success()) {
serialUdpDebug("JSON: Parsing failure");
return "";
}
if (!replyRoot.containsKey(field)) {
serialUdpDebug("JSON: No field named " + field + " in reply");
return "";
}
return replyRoot.get<String>(field);
}
String AirCasting::authenticate() {
HTTPClient http;
http.begin(this->conf->login);
http.addHeader("Cache-Control", "no-cache");
http.addHeader("Content-Type", "application/json");
http.addHeader("Connection", "close");
http.setAuthorization(this->conf->user.c_str(), this->conf->pass.c_str());
int rc = http.GET();
String payload = http.getString();
serialUdpDebug("AC:Auth: Code " + String(rc));
if (rc > 0) {
if (rc == HTTP_CODE_OK) {
String token = this->extractJsonField("authentication_token", payload);
serialUdpDebug("AC:Auth: token: " + token);
return token;
}
}
}
String AirCasting::createPayload(String target, String content) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
content.replace('"', '\"');
root[target] = content;
String jsonPayload; root.printTo(jsonPayload);
// DEBUG_SERIAL("AirCasting::createPayload: jsonPayload=" + jsonPayload);
return jsonPayload;
}
String AirCasting::makeFixedSession(String uuid) {
DynamicJsonBuffer jsonBuffer;
JsonObject& session = jsonBuffer.createObject();
session["uuid"] = uuid;
session["streams"] = session.createNestedObject("streams");
session["notes"] = session.createNestedObject("notes");
session["title"] = getSessionTitle();
session["tag_list"] = getSessionTags();
session["description"] = getSessionDescription();
session["calibration"] = 0;
session["contribute"] = true;
session["os_version"] = "ESP8266 0.1";
session["phone_model"] = "NodeMCU-1.0";
session["offset_60_db"] = 0,
// session["location"] = this->conf->location;
session["deleted"] = false;
session["start_time"] = date_ISO8601(now());
session["end_time"] = date_ISO8601(now() + 60);
session["timezone_offset"] = (SECS_PER_MIN * NtpConfig::getInstance()->ntpTZOffset);
session["type"] = "FixedSession";
session["is_indoor"] = false;
session["latitude"] = this->conf->coordinates[0];
session["longitude"] = this->conf->coordinates[1];
SERIALIZED_CONTENT(session);
}
String AirCasting::generateUUID() {
byte uuidNumber[16];
String uuidStr = ESP8266TrueRandom.uuidToString(uuidNumber);
return uuidStr;
}
String AirCasting::createFixedSession(String uuid) {
HTTPClient http;
http.begin(this->conf->session);
http.addHeader("Cache-Control", "no-cache");
http.addHeader("Content-Type", "application/json");
http.addHeader("Connection", "close");
http.setAuthorization(this->sess->token.c_str(), "X");
int rc = http.POST(this->createPayload("session", this->makeFixedSession(uuid)));
String payload = http.getString();
serialUdpDebug("AC:FixedSession: Code " + String(rc));
if (rc > 0) {
if (rc == HTTP_CODE_OK) {
serialUdpDebug("AC:FixedSession: UUID: " + uuid);
return uuid;
}
}
return "";
}
String AirCasting::getSessionUUID() {
/* if the value is there, skip and return it */
if (this->sess->uuid.length() > 0) {
return this->sess->uuid;
} else {
/**
* We need to split into read/write each one doing a fs mount
* because doing getDataStream() call while FS is open would
* end up in weird behavior:
* - unable to open() for writing
* - writing 0 bytes
* - ...
*/
if (this->sess->token.length() == 0 || this->sess->uuid.length() == 0) {
String uuid = this->generateUUID();
this->sess->token = this->authenticate();
this->sess->uuid = this->createFixedSession(uuid);
if (this->sess->writeToFile()) {
return this->sess->uuid;
}
}
}
return "";
}
String AirCasting::makeMeasurement(String date, unsigned int pm2_5) {
DynamicJsonBuffer jsonBuffer;
JsonObject& data = jsonBuffer.createObject();
data["session_uuid"] = this->sess->uuid;
data["sensor_package_name"] = getSensorPackageName();
data["sensor_name"] = getSensorName();
data["measurement_type"] = getMeasureType();
data["measurement_short_type"] = getMeasureShortType();
data["unit_symbol"] = getUnitSymbol();
data["unit_name"] = getUnitName();
data["threshold_very_low"] = 0;
data["threshold_low"] = 12;
data["threshold_medium"] = 35;
data["threshold_high"] = 55;
data["threshold_very_high"] = 150;
JsonArray& measurements = data.createNestedArray("measurements");
JsonObject& measure = measurements.createNestedObject();
measure["latitude"] = this->conf->coordinates[0];
measure["longitude"] = this->conf->coordinates[1];
measure["time"] = date;
measure["timezone_offset"] = (SECS_PER_MIN * NtpConfig::getInstance()->ntpTZOffset);
measure["milliseconds"] = 0;
measure["value"] = pm2_5;
measure["measured_value"] = pm2_5;
SERIALIZED_CONTENT(data);
}
bool AirCasting::push(String date, unsigned int pm2_5) {
HTTPClient http;
http.begin(this->conf->data);
http.addHeader("Cache-Control", "no-cache");
http.addHeader("Content-Type", "application/json");
http.addHeader("Connection", "close");
http.setAuthorization(this->sess->token.c_str(), "X");
int rc = http.POST(this->createPayload("data", this->makeMeasurement(date, pm2_5)));
String payload = http.getString();
serialUdpDebug("AC:push: Code " + String(rc));
if (rc > 0) {
if (rc == HTTP_CODE_OK) {
return true;
} else if (rc == HTTP_CODE_BAD_REQUEST) {
serialUdpDebug("AC:push: invalid session.");
// if we get HTTP/1.1 400 then it is likely the session has died.
// Remove file, we will re-authenticate and create a new session.
bool del = this->sess->removeFile();
serialUdpDebug("AC:push: removed session file: " + String(del));
}
}
return false;
}
#endif // PM25_NODEMCU_AIRCASTING_H