-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.cpp
More file actions
706 lines (601 loc) · 23.8 KB
/
web.cpp
File metadata and controls
706 lines (601 loc) · 23.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#include <Arduino.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <HTTPClient.h>
#include <LittleFS.h>
#include "constants.h"
#include "debug.h"
#include "fileSystem.h"
#include "flightLogger.h"
#include "monitor.h"
#include "stateMachine.h"
#include "time.h"
#include "utilities.h"
#include "web.h"
#include "wifi.h"
struct TaskParameters {
web instance;
char type[20];
};
web::web() {
}
void web::disable() {
if (_server == NULL)
return;
_server->end();
}
void web::loop() {
}
void web::setup() {
Serial.println(F("Setup network web server..."));
_server = new AsyncWebServer(80);
Serial.println(F("...network web server setup successful."));
}
void web::start() {
Serial.println(F("Start network web server..."));
configure();
_server->begin();
Serial.println(F("...network web server started successful."));
}
void web::configure() {
_server->onNotFound([this](AsyncWebServerRequest * request) {
this->serverNotFound(request);
});
_server->onFileUpload([this](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
this->serverHandleUpload(request, filename, index, data, len, final);
});
_server->on("/", HTTP_GET, [this](AsyncWebServerRequest * request) {
#ifdef DEBUG
Serial.println(F("\twebserver request...home"));
#endif
// if (server_authenticate(request)) {
// ESP_LOGD(TAG," Auth: Success");
// Serial.println(F("\twebserver request /"));
// request->send(200, "text/plain", "Hello, world");
// request->send(LittleFS, "/index.html", "text/html", false, [this](const String& var) {
// return this->serverProcessor(var);
// });
request->send(LittleFS, "/index.html", "text/html", false, NULL);
});
_server->on("/script.js", HTTP_GET, [this](AsyncWebServerRequest * request) {
#ifdef DEBUG
Serial.println(F("\twebserver request...script.js"));
#endif
request->send(LittleFS, "/script.js", "text/javascript", false, NULL);
});
// Route to load data JSON
_server->on("/data", HTTP_GET, [this](AsyncWebServerRequest *request) {
#ifdef DEBUG
Serial.println(F("\twebserver request...data"));
#endif
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
this->jsonAtmosphere(responseResult);
this->jsonHeader(responseResult);
this->jsonLaunch(responseResult, false);
this->jsonMonitor(responseResult);
this->jsonSamples(responseResult, false);
this->jsonWifi(responseResult);
response->setLength();
request->send(response);
});
// _server->on("^\\/flightLogs\\/([0-9]+)$", HTTP_GET, [](AsyncWebServerRequest * request) {
_server->on("/flightLog", HTTP_GET, [](AsyncWebServerRequest * request) {
// String flightNbrStr = request->pathArg(0);
String flightNbrStr = request->getParam("number")->value();
int flightNbr;
sscanf(flightNbrStr.c_str(), "%d", &flightNbr);
#ifdef DEBUG
Serial.print(F("\twebserver request...flightLogs download #"));
Serial.print(flightNbrStr);
Serial.print(F(", "));
Serial.println(flightNbr);
#endif
bool exists = _flightLogger.instance.exists(flightNbr);
if (exists) {
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
bool success = _flightLogger.instance.readFileAsJson(flightNbr, responseResult);
responseResult["success"] = success;
#ifdef DEBUG
Serial.print(F("\twebserver request...flightLogs download data #"));
Serial.println(flightNbr);
serializeJson(responseResult, Serial);
Serial.println(F(""));
#endif
char szBuf[80];
sprintf(szBuf, "attachment; filename=flight%s.json", flightNbr);
response->addHeader("Content-Disposition", szBuf);
response->addHeader("Connection", "close");
response->setLength();
request->send(response);
}
else {
Serial.print(F("\twebserver request...flightLogs download #"));
Serial.print(flightNbr);
Serial.println(F(" - not found"));
request->send(400, "text/plain", "ERROR : no flight log found");
}
#ifdef DEBUG
Serial.print(F("\twebserver request...flightLogs download #"));
Serial.print(flightNbr);
Serial.println(F(" - finished"));
#endif
});
// _server->on("^\\/flightLogs\\/([0-9]+)$", HTTP_GET, [](AsyncWebServerRequest * request) {
_server->on("/flightLogs/erase/status", HTTP_GET, [this](AsyncWebServerRequest * request) {
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
responseResult["status"] = _eraseFlightTaskStatus;
responseResult["success"] = true;
response->setLength();
request->send(response);
});
AsyncCallbackJsonWebHandler *handlerFlightLogsErase = new AsyncCallbackJsonWebHandler("/flightLogs/erase");
handlerFlightLogsErase->setMethod(HTTP_POST);
handlerFlightLogsErase->onRequest([this](AsyncWebServerRequest *request, JsonVariant &json) {
#ifdef DEBUG
Serial.println(F("\twebserver request...flightlogs erase"));
#endif
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
responseResult["type"] = json["type"];
if (_eraseFlightTaskStatus) {
responseResult["message"] = "Erase action is still running";
responseResult["success"] = false;
response->setLength();
request->send(response);
return;
}
_eraseFlightTaskStatus = true;
_eraseFlightType = json["type"] == "all" ? 1 : json["type"] == "last" ? 2 : 0;
if (_eraseFlightType == 0) {
responseResult["message"] = "Unrecognized type.";
responseResult["success"] = false;
response->setLength();
request->send(response);
return;
}
BaseType_t xReturned = xTaskCreatePinnedToCore(
&web::eraseFlightLogsTaskW, /* Function to implement the task */
"eraseFlightLogsTask", /* Name of the task */
4000, /* Stack size in words */
this, /* Task input parameter */
0, /* Priority of the task */
&_eraseFlightsTaskHandle, /* Task handle. */
0 /* Core where the task should run */
);
responseResult["success"] = true;
response->setLength();
request->send(response);
});
_server->addHandler(handlerFlightLogsErase);
// Route to load flightLogs JSON
_server->on("/flightLogs", HTTP_GET, [this](AsyncWebServerRequest *request) {
#ifdef DEBUG
Serial.println(F("\twebserver request...flightLogs"));
#endif
Serial.println(F("\twebserver request...constructing response..."));
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
Serial.println(F("\twebserver request..getting data..."));
JsonArray flightLogs = responseResult.createNestedArray("flightLogs");
_flightLogger.instance.listAsJson(flightLogs);
responseResult["success"] = true;
#ifdef DEBUG
Serial.println(F("\twebserver request...flightLogs data"));
serializeJson(responseResult, Serial);
Serial.println(F(""));
#endif
response->setLength();
request->send(response);
#ifdef DEBUG
Serial.println(F("\twebserver request...flightLogs - finished"));
#endif
});
// Route to load settings data JSON
_server->on("/settings/data", HTTP_GET, [this](AsyncWebServerRequest *request) {
#ifdef DEBUG
Serial.println(F("\twebserver request...settings data"));
#endif
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
this->jsonHeader(responseResult);
this->jsonLaunch(responseResult, true);
this->jsonSamples(responseResult, true);
this->jsonWifi(responseResult);
#ifdef DEBUG
Serial.println(F(""));
serializeJson(responseResult, Serial);
Serial.println(F(""));
#endif
response->setLength();
request->send(response);
});
_server->on("/settings", HTTP_GET, [this](AsyncWebServerRequest * request) {
#ifdef DEBUG
Serial.println(F("\twebserver request...settings"));
#endif
// if (server_authenticate(request)) {
// ESP_LOGD(TAG," Auth: Success");
// request->send(LittleFS, "/settings.html", "text/html", false, [this](const String& var) {
// return this->serverProcessor(var);
// });
request->send(LittleFS, "/settings.html", "text/html", false, NULL);
});
AsyncCallbackJsonWebHandler *handlerReset = new AsyncCallbackJsonWebHandler("/settings/reset");
handlerReset->setMethod(HTTP_POST);
handlerReset->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) {
#ifdef DEBUG
Serial.println(F("\twebserver request...settings reset"));
#endif
#ifdef DEBUG
Serial.println(F("\twebserver request...settings reset - state"));
#endif
_stateMachine.reset();
#ifdef DEBUG
Serial.println(F("\twebserver request...settings reset - state completed"));
#endif
#ifdef DEBUG
Serial.println(F("\twebserver request...settings reset - wifi"));
#endif
_wifi.reset();
#ifdef DEBUG
Serial.println(F("\twebserver request...settings reset - wifi completed"));
#endif
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
response->setLength();
request->send(response);
});
_server->addHandler(handlerReset);
AsyncCallbackJsonWebHandler *handlerSave = new AsyncCallbackJsonWebHandler("/settings/save");
handlerSave->setMethod(HTTP_POST);
handlerSave->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) {
#ifdef DEBUG
Serial.println(F("\twebserver request...settings save - start"));
#endif
#ifdef DEBUG
Serial.println(F("\twebserver request...settings inbound data"));
serializeJson(json, Serial);
Serial.println(F(""));
const char* wifiPassword = json["wifiPassword"];
debug("wifiPassword", wifiPassword);
const char* wifiSSID = json["wifiSSID"];
debug("wifiSSID", wifiSSID);
int launchDetect = json["launchDetect"];
debug("launchDetect", launchDetect);
int samplesAscent = json["samplesAscent"];
debug("samplesAscent", samplesAscent);
int samplesDescent = json["samplesDescent"];
debug("samplesDescent", samplesDescent);
int samplesGround = json["samplesGround"];
debug("samplesGround", samplesGround);
#endif
#ifdef DEBUG
Serial.println(F("\twebserver request...settings save - state"));
#endif
_stateMachine.save(launchDetect, samplesAscent, samplesDescent, samplesGround);
#ifdef DEBUG
Serial.println(F("\twebserver request...settings save - state completed"));
#endif
#ifdef DEBUG
Serial.println(F("\twebserver request...settings save - wifi"));
#endif
_wifi.save(wifiPassword, wifiSSID);
#ifdef DEBUG
Serial.println(F("\twebserver request...settings save - wifi completed"));
#endif
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
responseResult["success"] = true;
response->setLength();
request->send(response);
#ifdef DEBUG
Serial.println(F("\twebserver request...settings save - finished"));
#endif
});
_server->addHandler(handlerSave);
AsyncCallbackJsonWebHandler *handlerRequestTime = new AsyncCallbackJsonWebHandler("/settings/requestTime");
handlerRequestTime->setMethod(HTTP_POST);
handlerRequestTime->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) {
#ifdef DEBUG
Serial.println(F("\twebserver request...requestTime - start"));
#endif
#ifdef DEBUG
Serial.println(F("\twebserver request...requestTime data"));
serializeJson(json, Serial);
Serial.println(F(""));
#endif
unsigned long epochS = json["epochS"];
debug("epochS", epochS);
int epochMs = json["epochMs"];
debug("epochMs", epochMs);
int hours = json["hours"];
debug("hours", hours);
int minutes = json["minutes"];
debug("minutes", minutes);
int seconds = json["seconds"];
debug("seconds", seconds);
int month = json["month"];
debug("month", month);
int day = json["day"];
debug("day", day);
int year = json["year"];
debug("year", year);
setTime(epochS);
AsyncJsonResponse *response = new AsyncJsonResponse();
JsonObject responseResult = response->getRoot().to<JsonObject>();
responseResult["success"] = true;
#ifdef DEBUG
Serial.println(F("\twebserver request...requestTime data"));
serializeJson(responseResult, Serial);
Serial.println(F(""));
#endif
response->setLength();
request->send(response);
#ifdef DEBUG
Serial.println(F("\twebserver request...requestTime - finished"));
#endif
});
_server->addHandler(handlerRequestTime);
// Route to load style.css file
_server->on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println(F("\twebserver request /style.css"));
request->send(LittleFS, "/style.css", "text/css");
});
// Route to load logo.png file
_server->on("/logo.png", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println(F("\twebserver request /logo.png"));
// //request->send_P(200, "image/png", thzero_altimeters128x128, NULL);
// const size_t imageSize = sizeof thzero_altimeters128x128;
// AsyncWebServerResponse *response = request->beginChunkedResponse("image/bmp", [](uint8_t *buffer, size_t maxLen, size_t alreadySent) -> size_t {
// if (imageSize - alreadySent >= maxLen) {
// memcpy(buffer, thzero_altimeters128x128 + alreadySent, maxLen);
// return maxLen;
// }
// memcpy(buffer, thzero_altimeters128x128 + alreadySent, imageSize - alreadySent);
// return imageSize - alreadySent;
// });
// request->send(response);
request->send(LittleFS, "/logo.png", "image/png");
});
// Route to load battery.png file
_server->on("/battery.png", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println(F("\twebserver request /battery.png"));
request->send(LittleFS, "/battery.png", "image/png");
});
// Route to load transparent.png file
_server->on("/transparent.png", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println(F("\twebserver request /transparent.png"));
request->send(LittleFS, "/transparent.png", "image/png");
});
}
void web::serverNotFound(AsyncWebServerRequest *request) {
// ESP_LOGD(TAG,"Client: %s %s",request->client()->remoteIP().toString().c_str(), request->url().c_str());
request->send(404, "text/plain", "Not found");
}
// // used by server.on functions to discern whether a user has the correct httpapitoken OR is authenticated by username and password
// bool server_authenticate(AsyncWebServerRequest * request) {
// bool isAuthenticated = false;
// if (request->authenticate(config.httpuser.c_str(), config.httppassword.c_str())) {
// ESP_LOGD(TAG,"is authenticated via username and password");
// isAuthenticated = true;
// }
// return isAuthenticated;
// }
void web::serverHandleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
if (filename.endsWith(".bin") ) {
// .bin files uploaded are processed as application firmware updates
serverHandleUploadOTAUpdate(request, filename, index, data, len, final);
return;
}
// non .bin files are uploaded to the LITTLEFS partition
size_t freeBytes = _fileSystem.instance.totalBytes() - _fileSystem.instance.usedBytes();
if (len < freeBytes) {
serverHandleUploadLittleFS(request, filename, index, data, len, final);
return;
}
char szBuf[80];
sprintf(szBuf, "Cannot upload file size %d bytes, as LittleFS free space = %d bytes", len, freeBytes);
Serial.println(szBuf);
request->send(404, "text/plain", "Not enough free space in LittleFS partition.");
}
// handles non .bin file uploads to the LITTLEFS directory
void web::serverHandleUploadLittleFS(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
// make sure authenticated before allowing upload
// if (serverAuthenticate(request)) {
// Serial.println("Auth: Failed");
// return request->requestAuthentication();
// }
char szBuf[80];
sprintf(szBuf, "Client: %s %s",request->client()->remoteIP().toString().c_str(), request->url().c_str());
Serial.println(szBuf);
// open the file on first call
if (index == 0) {
sprintf(szBuf, "Upload Start : %s", filename.c_str());
Serial.println(szBuf);
filename = "/" + filename;
if (LittleFS.exists(filename)) {
bool res = LittleFS.remove(filename);
sprintf(szBuf, "Delete file %s : %s", filename, res == false ? "Error" : "OK");
Serial.println(szBuf);
}
_fileLittleFS = LittleFS.open(filename, FILE_WRITE);
}
if (len) {
// stream the incoming chunk to the opened file
feedWatchdog();
_fileLittleFS.write(data, len);
sprintf(szBuf, "Writing file : %s, index = %d, len = %d", filename.c_str(), index, len);
Serial.println(szBuf);
}
if (final) {
sprintf(szBuf, "Upload Complete : %s, size = %d", filename.c_str(), index+len);
Serial.println(szBuf);
// close the file handle after upload
_fileLittleFS.close();
}
}
// handles OTA firmware update
void web::serverHandleUploadOTAUpdate(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
// // make sure authenticated before allowing upload
// if (server_authenticate(request)) {
// char szBuf[80];
// sprintf(szBuf, "Client: %s %s",request->client()->remoteIP().toString().c_str(), request->url().c_str());
// Serial.println(szBuf);
// ESP_LOGD(TAG,"Client: %s %s",request->client()->remoteIP().toString().c_str(), request->url().c_str());
// if (!index) {
// ESP_LOGD(TAG,"OTA Update Start : %s", filename.c_str());
// if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
// Update.printError(Serial);
// }
// }
// if (len) {
// feedWatchdog();
// // flashing firmware to ESP
// if (Update.write(data, len) != len) {
// Update.printError(Serial);
// }
// ESP_LOGD(TAG,"Writing : %s index = %d len = %d", filename.c_str(), index, len);
// }
// if (final) {
// if (Update.end(true)) { //true to set the size to the current progress
// ESP_LOGD(TAG,"OTA Complete : %s, size = %d", filename.c_str(), index + len);
// }
// else {
// Update.printError(Serial);
// }
// delayMs(1000);
// ESP.restart(); // force reboot after firmware update
// }
// }
// else {
// ESP_LOGD(TAG,"Auth: Failed");
// return request->requestAuthentication();
// }
// }
}
// replace %var% in webpage with dynamically generated string
// String web::serverProcessor(const String& var) {
// // Serial.println(var);
// char temp[23];
// if (var == "TITLE")
// return String(BOARD_NAME);
// if (var == "COPYRIGHT")
// return String(COPYRIGHT);
// if (var == "COPYRIGHT_YEARS")
// return String(COPYRIGHT_YEARS);
// if (var == "VERSION")
// return String(MAJOR_VERSION) + "." + String(MINOR_VERSION);
// if (var == "LAUNCH_DETECT") {
// sprintf(temp, "%d", altitudeOffsetLiftoff);
// return temp;
// return "20"; // TODO
// }
// if (var == "MONITOR_BATTERY_VOLTAGE") {
// return "3.5"; // TODO: determine if battery level if plugged in...
// }
// if (var == "MONITOR_BATTERY_VOLTAGE_MAX") {
// return "3.7"; // TODO: determine if battery level if plugged in...
// }
// if (var == "MONITOR_BATTERY_STATUS")
// #ifdef MONITOR_BATTERY
// return "true"; // TODO: determine if battery is plugged in...
// #else
// return "false";
// #endif
// if (var == "MONITOR_MEMORY_FREE") {
// sprintf(temp, "%u", _monitor.heap());
// return temp;
// }
// if (var == "WIFI_ADDRESS")
// return _wifi.ipAddress();
// if (var == "WIFI_SSID")
// return _wifi.ssid();
// // if (var == "TOTALDATALOG")
// // return server_ui_size(FLASH_SIZE_BYTES);
// // if (var == "USEDDATALOG")
// // return server_ui_size(FlashLogFreeAddress);26
// return "?";
// }
void web::jsonAtmosphere(JsonObject root) {
root["altitudeASL"] = _flightLogger.pressureInitial;
root["pressureAbove"] = _flightLogger.altitudeInitial;
root["pressureAt"] = _flightLogger.pressureInitial; // TODO
root["temperature"] = _flightLogger.temperatureInitial; // TODO
}
void web::jsonHeader(JsonObject root) {
root["title"] = BOARD_NAME;
root["copyright"] = COPYRIGHT;
root["copyrightYears"] = COPYRIGHT_YEARS;
root["version"] = String(MAJOR_VERSION) + "." + String(MINOR_VERSION);
}
void web::jsonLaunch(JsonObject root, bool settings) {
root["launchDetect"] = _stateMachine.launchDetect();
if (settings) {
root["launchDetectValuesDefault"] = (int)ALTITUDE_LIFTOFF;
JsonArray launchDetectValues = root.createNestedArray("launchDetectValues");
_values("launchDetectValues", _stateMachine.launchDetectValues, launchDetectValues, sizeof(_stateMachine.launchDetectValues) / sizeof(_stateMachine.launchDetectValues[0]));
}
}
void web::jsonMonitor(JsonObject root) {
#ifdef MONITOR_BATTERY
root["monitorBatteryStatus"] = true;
#else
root["monitorBatteryStatus"] = false;
#endif
root["monitorBatteryVoltage"] = "3.5";
root["monitorBatteryVoltageMax"] = "3.7";
root["monitorMemoryFree"] = _monitor.heap();
root["fileSystemTotalBytes"] = _fileSystem.instance.totalBytes();
root["fileSystemUsedBytes"] = _fileSystem.instance.usedBytes();
}
void web::jsonSamples(JsonObject root, bool settings) {
root["samplesAscent"] = _stateMachine.sampleRateAirborneAscent();
root["samplesDescent"] = _stateMachine.sampleRateAirborneDescent();
root["samplesGround"] = _stateMachine.sampleRateGround();
if (settings) {
root["sampleRateAirborneAscentValuesDefault"] = (int)SAMPLE_RATE_AIRBORNE_ASCENT;
JsonArray sampleRateAirborneAscentValues = root.createNestedArray("sampleRateAirborneAscentValues");
_values("sampleRateAirborneAscentValues", _stateMachine.sampleRateAirborneAscentValues, sampleRateAirborneAscentValues, sizeof(_stateMachine.sampleRateAirborneAscentValues) / sizeof(_stateMachine.sampleRateAirborneAscentValues[0]));
root["sampleRateAirborneDescentValuesDefault"] = (int)SAMPLE_RATE_AIRBORNE_DESCENT;
JsonArray sampleRateAirborneDecentValues = root.createNestedArray("sampleRateAirborneDecentValues");
_values("sampleRateAirborneDecentValues", _stateMachine.sampleRateAirborneDecentValues, sampleRateAirborneDecentValues, sizeof(_stateMachine.sampleRateAirborneDecentValues) / sizeof(_stateMachine.sampleRateAirborneDecentValues[0]));
root["sampleRateGroundValuesDefault"] = (int)SAMPLE_RATE_GROUND;
JsonArray sampleRateGroundValues = root.createNestedArray("sampleRateGroundValues");
_values("sampleRateGroundValues", _stateMachine.sampleRateGroundValues, sampleRateGroundValues, sizeof(_stateMachine.sampleRateGroundValues) / sizeof(_stateMachine.sampleRateGroundValues[0]));
}
}
void web::jsonWifi(JsonObject root) {
root["wifiAddress"] = _wifi.ipAddress();
root["wifiPassword"] = _wifi.password();
root["wifiSSID"] = _wifi.ssid();
}
void web::_values(char *name, int values[], JsonArray valuesJson, int size) {
Serial.print("_values=");
Serial.println(name);
debug("values.length", size);
for (int i = 0; i < size; i++) {
Serial.print("_values[");
Serial.print(i);
Serial.print("]=");
Serial.println(values[i]);
valuesJson.add(values[i]);
}
}
void web::eraseFlightLogs() {
if (_eraseFlightType == 1)
_flightLogger.instance.eraseFlights();
else if (_eraseFlightType == 2)
_flightLogger.instance.eraseLast();
_eraseFlightType = 0;
_eraseFlightTaskStatus = false;
}
void web::eraseFlightLogsTaskW(void * parameter) {
web* instance = reinterpret_cast<web*>(parameter); // obtain the instance pointer
instance->eraseFlightLogs();
// Delete the task...
vTaskDelete(NULL);
}
web _web;