server_tflite.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 "Helper.h"
  9. #include "esp_camera.h"
  10. #include "time_sntp.h"
  11. #include "ClassControllCamera.h"
  12. #include "ClassFlowControll.h"
  13. #include "ClassLogFile.h"
  14. //#define DEBUG_DETAIL_ON
  15. ClassFlowControll tfliteflow;
  16. TaskHandle_t xHandleblink_task_doFlow = NULL;
  17. TaskHandle_t xHandletask_autodoFlow = NULL;
  18. bool flowisrunning = false;
  19. long auto_intervall = 0;
  20. bool auto_isrunning = false;
  21. int countRounds = 0;
  22. int getCountFlowRounds() {
  23. return countRounds;
  24. }
  25. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  26. {
  27. return tfliteflow.GetJPGStream(_filename, req);
  28. }
  29. esp_err_t GetRawJPG(httpd_req_t *req)
  30. {
  31. return tfliteflow.SendRawJPG(req);
  32. }
  33. bool isSetupModusActive() {
  34. return tfliteflow.getStatusSetupModus();
  35. return false;
  36. }
  37. void KillTFliteTasks()
  38. {
  39. #ifdef DEBUG_DETAIL_ON
  40. printf("Handle: xHandleblink_task_doFlow: %ld\n", (long) xHandleblink_task_doFlow);
  41. #endif
  42. if (xHandleblink_task_doFlow)
  43. {
  44. vTaskDelete(xHandleblink_task_doFlow);
  45. #ifdef DEBUG_DETAIL_ON
  46. printf("Killed: xHandleblink_task_doFlow\n");
  47. #endif
  48. }
  49. #ifdef DEBUG_DETAIL_ON
  50. printf("Handle: xHandletask_autodoFlow: %ld\n", (long) xHandletask_autodoFlow);
  51. #endif
  52. if (xHandletask_autodoFlow)
  53. {
  54. vTaskDelete(xHandletask_autodoFlow);
  55. #ifdef DEBUG_DETAIL_ON
  56. printf("Killed: xHandletask_autodoFlow\n");
  57. #endif
  58. }
  59. }
  60. void doInit(void)
  61. {
  62. string config = "/sdcard/config/config.ini";
  63. #ifdef DEBUG_DETAIL_ON
  64. printf("Start tfliteflow.InitFlow(config);\n");
  65. #endif
  66. tfliteflow.InitFlow(config);
  67. #ifdef DEBUG_DETAIL_ON
  68. printf("Finished tfliteflow.InitFlow(config);\n");
  69. #endif
  70. }
  71. bool doflow(void)
  72. {
  73. std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
  74. printf("doflow - start %s\n", zw_time.c_str());
  75. flowisrunning = true;
  76. tfliteflow.doFlow(zw_time);
  77. flowisrunning = false;
  78. #ifdef DEBUG_DETAIL_ON
  79. printf("doflow - end %s\n", zw_time.c_str());
  80. #endif
  81. return true;
  82. }
  83. void blink_task_doFlow(void *pvParameter)
  84. {
  85. #ifdef DEBUG_DETAIL_ON
  86. printf("blink_task_doFlow\n");
  87. #endif
  88. if (!flowisrunning)
  89. {
  90. flowisrunning = true;
  91. doflow();
  92. flowisrunning = false;
  93. }
  94. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  95. xHandleblink_task_doFlow = NULL;
  96. }
  97. esp_err_t handler_init(httpd_req_t *req)
  98. {
  99. #ifdef DEBUG_DETAIL_ON
  100. LogFile.WriteHeapInfo("handler_init - Start");
  101. printf("handler_doinit uri:\n"); printf(req->uri); printf("\n");
  102. #endif
  103. char* resp_str = "Init started<br>";
  104. httpd_resp_send(req, resp_str, strlen(resp_str));
  105. doInit();
  106. resp_str = "Init done<br>";
  107. httpd_resp_send(req, resp_str, strlen(resp_str));
  108. /* Respond with an empty chunk to signal HTTP response completion */
  109. httpd_resp_send_chunk(req, NULL, 0);
  110. #ifdef DEBUG_DETAIL_ON
  111. LogFile.WriteHeapInfo("handler_init - Done");
  112. #endif
  113. return ESP_OK;
  114. };
  115. esp_err_t handler_doflow(httpd_req_t *req)
  116. {
  117. #ifdef DEBUG_DETAIL_ON
  118. LogFile.WriteHeapInfo("handler_doflow - Start");
  119. #endif
  120. char* resp_str;
  121. printf("handler_doFlow uri: "); printf(req->uri); printf("\n");
  122. if (flowisrunning)
  123. {
  124. const char* resp_str = "doFlow läuft bereits und kann nicht nochmal gestartet werden";
  125. httpd_resp_send(req, resp_str, strlen(resp_str));
  126. return 2;
  127. }
  128. else
  129. {
  130. xTaskCreate(&blink_task_doFlow, "blink_doFlow", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, &xHandleblink_task_doFlow);
  131. }
  132. resp_str = "doFlow gestartet - dauert ca. 60 Sekunden";
  133. httpd_resp_send(req, resp_str, strlen(resp_str));
  134. /* Respond with an empty chunk to signal HTTP response completion */
  135. httpd_resp_send_chunk(req, NULL, 0);
  136. #ifdef DEBUG_DETAIL_ON
  137. LogFile.WriteHeapInfo("handler_doflow - Done");
  138. #endif
  139. return ESP_OK;
  140. };
  141. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  142. {
  143. #ifdef DEBUG_DETAIL_ON
  144. LogFile.WriteHeapInfo("handler_wasserzaehler - Start");
  145. #endif
  146. bool _rawValue = false;
  147. bool _noerror = false;
  148. string zw;
  149. printf("handler_wasserzaehler uri:\n"); printf(req->uri); printf("\n");
  150. char _query[100];
  151. char _size[10];
  152. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  153. {
  154. // printf("Query: "); printf(_query); printf("\n");
  155. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  156. {
  157. #ifdef DEBUG_DETAIL_ON
  158. printf("rawvalue is found"); printf(_size); printf("\n");
  159. #endif
  160. _rawValue = true;
  161. }
  162. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  163. {
  164. #ifdef DEBUG_DETAIL_ON
  165. printf("noerror is found"); printf(_size); printf("\n");
  166. #endif
  167. _noerror = true;
  168. }
  169. }
  170. zw = tfliteflow.getReadout(_rawValue, _noerror);
  171. if (zw.length() > 0)
  172. httpd_resp_sendstr_chunk(req, zw.c_str());
  173. string query = std::string(_query);
  174. // printf("Query: %s\n", query.c_str());
  175. if (query.find("full") != std::string::npos)
  176. {
  177. string txt, zw;
  178. txt = "<p>Aligned Image: <p><img src=\"/img_tmp/alg_roi.jpg\"> <p>\n";
  179. txt = txt + "Digital Counter: <p> ";
  180. httpd_resp_sendstr_chunk(req, txt.c_str());
  181. std::vector<HTMLInfo*> htmlinfo;
  182. htmlinfo = tfliteflow.GetAllDigital();
  183. for (int i = 0; i < htmlinfo.size(); ++i)
  184. {
  185. if (htmlinfo[i]->val == 10)
  186. zw = "NaN";
  187. else
  188. {
  189. zw = to_string((int) htmlinfo[i]->val);
  190. }
  191. txt = "<img src=\"/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  192. httpd_resp_sendstr_chunk(req, txt.c_str());
  193. delete htmlinfo[i];
  194. }
  195. htmlinfo.clear();
  196. txt = " <p> Analog Meter: <p> ";
  197. httpd_resp_sendstr_chunk(req, txt.c_str());
  198. htmlinfo = tfliteflow.GetAllAnalog();
  199. for (int i = 0; i < htmlinfo.size(); ++i)
  200. {
  201. std::stringstream stream;
  202. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  203. zw = stream.str();
  204. txt = "<img src=\"/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  205. httpd_resp_sendstr_chunk(req, txt.c_str());
  206. delete htmlinfo[i];
  207. }
  208. htmlinfo.clear();
  209. }
  210. /* Respond with an empty chunk to signal HTTP response completion */
  211. httpd_resp_sendstr_chunk(req, NULL);
  212. #ifdef DEBUG_DETAIL_ON
  213. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  214. #endif
  215. return ESP_OK;
  216. };
  217. esp_err_t handler_editflow(httpd_req_t *req)
  218. {
  219. #ifdef DEBUG_DETAIL_ON
  220. LogFile.WriteHeapInfo("handler_editflow - Start");
  221. #endif
  222. printf("handler_editflow uri: "); printf(req->uri); printf("\n");
  223. char _query[200];
  224. char _valuechar[30];
  225. string _task;
  226. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  227. {
  228. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  229. {
  230. #ifdef DEBUG_DETAIL_ON
  231. printf("task is found: %s\n", _valuechar);
  232. #endif
  233. _task = string(_valuechar);
  234. }
  235. }
  236. if (_task.compare("copy") == 0)
  237. {
  238. string in, out, zw;
  239. httpd_query_key_value(_query, "in", _valuechar, 30);
  240. in = string(_valuechar);
  241. httpd_query_key_value(_query, "out", _valuechar, 30);
  242. out = string(_valuechar);
  243. #ifdef DEBUG_DETAIL_ON
  244. printf("in: "); printf(in.c_str()); printf("\n");
  245. printf("out: "); printf(out.c_str()); printf("\n");
  246. #endif
  247. in = "/sdcard" + in;
  248. out = "/sdcard" + out;
  249. CopyFile(in, out);
  250. zw = "Copy Done";
  251. httpd_resp_sendstr_chunk(req, zw.c_str());
  252. }
  253. if (_task.compare("cutref") == 0)
  254. {
  255. string in, out, zw;
  256. int x, y, dx, dy;
  257. bool enhance = false;
  258. httpd_query_key_value(_query, "in", _valuechar, 30);
  259. in = string(_valuechar);
  260. httpd_query_key_value(_query, "out", _valuechar, 30);
  261. out = string(_valuechar);
  262. httpd_query_key_value(_query, "x", _valuechar, 30);
  263. zw = string(_valuechar);
  264. x = stoi(zw);
  265. httpd_query_key_value(_query, "y", _valuechar, 30);
  266. zw = string(_valuechar);
  267. y = stoi(zw);
  268. httpd_query_key_value(_query, "dx", _valuechar, 30);
  269. zw = string(_valuechar);
  270. dx = stoi(zw);
  271. httpd_query_key_value(_query, "dy", _valuechar, 30);
  272. zw = string(_valuechar);
  273. dy = stoi(zw);
  274. #ifdef DEBUG_DETAIL_ON
  275. printf("in: "); printf(in.c_str()); printf("\n");
  276. printf("out: "); printf(out.c_str()); printf("\n");
  277. printf("x: "); printf(zw.c_str()); printf("\n");
  278. printf("y: "); printf(zw.c_str()); printf("\n");
  279. printf("dx: "); printf(zw.c_str()); printf("\n");
  280. printf("dy: "); printf(zw.c_str()); printf("\n");
  281. #endif
  282. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  283. {
  284. zw = string(_valuechar);
  285. if (zw.compare("true") == 0)
  286. {
  287. enhance = true;
  288. }
  289. }
  290. in = "/sdcard" + in;
  291. out = "/sdcard" + out;
  292. string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  293. CAlignAndCutImage *caic = new CAlignAndCutImage(in);
  294. caic->CutAndSave(out2, x, y, dx, dy);
  295. delete caic;
  296. CImageBasis *cim = new CImageBasis(out2);
  297. if (enhance)
  298. {
  299. cim->Contrast(90);
  300. }
  301. cim->SaveToFile(out);
  302. delete cim;
  303. zw = "CutImage Done";
  304. httpd_resp_sendstr_chunk(req, zw.c_str());
  305. }
  306. if (_task.compare("test_take") == 0)
  307. {
  308. std::string _host = "";
  309. std::string _bri = "";
  310. std::string _con = "";
  311. std::string _sat = "";
  312. int bri = -100;
  313. int sat = -100;
  314. int con = -100;
  315. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  316. _host = std::string(_valuechar);
  317. }
  318. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK) {
  319. _bri = std::string(_valuechar);
  320. bri = stoi(_bri);
  321. }
  322. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK) {
  323. _con = std::string(_valuechar);
  324. con = stoi(_con);
  325. }
  326. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK) {
  327. _sat = std::string(_valuechar);
  328. sat = stoi(_sat);
  329. }
  330. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  331. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  332. bool changed = Camera.SetBrightnessContrastSaturation(bri, con, sat);
  333. std::string zw = tfliteflow.doSingleStep("[MakeImage]", _host);
  334. httpd_resp_sendstr_chunk(req, zw.c_str());
  335. }
  336. if (_task.compare("test_align") == 0)
  337. {
  338. std::string _host = "";
  339. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  340. _host = std::string(_valuechar);
  341. }
  342. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  343. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  344. std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
  345. httpd_resp_sendstr_chunk(req, zw.c_str());
  346. }
  347. if (_task.compare("test_analog") == 0)
  348. {
  349. std::string _host = "";
  350. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  351. _host = std::string(_valuechar);
  352. }
  353. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  354. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  355. std::string zw = tfliteflow.doSingleStep("[Analog]", _host);
  356. httpd_resp_sendstr_chunk(req, zw.c_str());
  357. }
  358. if (_task.compare("test_digits") == 0)
  359. {
  360. std::string _host = "";
  361. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  362. _host = std::string(_valuechar);
  363. }
  364. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  365. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  366. std::string zw = tfliteflow.doSingleStep("[Digits]", _host);
  367. httpd_resp_sendstr_chunk(req, zw.c_str());
  368. }
  369. /* Respond with an empty chunk to signal HTTP response completion */
  370. httpd_resp_sendstr_chunk(req, NULL);
  371. #ifdef DEBUG_DETAIL_ON
  372. LogFile.WriteHeapInfo("handler_editflow - Done");
  373. #endif
  374. return ESP_OK;
  375. };
  376. esp_err_t handler_prevalue(httpd_req_t *req)
  377. {
  378. #ifdef DEBUG_DETAIL_ON
  379. LogFile.WriteHeapInfo("handler_prevalue - Start");
  380. #endif
  381. const char* resp_str;
  382. string zw;
  383. #ifdef DEBUG_DETAIL_ON
  384. printf("handler_prevalue:\n"); printf(req->uri); printf("\n");
  385. #endif
  386. char _query[100];
  387. char _size[10] = "";
  388. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  389. {
  390. #ifdef DEBUG_DETAIL_ON
  391. printf("Query: "); printf(_query); printf("\n");
  392. #endif
  393. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  394. {
  395. #ifdef DEBUG_DETAIL_ON
  396. printf("Value: "); printf(_size); printf("\n");
  397. #endif
  398. }
  399. }
  400. if (strlen(_size) == 0)
  401. zw = tfliteflow.GetPrevalue();
  402. else
  403. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size);
  404. resp_str = zw.c_str();
  405. httpd_resp_send(req, resp_str, strlen(resp_str));
  406. /* Respond with an empty chunk to signal HTTP response completion */
  407. httpd_resp_send_chunk(req, NULL, 0);
  408. #ifdef DEBUG_DETAIL_ON
  409. LogFile.WriteHeapInfo("handler_prevalue - Start");
  410. #endif
  411. return ESP_OK;
  412. };
  413. void task_autodoFlow(void *pvParameter)
  414. {
  415. int64_t fr_start, fr_delta_ms;
  416. doInit();
  417. auto_isrunning = tfliteflow.isAutoStart(auto_intervall);
  418. if (isSetupModusActive()) {
  419. auto_isrunning = false;
  420. std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
  421. tfliteflow.doFlowMakeImageOnly(zw_time);
  422. }
  423. while (auto_isrunning)
  424. {
  425. std::string _zw = "task_autodoFlow - next round - Round #" + std::to_string(++countRounds);
  426. LogFile.WriteToFile(_zw);
  427. printf("Autoflow: start\n");
  428. fr_start = esp_timer_get_time();
  429. if (flowisrunning)
  430. {
  431. #ifdef DEBUG_DETAIL_ON
  432. printf("Autoflow: doFLow laeuft bereits!\n");
  433. #endif
  434. }
  435. else
  436. {
  437. #ifdef DEBUG_DETAIL_ON
  438. printf("Autoflow: doFLow wird gestartet\n");
  439. #endif
  440. flowisrunning = true;
  441. doflow();
  442. #ifdef DEBUG_DETAIL_ON
  443. printf("Remove older log files\n");
  444. #endif
  445. LogFile.RemoveOld();
  446. }
  447. LogFile.WriteToFile("task_autodoFlow - round done");
  448. //CPU Temp
  449. float cputmp = temperatureRead();
  450. std::stringstream stream;
  451. stream << std::fixed << std::setprecision(1) << cputmp;
  452. string zwtemp = "CPU Temperature: " + stream.str();
  453. LogFile.WriteToFile(zwtemp);
  454. printf("CPU Temperature: %.2f\n", cputmp);
  455. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  456. if (auto_intervall > fr_delta_ms)
  457. {
  458. const TickType_t xDelay = (auto_intervall - fr_delta_ms) / portTICK_PERIOD_MS;
  459. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  460. vTaskDelay( xDelay );
  461. }
  462. }
  463. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  464. xHandletask_autodoFlow = NULL;
  465. }
  466. void TFliteDoAutoStart()
  467. {
  468. xTaskCreate(&task_autodoFlow, "task_autodoFlow", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, &xHandletask_autodoFlow);
  469. }
  470. void register_server_tflite_uri(httpd_handle_t server)
  471. {
  472. ESP_LOGI(TAGTFLITE, "server_part_camera - Registering URI handlers");
  473. httpd_uri_t camuri = { };
  474. camuri.method = HTTP_GET;
  475. camuri.uri = "/doinit";
  476. camuri.handler = handler_init;
  477. camuri.user_ctx = (void*) "Light On";
  478. httpd_register_uri_handler(server, &camuri);
  479. camuri.uri = "/setPreValue.html";
  480. camuri.handler = handler_prevalue;
  481. camuri.user_ctx = (void*) "Prevalue";
  482. httpd_register_uri_handler(server, &camuri);
  483. camuri.uri = "/doflow";
  484. camuri.handler = handler_doflow;
  485. camuri.user_ctx = (void*) "Light Off";
  486. httpd_register_uri_handler(server, &camuri);
  487. camuri.uri = "/editflow.html";
  488. camuri.handler = handler_editflow;
  489. camuri.user_ctx = (void*) "EditFlow";
  490. httpd_register_uri_handler(server, &camuri);
  491. camuri.uri = "/wasserzaehler.html";
  492. camuri.handler = handler_wasserzaehler;
  493. camuri.user_ctx = (void*) "Wasserzaehler";
  494. httpd_register_uri_handler(server, &camuri);
  495. }