Просмотр исходного кода

Merge branch 'main' into fix/rest-api-before-flow-init

SybexXx 1 месяц назад
Родитель
Сommit
dc271fc63b

+ 8 - 1
Changelog.md

@@ -1,7 +1,14 @@
 # [Unreleased]
 
 ### Core Changes and Bug fixes
-- Fixed a crash (and, after a crash, a boot loop) when the REST API `/json` or `/metrics` is polled before the flow is initialized: both handlers read the post-processing results while they did not exist yet (a NULL pointer) during startup, during the 5-minute init delay that follows any crash, and while `/doinit` rebuilds the flow. They now answer "not yet available" until the flow is initialized, and the underlying accessors no longer dereference a missing post-processing step
+- Fix crash / boot loop when /json or /metrics is polled before the flow is initialized [#4165](https://github.com/jomjol/AI-on-the-edge-device/pull/4165)
+- Fix crash when webhook enabled without configured URL [#4136](https://github.com/jomjol/AI-on-the-edge-device/pull/4136)
+- Fix checkDigitConsistency corrupting sub-digits of large readings [#4145](https://github.com/jomjol/AI-on-the-edge-device/pull/4145)
+- perf(camera): memcpy/memset instead of per-pixel loops in capture path [#4168](https://github.com/jomjol/AI-on-the-edge-device/pull/4168)
+- fix(http): don't terminate HTTP responses twice [#4167](https://github.com/jomjol/AI-on-the-edge-device/pull/4167)
+- Update submodules, Refactor OTA update process and clean up code [#4135](https://github.com/jomjol/AI-on-the-edge-device/pull/4135)
+- Power reset camera fix [#4127](https://github.com/jomjol/AI-on-the-edge-device/pull/4127)
+- Update platform espressif32 to 6.11.0 [#4049](https://github.com/jomjol/AI-on-the-edge-device/pull/4049)
 
 
 # [16.1.0] - 2026-01-11

+ 3 - 15
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -710,8 +710,6 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
         return ESP_OK;
     }
 
-    stbi_uc *p_target;
-    stbi_uc *p_source;
     int channels = 3;
     int width = CCstatus.ImageWidth;
     int height = CCstatus.ImageHeight;
@@ -722,19 +720,9 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
 #endif
 
-    for (int x = 0; x < width; ++x)
-    {
-        for (int y = 0; y < height; ++y)
-        {
-            p_target = _Image->rgb_image + (channels * (y * width + x));
-            p_source = _zwImage->rgb_image + (channels * (y * width + x));
-
-            for (int c = 0; c < channels; c++)
-            {
-                p_target[c] = p_source[c];
-            }
-        }
-    }
+    // Both images are packed RGB888 with the same dimensions: copy the whole frame at once
+    // (the former per-pixel, column-by-column loop took ~2 s on an ESP32-CAM)
+    memcpy(_Image->rgb_image, _zwImage->rgb_image, width * height * channels);
 
     delete _zwImage;
 

+ 2 - 8
code/components/jomjol_fileserver_ota/server_file.cpp

@@ -421,15 +421,12 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
             return ESP_FAIL;
         }
 
-        /* Keep looping till the whole file is sent */
+        /* Keep looping till the whole file is sent (the last, empty chunk terminates the response) */
     } while (chunksize != 0);
 
     /* Close file after sending complete */
     fclose(fd);
     ESP_LOGI(TAG, "File sending complete");
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
     return ESP_OK;
 }
 
@@ -515,15 +512,12 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
             return ESP_FAIL;
         }
 
-        /* Keep looping till the whole file is sent */
+        /* Keep looping till the whole file is sent (the last, empty chunk terminates the response) */
     } while (chunksize != 0);
 
     /* Close file after sending complete */
     fclose(fd);
     ESP_LOGD(TAG, "File sending complete");
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
     return ESP_OK;
 }
 

+ 0 - 5
code/components/jomjol_fileserver_ota/server_ota.cpp

@@ -385,8 +385,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
 
         httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        /* Respond with an empty chunk to signal HTTP response completion */
-        httpd_resp_send_chunk(req, NULL, 0);
 
         ESP_LOGD(TAG, "Done empty directory /firmware");
         return ESP_OK;
@@ -448,7 +446,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
         unzip_file(in, out + "/");
         std::string zw = "Web Interface Update Successfull!\nNo reboot necessary";
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        httpd_resp_sendstr_chunk(req, NULL);
         return ESP_OK;
     }
 
@@ -474,14 +471,12 @@ esp_err_t handler_ota_update(httpd_req_t *req)
         std::string zw = "file deleted\n";
         ESP_LOGD(TAG, "%s", zw.c_str());
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        httpd_resp_send_chunk(req, NULL, 0);
         return ESP_OK;
     }
 
     std::string zw = "ota without parameter - should not be the case!";
     httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
     httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-    httpd_resp_send_chunk(req, NULL, 0);
 
     LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ota without parameter - should not be the case!");
 

+ 4 - 3
code/components/jomjol_flowcontroll/ClassFlowCNNGeneral.cpp

@@ -8,7 +8,9 @@
 #include "CTfLiteClass.h"
 #include "ClassLogFile.h"
 #include "esp_log.h"
+
 #include "../../include/defines.h"
+#include "Helper.h"
 
 static const char* TAG = "CNN";
 
@@ -671,8 +673,7 @@ bool ClassFlowCNNGeneral::doNeuralNetwork(string time) {
                 case Analogue:
                     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Analogue");
                     {
-                        float f1, f2;
-                        f1 = 0; f2 = 0;
+                        float f1 = 0, f2 = 0;
 
                         tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image);        
                         tflite->Invoke();
@@ -680,7 +681,7 @@ bool ClassFlowCNNGeneral::doNeuralNetwork(string time) {
 
                         f1 = tflite->GetOutputValue(0);
                         f2 = tflite->GetOutputValue(1);
-                        float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
+						float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
                               
                         if(GENERAL[n]->ROI[roi]->CCW) {
                             GENERAL[n]->ROI[roi]->result_float = 10 - (result * 10);

+ 7 - 6
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

@@ -1,5 +1,4 @@
 #include "ClassFlowPostProcessing.h"
-#include "Helper.h"
 #include "ClassFlowTakeImage.h"
 #include "ClassLogFile.h"
 
@@ -11,7 +10,9 @@
 #include "time_sntp.h"
 
 #include "esp_log.h"
+
 #include "../../include/defines.h"
+#include "Helper.h"
 
 static const char* TAG = "POSTPROC";
 
@@ -911,7 +912,7 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) {
                     if (NUMBERS[j]->isExtendedResolution) {
                         LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Neg: value=" + std::to_string(NUMBERS[j]->Value) 
                                                     + ", preValue=" + std::to_string(NUMBERS[j]->PreValue) 
-                                                    + ", preToll=" + std::to_string(NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))));
+                                                    + ", preToll=" + std::to_string(NUMBERS[j]->PreValue - (2 / pow(10, NUMBERS[j]->Nachkomma))));
                     } 
 
                     NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " "; 
@@ -1106,11 +1107,11 @@ string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue) {
     return input;
 }
 
-float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
+double ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
     int aktdigit, olddigit;
     int aktdigit_before, olddigit_before;
     int pot, pot_max;
-    float zw;
+    double zw;
     bool no_nulldurchgang = false;
 
     pot = _decilamshift;
@@ -1142,13 +1143,13 @@ float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilams
 
         if (no_nulldurchgang) {
             if (aktdigit != olddigit) {
-                input = input + ((float) (olddigit - aktdigit)) * pow(10, pot);     // New Digit is replaced by old Digit;
+                input = input + ((double)(olddigit - aktdigit)) * pow(10, pot);     // New Digit is replaced by old Digit;
             }
         }
         else {
             // despite zero crossing, digit was not incremented --> add 1
             if (aktdigit == olddigit) {
-                input = input + ((float) (1)) * pow(10, pot);   // add 1 at the point
+                input = input + ((double)(1)) * pow(10, pot);   // add 1 at the point
             }
         }
 			

+ 1 - 1
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h

@@ -30,7 +30,7 @@ protected:
     string ShiftDecimal(string in, int _decShift);
 
     string ErsetzteN(string, double _prevalue);
-    float checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue);
+    double checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue);
 
     void InitNUMBERS();
 	

+ 12 - 3
code/components/jomjol_flowcontroll/ClassFlowWebhook.cpp

@@ -118,10 +118,19 @@ bool ClassFlowWebhook::ReadParameter(FILE* pfile, string& aktparamgraph)
     }
     
     WebhookInit(uri,apikey);
-    WebhookEnable = true;
-    LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Webhook Enabled for Uri " + uri);
 
-    printf("uri:         %s\n", uri.c_str());   
+    if (uri.empty())
+    {
+        WebhookEnable = false;
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Webhook not enabled: no Uri configured");
+    }
+    else
+    {
+        WebhookEnable = true;
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Webhook Enabled for Uri " + uri);
+    }
+
+    printf("uri:         %s\n", uri.c_str());
     return true;
 }
 

+ 0 - 3
code/components/jomjol_flowcontroll/MainFlowControl.cpp

@@ -1520,9 +1520,6 @@ esp_err_t handler_current_date(httpd_req_t *req)
     httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
     httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
 
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_sendstr_chunk(req, NULL);
-
 #ifdef DEBUG_DETAIL_ON
     LogFile.WriteHeapInfo("handler_uptime - End");
 #endif

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

@@ -180,6 +180,132 @@ string getSDCardSectorSize()
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
+bool copy_string(char *dst, size_t dst_size, const char *src)
+{
+	if (dst == nullptr || src == nullptr || dst_size == 0)
+	{
+		return false;
+	}
+
+	size_t len = strlen(src);
+
+	if (len >= dst_size)
+	{
+		dst[0] = '\0';
+		return false; // Target too small
+	}
+
+	memcpy(dst, src, len + 1);
+	return true;
+}
+
+void mem_copy(uint8_t *_target, uint8_t *_source, int _size)
+{
+	if ((_target != nullptr) && (_source != nullptr) && (_size > 0))
+	{
+		uint8_t *dst = _target;
+		uint8_t *src = _source;
+
+		for (int i = 0; i < _size; ++i)
+		{
+			*dst++ = *src++;
+		}
+	}
+}
+
+void mem_copy32(void *_target, const void *_source, size_t _size)
+{
+	if ((_target != nullptr) && (_source != nullptr) && (_size > 0))
+	{
+		uint32_t *d32 = (uint32_t *)_target;
+		const uint32_t *s32 = (const uint32_t *)_source;
+
+		size_t words = _size / 4;
+
+		for (size_t i = 0; i < words; i++)
+		{
+			d32[i] = s32[i];
+		}
+
+		// Copy remaining bytes
+		uint8_t *d8 = (uint8_t *)(d32 + words);
+		const uint8_t *s8 = (const uint8_t *)(s32 + words);
+
+		for (size_t i = 0; i < (_size % 4); i++)
+		{
+			d8[i] = s8[i];
+		}
+	}
+}
+
+float fast_atan2(float y, float x)
+{
+	float abs_y = fabs(y) + 1e-10;
+	float r = (x - abs_y) / (x + abs_y);
+	float angle = 0.78539816339 - 0.78539816339 * r;
+	return (y < 0) ? -angle : angle;
+}
+
+double pow10_double(int pot)
+{
+	static const double pow10_table[] = {
+		1.0,
+		10.0,
+		100.0,
+		1000.0,
+		10000.0,
+		100000.0,
+		1000000.0,
+		10000000.0,
+		100000000.0,
+		1000000000.0};
+
+	if (pot >= 0 && pot < (int)(sizeof(pow10_table) / sizeof(pow10_table[0])))
+	{
+		return pow10_table[pot];
+	}
+
+	// Fallback if pot is larger
+	double result = 1.0;
+	for (int i = 0; i < pot; ++i)
+	{
+		result *= 10.0;
+	}
+
+	return result;
+}
+
+uint32_t pow10_int(int pot)
+{
+	// Lookup table for small exponents 0-9
+	static const uint32_t table[] = {
+		1,		   // 10^0
+		10,		   // 10^1
+		100,	   // 10^2
+		1000,	   // 10^3
+		10000,	   // 10^4
+		100000,	   // 10^5
+		1000000,   // 10^6
+		10000000,  // 10^7
+		100000000, // 10^8
+		1000000000 // 10^9
+	};
+
+	if (pot >= 0 && pot < (int)(sizeof(table) / sizeof(table[0])))
+	{
+		return table[pot];
+	}
+
+	// Fallback for larger pot
+	uint32_t result = 1;
+	for (int i = 0; i < pot; ++i)
+	{
+		result *= 10;
+	}
+
+	return result;
+}
+
 void memCopyGen(uint8_t *_source, uint8_t *_target, int _size)
 {
 	for (int i = 0; i < _size; ++i)

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

@@ -45,6 +45,14 @@ float temperatureRead();
 std::string intToHexString(int _valueInt);
 time_t addDays(time_t startTime, int days);
 
+bool copy_string(char *dst, size_t dst_size, const char *src);
+void mem_copy(uint8_t *_target, uint8_t *_source, int _size);
+void mem_copy32(void *_target, const void *_source, size_t size);
+float fast_atan2(float y, float x);
+
+double pow10_double(int pot); // faster than pow(10, pot)
+uint32_t pow10_int(int pot);  // faster than pow(10, pot)
+
 void memCopyGen(uint8_t* _source, uint8_t* _target, int _size);
 
 std::vector<string> HelperZerlegeZeile(std::string input, std::string _delimiter);

+ 4 - 10
code/components/jomjol_image_proc/CAlignAndCutImage.cpp

@@ -6,7 +6,9 @@
 #include <algorithm>
 #include <esp_log.h>
 #include "psram.h"
+
 #include "../../include/defines.h"
+#include "Helper.h"
 
 static const char* TAG = "c_align_and_cut_image";
 
@@ -57,7 +59,6 @@ bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
 
     delete ft;
 
-
     dx = _temp1->target_x - _temp1->found_x;
     dy = _temp1->target_y - _temp1->found_y;
 
@@ -69,8 +70,8 @@ bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
 
     float w_org, w_ist, d_winkel;
 
-    w_org = atan2(_temp2->found_y - _temp1->found_y, _temp2->found_x - _temp1->found_x);
-    w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
+	w_org = atan2((float)(_temp2->found_y - _temp1->found_y), (float)(_temp2->found_x - _temp1->found_x));
+    w_ist = atan2((float)(r1_y - r0_y), (float)(r1_x - r0_x));
 
     d_winkel = (w_ist - w_org) * 180 / M_PI;
 
@@ -91,13 +92,8 @@ bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
     return (isSimilar1 && isSimilar2);
 }
 
-
-
-
-
 void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
 {
-
     int x2, y2;
 
     x2 = x1 + dx;
@@ -131,7 +127,6 @@ void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int d
     stbi_write_bmp(_template1.c_str(), dx, dy, channels, odata);
 #endif
     
-
     RGBImageRelease();
 
     stbi_image_free(odata);
@@ -174,7 +169,6 @@ void CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *
     _target->RGBImageRelease();
 }
 
-
 CImageBasis* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
 {
     int x2, y2;

+ 4 - 18
code/components/jomjol_image_proc/CImageBasis.cpp

@@ -398,15 +398,8 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
         return;
     }
 
-    stbi_uc* p_source;    
-
-    for (int x = 0; x < width; ++x)
-        for (int y = 0; y < height; ++y)
-        {
-            p_source = rgb_image + (channels * (y * width + x));
-            for (int _channels = 0; _channels < channels; ++_channels)
-                p_source[_channels] = (uint8_t) 0;
-        }
+    // One contiguous memset instead of a per-pixel, column-by-column loop through PSRAM
+    memset(rgb_image, 0, width * height * channels);
 
     RGBImageRelease();
 }
@@ -418,17 +411,10 @@ void CImageBasis::EmptyImage()
         LogFile.WriteHeapInfo("EmptyImage");
     #endif
 
-    stbi_uc* p_source;
-
     RGBImageLock();
 
-    for (int x = 0; x < width; ++x)
-        for (int y = 0; y < height; ++y)
-        {
-            p_source = rgb_image + (channels * (y * width + x));
-            for (int _channels = 0; _channels < channels; ++_channels)
-                p_source[_channels] = (uint8_t) 0;
-        }
+    // One contiguous memset instead of a per-pixel, column-by-column loop through PSRAM
+    memset(rgb_image, 0, width * height * channels);
 
     RGBImageRelease();
 }

+ 26 - 0
code/components/jomjol_webhook/interface_webhook.cpp

@@ -28,6 +28,12 @@ void WebhookInit(std::string _uri, std::string _apiKey)
 
 bool WebhookPublish(std::vector<NumberPost*>* numbers)
 {
+    if (_webhookURI.empty())
+    {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "WebhookPublish aborted: no Uri configured");
+        return false;
+    }
+
     bool numbersWithError = false;
     cJSON *jsonArray = cJSON_CreateArray();
 
@@ -76,6 +82,14 @@ bool WebhookPublish(std::vector<NumberPost*>* numbers)
 
     esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
 
+    if (!http_client)
+    {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "WebhookPublish: failed to initialize HTTP client for Uri " + _webhookURI);
+        cJSON_Delete(jsonArray);
+        free(jsonString);
+        return numbersWithError;
+    }
+
     esp_http_client_set_header(http_client, "Content-Type", "application/json");
     esp_http_client_set_header(http_client, "APIKEY", _webhookApiKey.c_str());
 
@@ -98,6 +112,12 @@ bool WebhookPublish(std::vector<NumberPost*>* numbers)
 }
 
 void WebhookUploadPic(ImageData *Img) {
+    if (_webhookURI.empty())
+    {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "WebhookUploadPic aborted: no Uri configured");
+        return;
+    }
+
     LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting WebhookUploadPic");
 
     std::string fullURI = _webhookURI + "?timestamp=" + std::to_string(_lastTimestamp);
@@ -113,6 +133,12 @@ void WebhookUploadPic(ImageData *Img) {
 
     esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
 
+    if (!http_client)
+    {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "WebhookUploadPic: failed to initialize HTTP client for Uri " + fullURI);
+        return;
+    }
+
     esp_http_client_set_header(http_client, "Content-Type", "image/jpeg");
     esp_http_client_set_header(http_client, "APIKEY", _webhookApiKey.c_str());
 

+ 3 - 10
code/main/server_main.cpp

@@ -290,9 +290,7 @@ esp_err_t hello_main_handler(httpd_req_t *req)
         return ESP_FAIL;
     }
 
-    res = send_file(req, filetosend);
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
+    res = send_file(req, filetosend);   // send_file() already terminates the chunked response
 
     if (res != ESP_OK)
         return res;
@@ -324,13 +322,8 @@ esp_err_t img_tmp_handler(httpd_req_t *req)
     filetosend = filetosend + "/img_tmp/" + std::string(filename);
     ESP_LOGD(TAG, "File to upload: %s", filetosend.c_str());
 
-    esp_err_t res = send_file(req, filetosend); 
-    if (res != ESP_OK)
-        return res;
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
-    return ESP_OK;
+    // send_file() already terminates the chunked response
+    return send_file(req, filetosend);
 }