MainFlowControl.cpp 51 KB

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