server_tflite.cpp 15 KB

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