server_tflite.cpp 16 KB

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