mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
Feature: upload customization settings from a file #256
This commit is contained in:
@@ -13,7 +13,7 @@ ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs)
|
||||
, _ntpSettingsService(server, fs, &_securitySettingsService)
|
||||
, _ntpStatus(server, &_securitySettingsService)
|
||||
, _otaSettingsService(server, fs, &_securitySettingsService)
|
||||
, _uploadFirmwareService(server, &_securitySettingsService)
|
||||
, _uploadFileService(server, &_securitySettingsService)
|
||||
, _mqttSettingsService(server, fs, &_securitySettingsService)
|
||||
, _mqttStatus(server, &_mqttSettingsService, &_securitySettingsService)
|
||||
, _authenticationService(server, &_securitySettingsService)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include <NTPSettingsService.h>
|
||||
#include <NTPStatus.h>
|
||||
#include <OTASettingsService.h>
|
||||
#include <UploadFirmwareService.h>
|
||||
#include <UploadFileService.h>
|
||||
#include <RestartService.h>
|
||||
#include <SecuritySettingsService.h>
|
||||
#include <SystemStatus.h>
|
||||
@@ -78,7 +78,7 @@ class ESP8266React {
|
||||
NTPSettingsService _ntpSettingsService;
|
||||
NTPStatus _ntpStatus;
|
||||
OTASettingsService _otaSettingsService;
|
||||
UploadFirmwareService _uploadFirmwareService;
|
||||
UploadFileService _uploadFileService;
|
||||
MqttSettingsService _mqttSettingsService;
|
||||
MqttStatus _mqttStatus;
|
||||
AuthenticationService _authenticationService;
|
||||
|
||||
@@ -30,12 +30,12 @@ class FSPersistence {
|
||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
|
||||
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
|
||||
// jsonDocument.shrinkToFit(); // added by proddy
|
||||
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
||||
|
||||
// debug added by Proddy
|
||||
#if defined(EMSESP_DEBUG)
|
||||
#if defined(EMSESP_USE_SERIAL)
|
||||
Serial.println();
|
||||
Serial.printf("Reading file: %s: ", _filePath);
|
||||
serializeJson(jsonDocument, Serial);
|
||||
Serial.println();
|
||||
@@ -49,9 +49,17 @@ class FSPersistence {
|
||||
settingsFile.close();
|
||||
}
|
||||
|
||||
// If we reach here we have not been successful in loading the config,
|
||||
// hard-coded emergency defaults are now applied.
|
||||
// If we reach here we have not been successful in loading the config,
|
||||
// hard-coded emergency defaults are now applied.
|
||||
#if defined(EMSESP_DEBUG)
|
||||
#if defined(EMSESP_USE_SERIAL)
|
||||
Serial.println();
|
||||
Serial.printf("Applying defaults for %s: ", _filePath);
|
||||
Serial.println();
|
||||
#endif
|
||||
#endif
|
||||
applyDefaults();
|
||||
writeToFS(); // added to make sure the initial file is created
|
||||
}
|
||||
|
||||
bool writeToFS() {
|
||||
|
||||
122
lib/framework/UploadFileService.cpp
Normal file
122
lib/framework/UploadFileService.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include <UploadFileService.h>
|
||||
|
||||
using namespace std::placeholders; // for `_1` etc
|
||||
|
||||
static bool is_firmware = false;
|
||||
|
||||
UploadFileService::UploadFileService(AsyncWebServer * server, SecurityManager * securityManager)
|
||||
: _securityManager(securityManager) {
|
||||
server->on(UPLOAD_FILE_PATH,
|
||||
HTTP_POST,
|
||||
std::bind(&UploadFileService::uploadComplete, this, _1),
|
||||
std::bind(&UploadFileService::handleUpload, this, _1, _2, _3, _4, _5, _6));
|
||||
}
|
||||
|
||||
void UploadFileService::handleUpload(AsyncWebServerRequest * request, const String & filename, size_t index, uint8_t * data, size_t len, bool final) {
|
||||
// quit if not authorized
|
||||
Authentication authentication = _securityManager->authenticateRequest(request);
|
||||
if (!AuthenticationPredicates::IS_ADMIN(authentication)) {
|
||||
handleError(request, 403); // send the forbidden response
|
||||
return;
|
||||
}
|
||||
|
||||
// at init
|
||||
if (!index) {
|
||||
// check details of the file, to see if its a valid bin or json file
|
||||
std::string fname(filename.c_str());
|
||||
auto position = fname.find_last_of(".");
|
||||
std::string extension = fname.substr(position + 1);
|
||||
size_t fsize = request->contentLength();
|
||||
|
||||
Serial.printf("Received filename: %s, len: %d, index: %d, ext: %s, fsize: %d", filename.c_str(), len, index, extension.c_str(), fsize);
|
||||
Serial.println();
|
||||
|
||||
if ((extension == "bin") && (fsize > 1500000)) {
|
||||
is_firmware = true;
|
||||
} else if (extension == "json") {
|
||||
is_firmware = false;
|
||||
} else {
|
||||
is_firmware = false;
|
||||
return; // not support file type
|
||||
}
|
||||
|
||||
if (is_firmware) {
|
||||
// it's firmware - initialize the ArduinoOTA updater
|
||||
if (Update.begin(fsize)) {
|
||||
request->onDisconnect(UploadFileService::handleEarlyDisconnect); // success, let's make sure we end the update if the client hangs up
|
||||
} else {
|
||||
#if defined(EMSESP_USE_SERIAL)
|
||||
Update.printError(Serial);
|
||||
#endif
|
||||
handleError(request, 500); // failed to begin, send an error response
|
||||
}
|
||||
} else {
|
||||
// its a normal file, open a new temp file to write the contents too
|
||||
request->_tempFile = LITTLEFS.open(TEMP_FILENAME_PATH, "w");
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_firmware) {
|
||||
if (len) {
|
||||
request->_tempFile.write(data, len); // stream the incoming chunk to the opened file
|
||||
}
|
||||
|
||||
} else {
|
||||
// if we haven't delt with an error, continue with the firmware update
|
||||
if (!request->_tempObject) {
|
||||
if (Update.write(data, len) != len) {
|
||||
#if defined(EMSESP_USE_SERIAL)
|
||||
Update.printError(Serial);
|
||||
#endif
|
||||
handleError(request, 500);
|
||||
}
|
||||
if (final) {
|
||||
if (!Update.end(true)) {
|
||||
#if defined(EMSESP_USE_SERIAL)
|
||||
Update.printError(Serial);
|
||||
#endif
|
||||
handleError(request, 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
|
||||
// did we complete uploading a json file?
|
||||
if (request->_tempFile) {
|
||||
request->_tempFile.close(); // close the file handle as the upload is now done
|
||||
request->onDisconnect(RestartService::restartNow);
|
||||
AsyncWebServerResponse * response = request->beginResponse(200);
|
||||
request->send(response);
|
||||
return;
|
||||
}
|
||||
|
||||
// check if it was a firmware upgrade
|
||||
// if no error, send the success response
|
||||
if (is_firmware && !request->_tempObject) {
|
||||
request->onDisconnect(RestartService::restartNow);
|
||||
AsyncWebServerResponse * response = request->beginResponse(200);
|
||||
request->send(response);
|
||||
return;
|
||||
}
|
||||
|
||||
handleError(request, 403); // send the forbidden response
|
||||
}
|
||||
|
||||
void UploadFileService::handleError(AsyncWebServerRequest * request, int code) {
|
||||
// if we have had an error already, do nothing
|
||||
if (request->_tempObject) {
|
||||
return;
|
||||
}
|
||||
|
||||
// send the error code to the client and record the error code in the temp object
|
||||
request->_tempObject = new int(code);
|
||||
AsyncWebServerResponse * response = request->beginResponse(code);
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
void UploadFileService::handleEarlyDisconnect() {
|
||||
is_firmware = false;
|
||||
Update.abort();
|
||||
}
|
||||
@@ -1,20 +1,23 @@
|
||||
#ifndef UploadFirmwareService_h
|
||||
#define UploadFirmwareService_h
|
||||
#ifndef UploadFileService_h
|
||||
#define UploadFileService_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <Update.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <LITTLEFS.h>
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SecurityManager.h>
|
||||
#include <RestartService.h>
|
||||
|
||||
#define UPLOAD_FIRMWARE_PATH "/rest/uploadFirmware"
|
||||
#define UPLOAD_FILE_PATH "/rest/uploadFile"
|
||||
#define TEMP_FILENAME_PATH "/tmp_upload"
|
||||
|
||||
class UploadFirmwareService {
|
||||
class UploadFileService {
|
||||
public:
|
||||
UploadFirmwareService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||
UploadFileService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||
|
||||
private:
|
||||
SecurityManager * _securityManager;
|
||||
@@ -1,68 +0,0 @@
|
||||
#include <UploadFirmwareService.h>
|
||||
|
||||
using namespace std::placeholders; // for `_1` etc
|
||||
|
||||
UploadFirmwareService::UploadFirmwareService(AsyncWebServer * server, SecurityManager * securityManager)
|
||||
: _securityManager(securityManager) {
|
||||
server->on(UPLOAD_FIRMWARE_PATH,
|
||||
HTTP_POST,
|
||||
std::bind(&UploadFirmwareService::uploadComplete, this, _1),
|
||||
std::bind(&UploadFirmwareService::handleUpload, this, _1, _2, _3, _4, _5, _6));
|
||||
}
|
||||
|
||||
void UploadFirmwareService::handleUpload(AsyncWebServerRequest * request, const String & filename, size_t index, uint8_t * data, size_t len, bool final) {
|
||||
if (!index) {
|
||||
Authentication authentication = _securityManager->authenticateRequest(request);
|
||||
if (AuthenticationPredicates::IS_ADMIN(authentication)) {
|
||||
if (Update.begin(request->contentLength())) {
|
||||
// success, let's make sure we end the update if the client hangs up
|
||||
request->onDisconnect(UploadFirmwareService::handleEarlyDisconnect);
|
||||
} else {
|
||||
// failed to begin, send an error response
|
||||
Update.printError(Serial);
|
||||
handleError(request, 500);
|
||||
}
|
||||
} else {
|
||||
// send the forbidden response
|
||||
handleError(request, 403);
|
||||
}
|
||||
}
|
||||
|
||||
// if we haven't delt with an error, continue with the update
|
||||
if (!request->_tempObject) {
|
||||
if (Update.write(data, len) != len) {
|
||||
Update.printError(Serial);
|
||||
handleError(request, 500);
|
||||
}
|
||||
if (final) {
|
||||
if (!Update.end(true)) {
|
||||
Update.printError(Serial);
|
||||
handleError(request, 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UploadFirmwareService::uploadComplete(AsyncWebServerRequest * request) {
|
||||
// if no error, send the success response
|
||||
if (!request->_tempObject) {
|
||||
request->onDisconnect(RestartService::restartNow);
|
||||
AsyncWebServerResponse * response = request->beginResponse(200);
|
||||
request->send(response);
|
||||
}
|
||||
}
|
||||
|
||||
void UploadFirmwareService::handleError(AsyncWebServerRequest * request, int code) {
|
||||
// if we have had an error already, do nothing
|
||||
if (request->_tempObject) {
|
||||
return;
|
||||
}
|
||||
// send the error code to the client and record the error code in the temp object
|
||||
request->_tempObject = new int(code);
|
||||
AsyncWebServerResponse * response = request->beginResponse(code);
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
void UploadFirmwareService::handleEarlyDisconnect() {
|
||||
Update.abort();
|
||||
}
|
||||
Reference in New Issue
Block a user