MainFlowControl.cpp 63 KB

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