Răsfoiți Sursa

Catch empty ref images (#1397)

* slow down constant reboots caused by the flow. With this, after a restart due to exception/panic the first round gets delayed by 5 minutes

* catch empty reference images

* .

Co-authored-by: CaCO3 <caco@ruinelli.ch>
CaCO3 3 ani în urmă
părinte
comite
9f20c126be

+ 5 - 1
code/components/jomjol_flowcontroll/ClassFlowControll.cpp

@@ -34,7 +34,9 @@ static const char* TAG = "FLOW CTRL";
 std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host){
     std::string _classname = "";
     std::string result = "";
-//    ESP_LOGD(TAG, "_stepname: %s", _stepname.c_str());
+
+    ESP_LOGD(TAG, "Step %s start", _stepname.c_str());
+
     if ((_stepname.compare("[MakeImage]") == 0) || (_stepname.compare(";[MakeImage]") == 0)){
         _classname = "ClassFlowMakeImage";
     }
@@ -61,6 +63,8 @@ std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _
             result = FlowControll[i]->getHTMLSingleStep(_host);
         }
 
+    ESP_LOGD(TAG, "Step %s end", _stepname.c_str());
+
     return result;
 }
 

+ 8 - 0
code/components/jomjol_helper/Helper.cpp

@@ -9,6 +9,8 @@
 
 #include <iomanip>
 #include <sstream>
+#include <fstream>
+#include <iostream>
 
 #ifdef __cplusplus
 extern "C" {
@@ -194,6 +196,12 @@ std::string FormatFileName(std::string input)
 }
 
 
+std::size_t file_size(const std::string& file_name) {
+    std::ifstream file(file_name.c_str(),std::ios::in | std::ios::binary);
+    if (!file) return 0;
+    file.seekg (0, std::ios::end);
+    return static_cast<std::size_t>(file.tellg());
+}
 
 
 void FindReplace(std::string& line, std::string& oldString, std::string& newString) {

+ 1 - 0
code/components/jomjol_helper/Helper.h

@@ -7,6 +7,7 @@
 using namespace std;
 
 std::string FormatFileName(std::string input);
+std::size_t file_size(const std::string& file_name);
 void FindReplace(std::string& line, std::string& oldString, std::string& newString);
 
 bool CopyFile(string input, string output);

+ 17 - 1
code/components/jomjol_image_proc/CFindTemplate.cpp

@@ -1,6 +1,7 @@
 #include "CFindTemplate.h"
 
 #include "ClassLogFile.h"
+#include "Helper.h"
 
 #include <esp_log.h>
 
@@ -11,7 +12,19 @@ static const char* TAG = "C FIND TEMPL";
 
 bool CFindTemplate::FindTemplate(RefInfo *_ref)
 {
-    uint8_t* rgb_template = stbi_load(_ref->image_file.c_str(), &tpl_width, &tpl_height, &tpl_bpp, channels);
+    uint8_t* rgb_template;
+
+    if (file_size(_ref->image_file.c_str()) == 0) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _ref->image_file + " is empty!");
+        return false;
+    }
+   
+    rgb_template = stbi_load(_ref->image_file.c_str(), &tpl_width, &tpl_height, &tpl_bpp, channels);
+
+    if (rgb_template == NULL) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to load " + _ref->image_file + "! Is it corrupted?");
+        return false;
+    }
 
 //    ESP_LOGD(TAG, "FindTemplate 01");
 
@@ -71,6 +84,9 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
 #endif
         _ref->found_x = _ref->fastalg_x;
         _ref->found_y = _ref->fastalg_y;
+
+        stbi_image_free(rgb_template);
+        
         return true;
     }
 

+ 12 - 0
code/components/jomjol_image_proc/CImageBasis.cpp

@@ -436,8 +436,20 @@ CImageBasis::CImageBasis(std::string _image)
     long zwld = esp_get_free_heap_size();
     ESP_LOGD(TAG, "freeheapsize before: %ld", zwld);
 
+    if (file_size(_image.c_str()) == 0) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
+        return;
+    }
+
     RGBImageLock();
     rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
+
+    if (rgb_image == NULL) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to load " + _image + "! Is it corrupted?");
+        RGBImageRelease();
+        return;
+    }
+    
     RGBImageRelease();
 
     zwld = esp_get_free_heap_size();