MainFlowControl.cpp 62 KB

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