MainFlowControl.cpp 62 KB

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