MainFlowControl.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  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 autostartIsEnabled = false;
  34. int countRounds = 0;
  35. bool isPlannedReboot = false;
  36. static const char *TAG = "MAINCTRL";
  37. // #define DEBUG_DETAIL_ON
  38. void CheckIsPlannedReboot(void)
  39. {
  40. FILE *pfile;
  41. if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL)
  42. {
  43. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Initial boot or not a planned reboot");
  44. isPlannedReboot = false;
  45. }
  46. else
  47. {
  48. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot");
  49. DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
  50. isPlannedReboot = true;
  51. }
  52. }
  53. bool getIsPlannedReboot(void)
  54. {
  55. return isPlannedReboot;
  56. }
  57. int getCountFlowRounds(void)
  58. {
  59. return countRounds;
  60. }
  61. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  62. {
  63. return flowctrl.GetJPGStream(_filename, req);
  64. }
  65. esp_err_t GetRawJPG(httpd_req_t *req)
  66. {
  67. return flowctrl.SendRawJPG(req);
  68. }
  69. bool isSetupModusActive(void)
  70. {
  71. return flowctrl.getStatusSetupModus();
  72. }
  73. void DeleteMainFlowTask(void)
  74. {
  75. #ifdef DEBUG_DETAIL_ON
  76. ESP_LOGD(TAG, "DeleteMainFlowTask: xHandletask_autodoFlow: %ld", (long)xHandletask_autodoFlow);
  77. #endif
  78. if (xHandletask_autodoFlow != NULL)
  79. {
  80. vTaskDelete(xHandletask_autodoFlow);
  81. xHandletask_autodoFlow = NULL;
  82. }
  83. #ifdef DEBUG_DETAIL_ON
  84. ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
  85. #endif
  86. }
  87. void doInit(void)
  88. {
  89. #ifdef DEBUG_DETAIL_ON
  90. ESP_LOGD(TAG, "Start flowctrl.InitFlow(config);");
  91. #endif
  92. flowctrl.InitFlow(CONFIG_FILE);
  93. #ifdef DEBUG_DETAIL_ON
  94. ESP_LOGD(TAG, "Finished flowctrl.InitFlow(config);");
  95. #endif
  96. /* GPIO handler has to be initialized before MQTT init to ensure proper topic subscription */
  97. gpio_handler_init();
  98. #ifdef ENABLE_MQTT
  99. flowctrl.StartMQTTService();
  100. #endif // ENABLE_MQTT
  101. }
  102. bool doflow(void)
  103. {
  104. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  105. ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
  106. flowisrunning = true;
  107. flowctrl.doFlow(zw_time);
  108. flowisrunning = false;
  109. #ifdef DEBUG_DETAIL_ON
  110. ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
  111. #endif
  112. return true;
  113. }
  114. esp_err_t setCCstatusToCFstatus(void)
  115. {
  116. CFstatus.CamSensor_id = CCstatus.CamSensor_id;
  117. CFstatus.ImageFrameSize = CCstatus.ImageFrameSize;
  118. CFstatus.ImageContrast = CCstatus.ImageContrast;
  119. CFstatus.ImageBrightness = CCstatus.ImageBrightness;
  120. CFstatus.ImageSaturation = CCstatus.ImageSaturation;
  121. CFstatus.ImageQuality = CCstatus.ImageQuality;
  122. CFstatus.ImageGainceiling = CCstatus.ImageGainceiling;
  123. CFstatus.ImageAgc = CCstatus.ImageAgc;
  124. CFstatus.ImageAec = CCstatus.ImageAec;
  125. CFstatus.ImageHmirror = CCstatus.ImageHmirror;
  126. CFstatus.ImageVflip = CCstatus.ImageVflip;
  127. CFstatus.ImageAwb = CCstatus.ImageAwb;
  128. CFstatus.ImageAec2 = CCstatus.ImageAec2;
  129. CFstatus.ImageAecValue = CCstatus.ImageAecValue;
  130. CFstatus.ImageSpecialEffect = CCstatus.ImageSpecialEffect;
  131. CFstatus.ImageWbMode = CCstatus.ImageWbMode;
  132. CFstatus.ImageAeLevel = CCstatus.ImageAeLevel;
  133. CFstatus.ImageDcw = CCstatus.ImageDcw;
  134. CFstatus.ImageBpc = CCstatus.ImageBpc;
  135. CFstatus.ImageWpc = CCstatus.ImageWpc;
  136. CFstatus.ImageAwbGain = CCstatus.ImageAwbGain;
  137. CFstatus.ImageAgcGain = CCstatus.ImageAgcGain;
  138. CFstatus.ImageRawGma = CCstatus.ImageRawGma;
  139. CFstatus.ImageLenc = CCstatus.ImageLenc;
  140. CFstatus.ImageSharpness = CCstatus.ImageSharpness;
  141. CFstatus.ImageAutoSharpness = CCstatus.ImageAutoSharpness;
  142. CFstatus.ImageDenoiseLevel = CCstatus.ImageDenoiseLevel;
  143. CFstatus.ImageLedIntensity = CCstatus.ImageLedIntensity;
  144. CFstatus.ImageZoomEnabled = CCstatus.ImageZoomEnabled;
  145. CFstatus.ImageZoomOffsetX = CCstatus.ImageZoomOffsetX;
  146. CFstatus.ImageZoomOffsetY = CCstatus.ImageZoomOffsetY;
  147. CFstatus.ImageZoomSize = CCstatus.ImageZoomSize;
  148. CFstatus.WaitBeforePicture = CCstatus.WaitBeforePicture;
  149. return ESP_OK;
  150. }
  151. esp_err_t setCFstatusToCCstatus(void)
  152. {
  153. // CCstatus.CamSensor_id = CFstatus.CamSensor_id;
  154. CCstatus.ImageFrameSize = CFstatus.ImageFrameSize;
  155. CCstatus.ImageContrast = CFstatus.ImageContrast;
  156. CCstatus.ImageBrightness = CFstatus.ImageBrightness;
  157. CCstatus.ImageSaturation = CFstatus.ImageSaturation;
  158. CCstatus.ImageQuality = CFstatus.ImageQuality;
  159. CCstatus.ImageGainceiling = CFstatus.ImageGainceiling;
  160. CCstatus.ImageAgc = CFstatus.ImageAgc;
  161. CCstatus.ImageAec = CFstatus.ImageAec;
  162. CCstatus.ImageHmirror = CFstatus.ImageHmirror;
  163. CCstatus.ImageVflip = CFstatus.ImageVflip;
  164. CCstatus.ImageAwb = CFstatus.ImageAwb;
  165. CCstatus.ImageAec2 = CFstatus.ImageAec2;
  166. CCstatus.ImageAecValue = CFstatus.ImageAecValue;
  167. CCstatus.ImageSpecialEffect = CFstatus.ImageSpecialEffect;
  168. CCstatus.ImageWbMode = CFstatus.ImageWbMode;
  169. CCstatus.ImageAeLevel = CFstatus.ImageAeLevel;
  170. CCstatus.ImageDcw = CFstatus.ImageDcw;
  171. CCstatus.ImageBpc = CFstatus.ImageBpc;
  172. CCstatus.ImageWpc = CFstatus.ImageWpc;
  173. CCstatus.ImageAwbGain = CFstatus.ImageAwbGain;
  174. CCstatus.ImageAgcGain = CFstatus.ImageAgcGain;
  175. CCstatus.ImageRawGma = CFstatus.ImageRawGma;
  176. CCstatus.ImageLenc = CFstatus.ImageLenc;
  177. CCstatus.ImageSharpness = CFstatus.ImageSharpness;
  178. CCstatus.ImageAutoSharpness = CFstatus.ImageAutoSharpness;
  179. CCstatus.ImageDenoiseLevel = CFstatus.ImageDenoiseLevel;
  180. CCstatus.ImageLedIntensity = CFstatus.ImageLedIntensity;
  181. CCstatus.ImageZoomEnabled = CFstatus.ImageZoomEnabled;
  182. CCstatus.ImageZoomOffsetX = CFstatus.ImageZoomOffsetX;
  183. CCstatus.ImageZoomOffsetY = CFstatus.ImageZoomOffsetY;
  184. CCstatus.ImageZoomSize = CFstatus.ImageZoomSize;
  185. CCstatus.WaitBeforePicture = CFstatus.WaitBeforePicture;
  186. return ESP_OK;
  187. }
  188. esp_err_t setCFstatusToCam(void)
  189. {
  190. sensor_t *s = esp_camera_sensor_get();
  191. if (s != NULL)
  192. {
  193. s->set_framesize(s, CFstatus.ImageFrameSize);
  194. // s->set_contrast(s, CFstatus.ImageContrast); // -2 to 2
  195. // s->set_brightness(s, CFstatus.ImageBrightness); // -2 to 2
  196. Camera.SetCamContrastBrightness(s, CFstatus.ImageContrast, CFstatus.ImageBrightness);
  197. s->set_saturation(s, CFstatus.ImageSaturation); // -2 to 2
  198. s->set_quality(s, CFstatus.ImageQuality); // 0 - 63
  199. // s->set_gainceiling(s, CFstatus.ImageGainceiling); // Image gain (GAINCEILING_x2, x4, x8, x16, x32, x64 or x128)
  200. Camera.SetCamGainceiling(s, CFstatus.ImageGainceiling);
  201. s->set_gain_ctrl(s, CFstatus.ImageAgc); // 0 = disable , 1 = enable
  202. s->set_exposure_ctrl(s, CFstatus.ImageAec); // 0 = disable , 1 = enable
  203. s->set_hmirror(s, CFstatus.ImageHmirror); // 0 = disable , 1 = enable
  204. s->set_vflip(s, CFstatus.ImageVflip); // 0 = disable , 1 = enable
  205. s->set_whitebal(s, CFstatus.ImageAwb); // 0 = disable , 1 = enable
  206. s->set_aec2(s, CFstatus.ImageAec2); // 0 = disable , 1 = enable
  207. s->set_aec_value(s, CFstatus.ImageAecValue); // 0 to 1200
  208. // 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)
  209. Camera.SetCamSpecialEffect(s, CFstatus.ImageSpecialEffect);
  210. s->set_wb_mode(s, CFstatus.ImageWbMode); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
  211. s->set_ae_level(s, CFstatus.ImageAeLevel); // -2 to 2
  212. s->set_dcw(s, CFstatus.ImageDcw); // 0 = disable , 1 = enable
  213. s->set_bpc(s, CFstatus.ImageBpc); // 0 = disable , 1 = enable
  214. s->set_wpc(s, CFstatus.ImageWpc); // 0 = disable , 1 = enable
  215. s->set_awb_gain(s, CFstatus.ImageAwbGain); // 0 = disable , 1 = enable
  216. s->set_agc_gain(s, CFstatus.ImageAgcGain); // 0 to 30
  217. s->set_raw_gma(s, CFstatus.ImageRawGma); // 0 = disable , 1 = enable
  218. s->set_lenc(s, CFstatus.ImageLenc); // 0 = disable , 1 = enable
  219. // s->set_sharpness(s, CFstatus.ImageSharpness); // auto-sharpness is not officially supported, default to 0
  220. Camera.SetCamSharpness(CFstatus.ImageAutoSharpness, CFstatus.ImageSharpness);
  221. s->set_denoise(s, CFstatus.ImageDenoiseLevel); // The OV2640 does not support it, OV3660 and OV5640 (0 to 8)
  222. TickType_t xDelay2 = 100 / portTICK_PERIOD_MS;
  223. vTaskDelay(xDelay2);
  224. return ESP_OK;
  225. }
  226. else
  227. {
  228. return ESP_FAIL;
  229. }
  230. }
  231. esp_err_t handler_get_heap(httpd_req_t *req)
  232. {
  233. #ifdef DEBUG_DETAIL_ON
  234. LogFile.WriteHeapInfo("handler_get_heap - Start");
  235. ESP_LOGD(TAG, "handler_get_heap uri: %s", req->uri);
  236. #endif
  237. std::string zw = "Heap info:<br>" + getESPHeapInfo();
  238. #ifdef TASK_ANALYSIS_ON
  239. char *pcTaskList = (char *)calloc_psram_heap(std::string(TAG) + "->pcTaskList", 1, sizeof(char) * 768, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  240. if (pcTaskList)
  241. {
  242. vTaskList(pcTaskList);
  243. zw = zw + "<br><br>Task info:<br><pre>Name | State | Prio | Lowest stacksize | Creation order | CPU (-1=NoAffinity)<br>" + std::string(pcTaskList) + "</pre>";
  244. free_psram_heap(std::string(TAG) + "->pcTaskList", pcTaskList);
  245. }
  246. else
  247. {
  248. zw = zw + "<br><br>Task info:<br>ERROR - Allocation of TaskList buffer in PSRAM failed";
  249. }
  250. #endif
  251. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  252. if (zw.length() > 0)
  253. {
  254. httpd_resp_send(req, zw.c_str(), zw.length());
  255. }
  256. else
  257. {
  258. httpd_resp_send(req, NULL, 0);
  259. }
  260. #ifdef DEBUG_DETAIL_ON
  261. LogFile.WriteHeapInfo("handler_get_heap - Done");
  262. #endif
  263. return ESP_OK;
  264. }
  265. esp_err_t handler_init(httpd_req_t *req)
  266. {
  267. #ifdef DEBUG_DETAIL_ON
  268. LogFile.WriteHeapInfo("handler_init - Start");
  269. ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
  270. #endif
  271. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  272. const char *resp_str = "Init started<br>";
  273. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  274. doInit();
  275. resp_str = "Init done<br>";
  276. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  277. #ifdef DEBUG_DETAIL_ON
  278. LogFile.WriteHeapInfo("handler_init - Done");
  279. #endif
  280. return ESP_OK;
  281. }
  282. esp_err_t handler_stream(httpd_req_t *req)
  283. {
  284. #ifdef DEBUG_DETAIL_ON
  285. LogFile.WriteHeapInfo("handler_stream - Start");
  286. ESP_LOGD(TAG, "handler_stream uri: %s", req->uri);
  287. #endif
  288. char _query[50];
  289. char _value[10];
  290. bool flashlightOn = false;
  291. if (httpd_req_get_url_query_str(req, _query, 50) == ESP_OK)
  292. {
  293. // ESP_LOGD(TAG, "Query: %s", _query);
  294. if (httpd_query_key_value(_query, "flashlight", _value, 10) == ESP_OK)
  295. {
  296. #ifdef DEBUG_DETAIL_ON
  297. ESP_LOGD(TAG, "flashlight is found%s", _value);
  298. #endif
  299. if (strlen(_value) > 0)
  300. {
  301. flashlightOn = true;
  302. }
  303. }
  304. }
  305. Camera.CaptureToStream(req, flashlightOn);
  306. #ifdef DEBUG_DETAIL_ON
  307. LogFile.WriteHeapInfo("handler_stream - Done");
  308. #endif
  309. return ESP_OK;
  310. }
  311. esp_err_t handler_flow_start(httpd_req_t *req)
  312. {
  313. #ifdef DEBUG_DETAIL_ON
  314. LogFile.WriteHeapInfo("handler_flow_start - Start");
  315. #endif
  316. ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
  317. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  318. if (autostartIsEnabled)
  319. {
  320. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  321. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
  322. const char *resp_str = "The flow is going to be started immediately or is already running";
  323. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  324. }
  325. else
  326. {
  327. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
  328. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow start triggered by REST API, but flow is not active");
  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. CFstatus.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. int _ImageLedIntensity = std::stoi(_valuechar);
  1135. CFstatus.ImageLedIntensity = Camera.SetLEDIntensity(_ImageLedIntensity);
  1136. }
  1137. }
  1138. if (_task.compare("cam_settings") == 0)
  1139. {
  1140. // wird aufgerufen, wenn das Referenzbild + Kameraeinstellungen gespeichert wurden
  1141. setCFstatusToCCstatus(); // CFstatus >>> CCstatus
  1142. // Kameraeinstellungen wurden verädert
  1143. CFstatus.changedCameraSettings = true;
  1144. ESP_LOGD(TAG, "Cam Settings set");
  1145. std::string _zw = "CamSettingsSet";
  1146. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1147. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1148. }
  1149. else
  1150. {
  1151. // wird aufgerufen, wenn ein neues Referenzbild erstellt oder aktualisiert wurde
  1152. // CFstatus >>> Kamera
  1153. setCFstatusToCam();
  1154. Camera.SetQualityZoomSize(CFstatus.ImageQuality, CFstatus.ImageFrameSize, CFstatus.ImageZoomEnabled, CFstatus.ImageZoomOffsetX, CFstatus.ImageZoomOffsetY, CFstatus.ImageZoomSize, CFstatus.ImageVflip);
  1155. // Camera.SetZoomSize(CFstatus.ImageZoomEnabled, CFstatus.ImageZoomOffsetX, CFstatus.ImageZoomOffsetY, CFstatus.ImageZoomSize, CFstatus.ImageVflip);
  1156. // Kameraeinstellungen wurden verädert
  1157. CFstatus.changedCameraSettings = true;
  1158. ESP_LOGD(TAG, "test_take - vor TakeImage");
  1159. std::string image_temp = flowctrl.doSingleStep("[TakeImage]", _host);
  1160. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1161. httpd_resp_send(req, image_temp.c_str(), image_temp.length());
  1162. }
  1163. }
  1164. if (_task.compare("test_align") == 0)
  1165. {
  1166. std::string _host = "";
  1167. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  1168. {
  1169. _host = std::string(_valuechar);
  1170. }
  1171. std::string zw = flowctrl.doSingleStep("[Alignment]", _host);
  1172. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1173. httpd_resp_send(req, zw.c_str(), zw.length());
  1174. }
  1175. }
  1176. else
  1177. {
  1178. std::string _zw = "DeviceIsBusy";
  1179. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1180. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1181. }
  1182. #ifdef DEBUG_DETAIL_ON
  1183. LogFile.WriteHeapInfo("handler_editflow - Done");
  1184. #endif
  1185. return ESP_OK;
  1186. }
  1187. esp_err_t handler_statusflow(httpd_req_t *req)
  1188. {
  1189. #ifdef DEBUG_DETAIL_ON
  1190. LogFile.WriteHeapInfo("handler_statusflow - Start");
  1191. #endif
  1192. const char *resp_str;
  1193. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1194. if (bTaskAutoFlowCreated)
  1195. {
  1196. #ifdef DEBUG_DETAIL_ON
  1197. ESP_LOGD(TAG, "handler_statusflow: %s", req->uri);
  1198. #endif
  1199. string *zw = flowctrl.getActStatusWithTime();
  1200. resp_str = zw->c_str();
  1201. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1202. }
  1203. else
  1204. {
  1205. resp_str = "Flow task not yet created";
  1206. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1207. }
  1208. #ifdef DEBUG_DETAIL_ON
  1209. LogFile.WriteHeapInfo("handler_statusflow - Done");
  1210. #endif
  1211. return ESP_OK;
  1212. }
  1213. esp_err_t handler_cputemp(httpd_req_t *req)
  1214. {
  1215. #ifdef DEBUG_DETAIL_ON
  1216. LogFile.WriteHeapInfo("handler_cputemp - Start");
  1217. #endif
  1218. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1219. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  1220. #ifdef DEBUG_DETAIL_ON
  1221. LogFile.WriteHeapInfo("handler_cputemp - End");
  1222. #endif
  1223. return ESP_OK;
  1224. }
  1225. esp_err_t handler_rssi(httpd_req_t *req)
  1226. {
  1227. #ifdef DEBUG_DETAIL_ON
  1228. LogFile.WriteHeapInfo("handler_rssi - Start");
  1229. #endif
  1230. if (getWIFIisConnected())
  1231. {
  1232. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1233. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  1234. }
  1235. else
  1236. {
  1237. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  1238. return ESP_ERR_NOT_FOUND;
  1239. }
  1240. #ifdef DEBUG_DETAIL_ON
  1241. LogFile.WriteHeapInfo("handler_rssi - End");
  1242. #endif
  1243. return ESP_OK;
  1244. }
  1245. esp_err_t handler_current_date(httpd_req_t *req)
  1246. {
  1247. #ifdef DEBUG_DETAIL_ON
  1248. LogFile.WriteHeapInfo("handler_uptime - Start");
  1249. #endif
  1250. std::string formatedDateAndTime = getCurrentTimeString("%Y-%m-%d %H:%M:%S");
  1251. // std::string formatedDate = getCurrentTimeString("%Y-%m-%d");
  1252. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1253. httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
  1254. /* Respond with an empty chunk to signal HTTP response completion */
  1255. httpd_resp_sendstr_chunk(req, NULL);
  1256. #ifdef DEBUG_DETAIL_ON
  1257. LogFile.WriteHeapInfo("handler_uptime - End");
  1258. #endif
  1259. return ESP_OK;
  1260. }
  1261. esp_err_t handler_uptime(httpd_req_t *req)
  1262. {
  1263. #ifdef DEBUG_DETAIL_ON
  1264. LogFile.WriteHeapInfo("handler_uptime - Start");
  1265. #endif
  1266. std::string formatedUptime = getFormatedUptime(false);
  1267. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1268. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  1269. #ifdef DEBUG_DETAIL_ON
  1270. LogFile.WriteHeapInfo("handler_uptime - End");
  1271. #endif
  1272. return ESP_OK;
  1273. }
  1274. esp_err_t handler_prevalue(httpd_req_t *req)
  1275. {
  1276. #ifdef DEBUG_DETAIL_ON
  1277. LogFile.WriteHeapInfo("handler_prevalue - Start");
  1278. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  1279. #endif
  1280. // Default usage message when handler gets called without any parameter
  1281. const std::string RESTUsageInfo =
  1282. "00: Handler usage:<br>"
  1283. "- To retrieve actual PreValue, please provide only a numbersname, e.g. /setPreValue?numbers=main<br>"
  1284. "- To set PreValue to a new value, please provide a numbersname and a value, e.g. /setPreValue?numbers=main&value=1234.5678<br>"
  1285. "NOTE:<br>"
  1286. "value >= 0.0: Set PreValue to provided value<br>"
  1287. "value < 0.0: Set PreValue to actual RAW value (as long RAW value is a valid number, without N)";
  1288. // Default return error message when no return is programmed
  1289. std::string sReturnMessage = "E90: Uninitialized";
  1290. char _query[100];
  1291. char _numbersname[50] = "default";
  1292. char _value[20] = "";
  1293. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1294. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  1295. {
  1296. #ifdef DEBUG_DETAIL_ON
  1297. ESP_LOGD(TAG, "Query: %s", _query);
  1298. #endif
  1299. if (httpd_query_key_value(_query, "numbers", _numbersname, 50) != ESP_OK)
  1300. {
  1301. // If request is incomplete
  1302. sReturnMessage = "E91: Query parameter incomplete or not valid!<br> "
  1303. "Call /setPreValue to show REST API usage info and/or check documentation";
  1304. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1305. return ESP_FAIL;
  1306. }
  1307. if (httpd_query_key_value(_query, "value", _value, 20) == ESP_OK)
  1308. {
  1309. #ifdef DEBUG_DETAIL_ON
  1310. ESP_LOGD(TAG, "Value: %s", _value);
  1311. #endif
  1312. }
  1313. }
  1314. else
  1315. {
  1316. // if no parameter is provided, print handler usage
  1317. httpd_resp_send(req, RESTUsageInfo.c_str(), RESTUsageInfo.length());
  1318. return ESP_OK;
  1319. }
  1320. if (strlen(_value) == 0)
  1321. {
  1322. // If no value is povided --> return actual PreValue
  1323. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1324. if (sReturnMessage.empty())
  1325. {
  1326. sReturnMessage = "E92: Numbers name not found";
  1327. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1328. return ESP_FAIL;
  1329. }
  1330. }
  1331. else
  1332. {
  1333. // New value is positive: Set PreValue to provided value and return value
  1334. // New value is negative and actual RAW value is a valid number: Set PreValue to RAW value and return value
  1335. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "REST API handler_prevalue called: numbersname: " + std::string(_numbersname) + ", value: " + std::string(_value));
  1336. if (!flowctrl.UpdatePrevalue(_value, _numbersname, true))
  1337. {
  1338. sReturnMessage = "E93: Update request rejected. Please check device logs for more details";
  1339. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1340. return ESP_FAIL;
  1341. }
  1342. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1343. if (sReturnMessage.empty())
  1344. {
  1345. sReturnMessage = "E94: Numbers name not found";
  1346. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1347. return ESP_FAIL;
  1348. }
  1349. }
  1350. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1351. #ifdef DEBUG_DETAIL_ON
  1352. LogFile.WriteHeapInfo("handler_prevalue - End");
  1353. #endif
  1354. return ESP_OK;
  1355. }
  1356. void task_autodoFlow(void *pvParameter)
  1357. {
  1358. int64_t fr_start, fr_delta_ms;
  1359. bTaskAutoFlowCreated = true;
  1360. if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
  1361. {
  1362. flowctrl.setActStatus("Initialization (delayed)");
  1363. // #ifdef ENABLE_MQTT
  1364. // MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  1365. // #endif //ENABLE_MQTT
  1366. vTaskDelay(60 * 5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
  1367. }
  1368. ESP_LOGD(TAG, "task_autodoFlow: start");
  1369. doInit();
  1370. flowctrl.setAutoStartInterval(auto_interval);
  1371. autostartIsEnabled = flowctrl.getIsAutoStart();
  1372. if (isSetupModusActive())
  1373. {
  1374. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "We are in Setup Mode -> Not starting Auto Flow!");
  1375. autostartIsEnabled = false;
  1376. // 15.7.0 Setup Wizard cannot take a Reference Picture #2953
  1377. // std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  1378. // flowctrl.doFlowTakeImageOnly(zw_time);
  1379. }
  1380. if (autostartIsEnabled)
  1381. {
  1382. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting Flow...");
  1383. }
  1384. else
  1385. {
  1386. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Autostart is not enabled -> Not starting Flow");
  1387. }
  1388. while (autostartIsEnabled)
  1389. {
  1390. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  1391. time_t roundStartTime = getUpTime();
  1392. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  1393. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  1394. fr_start = esp_timer_get_time();
  1395. if (flowisrunning)
  1396. {
  1397. #ifdef DEBUG_DETAIL_ON
  1398. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  1399. #endif
  1400. }
  1401. else
  1402. {
  1403. #ifdef DEBUG_DETAIL_ON
  1404. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  1405. #endif
  1406. flowisrunning = true;
  1407. doflow();
  1408. #ifdef DEBUG_DETAIL_ON
  1409. ESP_LOGD(TAG, "Remove older log files");
  1410. #endif
  1411. LogFile.RemoveOldLogFile();
  1412. LogFile.RemoveOldDataLog();
  1413. }
  1414. // Round finished -> Logfile
  1415. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) + " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  1416. // CPU Temp -> Logfile
  1417. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  1418. // WIFI Signal Strength (RSSI) -> Logfile
  1419. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  1420. // Check if time is synchronized (if NTP is configured)
  1421. if (getUseNtp() && !getTimeIsSet())
  1422. {
  1423. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set!");
  1424. StatusLED(TIME_CHECK, 1, false);
  1425. }
  1426. #if (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES)
  1427. wifiRoamingQuery();
  1428. #endif
  1429. // Scan channels and check if an AP with better RSSI is available, then disconnect and try to reconnect to AP with better RSSI
  1430. // NOTE: Keep this direct before the following task delay, because scan is done in blocking mode and this takes ca. 1,5 - 2s.
  1431. #ifdef WLAN_USE_ROAMING_BY_SCANNING
  1432. wifiRoamByScanning();
  1433. #endif
  1434. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  1435. if (auto_interval > fr_delta_ms)
  1436. {
  1437. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  1438. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long)xDelay);
  1439. vTaskDelay(xDelay);
  1440. }
  1441. }
  1442. while (1)
  1443. {
  1444. // Keep flow task running to handle necessary sub tasks like reboot handler, etc..
  1445. vTaskDelay(2000 / portTICK_PERIOD_MS);
  1446. }
  1447. vTaskDelete(NULL); // Delete this task if it exits from the loop above
  1448. xHandletask_autodoFlow = NULL;
  1449. ESP_LOGD(TAG, "task_autodoFlow: end");
  1450. }
  1451. void InitializeFlowTask(void)
  1452. {
  1453. BaseType_t xReturned;
  1454. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1455. uint32_t stackSize = 16 * 1024;
  1456. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", stackSize, NULL, tskIDLE_PRIORITY + 2, &xHandletask_autodoFlow, 0);
  1457. if (xReturned != pdPASS)
  1458. {
  1459. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Creation task_autodoFlow failed. Requested stack size:" + std::to_string(stackSize));
  1460. LogFile.WriteHeapInfo("Creation task_autodoFlow failed");
  1461. }
  1462. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1463. }
  1464. void register_server_main_flow_task_uri(httpd_handle_t server)
  1465. {
  1466. ESP_LOGI(TAG, "server_main_flow_task - Registering URI handlers");
  1467. httpd_uri_t camuri = {};
  1468. camuri.method = HTTP_GET;
  1469. camuri.uri = "/doinit";
  1470. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_init);
  1471. camuri.user_ctx = (void *)"Light On";
  1472. httpd_register_uri_handler(server, &camuri);
  1473. // Legacy API => New: "/setPreValue"
  1474. camuri.uri = "/setPreValue.html";
  1475. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1476. camuri.user_ctx = (void *)"Prevalue";
  1477. httpd_register_uri_handler(server, &camuri);
  1478. camuri.uri = "/setPreValue";
  1479. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1480. camuri.user_ctx = (void *)"Prevalue";
  1481. httpd_register_uri_handler(server, &camuri);
  1482. camuri.uri = "/flow_start";
  1483. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_flow_start);
  1484. camuri.user_ctx = (void *)"Flow Start";
  1485. httpd_register_uri_handler(server, &camuri);
  1486. camuri.uri = "/statusflow.html";
  1487. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1488. camuri.user_ctx = (void *)"Light Off";
  1489. httpd_register_uri_handler(server, &camuri);
  1490. camuri.uri = "/statusflow";
  1491. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1492. camuri.user_ctx = (void *)"Light Off";
  1493. httpd_register_uri_handler(server, &camuri);
  1494. // Legacy API => New: "/cpu_temperature"
  1495. camuri.uri = "/cputemp.html";
  1496. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1497. camuri.user_ctx = (void *)"Light Off";
  1498. httpd_register_uri_handler(server, &camuri);
  1499. camuri.uri = "/cpu_temperature";
  1500. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1501. camuri.user_ctx = (void *)"Light Off";
  1502. httpd_register_uri_handler(server, &camuri);
  1503. // Legacy API => New: "/rssi"
  1504. camuri.uri = "/rssi.html";
  1505. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1506. camuri.user_ctx = (void *)"Light Off";
  1507. httpd_register_uri_handler(server, &camuri);
  1508. camuri.uri = "/rssi";
  1509. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1510. camuri.user_ctx = (void *)"Light Off";
  1511. httpd_register_uri_handler(server, &camuri);
  1512. camuri.uri = "/date";
  1513. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_current_date);
  1514. camuri.user_ctx = (void *)"Light Off";
  1515. httpd_register_uri_handler(server, &camuri);
  1516. camuri.uri = "/uptime";
  1517. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_uptime);
  1518. camuri.user_ctx = (void *)"Light Off";
  1519. httpd_register_uri_handler(server, &camuri);
  1520. camuri.uri = "/editflow";
  1521. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_editflow);
  1522. camuri.user_ctx = (void *)"EditFlow";
  1523. httpd_register_uri_handler(server, &camuri);
  1524. // Legacy API => New: "/value"
  1525. camuri.uri = "/value.html";
  1526. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1527. camuri.user_ctx = (void *)"Value";
  1528. httpd_register_uri_handler(server, &camuri);
  1529. camuri.uri = "/value";
  1530. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1531. camuri.user_ctx = (void *)"Value";
  1532. httpd_register_uri_handler(server, &camuri);
  1533. // Legacy API => New: "/value"
  1534. camuri.uri = "/wasserzaehler.html";
  1535. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1536. camuri.user_ctx = (void *)"Wasserzaehler";
  1537. httpd_register_uri_handler(server, &camuri);
  1538. camuri.uri = "/json";
  1539. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_json);
  1540. camuri.user_ctx = (void *)"JSON";
  1541. httpd_register_uri_handler(server, &camuri);
  1542. camuri.uri = "/heap";
  1543. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_get_heap);
  1544. camuri.user_ctx = (void *)"Heap";
  1545. httpd_register_uri_handler(server, &camuri);
  1546. camuri.uri = "/stream";
  1547. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_stream);
  1548. camuri.user_ctx = (void *)"stream";
  1549. httpd_register_uri_handler(server, &camuri);
  1550. /** will handle metrics requests */
  1551. camuri.uri = "/metrics";
  1552. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_openmetrics);
  1553. camuri.user_ctx = (void *)"metrics";
  1554. httpd_register_uri_handler(server, &camuri);
  1555. /** when adding a new handler, make sure to increment the value for config.max_uri_handlers in `main/server_main.cpp` */
  1556. }