#include "updatewebserver.h"
const char* UpdateWebserver::m_content =
""
""
"
"
""
""
""
"Update"
""
""
""
""
""
""
"
Update
"
""
""
""
""
""
"";
UpdateWebserver::UpdateWebserver(IPAddress addr, int port) :
ESP8266WebServer(addr, port)
{
init();
}
UpdateWebserver::UpdateWebserver(int port) :
ESP8266WebServer(port)
{
init();
}
UpdateWebserver::~UpdateWebserver()
{
}
void UpdateWebserver::init()
{
on("/update", HTTP_GET, [this]() {
sendHeader("Connection", "close");
send(200, "text/html", m_content);
});
on("/update", HTTP_POST, [this]() {
sendHeader("Connection", "close");
send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, [this]() {
HTTPUpload& upload = this->upload();
if (upload.status == UPLOAD_FILE_START) {
//Serial.setDebugOutput(true);
//Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
//Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
//Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
//Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
//Update.printError(Serial);
}
//Serial.setDebugOutput(false);
}
yield();
});
}