dataserver.cpp
10.8 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*************************************************************************
* GPS data logger for Freematics Esprit / ESP32
*
* Developed by Stanley Huang <stanley@freematics.com.au>
* Distributed under BSD license
* Visit https://freematics.com/products/esprit for more info
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Implemented HTTP APIs:
* /api/info - device info
* /api/live - live data (OBD/GPS/MEMS)
* /api/control - issue a control command
* /api/list - list of log files
* /api/log/<file #> - raw CSV format log file
* /api/data/<file #>?pid=<PID in hex> - JSON array of PID data
*************************************************************************/
#include <SPI.h>
#include <FS.h>
#include <SD.h>
#include <SPIFFS.h>
#include <httpd.h>
#include <FreematicsPlus.h>
#if defined(ESP32)
#include <WiFi.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
#include <apps/sntp/sntp.h>
#include <esp_spi_flash.h>
#include <esp_err.h>
#else
#error Unsupported board type
#endif
#include "config.h"
#define WIFI_TIMEOUT 5000
extern uint32_t fileid;
extern "C"
{
uint8_t temprature_sens_read();
uint32_t hall_sens_read();
}
#if ENABLE_HTTPD
HttpParam httpParam;
int handlerLiveData(UrlHandlerParam* param);
int handlerControl(UrlHandlerParam* param);
int handlerNMEA(UrlHandlerParam* param);
uint16_t hex2uint16(const char *p);
int handlerInfo(UrlHandlerParam* param)
{
char *buf = param->pucBuffer;
int bufsize = param->bufSize;
int bytes = snprintf(buf, bufsize, "{\"httpd\":{\"uptime\":%lu,\"clients\":%d,\"requests\":%u,\"traffic\":%u},\n",
millis(), httpParam.stats.clientCount, (unsigned int)httpParam.stats.reqCount, (unsigned int)(httpParam.stats.totalSentBytes >> 10));
time_t now;
time(&now);
struct tm timeinfo = { 0 };
localtime_r(&now, &timeinfo);
if (timeinfo.tm_year) {
bytes += snprintf(buf + bytes, bufsize - bytes, "\"rtc\":{\"date\":\"%04u-%02u-%02u\",\"time\":\"%02u:%02u:%02u\"},\n",
timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,
timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
}
int deviceTemp = (int)temprature_sens_read() * 165 / 255 - 40;
bytes += snprintf(buf + bytes, bufsize - bytes, "\"cpu\":{\"temperature\":%d,\"magnetic\":%d},\n",
deviceTemp, hall_sens_read());
#if STORAGE == STORAGE_SPIFFS
bytes += snprintf(buf + bytes, bufsize - bytes, "\"spiffs\":{\"total\":%u,\"used\":%u}",
SPIFFS.totalBytes(), SPIFFS.usedBytes());
#else
bytes += snprintf(buf + bytes, bufsize - bytes, "\"sd\":{\"total\":%llu,\"used\":%llu}",
SD.totalBytes(), SD.usedBytes());
#endif
if (bytes < bufsize - 1) buf[bytes++] = '}';
param->contentLength = bytes;
param->contentType=HTTPFILETYPE_JSON;
return FLAG_DATA_RAW;
}
#if STORAGE != STORAGE_NONE
class LogDataContext {
public:
File file;
uint32_t tsStart;
uint32_t tsEnd;
uint16_t pid;
};
int handlerLogFile(UrlHandlerParam* param)
{
LogDataContext* ctx = (LogDataContext*)param->hs->ptr;
param->contentType = HTTPFILETYPE_TEXT;
if (ctx) {
if (!param->pucBuffer) {
// connection to be closed, final calling, cleanup
ctx->file.close();
delete ctx;
param->hs->ptr = 0;
return 0;
}
} else {
int id = 0;
if (param->pucRequest[0] == '/') {
id = atoi(param->pucRequest + 1);
}
sprintf(param->pucBuffer, "/DATA/%u.CSV", id == 0 ? fileid : id);
ctx = new LogDataContext;
#if STORAGE == STORAGE_SPIFFS
ctx->file = SPIFFS.open(param->pucBuffer, FILE_READ);
#else
ctx->file = SD.open(param->pucBuffer, FILE_READ);
#endif
if (!ctx->file) {
strcat(param->pucBuffer, " not found");
param->contentLength = strlen(param->pucBuffer);
delete ctx;
return FLAG_DATA_RAW;
}
param->hs->ptr = (void*)ctx;
}
if (!ctx->file.available()) {
// EOF
return 0;
}
param->contentLength = ctx->file.readBytes(param->pucBuffer, param->bufSize);
param->contentType = HTTPFILETYPE_TEXT;
Serial.print(param->contentLength);
Serial.println(" bytes read");
return FLAG_DATA_STREAM;
}
int handlerLogData(UrlHandlerParam* param)
{
uint32_t duration = 0;
LogDataContext* ctx = (LogDataContext*)param->hs->ptr;
param->contentType = HTTPFILETYPE_JSON;
if (ctx) {
if (!param->pucBuffer) {
// connection to be closed, final calling, cleanup
ctx->file.close();
delete ctx;
param->hs->ptr = 0;
return 0;
}
} else {
int id = 0;
if (param->pucRequest[0] == '/') {
id = atoi(param->pucRequest + 1);
}
sprintf(param->pucBuffer, "/DATA/%u.CSV", id == 0 ? fileid : id);
ctx = new LogDataContext;
#if STORAGE == STORAGE_SPIFFS
ctx->file = SPIFFS.open(param->pucBuffer, FILE_READ);
#else
ctx->file = SD.open(param->pucBuffer, FILE_READ);
#endif
if (!ctx->file) {
param->contentLength = sprintf(param->pucBuffer, "{\"error\":\"Data file not found\"}");
delete ctx;
return FLAG_DATA_RAW;
}
ctx->pid = mwGetVarValueHex(param->pxVars, "pid", 0);
ctx->tsStart = mwGetVarValueInt(param->pxVars, "start", 0);
ctx->tsEnd = 0xffffffff;
duration = mwGetVarValueInt(param->pxVars, "duration", 0);
if (ctx->tsStart && duration) {
ctx->tsEnd = ctx->tsStart + duration;
duration = 0;
}
param->hs->ptr = (void*)ctx;
// JSON head
param->contentLength = sprintf(param->pucBuffer, "[");
}
int len = 0;
char buf[64];
uint32_t ts = 0;
for (;;) {
int c = ctx->file.read();
if (c == -1) {
if (param->contentLength == 0) {
// EOF
return 0;
}
// JSON tail
if (param->pucBuffer[param->contentLength - 1] == ',') param->contentLength--;
param->pucBuffer[param->contentLength++] = ']';
break;
}
if (c == '\n') {
// line end, process the line
buf[len] = 0;
char *value = strchr(buf, ',');
if (value++) {
uint16_t pid = hex2uint16(buf);
if (pid == 0) {
// timestamp
ts = atoi(value);
if (duration) {
ctx->tsEnd = ts + duration;
duration = 0;
}
} else if (pid == ctx->pid && ts >= ctx->tsStart && ts < ctx->tsEnd) {
// generate json array element
param->contentLength += snprintf(param->pucBuffer + param->contentLength, param->bufSize - param->contentLength,
"[%u,%s],", ts, value);
}
}
len = 0;
if (param->contentLength + 32 > param->bufSize) break;
} else if (len < sizeof(buf) - 1) {
buf[len++] = c;
}
}
return FLAG_DATA_STREAM;
}
int handlerLogList(UrlHandlerParam* param)
{
char *buf = param->pucBuffer;
int bufsize = param->bufSize;
File file;
#if STORAGE == STORAGE_SPIFFS
File root = SPIFFS.open("/");
#elif STORAGE == STORAGE_SD
File root = SD.open("/DATA");
#endif
int n = snprintf(buf, bufsize, "[");
if (root) {
while(file = root.openNextFile()) {
const char *fn = file.name();
if (!strncmp(fn, "/DATA/", 6)) {
fn += 6;
unsigned int size = file.size();
Serial.print(fn);
Serial.print(' ');
Serial.print(size);
Serial.println(" bytes");
unsigned int id = atoi(fn);
if (id) {
n += snprintf(buf + n, bufsize - n, "{\"id\":%u,\"size\":%u",
id, size);
if (id == fileid) {
n += snprintf(buf + n, bufsize - n, ",\"active\":true");
}
n += snprintf(buf + n, bufsize - n, "},");
}
}
}
if (buf[n - 1] == ',') n--;
}
n += snprintf(buf + n, bufsize - n, "]");
param->contentType=HTTPFILETYPE_JSON;
param->contentLength = n;
return FLAG_DATA_RAW;
}
#endif
UrlHandler urlHandlerList[]={
{"api/live", handlerLiveData},
{"api/info", handlerInfo},
{"api/nmea", handlerNMEA},
{"api/control", handlerControl},
#if STORAGE != STORAGE_NONE
{"api/list", handlerLogList},
{"api/data", handlerLogData},
{"api/log", handlerLogFile},
#endif
{0}
};
#endif
void obtainTime()
{
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, (char*)"pool.ntp.org");
sntp_init();
}
void serverProcess(int timeout)
{
#if ENABLE_HTTPD
mwHttpLoop(&httpParam, timeout);
#endif
}
bool serverCheckup(int wifiJoinPeriod)
{
static uint32_t wifiStartTime = 0;
if (WiFi.status() != WL_CONNECTED) {
if (wifiStartTime == 0 || millis() - wifiStartTime > wifiJoinPeriod) {
WiFi.disconnect(false);
#if ENABLE_WIFI_AP && ENABLE_WIFI_STATION
WiFi.mode (WIFI_AP_STA);
#elif ENABLE_WIFI_AP
WiFi.mode (WIFI_AP);
#elif ENABLE_WIFI_STATION
WiFi.mode (WIFI_STA);
#endif
#if ENABLE_WIFI_STATION
Serial.print("Connecting to hotspot (SSID:");
Serial.print(WIFI_SSID);
Serial.println(')');
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
wifiStartTime = millis();
#endif
}
} else {
if (wifiStartTime) {
// just connected
Serial.print("Connected to hotspot. IP:");
Serial.println(WiFi.localIP());
// start mDNS responder
MDNS.begin("gpslogger");
MDNS.addService("http", "tcp", 80);
MDNS.addService("nmea", "tcp", NMEA_TCP_PORT);
wifiStartTime = 0;
}
return true;
}
return false;
}
bool serverSetup(IPAddress& ip)
{
#if ENABLE_WIFI_AP && ENABLE_WIFI_STATION
WiFi.mode (WIFI_AP_STA);
#elif ENABLE_WIFI_AP
WiFi.mode (WIFI_AP);
#elif ENABLE_WIFI_STATION
WiFi.mode (WIFI_STA);
#endif
#if ENABLE_WIFI_AP
WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PASSWORD);
ip = WiFi.softAPIP();
#endif
#if ENABLE_HTTPD
mwInitParam(&httpParam, 80, "/spiffs");
httpParam.pxUrlHandler = urlHandlerList;
if (mwServerStart(&httpParam)) {
return false;
}
#endif
#if ENABLE_WIFI_STATION
obtainTime();
#endif
return true;
}