MainFlowControl.cpp 63 KB

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