server_tflite.cpp 14 KB

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