server_tflite.cpp 14 KB

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