MainFlowControl.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. #include "MainFlowControl.h"
  2. #include <string>
  3. #include <vector>
  4. #include "string.h"
  5. #include "esp_log.h"
  6. #include <esp_timer.h>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include "../../include/defines.h"
  10. #include "Helper.h"
  11. #include "statusled.h"
  12. #include "esp_camera.h"
  13. #include "time_sntp.h"
  14. #include "ClassControllCamera.h"
  15. #include "ClassFlowControll.h"
  16. #include "ClassLogFile.h"
  17. #include "server_GPIO.h"
  18. #include "server_file.h"
  19. #include "server_ota.h"
  20. #include "read_wlanini.h"
  21. #include "connect_wlan.h"
  22. #include "psram.h"
  23. #include "basic_auth.h"
  24. // support IDF 5.x
  25. #ifndef portTICK_RATE_MS
  26. #define portTICK_RATE_MS portTICK_PERIOD_MS
  27. #endif
  28. ClassFlowControll flowctrl;
  29. camera_flow_config_temp_t CFstatus;
  30. TaskHandle_t xHandletask_autodoFlow = NULL;
  31. bool bTaskAutoFlowCreated = false;
  32. // True only while the flow objects are fully built. bTaskAutoFlowCreated is already true during the
  33. // post-panic init delay and while doInit() rebuilds the flow, when flowctrl's post-processing is NULL
  34. // or half-built, so REST handlers that read the results must check this flag instead.
  35. bool bFlowInitialized = false;
  36. bool flowisrunning = false;
  37. long auto_interval = 0;
  38. bool autostartIsEnabled = false;
  39. int countRounds = 0;
  40. bool isPlannedReboot = false;
  41. static const char *TAG = "MAINCTRL";
  42. // #define DEBUG_DETAIL_ON
  43. void CheckIsPlannedReboot(void)
  44. {
  45. FILE *pfile;
  46. if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL)
  47. {
  48. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Initial boot or not a planned reboot");
  49. isPlannedReboot = false;
  50. }
  51. else
  52. {
  53. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot");
  54. DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
  55. isPlannedReboot = true;
  56. }
  57. }
  58. bool getIsPlannedReboot(void)
  59. {
  60. return isPlannedReboot;
  61. }
  62. int getCountFlowRounds(void)
  63. {
  64. return countRounds;
  65. }
  66. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  67. {
  68. return flowctrl.GetJPGStream(_filename, req);
  69. }
  70. esp_err_t GetRawJPG(httpd_req_t *req)
  71. {
  72. return flowctrl.SendRawJPG(req);
  73. }
  74. bool isSetupModusActive(void)
  75. {
  76. return flowctrl.getStatusSetupModus();
  77. }
  78. void DeleteMainFlowTask(void)
  79. {
  80. #ifdef DEBUG_DETAIL_ON
  81. ESP_LOGD(TAG, "DeleteMainFlowTask: xHandletask_autodoFlow: %ld", (long)xHandletask_autodoFlow);
  82. #endif
  83. if (xHandletask_autodoFlow != NULL)
  84. {
  85. vTaskDelete(xHandletask_autodoFlow);
  86. xHandletask_autodoFlow = NULL;
  87. }
  88. #ifdef DEBUG_DETAIL_ON
  89. ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
  90. #endif
  91. }
  92. void doInit(void)
  93. {
  94. bFlowInitialized = false;
  95. #ifdef DEBUG_DETAIL_ON
  96. ESP_LOGD(TAG, "Start flowctrl.InitFlow(config);");
  97. #endif
  98. flowctrl.InitFlow(CONFIG_FILE);
  99. bFlowInitialized = true;
  100. #ifdef DEBUG_DETAIL_ON
  101. ESP_LOGD(TAG, "Finished flowctrl.InitFlow(config);");
  102. #endif
  103. /* GPIO handler has to be initialized before MQTT init to ensure proper topic subscription */
  104. gpio_handler_init();
  105. #ifdef ENABLE_MQTT
  106. flowctrl.StartMQTTService();
  107. #endif // ENABLE_MQTT
  108. }
  109. bool doflow(void)
  110. {
  111. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  112. ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
  113. flowisrunning = true;
  114. flowctrl.doFlow(zw_time);
  115. flowisrunning = false;
  116. #ifdef DEBUG_DETAIL_ON
  117. ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
  118. #endif
  119. return true;
  120. }
  121. esp_err_t setCCstatusToCFstatus(void)
  122. {
  123. CFstatus.CamSensor_id = CCstatus.CamSensor_id;
  124. CFstatus.ImageFrameSize = CCstatus.ImageFrameSize;
  125. CFstatus.ImageContrast = CCstatus.ImageContrast;
  126. CFstatus.ImageBrightness = CCstatus.ImageBrightness;
  127. CFstatus.ImageSaturation = CCstatus.ImageSaturation;
  128. CFstatus.ImageQuality = CCstatus.ImageQuality;
  129. CFstatus.ImageGainceiling = CCstatus.ImageGainceiling;
  130. CFstatus.ImageAgc = CCstatus.ImageAgc;
  131. CFstatus.ImageAec = CCstatus.ImageAec;
  132. CFstatus.ImageHmirror = CCstatus.ImageHmirror;
  133. CFstatus.ImageVflip = CCstatus.ImageVflip;
  134. CFstatus.ImageAwb = CCstatus.ImageAwb;
  135. CFstatus.ImageAec2 = CCstatus.ImageAec2;
  136. CFstatus.ImageAecValue = CCstatus.ImageAecValue;
  137. CFstatus.ImageSpecialEffect = CCstatus.ImageSpecialEffect;
  138. CFstatus.ImageWbMode = CCstatus.ImageWbMode;
  139. CFstatus.ImageAeLevel = CCstatus.ImageAeLevel;
  140. CFstatus.ImageDcw = CCstatus.ImageDcw;
  141. CFstatus.ImageBpc = CCstatus.ImageBpc;
  142. CFstatus.ImageWpc = CCstatus.ImageWpc;
  143. CFstatus.ImageAwbGain = CCstatus.ImageAwbGain;
  144. CFstatus.ImageAgcGain = CCstatus.ImageAgcGain;
  145. CFstatus.ImageRawGma = CCstatus.ImageRawGma;
  146. CFstatus.ImageLenc = CCstatus.ImageLenc;
  147. CFstatus.ImageSharpness = CCstatus.ImageSharpness;
  148. CFstatus.ImageAutoSharpness = CCstatus.ImageAutoSharpness;
  149. CFstatus.ImageDenoiseLevel = CCstatus.ImageDenoiseLevel;
  150. CFstatus.ImageLedIntensity = CCstatus.ImageLedIntensity;
  151. CFstatus.ImageZoomEnabled = CCstatus.ImageZoomEnabled;
  152. CFstatus.ImageZoomOffsetX = CCstatus.ImageZoomOffsetX;
  153. CFstatus.ImageZoomOffsetY = CCstatus.ImageZoomOffsetY;
  154. CFstatus.ImageZoomSize = CCstatus.ImageZoomSize;
  155. CFstatus.WaitBeforePicture = CCstatus.WaitBeforePicture;
  156. return ESP_OK;
  157. }
  158. esp_err_t setCFstatusToCCstatus(void)
  159. {
  160. // CCstatus.CamSensor_id = CFstatus.CamSensor_id;
  161. CCstatus.ImageFrameSize = CFstatus.ImageFrameSize;
  162. CCstatus.ImageContrast = CFstatus.ImageContrast;
  163. CCstatus.ImageBrightness = CFstatus.ImageBrightness;
  164. CCstatus.ImageSaturation = CFstatus.ImageSaturation;
  165. CCstatus.ImageQuality = CFstatus.ImageQuality;
  166. CCstatus.ImageGainceiling = CFstatus.ImageGainceiling;
  167. CCstatus.ImageAgc = CFstatus.ImageAgc;
  168. CCstatus.ImageAec = CFstatus.ImageAec;
  169. CCstatus.ImageHmirror = CFstatus.ImageHmirror;
  170. CCstatus.ImageVflip = CFstatus.ImageVflip;
  171. CCstatus.ImageAwb = CFstatus.ImageAwb;
  172. CCstatus.ImageAec2 = CFstatus.ImageAec2;
  173. CCstatus.ImageAecValue = CFstatus.ImageAecValue;
  174. CCstatus.ImageSpecialEffect = CFstatus.ImageSpecialEffect;
  175. CCstatus.ImageWbMode = CFstatus.ImageWbMode;
  176. CCstatus.ImageAeLevel = CFstatus.ImageAeLevel;
  177. CCstatus.ImageDcw = CFstatus.ImageDcw;
  178. CCstatus.ImageBpc = CFstatus.ImageBpc;
  179. CCstatus.ImageWpc = CFstatus.ImageWpc;
  180. CCstatus.ImageAwbGain = CFstatus.ImageAwbGain;
  181. CCstatus.ImageAgcGain = CFstatus.ImageAgcGain;
  182. CCstatus.ImageRawGma = CFstatus.ImageRawGma;
  183. CCstatus.ImageLenc = CFstatus.ImageLenc;
  184. CCstatus.ImageSharpness = CFstatus.ImageSharpness;
  185. CCstatus.ImageAutoSharpness = CFstatus.ImageAutoSharpness;
  186. CCstatus.ImageDenoiseLevel = CFstatus.ImageDenoiseLevel;
  187. CCstatus.ImageLedIntensity = CFstatus.ImageLedIntensity;
  188. CCstatus.ImageZoomEnabled = CFstatus.ImageZoomEnabled;
  189. CCstatus.ImageZoomOffsetX = CFstatus.ImageZoomOffsetX;
  190. CCstatus.ImageZoomOffsetY = CFstatus.ImageZoomOffsetY;
  191. CCstatus.ImageZoomSize = CFstatus.ImageZoomSize;
  192. CCstatus.WaitBeforePicture = CFstatus.WaitBeforePicture;
  193. return ESP_OK;
  194. }
  195. esp_err_t setCFstatusToCam(void)
  196. {
  197. sensor_t *s = esp_camera_sensor_get();
  198. if (s != NULL)
  199. {
  200. s->set_framesize(s, CFstatus.ImageFrameSize);
  201. // s->set_contrast(s, CFstatus.ImageContrast); // -2 to 2
  202. // s->set_brightness(s, CFstatus.ImageBrightness); // -2 to 2
  203. Camera.SetCamContrastBrightness(s, CFstatus.ImageContrast, CFstatus.ImageBrightness);
  204. s->set_saturation(s, CFstatus.ImageSaturation); // -2 to 2
  205. s->set_quality(s, CFstatus.ImageQuality); // 0 - 63
  206. // s->set_gainceiling(s, CFstatus.ImageGainceiling); // Image gain (GAINCEILING_x2, x4, x8, x16, x32, x64 or x128)
  207. Camera.SetCamGainceiling(s, CFstatus.ImageGainceiling);
  208. s->set_gain_ctrl(s, CFstatus.ImageAgc); // 0 = disable , 1 = enable
  209. s->set_exposure_ctrl(s, CFstatus.ImageAec); // 0 = disable , 1 = enable
  210. s->set_hmirror(s, CFstatus.ImageHmirror); // 0 = disable , 1 = enable
  211. s->set_vflip(s, CFstatus.ImageVflip); // 0 = disable , 1 = enable
  212. s->set_whitebal(s, CFstatus.ImageAwb); // 0 = disable , 1 = enable
  213. s->set_aec2(s, CFstatus.ImageAec2); // 0 = disable , 1 = enable
  214. s->set_aec_value(s, CFstatus.ImageAecValue); // 0 to 1200
  215. // s->set_special_effect(s, CFstatus.ImageSpecialEffect); // 0 to 6 (0 - No Effect, 1 - Negative, 2 - Grayscale, 3 - Red Tint, 4 - Green Tint, 5 - Blue Tint, 6 - Sepia)
  216. Camera.SetCamSpecialEffect(s, CFstatus.ImageSpecialEffect);
  217. s->set_wb_mode(s, CFstatus.ImageWbMode); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
  218. s->set_ae_level(s, CFstatus.ImageAeLevel); // -2 to 2
  219. s->set_dcw(s, CFstatus.ImageDcw); // 0 = disable , 1 = enable
  220. s->set_bpc(s, CFstatus.ImageBpc); // 0 = disable , 1 = enable
  221. s->set_wpc(s, CFstatus.ImageWpc); // 0 = disable , 1 = enable
  222. s->set_awb_gain(s, CFstatus.ImageAwbGain); // 0 = disable , 1 = enable
  223. s->set_agc_gain(s, CFstatus.ImageAgcGain); // 0 to 30
  224. s->set_raw_gma(s, CFstatus.ImageRawGma); // 0 = disable , 1 = enable
  225. s->set_lenc(s, CFstatus.ImageLenc); // 0 = disable , 1 = enable
  226. // s->set_sharpness(s, CFstatus.ImageSharpness); // auto-sharpness is not officially supported, default to 0
  227. Camera.SetCamSharpness(CFstatus.ImageAutoSharpness, CFstatus.ImageSharpness);
  228. s->set_denoise(s, CFstatus.ImageDenoiseLevel); // The OV2640 does not support it, OV3660 and OV5640 (0 to 8)
  229. TickType_t xDelay2 = 100 / portTICK_PERIOD_MS;
  230. vTaskDelay(xDelay2);
  231. return ESP_OK;
  232. }
  233. else
  234. {
  235. return ESP_FAIL;
  236. }
  237. }
  238. esp_err_t handler_get_heap(httpd_req_t *req)
  239. {
  240. #ifdef DEBUG_DETAIL_ON
  241. LogFile.WriteHeapInfo("handler_get_heap - Start");
  242. ESP_LOGD(TAG, "handler_get_heap uri: %s", req->uri);
  243. #endif
  244. std::string zw = "Heap info:<br>" + getESPHeapInfo();
  245. #ifdef TASK_ANALYSIS_ON
  246. char *pcTaskList = (char *)calloc_psram_heap(std::string(TAG) + "->pcTaskList", 1, sizeof(char) * 768, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  247. if (pcTaskList)
  248. {
  249. vTaskList(pcTaskList);
  250. zw = zw + "<br><br>Task info:<br><pre>Name | State | Prio | Lowest stacksize | Creation order | CPU (-1=NoAffinity)<br>" + std::string(pcTaskList) + "</pre>";
  251. free_psram_heap(std::string(TAG) + "->pcTaskList", pcTaskList);
  252. }
  253. else
  254. {
  255. zw = zw + "<br><br>Task info:<br>ERROR - Allocation of TaskList buffer in PSRAM failed";
  256. }
  257. #endif
  258. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  259. if (zw.length() > 0)
  260. {
  261. httpd_resp_send(req, zw.c_str(), zw.length());
  262. }
  263. else
  264. {
  265. httpd_resp_send(req, NULL, 0);
  266. }
  267. #ifdef DEBUG_DETAIL_ON
  268. LogFile.WriteHeapInfo("handler_get_heap - Done");
  269. #endif
  270. return ESP_OK;
  271. }
  272. esp_err_t handler_init(httpd_req_t *req)
  273. {
  274. #ifdef DEBUG_DETAIL_ON
  275. LogFile.WriteHeapInfo("handler_init - Start");
  276. ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
  277. #endif
  278. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  279. // Rebuilding the flow at runtime is not safe: InitFlow() keeps the old ClassFlow objects (a second full
  280. // pipeline, ~1.2 MB PSRAM, until allocations fail and the device panics) and races with a running round.
  281. // Apply the configuration with a reboot instead.
  282. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "/doinit requested -> rebooting to apply the configuration");
  283. const char *resp_str = "Reboot to apply the configuration<br>";
  284. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  285. doReboot();
  286. #ifdef DEBUG_DETAIL_ON
  287. LogFile.WriteHeapInfo("handler_init - Done");
  288. #endif
  289. return ESP_OK;
  290. }
  291. esp_err_t handler_stream(httpd_req_t *req)
  292. {
  293. #ifdef DEBUG_DETAIL_ON
  294. LogFile.WriteHeapInfo("handler_stream - Start");
  295. ESP_LOGD(TAG, "handler_stream uri: %s", req->uri);
  296. #endif
  297. char _query[50];
  298. char _value[10];
  299. bool flashlightOn = false;
  300. if (httpd_req_get_url_query_str(req, _query, 50) == ESP_OK)
  301. {
  302. // ESP_LOGD(TAG, "Query: %s", _query);
  303. if (httpd_query_key_value(_query, "flashlight", _value, 10) == ESP_OK)
  304. {
  305. #ifdef DEBUG_DETAIL_ON
  306. ESP_LOGD(TAG, "flashlight is found%s", _value);
  307. #endif
  308. if (strlen(_value) > 0)
  309. {
  310. flashlightOn = true;
  311. }
  312. }
  313. }
  314. Camera.CaptureToStream(req, flashlightOn);
  315. #ifdef DEBUG_DETAIL_ON
  316. LogFile.WriteHeapInfo("handler_stream - Done");
  317. #endif
  318. return ESP_OK;
  319. }
  320. esp_err_t handler_flow_start(httpd_req_t *req)
  321. {
  322. #ifdef DEBUG_DETAIL_ON
  323. LogFile.WriteHeapInfo("handler_flow_start - Start");
  324. #endif
  325. ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
  326. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  327. if (autostartIsEnabled)
  328. {
  329. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  330. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
  331. const char *resp_str = "The flow is going to be started immediately or is already running";
  332. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  333. }
  334. else
  335. {
  336. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
  337. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow start triggered by REST API, but flow is not active");
  338. }
  339. #ifdef DEBUG_DETAIL_ON
  340. LogFile.WriteHeapInfo("handler_flow_start - Done");
  341. #endif
  342. return ESP_OK;
  343. }
  344. #ifdef ENABLE_MQTT
  345. esp_err_t MQTTCtrlFlowStart(std::string _topic)
  346. {
  347. #ifdef DEBUG_DETAIL_ON
  348. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Start");
  349. #endif
  350. ESP_LOGD(TAG, "MQTTCtrlFlowStart: topic %s", _topic.c_str());
  351. if (autostartIsEnabled)
  352. {
  353. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  354. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by MQTT topic " + _topic);
  355. }
  356. else
  357. {
  358. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by MQTT topic " + _topic + ", but flow is not active!");
  359. }
  360. #ifdef DEBUG_DETAIL_ON
  361. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Done");
  362. #endif
  363. return ESP_OK;
  364. }
  365. #endif // ENABLE_MQTT
  366. esp_err_t handler_json(httpd_req_t *req)
  367. {
  368. #ifdef DEBUG_DETAIL_ON
  369. LogFile.WriteHeapInfo("handler_json - Start");
  370. #endif
  371. ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
  372. if (bTaskAutoFlowCreated && bFlowInitialized)
  373. {
  374. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  375. httpd_resp_set_type(req, "application/json");
  376. std::string zw = flowctrl.getJSON();
  377. if (zw.length() > 0)
  378. {
  379. httpd_resp_send(req, zw.c_str(), zw.length());
  380. }
  381. else
  382. {
  383. httpd_resp_send(req, NULL, 0);
  384. }
  385. }
  386. else
  387. {
  388. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!");
  389. return ESP_ERR_NOT_FOUND;
  390. }
  391. #ifdef DEBUG_DETAIL_ON
  392. LogFile.WriteHeapInfo("handler_JSON - Done");
  393. #endif
  394. return ESP_OK;
  395. }
  396. /**
  397. * Generates a http response containing the OpenMetrics (https://openmetrics.io/) text wire format
  398. * according to https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#text-format.
  399. *
  400. * A MetricFamily with a Metric for each Sequence is provided. If no valid value is available, the metric is not provided.
  401. * MetricPoints are provided without a timestamp. Additional metrics with some device information is also provided.
  402. *
  403. * The metric name prefix is 'ai_on_the_edge_device_'.
  404. *
  405. * example configuration for Prometheus (`prometheus.yml`):
  406. *
  407. * - job_name: watermeter
  408. * static_configs:
  409. * - targets: ['watermeter.fritz.box']
  410. *
  411. */
  412. esp_err_t handler_openmetrics(httpd_req_t *req)
  413. {
  414. #ifdef DEBUG_DETAIL_ON
  415. LogFile.WriteHeapInfo("handler_openmetrics - Start");
  416. #endif
  417. ESP_LOGD(TAG, "handler_openmetrics uri: %s", req->uri);
  418. if (bTaskAutoFlowCreated && bFlowInitialized)
  419. {
  420. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  421. httpd_resp_set_type(req, "text/plain"); // application/openmetrics-text is not yet supported by prometheus so we use text/plain for now
  422. const string metricNamePrefix = "ai_on_the_edge_device";
  423. // get current measurement (flow)
  424. string response = createSequenceMetrics(metricNamePrefix, flowctrl.getNumbers());
  425. // CPU Temperature
  426. response += createMetric(metricNamePrefix + "_cpu_temperature_celsius", "current cpu temperature in celsius", "gauge", std::to_string((int)temperatureRead()));
  427. // WiFi signal strength
  428. response += createMetric(metricNamePrefix + "_rssi_dbm", "current WiFi signal strength in dBm", "gauge", std::to_string(get_WIFI_RSSI()));
  429. // memory info
  430. response += createMetric(metricNamePrefix + "_memory_heap_free_bytes", "available heap memory", "gauge", std::to_string(getESPHeapSize()));
  431. // device uptime
  432. response += createMetric(metricNamePrefix + "_uptime_seconds", "device uptime in seconds", "gauge", std::to_string((long)getUpTime()));
  433. // data aquisition round
  434. response += createMetric(metricNamePrefix + "_rounds_total", "data aquisition rounds since device startup", "counter", std::to_string(countRounds));
  435. // the response always contains at least the metadata (HELP, TYPE) for the MetricFamily so no length check is needed
  436. httpd_resp_send(req, response.c_str(), response.length());
  437. }
  438. else
  439. {
  440. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /metrics not yet available!");
  441. return ESP_ERR_NOT_FOUND;
  442. }
  443. #ifdef DEBUG_DETAIL_ON
  444. LogFile.WriteHeapInfo("handler_openmetrics - Done");
  445. #endif
  446. return ESP_OK;
  447. }
  448. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  449. {
  450. #ifdef DEBUG_DETAIL_ON
  451. LogFile.WriteHeapInfo("handler water counter - Start");
  452. #endif
  453. if (bTaskAutoFlowCreated)
  454. {
  455. bool _rawValue = false;
  456. bool _noerror = false;
  457. bool _all = false;
  458. std::string _type = "value";
  459. std::string zw;
  460. ESP_LOGD(TAG, "handler water counter uri: %s", req->uri);
  461. char _query[100];
  462. char _size[10];
  463. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  464. {
  465. // ESP_LOGD(TAG, "Query: %s", _query);
  466. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  467. {
  468. #ifdef DEBUG_DETAIL_ON
  469. ESP_LOGD(TAG, "all is found%s", _size);
  470. #endif
  471. _all = true;
  472. }
  473. if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
  474. {
  475. #ifdef DEBUG_DETAIL_ON
  476. ESP_LOGD(TAG, "all is found: %s", _size);
  477. #endif
  478. _type = std::string(_size);
  479. }
  480. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  481. {
  482. #ifdef DEBUG_DETAIL_ON
  483. ESP_LOGD(TAG, "rawvalue is found: %s", _size);
  484. #endif
  485. _rawValue = true;
  486. }
  487. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  488. {
  489. #ifdef DEBUG_DETAIL_ON
  490. ESP_LOGD(TAG, "noerror is found: %s", _size);
  491. #endif
  492. _noerror = true;
  493. }
  494. }
  495. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  496. if (_all)
  497. {
  498. httpd_resp_set_type(req, "text/plain");
  499. ESP_LOGD(TAG, "TYPE: %s", _type.c_str());
  500. int _intype = READOUT_TYPE_VALUE;
  501. if (_type == "prevalue")
  502. {
  503. _intype = READOUT_TYPE_PREVALUE;
  504. }
  505. if (_type == "raw")
  506. {
  507. _intype = READOUT_TYPE_RAWVALUE;
  508. }
  509. if (_type == "error")
  510. {
  511. _intype = READOUT_TYPE_ERROR;
  512. }
  513. zw = flowctrl.getReadoutAll(_intype);
  514. ESP_LOGD(TAG, "ZW: %s", zw.c_str());
  515. if (zw.length() > 0)
  516. {
  517. httpd_resp_send(req, zw.c_str(), zw.length());
  518. }
  519. return ESP_OK;
  520. }
  521. std::string status = flowctrl.getActStatus();
  522. std::string query = std::string(_query);
  523. // ESP_LOGD(TAG, "Query: %s, query.c_str());
  524. if (query.find("full") != std::string::npos)
  525. {
  526. std::string txt;
  527. txt = "<body style=\"font-family: arial\">";
  528. if ((countRounds <= 1) && (status != std::string("Flow finished")))
  529. {
  530. // First round not completed yet
  531. txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + status + "</h3>\n";
  532. }
  533. else
  534. {
  535. txt += "<h3>Value</h3>";
  536. }
  537. httpd_resp_sendstr_chunk(req, txt.c_str());
  538. }
  539. zw = flowctrl.getReadout(_rawValue, _noerror, 0);
  540. if (zw.length() > 0)
  541. {
  542. httpd_resp_sendstr_chunk(req, zw.c_str());
  543. }
  544. if (query.find("full") != std::string::npos)
  545. {
  546. std::string txt, zw;
  547. if ((countRounds <= 1) && (status != std::string("Flow finished")))
  548. {
  549. // First round not completed yet
  550. // Nothing to do
  551. }
  552. else
  553. {
  554. /* Digit ROIs */
  555. txt = "<body style=\"font-family: arial\">";
  556. txt += "<hr><h3>Recognized Digit ROIs (previous round)</h3>\n";
  557. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  558. std::vector<HTMLInfo *> htmlinfodig;
  559. htmlinfodig = flowctrl.GetAllDigit();
  560. for (int i = 0; i < htmlinfodig.size(); ++i)
  561. {
  562. if (flowctrl.GetTypeDigit() == Digit)
  563. {
  564. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  565. if ((htmlinfodig[i]->val >= 10) || (htmlinfodig[i]->val < 0))
  566. {
  567. zw = "NaN";
  568. }
  569. else
  570. {
  571. zw = std::to_string((int)htmlinfodig[i]->val);
  572. }
  573. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  574. }
  575. else
  576. {
  577. std::stringstream stream;
  578. stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
  579. zw = stream.str();
  580. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  581. if ((std::stod(zw) >= 10) || (std::stod(zw) < 0))
  582. {
  583. zw = "NaN";
  584. }
  585. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  586. }
  587. delete htmlinfodig[i];
  588. }
  589. htmlinfodig.clear();
  590. txt += "</tr></table>\n";
  591. httpd_resp_sendstr_chunk(req, txt.c_str());
  592. /* Analog ROIs */
  593. txt = "<hr><h3>Recognized Analog ROIs (previous round)</h3>\n";
  594. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  595. std::vector<HTMLInfo *> htmlinfoana;
  596. htmlinfoana = flowctrl.GetAllAnalog();
  597. for (int i = 0; i < htmlinfoana.size(); ++i)
  598. {
  599. std::stringstream stream;
  600. stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
  601. zw = stream.str();
  602. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  603. if ((std::stod(zw) >= 10) || (std::stod(zw) < 0))
  604. {
  605. zw = "NaN";
  606. }
  607. txt += "<td style=\"width: 150px;\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"></p></td>\n";
  608. delete htmlinfoana[i];
  609. }
  610. htmlinfoana.clear();
  611. txt += "</tr>\n</table>\n";
  612. httpd_resp_sendstr_chunk(req, txt.c_str());
  613. /* Full Image
  614. * Only show it after the image got taken */
  615. txt = "<hr><h3>Full Image (current round)</h3>\n";
  616. if ((status == std::string("Initialization")) ||
  617. (status == std::string("Initialization (delayed)")) ||
  618. (status == std::string("Take Image")))
  619. {
  620. txt += "<p>Current state: " + status + "</p>\n";
  621. }
  622. else
  623. {
  624. txt += "<img src=\"/img_tmp/alg_roi.jpg\">\n";
  625. }
  626. httpd_resp_sendstr_chunk(req, txt.c_str());
  627. }
  628. }
  629. /* Respond with an empty chunk to signal HTTP response completion */
  630. httpd_resp_sendstr_chunk(req, NULL);
  631. }
  632. else
  633. {
  634. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!");
  635. return ESP_ERR_NOT_FOUND;
  636. }
  637. #ifdef DEBUG_DETAIL_ON
  638. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  639. #endif
  640. return ESP_OK;
  641. }
  642. esp_err_t handler_editflow(httpd_req_t *req)
  643. {
  644. #ifdef DEBUG_DETAIL_ON
  645. LogFile.WriteHeapInfo("handler_editflow - Start");
  646. #endif
  647. ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri);
  648. char _query[512];
  649. char _valuechar[30];
  650. std::string _task;
  651. if (httpd_req_get_url_query_str(req, _query, 512) == ESP_OK)
  652. {
  653. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  654. {
  655. #ifdef DEBUG_DETAIL_ON
  656. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  657. #endif
  658. _task = std::string(_valuechar);
  659. }
  660. }
  661. if (_task.compare("namenumbers") == 0)
  662. {
  663. ESP_LOGD(TAG, "Get NUMBER list");
  664. return get_numbers_file_handler(req);
  665. }
  666. if (_task.compare("data") == 0)
  667. {
  668. ESP_LOGD(TAG, "Get data list");
  669. return get_data_file_handler(req);
  670. }
  671. if (_task.compare("tflite") == 0)
  672. {
  673. ESP_LOGD(TAG, "Get tflite list");
  674. return get_tflite_file_handler(req);
  675. }
  676. if (_task.compare("copy") == 0)
  677. {
  678. std::string in, out, zw;
  679. httpd_query_key_value(_query, "in", _valuechar, 30);
  680. in = std::string(_valuechar);
  681. httpd_query_key_value(_query, "out", _valuechar, 30);
  682. out = std::string(_valuechar);
  683. #ifdef DEBUG_DETAIL_ON
  684. ESP_LOGD(TAG, "in: %s", in.c_str());
  685. ESP_LOGD(TAG, "out: %s", out.c_str());
  686. #endif
  687. in = "/sdcard" + in;
  688. out = "/sdcard" + out;
  689. CopyFile(in, out);
  690. zw = "Copy Done";
  691. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  692. httpd_resp_send(req, zw.c_str(), zw.length());
  693. }
  694. if (_task.compare("cutref") == 0)
  695. {
  696. std::string in, out, zw;
  697. int x = 0, y = 0, dx = 20, dy = 20;
  698. bool enhance = false;
  699. httpd_query_key_value(_query, "in", _valuechar, 30);
  700. in = std::string(_valuechar);
  701. httpd_query_key_value(_query, "out", _valuechar, 30);
  702. out = std::string(_valuechar);
  703. httpd_query_key_value(_query, "x", _valuechar, 30);
  704. std::string _x = std::string(_valuechar);
  705. if (isStringNumeric(_x))
  706. {
  707. x = std::stoi(_x);
  708. }
  709. httpd_query_key_value(_query, "y", _valuechar, 30);
  710. std::string _y = std::string(_valuechar);
  711. if (isStringNumeric(_y))
  712. {
  713. y = std::stoi(_y);
  714. }
  715. httpd_query_key_value(_query, "dx", _valuechar, 30);
  716. std::string _dx = std::string(_valuechar);
  717. if (isStringNumeric(_dx))
  718. {
  719. dx = std::stoi(_dx);
  720. }
  721. httpd_query_key_value(_query, "dy", _valuechar, 30);
  722. std::string _dy = std::string(_valuechar);
  723. if (isStringNumeric(_dy))
  724. {
  725. dy = std::stoi(_dy);
  726. }
  727. #ifdef DEBUG_DETAIL_ON
  728. ESP_LOGD(TAG, "in: %s", in.c_str());
  729. ESP_LOGD(TAG, "out: %s", out.c_str());
  730. ESP_LOGD(TAG, "x: %s", _x.c_str());
  731. ESP_LOGD(TAG, "y: %s", _y.c_str());
  732. ESP_LOGD(TAG, "dx: %s", _dx.c_str());
  733. ESP_LOGD(TAG, "dy: %s", _dy.c_str());
  734. #endif
  735. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  736. {
  737. string _enhance = std::string(_valuechar);
  738. if (_enhance.compare("true") == 0)
  739. {
  740. enhance = true;
  741. }
  742. }
  743. in = "/sdcard" + in;
  744. out = "/sdcard" + out;
  745. std::string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  746. if ((flowctrl.SetupModeActive || (flowctrl.getActStatus() == std::string("Flow finished"))) && psram_init_shared_memory_for_take_image_step())
  747. {
  748. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Taking image for Alignment Mark Update...");
  749. CAlignAndCutImage *caic = new CAlignAndCutImage("cutref", in);
  750. caic->CutAndSave(out2, x, y, dx, dy);
  751. delete caic;
  752. CImageBasis *cim = new CImageBasis("cutref", out2);
  753. if (enhance)
  754. {
  755. cim->Contrast(90);
  756. }
  757. cim->SaveToFile(out);
  758. delete cim;
  759. psram_deinit_shared_memory_for_take_image_step();
  760. zw = "CutImage Done";
  761. }
  762. else
  763. {
  764. 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() + "')!");
  765. zw = "Device Busy";
  766. }
  767. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  768. httpd_resp_send(req, zw.c_str(), zw.length());
  769. }
  770. // wird beim Erstellen eines neuen Referenzbildes aufgerufen
  771. std::string sys_status = flowctrl.getActStatus();
  772. if ((sys_status != "Take Image") && (sys_status != "Aligning"))
  773. {
  774. if ((_task.compare("test_take") == 0) || (_task.compare("cam_settings") == 0))
  775. {
  776. std::string _host = "";
  777. // laden der aktuellen Kameraeinstellungen(CCstatus) in den Zwischenspeicher(CFstatus)
  778. setCCstatusToCFstatus(); // CCstatus >>> CFstatus
  779. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  780. {
  781. _host = std::string(_valuechar);
  782. }
  783. if (httpd_query_key_value(_query, "waitb", _valuechar, 30) == ESP_OK)
  784. {
  785. std::string _waitb = std::string(_valuechar);
  786. if (isStringNumeric(_waitb))
  787. {
  788. CFstatus.WaitBeforePicture = std::stoi(_valuechar);
  789. }
  790. }
  791. if (httpd_query_key_value(_query, "aecgc", _valuechar, 30) == ESP_OK)
  792. {
  793. std::string _aecgc = std::string(_valuechar);
  794. if (isStringNumeric(_aecgc))
  795. {
  796. int _aecgc_ = std::stoi(_valuechar);
  797. switch (_aecgc_)
  798. {
  799. case 1:
  800. CFstatus.ImageGainceiling = GAINCEILING_4X;
  801. break;
  802. case 2:
  803. CFstatus.ImageGainceiling = GAINCEILING_8X;
  804. break;
  805. case 3:
  806. CFstatus.ImageGainceiling = GAINCEILING_16X;
  807. break;
  808. case 4:
  809. CFstatus.ImageGainceiling = GAINCEILING_32X;
  810. break;
  811. case 5:
  812. CFstatus.ImageGainceiling = GAINCEILING_64X;
  813. break;
  814. case 6:
  815. CFstatus.ImageGainceiling = GAINCEILING_128X;
  816. break;
  817. default:
  818. CFstatus.ImageGainceiling = GAINCEILING_2X;
  819. }
  820. }
  821. else
  822. {
  823. if (_aecgc == "X4") {
  824. CFstatus.ImageGainceiling = GAINCEILING_4X;
  825. }
  826. else if (_aecgc == "X8") {
  827. CFstatus.ImageGainceiling = GAINCEILING_8X;
  828. }
  829. else if (_aecgc == "X16") {
  830. CFstatus.ImageGainceiling = GAINCEILING_16X;
  831. }
  832. else if (_aecgc == "X32") {
  833. CFstatus.ImageGainceiling = GAINCEILING_32X;
  834. }
  835. else if (_aecgc == "X64") {
  836. CFstatus.ImageGainceiling = GAINCEILING_64X;
  837. }
  838. else if (_aecgc == "X128") {
  839. CFstatus.ImageGainceiling = GAINCEILING_128X;
  840. }
  841. else {
  842. CFstatus.ImageGainceiling = GAINCEILING_2X;
  843. }
  844. }
  845. }
  846. if (httpd_query_key_value(_query, "qual", _valuechar, 30) == ESP_OK)
  847. {
  848. std::string _qual = std::string(_valuechar);
  849. if (isStringNumeric(_qual))
  850. {
  851. int _qual_ = std::stoi(_valuechar);
  852. CFstatus.ImageQuality = clipInt(_qual_, 63, 6);
  853. }
  854. }
  855. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK)
  856. {
  857. std::string _bri = std::string(_valuechar);
  858. if (isStringNumeric(_bri))
  859. {
  860. int _bri_ = std::stoi(_valuechar);
  861. CFstatus.ImageBrightness = clipInt(_bri_, 2, -2);
  862. }
  863. }
  864. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK)
  865. {
  866. std::string _con = std::string(_valuechar);
  867. if (isStringNumeric(_con))
  868. {
  869. int _con_ = std::stoi(_valuechar);
  870. CFstatus.ImageContrast = clipInt(_con_, 2, -2);
  871. }
  872. }
  873. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK)
  874. {
  875. std::string _sat = std::string(_valuechar);
  876. if (isStringNumeric(_sat))
  877. {
  878. int _sat_ = std::stoi(_valuechar);
  879. CFstatus.ImageSaturation = clipInt(_sat_, 2, -2);
  880. }
  881. }
  882. if (httpd_query_key_value(_query, "shp", _valuechar, 30) == ESP_OK)
  883. {
  884. std::string _shp = std::string(_valuechar);
  885. if (isStringNumeric(_shp))
  886. {
  887. int _shp_ = std::stoi(_valuechar);
  888. if (CCstatus.CamSensor_id == OV2640_PID)
  889. {
  890. CFstatus.ImageSharpness = clipInt(_shp_, 2, -2);
  891. }
  892. else
  893. {
  894. CFstatus.ImageSharpness = clipInt(_shp_, 3, -3);
  895. }
  896. }
  897. }
  898. if (httpd_query_key_value(_query, "ashp", _valuechar, 30) == ESP_OK)
  899. {
  900. std::string _ashp = std::string(_valuechar);
  901. CFstatus.ImageAutoSharpness = alphanumericToBoolean(_ashp);
  902. }
  903. if (httpd_query_key_value(_query, "spe", _valuechar, 30) == ESP_OK)
  904. {
  905. std::string _spe = std::string(_valuechar);
  906. if (isStringNumeric(_spe))
  907. {
  908. int _spe_ = std::stoi(_valuechar);
  909. CFstatus.ImageSpecialEffect = clipInt(_spe_, 6, 0);
  910. }
  911. else
  912. {
  913. if (_spe == "negative") {
  914. CFstatus.ImageSpecialEffect = 1;
  915. }
  916. else if (_spe == "grayscale") {
  917. CFstatus.ImageSpecialEffect = 2;
  918. }
  919. else if (_spe == "red") {
  920. CFstatus.ImageSpecialEffect = 3;
  921. }
  922. else if (_spe == "green") {
  923. CFstatus.ImageSpecialEffect = 4;
  924. }
  925. else if (_spe == "blue") {
  926. CFstatus.ImageSpecialEffect = 5;
  927. }
  928. else if (_spe == "retro") {
  929. CFstatus.ImageSpecialEffect = 6;
  930. }
  931. else {
  932. CFstatus.ImageSpecialEffect = 0;
  933. }
  934. }
  935. }
  936. if (httpd_query_key_value(_query, "wbm", _valuechar, 30) == ESP_OK)
  937. {
  938. std::string _wbm = std::string(_valuechar);
  939. if (isStringNumeric(_wbm))
  940. {
  941. int _wbm_ = std::stoi(_valuechar);
  942. CFstatus.ImageWbMode = clipInt(_wbm_, 4, 0);
  943. }
  944. else
  945. {
  946. if (_wbm == "sunny") {
  947. CFstatus.ImageWbMode = 1;
  948. }
  949. else if (_wbm == "cloudy") {
  950. CFstatus.ImageWbMode = 2;
  951. }
  952. else if (_wbm == "office") {
  953. CFstatus.ImageWbMode = 3;
  954. }
  955. else if (_wbm == "home") {
  956. CFstatus.ImageWbMode = 4;
  957. }
  958. else {
  959. CFstatus.ImageWbMode = 0;
  960. }
  961. }
  962. }
  963. if (httpd_query_key_value(_query, "awb", _valuechar, 30) == ESP_OK)
  964. {
  965. std::string _awb = std::string(_valuechar);
  966. CFstatus.ImageAwb = alphanumericToBoolean(_awb);
  967. }
  968. if (httpd_query_key_value(_query, "awbg", _valuechar, 30) == ESP_OK)
  969. {
  970. std::string _awbg = std::string(_valuechar);
  971. CFstatus.ImageAwbGain = alphanumericToBoolean(_awbg);
  972. }
  973. if (httpd_query_key_value(_query, "aec", _valuechar, 30) == ESP_OK)
  974. {
  975. std::string _aec = std::string(_valuechar);
  976. CFstatus.ImageAec = alphanumericToBoolean(_aec);
  977. }
  978. if (httpd_query_key_value(_query, "aec2", _valuechar, 30) == ESP_OK)
  979. {
  980. std::string _aec2 = std::string(_valuechar);
  981. CFstatus.ImageAec2 = alphanumericToBoolean(_aec2);
  982. }
  983. if (httpd_query_key_value(_query, "ael", _valuechar, 30) == ESP_OK)
  984. {
  985. std::string _ael = std::string(_valuechar);
  986. if (isStringNumeric(_ael))
  987. {
  988. int _ael_ = std::stoi(_valuechar);
  989. if (CCstatus.CamSensor_id == OV2640_PID)
  990. {
  991. CFstatus.ImageAeLevel = clipInt(_ael_, 2, -2);
  992. }
  993. else
  994. {
  995. CFstatus.ImageAeLevel = clipInt(_ael_, 5, -5);
  996. }
  997. }
  998. }
  999. if (httpd_query_key_value(_query, "aecv", _valuechar, 30) == ESP_OK)
  1000. {
  1001. std::string _aecv = std::string(_valuechar);
  1002. if (isStringNumeric(_aecv))
  1003. {
  1004. int _aecv_ = std::stoi(_valuechar);
  1005. CFstatus.ImageAecValue = clipInt(_aecv_, 1200, 0);
  1006. }
  1007. }
  1008. if (httpd_query_key_value(_query, "agc", _valuechar, 30) == ESP_OK)
  1009. {
  1010. std::string _agc = std::string(_valuechar);
  1011. CFstatus.ImageAgc = alphanumericToBoolean(_agc);
  1012. }
  1013. if (httpd_query_key_value(_query, "agcg", _valuechar, 30) == ESP_OK)
  1014. {
  1015. std::string _agcg = std::string(_valuechar);
  1016. if (isStringNumeric(_agcg))
  1017. {
  1018. int _agcg_ = std::stoi(_valuechar);
  1019. CFstatus.ImageAgcGain = clipInt(_agcg_, 30, 0);
  1020. }
  1021. }
  1022. if (httpd_query_key_value(_query, "bpc", _valuechar, 30) == ESP_OK)
  1023. {
  1024. std::string _bpc = std::string(_valuechar);
  1025. CFstatus.ImageBpc = alphanumericToBoolean(_bpc);
  1026. }
  1027. if (httpd_query_key_value(_query, "wpc", _valuechar, 30) == ESP_OK)
  1028. {
  1029. std::string _wpc = std::string(_valuechar);
  1030. CFstatus.ImageWpc = alphanumericToBoolean(_wpc);
  1031. }
  1032. if (httpd_query_key_value(_query, "rgma", _valuechar, 30) == ESP_OK)
  1033. {
  1034. std::string _rgma = std::string(_valuechar);
  1035. CFstatus.ImageRawGma = alphanumericToBoolean(_rgma);
  1036. }
  1037. if (httpd_query_key_value(_query, "lenc", _valuechar, 30) == ESP_OK)
  1038. {
  1039. std::string _lenc = std::string(_valuechar);
  1040. CFstatus.ImageLenc = alphanumericToBoolean(_lenc);
  1041. }
  1042. if (httpd_query_key_value(_query, "mirror", _valuechar, 30) == ESP_OK)
  1043. {
  1044. std::string _mirror = std::string(_valuechar);
  1045. CFstatus.ImageHmirror = alphanumericToBoolean(_mirror);
  1046. }
  1047. if (httpd_query_key_value(_query, "flip", _valuechar, 30) == ESP_OK)
  1048. {
  1049. std::string _flip = std::string(_valuechar);
  1050. CFstatus.ImageVflip = alphanumericToBoolean(_flip);
  1051. }
  1052. if (httpd_query_key_value(_query, "dcw", _valuechar, 30) == ESP_OK)
  1053. {
  1054. std::string _dcw = std::string(_valuechar);
  1055. CFstatus.ImageDcw = alphanumericToBoolean(_dcw);
  1056. }
  1057. if (httpd_query_key_value(_query, "den", _valuechar, 30) == ESP_OK)
  1058. {
  1059. std::string _idlv = std::string(_valuechar);
  1060. if (isStringNumeric(_idlv))
  1061. {
  1062. int _ImageDenoiseLevel = std::stoi(_valuechar);
  1063. if (CCstatus.CamSensor_id == OV2640_PID)
  1064. {
  1065. CFstatus.ImageDenoiseLevel = 0;
  1066. }
  1067. else
  1068. {
  1069. CFstatus.ImageDenoiseLevel = clipInt(_ImageDenoiseLevel, 8, 0);
  1070. }
  1071. }
  1072. }
  1073. if (httpd_query_key_value(_query, "zoom", _valuechar, 30) == ESP_OK)
  1074. {
  1075. std::string _zoom = std::string(_valuechar);
  1076. CFstatus.ImageZoomEnabled = alphanumericToBoolean(_zoom);
  1077. }
  1078. if (httpd_query_key_value(_query, "zoomx", _valuechar, 30) == ESP_OK)
  1079. {
  1080. std::string _zoomx = std::string(_valuechar);
  1081. if (isStringNumeric(_zoomx))
  1082. {
  1083. int _ImageZoomOffsetX = std::stoi(_valuechar);
  1084. if (CCstatus.CamSensor_id == OV2640_PID)
  1085. {
  1086. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 480, -480);
  1087. }
  1088. else if (CCstatus.CamSensor_id == OV3660_PID)
  1089. {
  1090. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 704, -704);
  1091. }
  1092. else if (CCstatus.CamSensor_id == OV5640_PID)
  1093. {
  1094. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 960, -960);
  1095. }
  1096. }
  1097. }
  1098. if (httpd_query_key_value(_query, "zoomy", _valuechar, 30) == ESP_OK)
  1099. {
  1100. std::string _zoomy = std::string(_valuechar);
  1101. if (isStringNumeric(_zoomy))
  1102. {
  1103. int _ImageZoomOffsetY = std::stoi(_valuechar);
  1104. if (CCstatus.CamSensor_id == OV2640_PID)
  1105. {
  1106. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 360, -360);
  1107. }
  1108. else if (CCstatus.CamSensor_id == OV3660_PID)
  1109. {
  1110. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 528, -528);
  1111. }
  1112. else if (CCstatus.CamSensor_id == OV5640_PID)
  1113. {
  1114. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 720, -720);
  1115. }
  1116. }
  1117. }
  1118. if (httpd_query_key_value(_query, "zooms", _valuechar, 30) == ESP_OK)
  1119. {
  1120. std::string _zooms = std::string(_valuechar);
  1121. if (isStringNumeric(_zooms))
  1122. {
  1123. int _ImageZoomSize = std::stoi(_valuechar);
  1124. if (CCstatus.CamSensor_id == OV2640_PID)
  1125. {
  1126. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 29, 0);
  1127. }
  1128. else if (CCstatus.CamSensor_id == OV3660_PID)
  1129. {
  1130. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 43, 0);
  1131. }
  1132. else if (CCstatus.CamSensor_id == OV5640_PID)
  1133. {
  1134. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 59, 0);
  1135. }
  1136. }
  1137. }
  1138. if (httpd_query_key_value(_query, "ledi", _valuechar, 30) == ESP_OK)
  1139. {
  1140. std::string _ledi = std::string(_valuechar);
  1141. if (isStringNumeric(_ledi))
  1142. {
  1143. int _ImageLedIntensity = std::stoi(_valuechar);
  1144. CFstatus.ImageLedIntensity = Camera.SetLEDIntensity(_ImageLedIntensity);
  1145. }
  1146. }
  1147. if (_task.compare("cam_settings") == 0)
  1148. {
  1149. // wird aufgerufen, wenn das Referenzbild + Kameraeinstellungen gespeichert wurden
  1150. setCFstatusToCCstatus(); // CFstatus >>> CCstatus
  1151. // Kameraeinstellungen wurden verädert
  1152. CFstatus.changedCameraSettings = true;
  1153. ESP_LOGD(TAG, "Cam Settings set");
  1154. std::string _zw = "CamSettingsSet";
  1155. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1156. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1157. }
  1158. else
  1159. {
  1160. // wird aufgerufen, wenn ein neues Referenzbild erstellt oder aktualisiert wurde
  1161. // CFstatus >>> Kamera
  1162. setCFstatusToCam();
  1163. Camera.SetQualityZoomSize(CFstatus.ImageQuality, CFstatus.ImageFrameSize, CFstatus.ImageZoomEnabled, CFstatus.ImageZoomOffsetX, CFstatus.ImageZoomOffsetY, CFstatus.ImageZoomSize, CFstatus.ImageVflip);
  1164. // Camera.SetZoomSize(CFstatus.ImageZoomEnabled, CFstatus.ImageZoomOffsetX, CFstatus.ImageZoomOffsetY, CFstatus.ImageZoomSize, CFstatus.ImageVflip);
  1165. // Kameraeinstellungen wurden verädert
  1166. CFstatus.changedCameraSettings = true;
  1167. ESP_LOGD(TAG, "test_take - vor TakeImage");
  1168. std::string image_temp = flowctrl.doSingleStep("[TakeImage]", _host);
  1169. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1170. httpd_resp_send(req, image_temp.c_str(), image_temp.length());
  1171. }
  1172. }
  1173. if (_task.compare("test_align") == 0)
  1174. {
  1175. std::string _host = "";
  1176. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  1177. {
  1178. _host = std::string(_valuechar);
  1179. }
  1180. std::string zw = flowctrl.doSingleStep("[Alignment]", _host);
  1181. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1182. httpd_resp_send(req, zw.c_str(), zw.length());
  1183. }
  1184. }
  1185. else
  1186. {
  1187. std::string _zw = "DeviceIsBusy";
  1188. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1189. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1190. }
  1191. #ifdef DEBUG_DETAIL_ON
  1192. LogFile.WriteHeapInfo("handler_editflow - Done");
  1193. #endif
  1194. return ESP_OK;
  1195. }
  1196. esp_err_t handler_statusflow(httpd_req_t *req)
  1197. {
  1198. #ifdef DEBUG_DETAIL_ON
  1199. LogFile.WriteHeapInfo("handler_statusflow - Start");
  1200. #endif
  1201. const char *resp_str;
  1202. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1203. if (bTaskAutoFlowCreated)
  1204. {
  1205. #ifdef DEBUG_DETAIL_ON
  1206. ESP_LOGD(TAG, "handler_statusflow: %s", req->uri);
  1207. #endif
  1208. std::string zw = flowctrl.getActStatusWithTime();
  1209. resp_str = zw.c_str();
  1210. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1211. }
  1212. else
  1213. {
  1214. resp_str = "Flow task not yet created";
  1215. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1216. }
  1217. #ifdef DEBUG_DETAIL_ON
  1218. LogFile.WriteHeapInfo("handler_statusflow - Done");
  1219. #endif
  1220. return ESP_OK;
  1221. }
  1222. esp_err_t handler_cputemp(httpd_req_t *req)
  1223. {
  1224. #ifdef DEBUG_DETAIL_ON
  1225. LogFile.WriteHeapInfo("handler_cputemp - Start");
  1226. #endif
  1227. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1228. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  1229. #ifdef DEBUG_DETAIL_ON
  1230. LogFile.WriteHeapInfo("handler_cputemp - End");
  1231. #endif
  1232. return ESP_OK;
  1233. }
  1234. esp_err_t handler_rssi(httpd_req_t *req)
  1235. {
  1236. #ifdef DEBUG_DETAIL_ON
  1237. LogFile.WriteHeapInfo("handler_rssi - Start");
  1238. #endif
  1239. if (getWIFIisConnected())
  1240. {
  1241. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1242. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  1243. }
  1244. else
  1245. {
  1246. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  1247. return ESP_ERR_NOT_FOUND;
  1248. }
  1249. #ifdef DEBUG_DETAIL_ON
  1250. LogFile.WriteHeapInfo("handler_rssi - End");
  1251. #endif
  1252. return ESP_OK;
  1253. }
  1254. esp_err_t handler_current_date(httpd_req_t *req)
  1255. {
  1256. #ifdef DEBUG_DETAIL_ON
  1257. LogFile.WriteHeapInfo("handler_uptime - Start");
  1258. #endif
  1259. std::string formatedDateAndTime = getCurrentTimeString("%Y-%m-%d %H:%M:%S");
  1260. // std::string formatedDate = getCurrentTimeString("%Y-%m-%d");
  1261. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1262. httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
  1263. #ifdef DEBUG_DETAIL_ON
  1264. LogFile.WriteHeapInfo("handler_uptime - End");
  1265. #endif
  1266. return ESP_OK;
  1267. }
  1268. esp_err_t handler_uptime(httpd_req_t *req)
  1269. {
  1270. #ifdef DEBUG_DETAIL_ON
  1271. LogFile.WriteHeapInfo("handler_uptime - Start");
  1272. #endif
  1273. std::string formatedUptime = getFormatedUptime(false);
  1274. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1275. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  1276. #ifdef DEBUG_DETAIL_ON
  1277. LogFile.WriteHeapInfo("handler_uptime - End");
  1278. #endif
  1279. return ESP_OK;
  1280. }
  1281. esp_err_t handler_prevalue(httpd_req_t *req)
  1282. {
  1283. #ifdef DEBUG_DETAIL_ON
  1284. LogFile.WriteHeapInfo("handler_prevalue - Start");
  1285. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  1286. #endif
  1287. // Default usage message when handler gets called without any parameter
  1288. const std::string RESTUsageInfo =
  1289. "00: Handler usage:<br>"
  1290. "- To retrieve actual PreValue, please provide only a numbersname, e.g. /setPreValue?numbers=main<br>"
  1291. "- To set PreValue to a new value, please provide a numbersname and a value, e.g. /setPreValue?numbers=main&value=1234.5678<br>"
  1292. "NOTE:<br>"
  1293. "value >= 0.0: Set PreValue to provided value<br>"
  1294. "value < 0.0: Set PreValue to actual RAW value (as long RAW value is a valid number, without N)";
  1295. // Default return error message when no return is programmed
  1296. std::string sReturnMessage = "E90: Uninitialized";
  1297. char _query[100];
  1298. char _numbersname[50] = "default";
  1299. char _value[20] = "";
  1300. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1301. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  1302. {
  1303. #ifdef DEBUG_DETAIL_ON
  1304. ESP_LOGD(TAG, "Query: %s", _query);
  1305. #endif
  1306. if (httpd_query_key_value(_query, "numbers", _numbersname, 50) != ESP_OK)
  1307. {
  1308. // If request is incomplete
  1309. sReturnMessage = "E91: Query parameter incomplete or not valid!<br> "
  1310. "Call /setPreValue to show REST API usage info and/or check documentation";
  1311. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1312. return ESP_FAIL;
  1313. }
  1314. if (httpd_query_key_value(_query, "value", _value, 20) == ESP_OK)
  1315. {
  1316. #ifdef DEBUG_DETAIL_ON
  1317. ESP_LOGD(TAG, "Value: %s", _value);
  1318. #endif
  1319. }
  1320. }
  1321. else
  1322. {
  1323. // if no parameter is provided, print handler usage
  1324. httpd_resp_send(req, RESTUsageInfo.c_str(), RESTUsageInfo.length());
  1325. return ESP_OK;
  1326. }
  1327. if (strlen(_value) == 0)
  1328. {
  1329. // If no value is povided --> return actual PreValue
  1330. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1331. if (sReturnMessage.empty())
  1332. {
  1333. sReturnMessage = "E92: Numbers name not found";
  1334. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1335. return ESP_FAIL;
  1336. }
  1337. }
  1338. else
  1339. {
  1340. // New value is positive: Set PreValue to provided value and return value
  1341. // New value is negative and actual RAW value is a valid number: Set PreValue to RAW value and return value
  1342. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "REST API handler_prevalue called: numbersname: " + std::string(_numbersname) + ", value: " + std::string(_value));
  1343. if (!flowctrl.UpdatePrevalue(_value, _numbersname, true))
  1344. {
  1345. sReturnMessage = "E93: Update request rejected. Please check device logs for more details";
  1346. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1347. return ESP_FAIL;
  1348. }
  1349. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1350. if (sReturnMessage.empty())
  1351. {
  1352. sReturnMessage = "E94: Numbers name not found";
  1353. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1354. return ESP_FAIL;
  1355. }
  1356. }
  1357. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1358. #ifdef DEBUG_DETAIL_ON
  1359. LogFile.WriteHeapInfo("handler_prevalue - End");
  1360. #endif
  1361. return ESP_OK;
  1362. }
  1363. void task_autodoFlow(void *pvParameter)
  1364. {
  1365. int64_t fr_start, fr_delta_ms;
  1366. bTaskAutoFlowCreated = true;
  1367. if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
  1368. {
  1369. flowctrl.setActStatus("Initialization (delayed)");
  1370. // #ifdef ENABLE_MQTT
  1371. // MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  1372. // #endif //ENABLE_MQTT
  1373. vTaskDelay(60 * 5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
  1374. }
  1375. ESP_LOGD(TAG, "task_autodoFlow: start");
  1376. doInit();
  1377. flowctrl.setAutoStartInterval(auto_interval);
  1378. autostartIsEnabled = flowctrl.getIsAutoStart();
  1379. if (isSetupModusActive())
  1380. {
  1381. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "We are in Setup Mode -> Not starting Auto Flow!");
  1382. autostartIsEnabled = false;
  1383. // 15.7.0 Setup Wizard cannot take a Reference Picture #2953
  1384. // std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  1385. // flowctrl.doFlowTakeImageOnly(zw_time);
  1386. }
  1387. if (autostartIsEnabled)
  1388. {
  1389. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting Flow...");
  1390. }
  1391. else
  1392. {
  1393. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Autostart is not enabled -> Not starting Flow");
  1394. }
  1395. while (autostartIsEnabled)
  1396. {
  1397. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  1398. time_t roundStartTime = getUpTime();
  1399. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  1400. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  1401. fr_start = esp_timer_get_time();
  1402. if (flowisrunning)
  1403. {
  1404. #ifdef DEBUG_DETAIL_ON
  1405. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  1406. #endif
  1407. }
  1408. else
  1409. {
  1410. #ifdef DEBUG_DETAIL_ON
  1411. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  1412. #endif
  1413. flowisrunning = true;
  1414. doflow();
  1415. #ifdef DEBUG_DETAIL_ON
  1416. ESP_LOGD(TAG, "Remove older log files");
  1417. #endif
  1418. LogFile.RemoveOldLogFile();
  1419. LogFile.RemoveOldDataLog();
  1420. }
  1421. // Round finished -> Logfile
  1422. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) + " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  1423. // CPU Temp -> Logfile
  1424. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  1425. // WIFI Signal Strength (RSSI) -> Logfile
  1426. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  1427. // Check if time is synchronized (if NTP is configured)
  1428. if (getUseNtp() && !getTimeIsSet())
  1429. {
  1430. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set!");
  1431. StatusLED(TIME_CHECK, 1, false);
  1432. }
  1433. #if (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES)
  1434. wifiRoamingQuery();
  1435. #endif
  1436. // Scan channels and check if an AP with better RSSI is available, then disconnect and try to reconnect to AP with better RSSI
  1437. // NOTE: Keep this direct before the following task delay, because scan is done in blocking mode and this takes ca. 1,5 - 2s.
  1438. #ifdef WLAN_USE_ROAMING_BY_SCANNING
  1439. wifiRoamByScanning();
  1440. #endif
  1441. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  1442. if (auto_interval > fr_delta_ms)
  1443. {
  1444. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  1445. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long)xDelay);
  1446. vTaskDelay(xDelay);
  1447. }
  1448. }
  1449. while (1)
  1450. {
  1451. // Keep flow task running to handle necessary sub tasks like reboot handler, etc..
  1452. vTaskDelay(2000 / portTICK_PERIOD_MS);
  1453. }
  1454. vTaskDelete(NULL); // Delete this task if it exits from the loop above
  1455. xHandletask_autodoFlow = NULL;
  1456. ESP_LOGD(TAG, "task_autodoFlow: end");
  1457. }
  1458. void InitializeFlowTask(void)
  1459. {
  1460. BaseType_t xReturned;
  1461. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1462. uint32_t stackSize = 16 * 1024;
  1463. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", stackSize, NULL, tskIDLE_PRIORITY + 2, &xHandletask_autodoFlow, 0);
  1464. if (xReturned != pdPASS)
  1465. {
  1466. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Creation task_autodoFlow failed. Requested stack size:" + std::to_string(stackSize));
  1467. LogFile.WriteHeapInfo("Creation task_autodoFlow failed");
  1468. }
  1469. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1470. }
  1471. void register_server_main_flow_task_uri(httpd_handle_t server)
  1472. {
  1473. ESP_LOGI(TAG, "server_main_flow_task - Registering URI handlers");
  1474. httpd_uri_t camuri = {};
  1475. camuri.method = HTTP_GET;
  1476. camuri.uri = "/doinit";
  1477. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_init);
  1478. camuri.user_ctx = (void *)"Light On";
  1479. httpd_register_uri_handler(server, &camuri);
  1480. // Legacy API => New: "/setPreValue"
  1481. camuri.uri = "/setPreValue.html";
  1482. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1483. camuri.user_ctx = (void *)"Prevalue";
  1484. httpd_register_uri_handler(server, &camuri);
  1485. camuri.uri = "/setPreValue";
  1486. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1487. camuri.user_ctx = (void *)"Prevalue";
  1488. httpd_register_uri_handler(server, &camuri);
  1489. camuri.uri = "/flow_start";
  1490. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_flow_start);
  1491. camuri.user_ctx = (void *)"Flow Start";
  1492. httpd_register_uri_handler(server, &camuri);
  1493. camuri.uri = "/statusflow.html";
  1494. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1495. camuri.user_ctx = (void *)"Light Off";
  1496. httpd_register_uri_handler(server, &camuri);
  1497. camuri.uri = "/statusflow";
  1498. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1499. camuri.user_ctx = (void *)"Light Off";
  1500. httpd_register_uri_handler(server, &camuri);
  1501. // Legacy API => New: "/cpu_temperature"
  1502. camuri.uri = "/cputemp.html";
  1503. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1504. camuri.user_ctx = (void *)"Light Off";
  1505. httpd_register_uri_handler(server, &camuri);
  1506. camuri.uri = "/cpu_temperature";
  1507. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1508. camuri.user_ctx = (void *)"Light Off";
  1509. httpd_register_uri_handler(server, &camuri);
  1510. // Legacy API => New: "/rssi"
  1511. camuri.uri = "/rssi.html";
  1512. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1513. camuri.user_ctx = (void *)"Light Off";
  1514. httpd_register_uri_handler(server, &camuri);
  1515. camuri.uri = "/rssi";
  1516. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1517. camuri.user_ctx = (void *)"Light Off";
  1518. httpd_register_uri_handler(server, &camuri);
  1519. camuri.uri = "/date";
  1520. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_current_date);
  1521. camuri.user_ctx = (void *)"Light Off";
  1522. httpd_register_uri_handler(server, &camuri);
  1523. camuri.uri = "/uptime";
  1524. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_uptime);
  1525. camuri.user_ctx = (void *)"Light Off";
  1526. httpd_register_uri_handler(server, &camuri);
  1527. camuri.uri = "/editflow";
  1528. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_editflow);
  1529. camuri.user_ctx = (void *)"EditFlow";
  1530. httpd_register_uri_handler(server, &camuri);
  1531. // Legacy API => New: "/value"
  1532. camuri.uri = "/value.html";
  1533. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1534. camuri.user_ctx = (void *)"Value";
  1535. httpd_register_uri_handler(server, &camuri);
  1536. camuri.uri = "/value";
  1537. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1538. camuri.user_ctx = (void *)"Value";
  1539. httpd_register_uri_handler(server, &camuri);
  1540. // Legacy API => New: "/value"
  1541. camuri.uri = "/wasserzaehler.html";
  1542. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1543. camuri.user_ctx = (void *)"Wasserzaehler";
  1544. httpd_register_uri_handler(server, &camuri);
  1545. camuri.uri = "/json";
  1546. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_json);
  1547. camuri.user_ctx = (void *)"JSON";
  1548. httpd_register_uri_handler(server, &camuri);
  1549. camuri.uri = "/heap";
  1550. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_get_heap);
  1551. camuri.user_ctx = (void *)"Heap";
  1552. httpd_register_uri_handler(server, &camuri);
  1553. camuri.uri = "/stream";
  1554. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_stream);
  1555. camuri.user_ctx = (void *)"stream";
  1556. httpd_register_uri_handler(server, &camuri);
  1557. /** will handle metrics requests */
  1558. camuri.uri = "/metrics";
  1559. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_openmetrics);
  1560. camuri.user_ctx = (void *)"metrics";
  1561. httpd_register_uri_handler(server, &camuri);
  1562. /** when adding a new handler, make sure to increment the value for config.max_uri_handlers in `main/server_main.cpp` */
  1563. }