-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAsyncElegantOtaSpiffs.h
80 lines (69 loc) · 2.82 KB
/
AsyncElegantOtaSpiffs.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
/*
* Based on: https://github.com/ayushsharma82/AsyncElegantOTA
* Modified by: Kristian Sloth Lauszus ([email protected])
*/
#ifndef __AsyncElegantOTSpiff_h
#define __AsyncElegantOTSpiff_h
#include <ESPAsyncWebServer.h>
#include <elegantWebpage.h>
#include <Updater.h>
#ifndef U_SPIFFS
// Needed for backwards compatibility: https://github.com/esp8266/Arduino/commit/a389a995fb12459819e33970ec80695f1eaecc58
#define U_SPIFFS U_FS
#endif
class AsyncElegantOtaSpiffs {
public:
AsyncElegantOtaSpiffs(int ledPin = -1, uint8_t ledOn = LOW) : ledPin(ledPin), ledOn(ledOn) {};
void begin(AsyncWebServer *server) {
server->on("/update", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse_P(200, F("text/html"), ELEGANT_HTML, ELEGANT_HTML_SIZE);
response->addHeader(F("Content-Encoding"), F("gzip"));
request->send(response);
});
server->on("/update", HTTP_POST, [this](AsyncWebServerRequest *request) {
// the request handler is triggered after the upload has finished...
// create the response, add header, and send response
AsyncWebServerResponse *response = request->beginResponse(Update.hasError() ? 500 : 200, F("text/plain"), Update.hasError() ? F("FAIL") : F("OK"));
response->addHeader(F("Connection"), F("close"));
response->addHeader(F("Access-Control-Allow-Origin"), F("*"));
request->send(response);
restartRequired = true;
}, [this](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
// Upload handler chunks in data
if (!index) {
size_t content_len = request->contentLength();
int cmd = filename.indexOf(F("spiffs")) > -1 ? U_SPIFFS : U_FLASH;
#if defined(ESP8266)
Update.runAsync(true);
#endif
if (!Update.begin(content_len, cmd, ledPin, ledOn))
Update.printError(Serial);
}
// Write chunked data to the free sketch space
if (Update.write(data, len) != len)
Update.printError(Serial);
if (final) { // If the final flag is set then this is the last frame of data
if (!Update.end(true)) // True to set the size to the current progress
Update.printError(Serial);
}
});
}
void loop() {
if (restartRequired) {
delay(1000);
#if defined(ESP8266)
ESP.restart();
#elif defined(ESP32)
esp_task_wdt_init(1, true);
esp_task_wdt_add(NULL);
while (true)
;
#endif
}
}
private:
bool restartRequired = false;
int ledPin;
uint8_t ledOn;
};
#endif // __AsyncElegantOTSpiff_h