server_tflite.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. #include "server_tflite.h"
  2. #include <string>
  3. #include <vector>
  4. #include "string.h"
  5. #include "esp_log.h"
  6. #include <iomanip>
  7. #include <sstream>
  8. #include "../../include/defines.h"
  9. #include "Helper.h"
  10. #include "esp_camera.h"
  11. #include "time_sntp.h"
  12. #include "ClassControllCamera.h"
  13. #include "ClassFlowControll.h"
  14. #include "ClassLogFile.h"
  15. #include "server_GPIO.h"
  16. #include "server_file.h"
  17. #include "connect_wlan.h"
  18. ClassFlowControll tfliteflow;
  19. TaskHandle_t xHandletask_autodoFlow = NULL;
  20. bool bTaskAutoFlowCreated = false;
  21. bool flowisrunning = false;
  22. long auto_interval = 0;
  23. bool auto_isrunning = false;
  24. int countRounds = 0;
  25. bool isPlannedReboot = false;
  26. static const char *TAG = "TFLITE SERVER";
  27. //#define DEBUG_DETAIL_ON
  28. void CheckIsPlannedReboot()
  29. {
  30. FILE *pfile;
  31. if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL)
  32. {
  33. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Not a planned reboot.");
  34. isPlannedReboot = false;
  35. }
  36. else
  37. {
  38. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot.");
  39. DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
  40. isPlannedReboot = true;
  41. }
  42. }
  43. int getCountFlowRounds()
  44. {
  45. return countRounds;
  46. }
  47. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  48. {
  49. return tfliteflow.GetJPGStream(_filename, req);
  50. }
  51. esp_err_t GetRawJPG(httpd_req_t *req)
  52. {
  53. return tfliteflow.SendRawJPG(req);
  54. }
  55. bool isSetupModusActive() {
  56. return tfliteflow.getStatusSetupModus();
  57. return false;
  58. }
  59. void KillTFliteTasks()
  60. {
  61. #ifdef DEBUG_DETAIL_ON
  62. ESP_LOGD(TAG, "KillTFliteTasks: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
  63. #endif
  64. if( xHandletask_autodoFlow != NULL )
  65. {
  66. vTaskDelete(xHandletask_autodoFlow);
  67. xHandletask_autodoFlow = NULL;
  68. }
  69. #ifdef DEBUG_DETAIL_ON
  70. ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
  71. #endif
  72. }
  73. void doInit(void)
  74. {
  75. #ifdef DEBUG_DETAIL_ON
  76. ESP_LOGD(TAG, "Start tfliteflow.InitFlow(config);");
  77. #endif
  78. tfliteflow.InitFlow(CONFIG_FILE);
  79. #ifdef DEBUG_DETAIL_ON
  80. ESP_LOGD(TAG, "Finished tfliteflow.InitFlow(config);");
  81. #endif
  82. /* GPIO handler has to be initialized before MQTT init to ensure proper topic subscription */
  83. gpio_handler_init();
  84. #ifdef ENABLE_MQTT
  85. tfliteflow.StartMQTTService();
  86. #endif //ENABLE_MQTT
  87. }
  88. bool doflow(void)
  89. {
  90. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  91. ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
  92. flowisrunning = true;
  93. tfliteflow.doFlow(zw_time);
  94. flowisrunning = false;
  95. #ifdef DEBUG_DETAIL_ON
  96. ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
  97. #endif
  98. return true;
  99. }
  100. esp_err_t handler_get_heap(httpd_req_t *req)
  101. {
  102. #ifdef DEBUG_DETAIL_ON
  103. LogFile.WriteHeapInfo("handler_get_heap - Start");
  104. ESP_LOGD(TAG, "handler_get_heap uri: %s", req->uri);
  105. #endif
  106. std::string zw = "Heap info:<br>" + getESPHeapInfo();
  107. #ifdef TASK_ANALYSIS_ON
  108. char* pcTaskList = (char*) heap_caps_calloc(1, sizeof(char) * 768, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  109. if (pcTaskList) {
  110. vTaskList(pcTaskList);
  111. zw = zw + "<br><br>Task info:<br><pre>Name | State | Prio | Lowest stacksize | Creation order | CPU (-1=NoAffinity)<br>"
  112. + std::string(pcTaskList) + "</pre>";
  113. heap_caps_free(pcTaskList);
  114. }
  115. else {
  116. zw = zw + "<br><br>Task info:<br>ERROR - Allocation of TaskList buffer in PSRAM failed";
  117. }
  118. #endif
  119. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  120. if (zw.length() > 0)
  121. {
  122. httpd_resp_send(req, zw.c_str(), zw.length());
  123. }
  124. else
  125. {
  126. httpd_resp_send(req, NULL, 0);
  127. }
  128. #ifdef DEBUG_DETAIL_ON
  129. LogFile.WriteHeapInfo("handler_get_heap - Done");
  130. #endif
  131. return ESP_OK;
  132. }
  133. esp_err_t handler_init(httpd_req_t *req)
  134. {
  135. #ifdef DEBUG_DETAIL_ON
  136. LogFile.WriteHeapInfo("handler_init - Start");
  137. ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
  138. #endif
  139. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  140. const char* resp_str = "Init started<br>";
  141. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  142. doInit();
  143. resp_str = "Init done<br>";
  144. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  145. #ifdef DEBUG_DETAIL_ON
  146. LogFile.WriteHeapInfo("handler_init - Done");
  147. #endif
  148. return ESP_OK;
  149. }
  150. esp_err_t handler_flow_start(httpd_req_t *req) {
  151. #ifdef DEBUG_DETAIL_ON
  152. LogFile.WriteHeapInfo("handler_flow_start - Start");
  153. #endif
  154. ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
  155. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  156. if (auto_isrunning) {
  157. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  158. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
  159. const char* resp_str = "The flow is going to be started immediately or is already running";
  160. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  161. }
  162. else {
  163. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
  164. const char* resp_str = "WARNING: Flow start triggered by REST API, but flow is not active";
  165. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  166. }
  167. #ifdef DEBUG_DETAIL_ON
  168. LogFile.WriteHeapInfo("handler_flow_start - Done");
  169. #endif
  170. return ESP_OK;
  171. }
  172. #ifdef ENABLE_MQTT
  173. esp_err_t MQTTCtrlFlowStart(std::string _topic) {
  174. #ifdef DEBUG_DETAIL_ON
  175. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Start");
  176. #endif
  177. ESP_LOGD(TAG, "MQTTCtrlFlowStart: topic %s", _topic.c_str());
  178. if (auto_isrunning)
  179. {
  180. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  181. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by MQTT topic " + _topic);
  182. }
  183. else
  184. {
  185. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by MQTT topic " + _topic + ", but flow is not active!");
  186. }
  187. #ifdef DEBUG_DETAIL_ON
  188. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Done");
  189. #endif
  190. return ESP_OK;
  191. }
  192. #endif //ENABLE_MQTT
  193. esp_err_t handler_json(httpd_req_t *req)
  194. {
  195. #ifdef DEBUG_DETAIL_ON
  196. LogFile.WriteHeapInfo("handler_json - Start");
  197. #endif
  198. ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
  199. if (bTaskAutoFlowCreated)
  200. {
  201. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  202. httpd_resp_set_type(req, "application/json");
  203. std::string zw = tfliteflow.getJSON();
  204. if (zw.length() > 0)
  205. {
  206. httpd_resp_send(req, zw.c_str(), zw.length());
  207. }
  208. else
  209. {
  210. httpd_resp_send(req, NULL, 0);
  211. }
  212. }
  213. else
  214. {
  215. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!");
  216. return ESP_ERR_NOT_FOUND;
  217. }
  218. #ifdef DEBUG_DETAIL_ON
  219. LogFile.WriteHeapInfo("handler_JSON - Done");
  220. #endif
  221. return ESP_OK;
  222. }
  223. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  224. {
  225. #ifdef DEBUG_DETAIL_ON
  226. LogFile.WriteHeapInfo("handler water counter - Start");
  227. #endif
  228. if (bTaskAutoFlowCreated)
  229. {
  230. bool _rawValue = false;
  231. bool _noerror = false;
  232. bool _all = false;
  233. std::string _type = "value";
  234. string zw;
  235. ESP_LOGD(TAG, "handler water counter uri: %s", req->uri);
  236. char _query[100];
  237. char _size[10];
  238. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  239. {
  240. // ESP_LOGD(TAG, "Query: %s", _query);
  241. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  242. {
  243. #ifdef DEBUG_DETAIL_ON
  244. ESP_LOGD(TAG, "all is found%s", _size);
  245. #endif
  246. _all = true;
  247. }
  248. if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
  249. {
  250. #ifdef DEBUG_DETAIL_ON
  251. ESP_LOGD(TAG, "all is found: %s", _size);
  252. #endif
  253. _type = std::string(_size);
  254. }
  255. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  256. {
  257. #ifdef DEBUG_DETAIL_ON
  258. ESP_LOGD(TAG, "rawvalue is found: %s", _size);
  259. #endif
  260. _rawValue = true;
  261. }
  262. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  263. {
  264. #ifdef DEBUG_DETAIL_ON
  265. ESP_LOGD(TAG, "noerror is found: %s", _size);
  266. #endif
  267. _noerror = true;
  268. }
  269. }
  270. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  271. if (_all)
  272. {
  273. httpd_resp_set_type(req, "text/plain");
  274. ESP_LOGD(TAG, "TYPE: %s", _type.c_str());
  275. int _intype = READOUT_TYPE_VALUE;
  276. if (_type == "prevalue")
  277. _intype = READOUT_TYPE_PREVALUE;
  278. if (_type == "raw")
  279. _intype = READOUT_TYPE_RAWVALUE;
  280. if (_type == "error")
  281. _intype = READOUT_TYPE_ERROR;
  282. zw = tfliteflow.getReadoutAll(_intype);
  283. ESP_LOGD(TAG, "ZW: %s", zw.c_str());
  284. if (zw.length() > 0)
  285. httpd_resp_send(req, zw.c_str(), zw.length());
  286. return ESP_OK;
  287. }
  288. zw = tfliteflow.getReadout(_rawValue, _noerror);
  289. if (zw.length() > 0)
  290. httpd_resp_sendstr_chunk(req, zw.c_str());
  291. string query = std::string(_query);
  292. // ESP_LOGD(TAG, "Query: %s, query.c_str());
  293. if (query.find("full") != std::string::npos)
  294. {
  295. string txt, zw;
  296. std::string *status = tfliteflow.getActStatus();
  297. if ((countRounds <= 1) && (*status != std::string("Flow finished"))) { // First round not completed yet
  298. txt = "<body style=\"font-family: arial\">";
  299. txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + *status + "</h3>\n";
  300. httpd_resp_sendstr_chunk(req, txt.c_str());
  301. }
  302. else {
  303. /* Digital ROIs */
  304. txt = "<body style=\"font-family: arial\">";
  305. txt += "<h3>Recognized Digit ROIs (previous round)</h3>\n";
  306. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  307. std::vector<HTMLInfo*> htmlinfodig;
  308. htmlinfodig = tfliteflow.GetAllDigital();
  309. for (int i = 0; i < htmlinfodig.size(); ++i)
  310. {
  311. if (tfliteflow.GetTypeDigital() == Digital)
  312. {
  313. if (htmlinfodig[i]->val == 10)
  314. zw = "NaN";
  315. else
  316. zw = to_string((int) htmlinfodig[i]->val);
  317. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  318. }
  319. else
  320. {
  321. std::stringstream stream;
  322. stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
  323. zw = stream.str();
  324. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  325. }
  326. delete htmlinfodig[i];
  327. }
  328. htmlinfodig.clear();
  329. txt += "</tr></table>\n";
  330. httpd_resp_sendstr_chunk(req, txt.c_str());
  331. /* Analog ROIs */
  332. txt = "<h3>Recognized Analog ROIs (previous round)</h3>\n";
  333. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  334. std::vector<HTMLInfo*> htmlinfoana;
  335. htmlinfoana = tfliteflow.GetAllAnalog();
  336. for (int i = 0; i < htmlinfoana.size(); ++i)
  337. {
  338. std::stringstream stream;
  339. stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
  340. zw = stream.str();
  341. txt += "<td style=\"width: 150px;\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"></p></td>\n";
  342. delete htmlinfoana[i];
  343. }
  344. htmlinfoana.clear();
  345. txt += "</tr>\n</table>\n";
  346. httpd_resp_sendstr_chunk(req, txt.c_str());
  347. /* Full Image
  348. * Only show it after the image got taken and aligned */
  349. txt = "<h3>Aligned Image (current round)</h3>\n";
  350. if ((*status == std::string("Initialization")) ||
  351. (*status == std::string("Initialization (delayed)")) ||
  352. (*status == std::string("Take Image"))) {
  353. txt += "<p>Current state: " + *status + "</p>\n";
  354. }
  355. else {
  356. txt += "<img src=\"/img_tmp/alg_roi.jpg\">\n";
  357. }
  358. httpd_resp_sendstr_chunk(req, txt.c_str());
  359. }
  360. }
  361. /* Respond with an empty chunk to signal HTTP response completion */
  362. httpd_resp_sendstr_chunk(req, NULL);
  363. }
  364. else
  365. {
  366. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!");
  367. return ESP_ERR_NOT_FOUND;
  368. }
  369. #ifdef DEBUG_DETAIL_ON
  370. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  371. #endif
  372. return ESP_OK;
  373. }
  374. esp_err_t handler_editflow(httpd_req_t *req)
  375. {
  376. #ifdef DEBUG_DETAIL_ON
  377. LogFile.WriteHeapInfo("handler_editflow - Start");
  378. #endif
  379. ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri);
  380. char _query[200];
  381. char _valuechar[30];
  382. string _task;
  383. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  384. {
  385. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  386. {
  387. #ifdef DEBUG_DETAIL_ON
  388. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  389. #endif
  390. _task = string(_valuechar);
  391. }
  392. }
  393. if (_task.compare("namenumbers") == 0)
  394. {
  395. ESP_LOGD(TAG, "Get NUMBER list");
  396. return get_numbers_file_handler(req);
  397. }
  398. if (_task.compare("data") == 0)
  399. {
  400. ESP_LOGD(TAG, "Get data list");
  401. return get_data_file_handler(req);
  402. }
  403. if (_task.compare("tflite") == 0)
  404. {
  405. ESP_LOGD(TAG, "Get tflite list");
  406. return get_tflite_file_handler(req);
  407. }
  408. if (_task.compare("copy") == 0)
  409. {
  410. string in, out, zw;
  411. httpd_query_key_value(_query, "in", _valuechar, 30);
  412. in = string(_valuechar);
  413. httpd_query_key_value(_query, "out", _valuechar, 30);
  414. out = string(_valuechar);
  415. #ifdef DEBUG_DETAIL_ON
  416. ESP_LOGD(TAG, "in: %s", in.c_str());
  417. ESP_LOGD(TAG, "out: %s", out.c_str());
  418. #endif
  419. in = "/sdcard" + in;
  420. out = "/sdcard" + out;
  421. CopyFile(in, out);
  422. zw = "Copy Done";
  423. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  424. httpd_resp_send(req, zw.c_str(), zw.length());
  425. }
  426. if (_task.compare("cutref") == 0)
  427. {
  428. string in, out, zw;
  429. int x, y, dx, dy;
  430. bool enhance = false;
  431. httpd_query_key_value(_query, "in", _valuechar, 30);
  432. in = string(_valuechar);
  433. httpd_query_key_value(_query, "out", _valuechar, 30);
  434. out = string(_valuechar);
  435. httpd_query_key_value(_query, "x", _valuechar, 30);
  436. zw = string(_valuechar);
  437. x = stoi(zw);
  438. httpd_query_key_value(_query, "y", _valuechar, 30);
  439. zw = string(_valuechar);
  440. y = stoi(zw);
  441. httpd_query_key_value(_query, "dx", _valuechar, 30);
  442. zw = string(_valuechar);
  443. dx = stoi(zw);
  444. httpd_query_key_value(_query, "dy", _valuechar, 30);
  445. zw = string(_valuechar);
  446. dy = stoi(zw);
  447. #ifdef DEBUG_DETAIL_ON
  448. ESP_LOGD(TAG, "in: %s", in.c_str());
  449. ESP_LOGD(TAG, "out: %s", out.c_str());
  450. ESP_LOGD(TAG, "x: %s", zw.c_str());
  451. ESP_LOGD(TAG, "y: %s", zw.c_str());
  452. ESP_LOGD(TAG, "dx: %s", zw.c_str());
  453. ESP_LOGD(TAG, "dy: %s", zw.c_str());
  454. #endif
  455. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  456. {
  457. zw = string(_valuechar);
  458. if (zw.compare("true") == 0)
  459. {
  460. enhance = true;
  461. }
  462. }
  463. in = "/sdcard" + in;
  464. out = "/sdcard" + out;
  465. string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  466. CAlignAndCutImage *caic = new CAlignAndCutImage(in);
  467. caic->CutAndSave(out2, x, y, dx, dy);
  468. delete caic;
  469. CImageBasis *cim = new CImageBasis(out2);
  470. if (enhance)
  471. {
  472. cim->Contrast(90);
  473. }
  474. cim->SaveToFile(out);
  475. delete cim;
  476. zw = "CutImage Done";
  477. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  478. httpd_resp_send(req, zw.c_str(), zw.length());
  479. }
  480. if (_task.compare("test_take") == 0)
  481. {
  482. std::string _host = "";
  483. std::string _bri = "";
  484. std::string _con = "";
  485. std::string _sat = "";
  486. std::string _int = "";
  487. int bri = -100;
  488. int sat = -100;
  489. int con = -100;
  490. int intens = -100;
  491. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  492. _host = std::string(_valuechar);
  493. }
  494. if (httpd_query_key_value(_query, "int", _valuechar, 30) == ESP_OK) {
  495. _int = std::string(_valuechar);
  496. intens = stoi(_int);
  497. }
  498. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK) {
  499. _bri = std::string(_valuechar);
  500. bri = stoi(_bri);
  501. }
  502. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK) {
  503. _con = std::string(_valuechar);
  504. con = stoi(_con);
  505. }
  506. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK) {
  507. _sat = std::string(_valuechar);
  508. sat = stoi(_sat);
  509. }
  510. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  511. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  512. Camera.SetBrightnessContrastSaturation(bri, con, sat);
  513. Camera.SetLEDIntensity(intens);
  514. ESP_LOGD(TAG, "test_take - vor TakeImage");
  515. std::string zw = tfliteflow.doSingleStep("[TakeImage]", _host);
  516. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  517. httpd_resp_send(req, zw.c_str(), zw.length());
  518. }
  519. if (_task.compare("test_align") == 0)
  520. {
  521. std::string _host = "";
  522. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  523. _host = std::string(_valuechar);
  524. }
  525. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  526. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  527. std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
  528. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  529. httpd_resp_send(req, zw.c_str(), zw.length());
  530. }
  531. #ifdef DEBUG_DETAIL_ON
  532. LogFile.WriteHeapInfo("handler_editflow - Done");
  533. #endif
  534. return ESP_OK;
  535. }
  536. esp_err_t handler_statusflow(httpd_req_t *req)
  537. {
  538. #ifdef DEBUG_DETAIL_ON
  539. LogFile.WriteHeapInfo("handler_prevalue - Start");
  540. #endif
  541. const char* resp_str;
  542. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  543. if (bTaskAutoFlowCreated)
  544. {
  545. #ifdef DEBUG_DETAIL_ON
  546. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  547. #endif
  548. string* zw = tfliteflow.getActStatusWithTime();
  549. resp_str = zw->c_str();
  550. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  551. }
  552. else
  553. {
  554. resp_str = "Flow task not yet created";
  555. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  556. }
  557. #ifdef DEBUG_DETAIL_ON
  558. LogFile.WriteHeapInfo("handler_prevalue - Done");
  559. #endif
  560. return ESP_OK;
  561. }
  562. esp_err_t handler_cputemp(httpd_req_t *req)
  563. {
  564. #ifdef DEBUG_DETAIL_ON
  565. LogFile.WriteHeapInfo("handler_cputemp - Start");
  566. #endif
  567. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  568. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  569. #ifdef DEBUG_DETAIL_ON
  570. LogFile.WriteHeapInfo("handler_cputemp - End");
  571. #endif
  572. return ESP_OK;
  573. }
  574. esp_err_t handler_rssi(httpd_req_t *req)
  575. {
  576. #ifdef DEBUG_DETAIL_ON
  577. LogFile.WriteHeapInfo("handler_rssi - Start");
  578. #endif
  579. if (getWIFIisConnected())
  580. {
  581. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  582. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  583. }
  584. else
  585. {
  586. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  587. return ESP_ERR_NOT_FOUND;
  588. }
  589. #ifdef DEBUG_DETAIL_ON
  590. LogFile.WriteHeapInfo("handler_rssi - End");
  591. #endif
  592. return ESP_OK;
  593. }
  594. esp_err_t handler_uptime(httpd_req_t *req)
  595. {
  596. #ifdef DEBUG_DETAIL_ON
  597. LogFile.WriteHeapInfo("handler_uptime - Start");
  598. #endif
  599. std::string formatedUptime = getFormatedUptime(false);
  600. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  601. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  602. #ifdef DEBUG_DETAIL_ON
  603. LogFile.WriteHeapInfo("handler_uptime - End");
  604. #endif
  605. return ESP_OK;
  606. }
  607. esp_err_t handler_prevalue(httpd_req_t *req)
  608. {
  609. #ifdef DEBUG_DETAIL_ON
  610. LogFile.WriteHeapInfo("handler_prevalue - Start");
  611. #endif
  612. const char* resp_str;
  613. string zw;
  614. #ifdef DEBUG_DETAIL_ON
  615. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  616. #endif
  617. char _query[100];
  618. char _size[10] = "";
  619. char _numbers[50] = "default";
  620. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  621. {
  622. #ifdef DEBUG_DETAIL_ON
  623. ESP_LOGD(TAG, "Query: %s", _query);
  624. #endif
  625. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  626. {
  627. #ifdef DEBUG_DETAIL_ON
  628. ESP_LOGD(TAG, "Value: %s", _size);
  629. #endif
  630. }
  631. httpd_query_key_value(_query, "numbers", _numbers, 50);
  632. }
  633. if (strlen(_size) == 0)
  634. {
  635. zw = tfliteflow.GetPrevalue(std::string(_numbers));
  636. }
  637. else
  638. {
  639. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size, _numbers, true);
  640. }
  641. resp_str = zw.c_str();
  642. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  643. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  644. #ifdef DEBUG_DETAIL_ON
  645. LogFile.WriteHeapInfo("handler_prevalue - End");
  646. #endif
  647. return ESP_OK;
  648. }
  649. void task_autodoFlow(void *pvParameter)
  650. {
  651. int64_t fr_start, fr_delta_ms;
  652. bTaskAutoFlowCreated = true;
  653. if (!isPlannedReboot)
  654. {
  655. if (esp_reset_reason() == ESP_RST_PANIC) {
  656. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Restarted due to an Exception/panic! Postponing first round start by 5 minutes to allow for an OTA Update or to fetch the log!");
  657. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!");
  658. LogFile.setLogLevel(ESP_LOG_DEBUG);
  659. tfliteflow.setActStatus("Initialization (delayed)");
  660. //#ifdef ENABLE_MQTT
  661. //MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  662. //#endif //ENABLE_MQTT
  663. vTaskDelay(60*5000 / portTICK_RATE_MS); // Wait 5 minutes to give time to do an OTA Update or fetch the log
  664. }
  665. }
  666. ESP_LOGD(TAG, "task_autodoFlow: start");
  667. doInit();
  668. auto_isrunning = tfliteflow.isAutoStart(auto_interval);
  669. if (isSetupModusActive())
  670. {
  671. auto_isrunning = false;
  672. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  673. tfliteflow.doFlowTakeImageOnly(zw_time);
  674. }
  675. while (auto_isrunning)
  676. {
  677. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  678. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  679. time_t roundStartTime = getUpTime();
  680. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  681. fr_start = esp_timer_get_time();
  682. if (flowisrunning)
  683. {
  684. #ifdef DEBUG_DETAIL_ON
  685. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  686. #endif
  687. }
  688. else
  689. {
  690. #ifdef DEBUG_DETAIL_ON
  691. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  692. #endif
  693. flowisrunning = true;
  694. doflow();
  695. #ifdef DEBUG_DETAIL_ON
  696. ESP_LOGD(TAG, "Remove older log files");
  697. #endif
  698. LogFile.RemoveOldLogFile();
  699. LogFile.RemoveOldDataLog();
  700. }
  701. //CPU Temp -> Logfile
  702. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  703. // WIFI Signal Strength (RSSI) -> Logfile
  704. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  705. //Round finished -> Logfile
  706. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) +
  707. " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  708. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  709. if (auto_interval > fr_delta_ms)
  710. {
  711. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  712. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long) xDelay);
  713. vTaskDelay( xDelay );
  714. }
  715. }
  716. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  717. xHandletask_autodoFlow = NULL;
  718. ESP_LOGD(TAG, "task_autodoFlow: end");
  719. }
  720. void TFliteDoAutoStart()
  721. {
  722. BaseType_t xReturned;
  723. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  724. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow, 0);
  725. //xReturned = xTaskCreate(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow);
  726. if( xReturned != pdPASS )
  727. {
  728. ESP_LOGD(TAG, "ERROR task_autodoFlow konnte nicht erzeugt werden!");
  729. }
  730. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  731. }
  732. #ifdef ENABLE_MQTT
  733. std::string GetMQTTMainTopic()
  734. {
  735. return tfliteflow.GetMQTTMainTopic();
  736. }
  737. #endif//ENABLE_MQTT
  738. void register_server_tflite_uri(httpd_handle_t server)
  739. {
  740. ESP_LOGI(TAG, "server_part_camera - Registering URI handlers");
  741. httpd_uri_t camuri = { };
  742. camuri.method = HTTP_GET;
  743. camuri.uri = "/doinit";
  744. camuri.handler = handler_init;
  745. camuri.user_ctx = (void*) "Light On";
  746. httpd_register_uri_handler(server, &camuri);
  747. // Legacy API => New: "/setPreValue"
  748. camuri.uri = "/setPreValue.html";
  749. camuri.handler = handler_prevalue;
  750. camuri.user_ctx = (void*) "Prevalue";
  751. httpd_register_uri_handler(server, &camuri);
  752. camuri.uri = "/setPreValue";
  753. camuri.handler = handler_prevalue;
  754. camuri.user_ctx = (void*) "Prevalue";
  755. httpd_register_uri_handler(server, &camuri);
  756. camuri.uri = "/flow_start";
  757. camuri.handler = handler_flow_start;
  758. camuri.user_ctx = (void*) "Flow Start";
  759. httpd_register_uri_handler(server, &camuri);
  760. camuri.uri = "/statusflow.html";
  761. camuri.handler = handler_statusflow;
  762. camuri.user_ctx = (void*) "Light Off";
  763. httpd_register_uri_handler(server, &camuri);
  764. camuri.uri = "/statusflow";
  765. camuri.handler = handler_statusflow;
  766. camuri.user_ctx = (void*) "Light Off";
  767. httpd_register_uri_handler(server, &camuri);
  768. // Legacy API => New: "/cpu_temperature"
  769. camuri.uri = "/cputemp.html";
  770. camuri.handler = handler_cputemp;
  771. camuri.user_ctx = (void*) "Light Off";
  772. httpd_register_uri_handler(server, &camuri);
  773. camuri.uri = "/cpu_temperature";
  774. camuri.handler = handler_cputemp;
  775. camuri.user_ctx = (void*) "Light Off";
  776. httpd_register_uri_handler(server, &camuri);
  777. // Legacy API => New: "/rssi"
  778. camuri.uri = "/rssi.html";
  779. camuri.handler = handler_rssi;
  780. camuri.user_ctx = (void*) "Light Off";
  781. httpd_register_uri_handler(server, &camuri);
  782. camuri.uri = "/rssi";
  783. camuri.handler = handler_rssi;
  784. camuri.user_ctx = (void*) "Light Off";
  785. httpd_register_uri_handler(server, &camuri);
  786. camuri.uri = "/uptime";
  787. camuri.handler = handler_uptime;
  788. camuri.user_ctx = (void*) "Light Off";
  789. httpd_register_uri_handler(server, &camuri);
  790. camuri.uri = "/editflow";
  791. camuri.handler = handler_editflow;
  792. camuri.user_ctx = (void*) "EditFlow";
  793. httpd_register_uri_handler(server, &camuri);
  794. // Legacy API => New: "/value"
  795. camuri.uri = "/value.html";
  796. camuri.handler = handler_wasserzaehler;
  797. camuri.user_ctx = (void*) "Value";
  798. httpd_register_uri_handler(server, &camuri);
  799. camuri.uri = "/value";
  800. camuri.handler = handler_wasserzaehler;
  801. camuri.user_ctx = (void*) "Value";
  802. httpd_register_uri_handler(server, &camuri);
  803. // Legacy API => New: "/value"
  804. camuri.uri = "/wasserzaehler.html";
  805. camuri.handler = handler_wasserzaehler;
  806. camuri.user_ctx = (void*) "Wasserzaehler";
  807. httpd_register_uri_handler(server, &camuri);
  808. camuri.uri = "/json";
  809. camuri.handler = handler_json;
  810. camuri.user_ctx = (void*) "JSON";
  811. httpd_register_uri_handler(server, &camuri);
  812. camuri.uri = "/heap";
  813. camuri.handler = handler_get_heap;
  814. camuri.user_ctx = (void*) "Heap";
  815. httpd_register_uri_handler(server, &camuri);
  816. }