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

fix(flow): lock the status text and PreValue against HTTP/MQTT tasks

- getActStatus()/getActStatusWithTime() returned pointers to strings the
  flow task reassigns at every step. /statusflow (polled by the overview
  page), /value, /editflow and the alg_roi.jpg handler read them in the
  HTTP task, i.e. possibly freed memory. They now return a copy taken
  under a mutex, and all writes go through setStatus().
- /setPreValue and the MQTT set_prevalue command changed NUMBERS and
  wrote prevalue.ini while the flow task could be doing the same in
  post-processing (two writers on the same std::strings, two "w"
  handles on prevalue.ini). SetPreValue() and
  ClassFlowPostProcessing::doFlow() now share a mutex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Victor Gladkikh 1 месяц назад
Родитель
Сommit
1ab8a4a91c

+ 25 - 19
code/components/jomjol_flowcontroll/ClassFlowControll.cpp

@@ -214,8 +214,7 @@ void ClassFlowControll::SetInitialParameter(void)
     flowpostprocessing = NULL;
     disabled = false;
     aktRunNr = 0;
-    aktstatus = "Flow task not yet created";
-    aktstatusWithTime = aktstatus;
+    setStatus("Flow task not yet created", "Flow task not yet created");
 }
 
 bool ClassFlowControll::getIsAutoStart(void)
@@ -308,8 +307,7 @@ ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
 
 void ClassFlowControll::InitFlow(std::string config)
 {
-    aktstatus = "Initialization";
-    aktstatusWithTime = aktstatus;
+    setStatus("Initialization", "Initialization");
 
     //#ifdef ENABLE_MQTT
         //MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization", 1, false); // Right now, not possible -> MQTT Service is going to be started later
@@ -354,20 +352,28 @@ void ClassFlowControll::InitFlow(std::string config)
     fclose(pFile);
 }
 
-std::string* ClassFlowControll::getActStatusWithTime()
+std::string ClassFlowControll::getActStatusWithTime()
 {
-    return &aktstatusWithTime;
+    std::lock_guard<std::mutex> lock(statusMutex);
+    return aktstatusWithTime;
 }
 
-std::string* ClassFlowControll::getActStatus()
+std::string ClassFlowControll::getActStatus()
 {
-    return &aktstatus;
+    std::lock_guard<std::mutex> lock(statusMutex);
+    return aktstatus;
+}
+
+void ClassFlowControll::setStatus(const std::string &_status, const std::string &_statusWithTime)
+{
+    std::lock_guard<std::mutex> lock(statusMutex);
+    aktstatus = _status;
+    aktstatusWithTime = _statusWithTime;
 }
 
 void ClassFlowControll::setActStatus(std::string _aktstatus)
 {
-    aktstatus = _aktstatus;
-    aktstatusWithTime = aktstatus;
+    setStatus(_aktstatus, _aktstatus);
 }
 
 void ClassFlowControll::doFlowTakeImageOnly(string time)
@@ -377,8 +383,8 @@ void ClassFlowControll::doFlowTakeImageOnly(string time)
     for (int i = 0; i < FlowControll.size(); ++i) {
         if (FlowControll[i]->name() == "ClassFlowTakeImage") {
             zw_time = getCurrentTimeString("%H:%M:%S");
-            aktstatus = TranslateAktstatus(FlowControll[i]->name());
-            aktstatusWithTime = aktstatus + " (" + zw_time + ")";
+            std::string status = TranslateAktstatus(FlowControll[i]->name());
+            setStatus(status, status + " (" + zw_time + ")");
             #ifdef ENABLE_MQTT
                 MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, 1, false);
             #endif //ENABLE_MQTT
@@ -409,8 +415,8 @@ bool ClassFlowControll::doFlow(string time)
 
     for (int i = 0; i < FlowControll.size(); ++i) {
         zw_time = getCurrentTimeString("%H:%M:%S");
-        aktstatus = TranslateAktstatus(FlowControll[i]->name());
-        aktstatusWithTime = aktstatus + " (" + zw_time + ")";
+        std::string status = TranslateAktstatus(FlowControll[i]->name());
+        setStatus(status, status + " (" + zw_time + ")");
         LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Status: " + aktstatusWithTime);
         #ifdef ENABLE_MQTT
             MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
@@ -442,8 +448,7 @@ bool ClassFlowControll::doFlow(string time)
     }
 
     zw_time = getCurrentTimeString("%H:%M:%S");
-    aktstatus = "Flow finished";
-    aktstatusWithTime = aktstatus + " (" + zw_time + ")";
+    setStatus("Flow finished", std::string("Flow finished (") + zw_time + ")");
     //LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatusWithTime);
     #ifdef ENABLE_MQTT
         MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
@@ -706,8 +711,9 @@ esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
         }
     }
     else if (_fn == "alg_roi.jpg") {
+        std::string status = getActStatus(); // copy: the flow task changes it while this handler runs
         #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG      // no CImageBasis needed to create alg_roi.jpg (ca. 790kB less RAM)
-            if (aktstatus.find("Initialization (delayed)") != -1) {
+            if (status.find("Initialization (delayed)") != -1) {
                 std::string filename = "/sdcard/html/Flowstate_initialization_delayed.jpg";
                 result = send_file(req, filename);
                 /*    
@@ -738,7 +744,7 @@ esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
                 free(fileBuffer);
                 */
             }
-            else if (aktstatus.find("Initialization") != -1) {
+            else if (status.find("Initialization") != -1) {
                 std::string filename = "/sdcard/html/Flowstate_initialization.jpg";
                 result = send_file(req, filename);
                 /*
@@ -769,7 +775,7 @@ esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
                 free(fileBuffer);
                 */
             }
-            else if (aktstatus.find("Take Image") != -1) {
+            else if (status.find("Take Image") != -1) {
                 if (flowalignment && flowalignment->AlgROI) {
                     std::string filename = "/sdcard/html/Flowstate_take_image.jpg";
                     result = send_file(req, filename);

+ 5 - 2
code/components/jomjol_flowcontroll/ClassFlowControll.h

@@ -4,6 +4,7 @@
 #define CLASSFLOWCONTROLL_H
 
 #include <string>
+#include <mutex>
 
 #include "ClassFlow.h"
 #include "ClassFlowTakeImage.h"
@@ -40,6 +41,8 @@ protected:
 	void SetInitialParameter(void);	
 	std::string aktstatusWithTime;
 	std::string aktstatus;
+	std::mutex statusMutex;	// status is written by the flow task and read by HTTP handlers
+	void setStatus(const std::string &_status, const std::string &_statusWithTime);
 	int aktRunNr;
 
 public:
@@ -73,8 +76,8 @@ public:
 	bool getIsAutoStart();
 	void setAutoStartInterval(long &_interval);
 
-	std::string* getActStatusWithTime();
-	std::string* getActStatus();
+	std::string getActStatusWithTime();
+	std::string getActStatus();
 	void setActStatus(std::string _aktstatus);
 
 	std::vector<HTMLInfo*> GetAllDigit();

+ 2 - 0
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

@@ -102,6 +102,7 @@ string ClassFlowPostProcessing::GetPreValue(std::string _number) {
 }
 
 bool ClassFlowPostProcessing::SetPreValue(double _newvalue, string _numbers, bool _extern) {
+    std::lock_guard<std::mutex> lock(preValueMutex);
     //ESP_LOGD(TAG, "SetPrevalue: %f, %s", zw, _numbers.c_str());
 
     for (int j = 0; j < NUMBERS.size(); ++j) {
@@ -759,6 +760,7 @@ string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift) {
 }
 
 bool ClassFlowPostProcessing::doFlow(string zwtime) {
+    std::lock_guard<std::mutex> lock(preValueMutex);
     string zwvalue;
     time_t imagetime = flowTakeImage->getTimeImageTaken();
 	

+ 3 - 0
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h

@@ -9,11 +9,14 @@
 #include "ClassFlowDefineTypes.h"
 
 #include <string>
+#include <mutex>
 
 class ClassFlowPostProcessing :
     public ClassFlow
 {
 protected:
+    std::mutex preValueMutex;   // PreValue is set from HTTP/MQTT tasks while the flow task post-processes
+
     bool UpdatePreValueINI;
 
     int PreValueAgeStartup; 

+ 14 - 14
code/components/jomjol_flowcontroll/MainFlowControl.cpp

@@ -659,7 +659,7 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
             return ESP_OK;
         }
 
-        std::string *status = flowctrl.getActStatus();
+        std::string status = flowctrl.getActStatus();
         std::string query = std::string(_query);
         //    ESP_LOGD(TAG, "Query: %s, query.c_str());
 
@@ -668,10 +668,10 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
             std::string txt;
             txt = "<body style=\"font-family: arial\">";
 
-            if ((countRounds <= 1) && (*status != std::string("Flow finished")))
+            if ((countRounds <= 1) && (status != std::string("Flow finished")))
             {
                 // First round not completed yet
-                txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + *status + "</h3>\n";
+                txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + status + "</h3>\n";
             }
             else
             {
@@ -692,7 +692,7 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
         {
             std::string txt, zw;
 
-            if ((countRounds <= 1) && (*status != std::string("Flow finished")))
+            if ((countRounds <= 1) && (status != std::string("Flow finished")))
             {
                 // First round not completed yet
                 // Nothing to do
@@ -777,11 +777,11 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
                  * Only show it after the image got taken */
                 txt = "<hr><h3>Full Image (current round)</h3>\n";
 
-                if ((*status == std::string("Initialization")) ||
-                    (*status == std::string("Initialization (delayed)")) ||
-                    (*status == std::string("Take Image")))
+                if ((status == std::string("Initialization")) ||
+                    (status == std::string("Initialization (delayed)")) ||
+                    (status == std::string("Take Image")))
                 {
-                    txt += "<p>Current state: " + *status + "</p>\n";
+                    txt += "<p>Current state: " + status + "</p>\n";
                 }
                 else
                 {
@@ -935,7 +935,7 @@ esp_err_t handler_editflow(httpd_req_t *req)
 
         std::string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
 
-        if ((flowctrl.SetupModeActive || (*flowctrl.getActStatus() == std::string("Flow finished"))) && psram_init_shared_memory_for_take_image_step())
+        if ((flowctrl.SetupModeActive || (flowctrl.getActStatus() == std::string("Flow finished"))) && psram_init_shared_memory_for_take_image_step())
         {
             LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Taking image for Alignment Mark Update...");
 
@@ -958,7 +958,7 @@ esp_err_t handler_editflow(httpd_req_t *req)
         }
         else
         {
-            LogFile.WriteToFile(ESP_LOG_WARN, TAG, std::string("Taking image for Alignment Mark not possible while device") + " is busy with a round (Current State: '" + *flowctrl.getActStatus() + "')!");
+            LogFile.WriteToFile(ESP_LOG_WARN, TAG, std::string("Taking image for Alignment Mark not possible while device") + " is busy with a round (Current State: '" + flowctrl.getActStatus() + "')!");
             zw = "Device Busy";
         }
 
@@ -967,9 +967,9 @@ esp_err_t handler_editflow(httpd_req_t *req)
     }
 
     // wird beim Erstellen eines neuen Referenzbildes aufgerufen
-    std::string *sys_status = flowctrl.getActStatus();
+    std::string sys_status = flowctrl.getActStatus();
 
-    if ((sys_status->c_str() != std::string("Take Image")) && (sys_status->c_str() != std::string("Aligning")))
+    if ((sys_status != "Take Image") && (sys_status != "Aligning"))
     {
         if ((_task.compare("test_take") == 0) || (_task.compare("cam_settings") == 0))
         {
@@ -1453,8 +1453,8 @@ esp_err_t handler_statusflow(httpd_req_t *req)
         ESP_LOGD(TAG, "handler_statusflow: %s", req->uri);
 #endif
 
-        string *zw = flowctrl.getActStatusWithTime();
-        resp_str = zw->c_str();
+        std::string zw = flowctrl.getActStatusWithTime();
+        resp_str = zw.c_str();
 
         httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
     }