mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-06 11:36:51 +03:00
Remove html directory on update (#3584)
* delete HTML directory on an update * delete HTML directory on an update * rename html folder * swap HTML folders after extracting * . * . * . * . * . * . * move SD card check, SD card directories setup and update to before the PSRAM init. The update should be as early as possible to allow updates even if the PSRAM or cam fails. * . * . * Update Helper.cpp * Update Helper.h * . --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> Co-authored-by: SybexX <Heinrich-Tuning@web.de>
This commit is contained in:
@@ -61,7 +61,7 @@ struct file_server_data {
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
string SUFFIX_ZW = "_0xge";
|
string SUFFIX_ZW = "_tmp";
|
||||||
|
|
||||||
static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file);
|
static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file);
|
||||||
static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file);
|
static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file);
|
||||||
@@ -911,7 +911,7 @@ void delete_all_in_directory(std::string _directory)
|
|||||||
closedir(dir);
|
closedir(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main, bool _initial_setup)
|
std::string unzip_new(std::string _in_zip_file, std::string _html_tmp, std::string _html_final, std::string _target_bin, std::string _main, bool _initial_setup)
|
||||||
{
|
{
|
||||||
int i, sort_iter;
|
int i, sort_iter;
|
||||||
mz_bool status;
|
mz_bool status;
|
||||||
@@ -993,10 +993,15 @@ std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::st
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
zw = _target_zip + zw;
|
zw = _html_tmp + zw;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// files in the html folder shall be redirected to the temporary html folder
|
||||||
|
if (zw.find(_html_final) == 0) {
|
||||||
|
FindReplace(zw, _html_final, _html_tmp);
|
||||||
|
}
|
||||||
|
|
||||||
string filename_zw = zw + SUFFIX_ZW;
|
string filename_zw = zw + SUFFIX_ZW;
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
void register_server_file_uri(httpd_handle_t server, const char *base_path);
|
void register_server_file_uri(httpd_handle_t server, const char *base_path);
|
||||||
|
|
||||||
void unzip(std::string _in_zip_file, std::string _target_directory);
|
void unzip(std::string _in_zip_file, std::string _target_directory);
|
||||||
std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main = "/sdcard/", bool _initial_setup = false);
|
std::string unzip_new(std::string _in_zip_file, std::string _html_tmp, std::string _html_final, std::string _target_bin, std::string _main = "/sdcard/", bool _initial_setup = false);
|
||||||
|
|
||||||
|
|
||||||
void delete_all_in_directory(std::string _directory);
|
void delete_all_in_directory(std::string _directory);
|
||||||
|
|||||||
@@ -76,20 +76,35 @@ void task_do_Update_ZIP(void *pvParameter)
|
|||||||
|
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "File: " + _file_name_update + " Filetype: " + filetype);
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "File: " + _file_name_update + " Filetype: " + filetype);
|
||||||
|
|
||||||
|
|
||||||
if (filetype == "ZIP")
|
if (filetype == "ZIP")
|
||||||
{
|
{
|
||||||
std::string in, out, outbin, zw, retfirmware;
|
std::string in, outHtml, outHtmlTmp, outHtmlOld, outbin, zw, retfirmware;
|
||||||
|
|
||||||
out = "/sdcard/html";
|
outHtml = "/sdcard/html";
|
||||||
|
outHtmlTmp = "/sdcard/html_tmp";
|
||||||
|
outHtmlOld = "/sdcard/html_old";
|
||||||
outbin = "/sdcard/firmware";
|
outbin = "/sdcard/firmware";
|
||||||
|
|
||||||
retfirmware = unzip_new(_file_name_update, out+"/", outbin+"/", "/sdcard/", initial_setup);
|
/* Remove the old and tmp html folder in case they still exist */
|
||||||
|
removeFolder(outHtmlTmp.c_str(), TAG);
|
||||||
|
removeFolder(outHtmlOld.c_str(), TAG);
|
||||||
|
|
||||||
|
/* Extract the ZIP file. The content of the html folder gets extracted to the temporar folder html-temp. */
|
||||||
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Extracting ZIP file " + _file_name_update + "...");
|
||||||
|
retfirmware = unzip_new(_file_name_update, outHtmlTmp+"/", outHtml+"/", outbin+"/", "/sdcard/", initial_setup);
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Files unzipped.");
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Files unzipped.");
|
||||||
|
|
||||||
|
/* ZIP file got extracted, replace the old html folder with the new one */
|
||||||
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Renaming folder " + outHtml + " to " + outHtmlOld + "...");
|
||||||
|
RenameFolder(outHtml, outHtmlOld);
|
||||||
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Renaming folder " + outHtmlTmp + " to " + outHtml + "...");
|
||||||
|
RenameFolder(outHtmlTmp, outHtml);
|
||||||
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Deleting folder " + outHtmlOld + "...");
|
||||||
|
removeFolder(outHtmlOld.c_str(), TAG);
|
||||||
|
|
||||||
if (retfirmware.length() > 0)
|
if (retfirmware.length() > 0)
|
||||||
{
|
{
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Found firmware.bin");
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Found firmware.bin");
|
||||||
ota_update_task(retfirmware);
|
ota_update_task(retfirmware);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -434,7 +449,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ((filetype == "TFLITE") || (filetype == "TFL"))
|
if ((filetype == "TFLITE") || (filetype == "TFL"))
|
||||||
{
|
{
|
||||||
std::string out = "/sdcard/config/" + getFileFullFileName(fn);
|
std::string out = "/sdcard/config/" + getFileFullFileName(fn);
|
||||||
|
|||||||
@@ -365,19 +365,35 @@ size_t findDelimiterPos(string input, string delimiter)
|
|||||||
|
|
||||||
bool RenameFile(string from, string to)
|
bool RenameFile(string from, string to)
|
||||||
{
|
{
|
||||||
// ESP_LOGI(logTag, "Deleting file: %s", fn.c_str());
|
// ESP_LOGI(logTag, "Renaming File: %s", from.c_str());
|
||||||
/* Delete file */
|
|
||||||
FILE *fpSourceFile = fopen(from.c_str(), "rb");
|
FILE *fpSourceFile = fopen(from.c_str(), "rb");
|
||||||
|
|
||||||
// Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
|
// Sourcefile does not exist otherwise there is a mistake when renaming!
|
||||||
if (!fpSourceFile)
|
if (!fpSourceFile)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "DeleteFile: File %s existiert nicht!", from.c_str());
|
ESP_LOGE(TAG, "RenameFile: File %s does not exist!", from.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fpSourceFile);
|
fclose(fpSourceFile);
|
||||||
|
rename(from.c_str(), to.c_str());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RenameFolder(string from, string to)
|
||||||
|
{
|
||||||
|
// ESP_LOGI(logTag, "Renaming Folder: %s", from.c_str());
|
||||||
|
DIR *fpSourceFolder = opendir(from.c_str());
|
||||||
|
|
||||||
|
// Sourcefolder does not exist otherwise there is a mistake when renaming!
|
||||||
|
if (!fpSourceFolder)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "RenameFolder: Folder %s does not exist!", from.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(fpSourceFolder);
|
||||||
rename(from.c_str(), to.c_str());
|
rename(from.c_str(), to.c_str());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -387,7 +403,7 @@ bool FileExists(string filename)
|
|||||||
{
|
{
|
||||||
FILE *fpSourceFile = fopen(filename.c_str(), "rb");
|
FILE *fpSourceFile = fopen(filename.c_str(), "rb");
|
||||||
|
|
||||||
// Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
|
// Sourcefile does not exist
|
||||||
if (!fpSourceFile)
|
if (!fpSourceFile)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -398,22 +414,36 @@ bool FileExists(string filename)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DeleteFile(string fn)
|
bool FolderExists(string foldername)
|
||||||
{
|
{
|
||||||
// ESP_LOGI(logTag, "Deleting file: %s", fn.c_str());
|
DIR *fpSourceFolder = opendir(foldername.c_str());
|
||||||
/* Delete file */
|
|
||||||
FILE *fpSourceFile = fopen(fn.c_str(), "rb");
|
|
||||||
|
|
||||||
// Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
|
// Sourcefolder does not exist
|
||||||
|
if (!fpSourceFolder)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(fpSourceFolder);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeleteFile(string filename)
|
||||||
|
{
|
||||||
|
// ESP_LOGI(logTag, "Deleting file: %s", filename.c_str());
|
||||||
|
/* Delete file */
|
||||||
|
FILE *fpSourceFile = fopen(filename.c_str(), "rb");
|
||||||
|
|
||||||
|
// Sourcefile does not exist otherwise there is a mistake in copying!
|
||||||
if (!fpSourceFile)
|
if (!fpSourceFile)
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", fn.c_str());
|
ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", filename.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fpSourceFile);
|
fclose(fpSourceFile);
|
||||||
|
unlink(filename.c_str());
|
||||||
unlink(fn.c_str());
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,12 @@ std::size_t file_size(const std::string& file_name);
|
|||||||
void FindReplace(std::string& line, std::string& oldString, std::string& newString);
|
void FindReplace(std::string& line, std::string& oldString, std::string& newString);
|
||||||
|
|
||||||
bool CopyFile(string input, string output);
|
bool CopyFile(string input, string output);
|
||||||
bool DeleteFile(string fn);
|
bool DeleteFile(string filename);
|
||||||
bool RenameFile(string from, string to);
|
bool RenameFile(string from, string to);
|
||||||
|
bool RenameFolder(string from, string to);
|
||||||
bool MakeDir(std::string _what);
|
bool MakeDir(std::string _what);
|
||||||
bool FileExists(string filename);
|
bool FileExists(string filename);
|
||||||
|
bool FolderExists(string foldername);
|
||||||
|
|
||||||
string RundeOutput(double _in, int _anzNachkomma);
|
string RundeOutput(double _in, int _anzNachkomma);
|
||||||
|
|
||||||
|
|||||||
@@ -254,6 +254,35 @@ extern "C" void app_main(void)
|
|||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "==================== Start ======================");
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "==================== Start ======================");
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "=================================================");
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "=================================================");
|
||||||
|
|
||||||
|
// SD card: basic R/W check
|
||||||
|
// ********************************************
|
||||||
|
int iSDCardStatus = SDCardCheckRW();
|
||||||
|
if (iSDCardStatus < 0) {
|
||||||
|
if (iSDCardStatus <= -1 && iSDCardStatus >= -2) { // write error
|
||||||
|
StatusLED(SDCARD_CHECK, 1, true);
|
||||||
|
}
|
||||||
|
else if (iSDCardStatus <= -3 && iSDCardStatus >= -5) { // read error
|
||||||
|
StatusLED(SDCARD_CHECK, 2, true);
|
||||||
|
}
|
||||||
|
else if (iSDCardStatus == -6) { // delete error
|
||||||
|
StatusLED(SDCARD_CHECK, 3, true);
|
||||||
|
}
|
||||||
|
setSystemStatusFlag(SYSTEM_STATUS_SDCARD_CHECK_BAD); // reduced web interface going to be loaded
|
||||||
|
}
|
||||||
|
|
||||||
|
// SD card: Create further mandatory directories (if not already existing)
|
||||||
|
// Correct creation of these folders will be checked with function "SDCardCheckFolderFilePresence"
|
||||||
|
// ********************************************
|
||||||
|
MakeDir("/sdcard/firmware"); // mandatory for OTA firmware update
|
||||||
|
MakeDir("/sdcard/img_tmp"); // mandatory for setting up alignment marks
|
||||||
|
MakeDir("/sdcard/demo"); // mandatory for demo mode
|
||||||
|
MakeDir("/sdcard/config/certs"); // mandatory for mqtt certificates
|
||||||
|
|
||||||
|
// Check for updates
|
||||||
|
// ********************************************
|
||||||
|
CheckOTAUpdate();
|
||||||
|
CheckUpdate();
|
||||||
|
|
||||||
// Init external PSRAM
|
// Init external PSRAM
|
||||||
// ********************************************
|
// ********************************************
|
||||||
esp_err_t PSRAMStatus = esp_psram_init();
|
esp_err_t PSRAMStatus = esp_psram_init();
|
||||||
@@ -352,22 +381,6 @@ extern "C" void app_main(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SD card: basic R/W check
|
|
||||||
// ********************************************
|
|
||||||
int iSDCardStatus = SDCardCheckRW();
|
|
||||||
if (iSDCardStatus < 0) {
|
|
||||||
if (iSDCardStatus <= -1 && iSDCardStatus >= -2) { // write error
|
|
||||||
StatusLED(SDCARD_CHECK, 1, true);
|
|
||||||
}
|
|
||||||
else if (iSDCardStatus <= -3 && iSDCardStatus >= -5) { // read error
|
|
||||||
StatusLED(SDCARD_CHECK, 2, true);
|
|
||||||
}
|
|
||||||
else if (iSDCardStatus == -6) { // delete error
|
|
||||||
StatusLED(SDCARD_CHECK, 3, true);
|
|
||||||
}
|
|
||||||
setSystemStatusFlag(SYSTEM_STATUS_SDCARD_CHECK_BAD); // reduced web interface going to be loaded
|
|
||||||
}
|
|
||||||
|
|
||||||
// Migrate parameter in config.ini to new naming (firmware 15.0 and newer)
|
// Migrate parameter in config.ini to new naming (firmware 15.0 and newer)
|
||||||
// ********************************************
|
// ********************************************
|
||||||
migrateConfiguration();
|
migrateConfiguration();
|
||||||
@@ -380,19 +393,6 @@ extern "C" void app_main(void)
|
|||||||
// ********************************************
|
// ********************************************
|
||||||
setCpuFrequency();
|
setCpuFrequency();
|
||||||
|
|
||||||
// SD card: Create further mandatory directories (if not already existing)
|
|
||||||
// Correct creation of these folders will be checked with function "SDCardCheckFolderFilePresence"
|
|
||||||
// ********************************************
|
|
||||||
MakeDir("/sdcard/firmware"); // mandatory for OTA firmware update
|
|
||||||
MakeDir("/sdcard/img_tmp"); // mandatory for setting up alignment marks
|
|
||||||
MakeDir("/sdcard/demo"); // mandatory for demo mode
|
|
||||||
MakeDir("/sdcard/config/certs"); // mandatory for mqtt certificates
|
|
||||||
|
|
||||||
// Check for updates
|
|
||||||
// ********************************************
|
|
||||||
CheckOTAUpdate();
|
|
||||||
CheckUpdate();
|
|
||||||
|
|
||||||
// Start SoftAP for initial remote setup
|
// Start SoftAP for initial remote setup
|
||||||
// Note: Start AP if no wlan.ini and/or config.ini available, e.g. SD card empty; function does not exit anymore until reboot
|
// Note: Start AP if no wlan.ini and/or config.ini available, e.g. SD card empty; function does not exit anymore until reboot
|
||||||
// ********************************************
|
// ********************************************
|
||||||
|
|||||||
Reference in New Issue
Block a user