71 lines
3.3 KiB
C++
71 lines
3.3 KiB
C++
#include <ESPMegaDisplayOTA.hpp>
|
|
|
|
ESPMegaDisplayOTA::ESPMegaDisplayOTA(ESPMegaDisplay *display, ESPMegaWebServer *webServer) {
|
|
this->display = display;
|
|
this->server = webServer->getServer();
|
|
char ota_begin_path[100];
|
|
char ota_write_path[100];
|
|
char ota_end_path[100];
|
|
sprintf(ota_begin_path, "%s/ota/begin", base_path);
|
|
sprintf(ota_write_path, "%s/ota/write", base_path);
|
|
sprintf(ota_end_path, "%s/ota/end", base_path);
|
|
this->otaUpdateBeginWebHandler = new AsyncCallbackJsonWebHandler(ota_begin_path, std::bind(&ESPMegaDisplayOTA::otaUpdateBeginHandler, this, std::placeholders::_1, std::placeholders::_2));
|
|
this->otaUpdateWriteWebHandler = new AsyncCallbackJsonWebHandler(ota_write_path, std::bind(&ESPMegaDisplayOTA::otaUpdateWriteHandler, this, std::placeholders::_1, std::placeholders::_2));
|
|
this->otaUpdateEndWebHandler = new AsyncCallbackJsonWebHandler(ota_end_path, std::bind(&ESPMegaDisplayOTA::otaUpdateEndHandler, this, std::placeholders::_1, std::placeholders::_2));
|
|
}
|
|
|
|
void ESPMegaDisplayOTA::begin(const char* base_path) {
|
|
this->server->addHandler(this->otaUpdateBeginWebHandler);
|
|
this->server->addHandler(this->otaUpdateWriteWebHandler);
|
|
this->server->addHandler(this->otaUpdateEndWebHandler);
|
|
}
|
|
|
|
void ESPMegaDisplayOTA::otaUpdateBeginHandler(AsyncWebServerRequest *request, JsonVariant &json) {
|
|
// The content type of the request is application/json
|
|
// The body of the request is a JSON object with the following field:
|
|
// - size: the size of the update
|
|
|
|
// Parse the JSON object
|
|
JsonObject content = json.as<JsonObject>();
|
|
// Check if the size field is present
|
|
if(!content.containsKey("size"))
|
|
return;
|
|
// Get the size field
|
|
this->updateSize = content["size"].as<size_t>();
|
|
// Begin the update
|
|
if(!this->display->beginUpdate(this->updateSize)) {
|
|
// If the update cannot be started, return an error
|
|
request->send(500, "application/json", "{\"status\": \"error\"}");
|
|
} else {
|
|
// If the update can be started, return a success
|
|
request->send(200, "application/json", "{\"status\": \"success\"}");
|
|
}
|
|
|
|
}
|
|
void ESPMegaDisplayOTA::otaUpdateWriteHandler(AsyncWebServerRequest *request, JsonVariant &json) {
|
|
// The content type of the request is application/json
|
|
// The body of the request is a JSON object with the following field:
|
|
// - size: the size of the update in bytes
|
|
// - data: the data to write
|
|
|
|
//Parse the JSON object
|
|
JsonObject content = json.as<JsonObject>();
|
|
// Check if the size and data fields are present
|
|
if(!content.containsKey("size") || !content.containsKey("data"))
|
|
request->send(500, "application/json", "{\"status\": \"error\", \"message\": \"The size or data field is missing\"}");
|
|
return;
|
|
// Get the size field
|
|
size_t size = content["size"].as<size_t>();
|
|
if(size>4096) {
|
|
// If the size is greater than 4096 bytes, return an error
|
|
request->send(500, "application/json", "{\"status\": \"error\", \"message\": \"The size of the update is too big\"}");
|
|
return;
|
|
}
|
|
// Get the data field
|
|
JsonArray data = content["data"].as<JsonArray>();
|
|
// Write the data
|
|
}
|
|
void ESPMegaDisplayOTA::otaUpdateEndHandler(AsyncWebServerRequest *request, JsonVariant &json) {
|
|
|
|
}
|