server_tflite.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "esp_camera.h"
  9. #include "time_sntp.h"
  10. #include "ClassControllCamera.h"
  11. #include "ClassFlowControll.h"
  12. #include "ClassLogFile.h"
  13. ClassFlowControll tfliteflow;
  14. TaskHandle_t xHandleblink_task_doFlow = NULL;
  15. TaskHandle_t xHandletask_autodoFlow = NULL;
  16. bool flowisrunning = false;
  17. long auto_intervall = 0;
  18. bool auto_isrunning = false;
  19. void KillTFliteTasks()
  20. {
  21. printf("Handle: xHandleblink_task_doFlow: %ld\n", (long) xHandleblink_task_doFlow);
  22. if (xHandleblink_task_doFlow)
  23. {
  24. vTaskDelete(xHandleblink_task_doFlow);
  25. printf("Killed: xHandleblink_task_doFlow\n");
  26. }
  27. printf("Handle: xHandletask_autodoFlow: %ld\n", (long) xHandletask_autodoFlow);
  28. if (xHandletask_autodoFlow)
  29. {
  30. vTaskDelete(xHandletask_autodoFlow);
  31. printf("Killed: xHandletask_autodoFlow\n");
  32. }
  33. }
  34. void doInit(void)
  35. {
  36. string config = "/sdcard/config/config.ini";
  37. printf("Start tfliteflow.InitFlow(config);\n");
  38. tfliteflow.InitFlow(config);
  39. printf("Finished tfliteflow.InitFlow(config);\n");
  40. }
  41. bool doflow(void)
  42. {
  43. int i;
  44. std::string zw_time = gettimestring("%Y%m%d-%H%M%S");
  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. const char* resp_str;
  102. string zw;
  103. bool _rawValue = false;
  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. }
  116. zw = tfliteflow.getReadout(_rawValue);
  117. if (zw.length() > 0)
  118. httpd_resp_sendstr_chunk(req, zw.c_str());
  119. string query = std::string(_query);
  120. // printf("Query: %s\n", query.c_str());
  121. if (query.find("full") != std::string::npos)
  122. {
  123. string txt, zw;
  124. txt = "<p>Aligned Image: <p><img src=\"/img_tmp/alg.jpg\"> <p>\n";
  125. txt = txt + "Digital Counter: <p> ";
  126. httpd_resp_sendstr_chunk(req, txt.c_str());
  127. std::vector<HTMLInfo*> htmlinfo;
  128. htmlinfo = tfliteflow.GetAllDigital();
  129. for (int i = 0; i < htmlinfo.size(); ++i)
  130. {
  131. if (htmlinfo[i]->val == 10)
  132. zw = "NaN";
  133. else
  134. {
  135. zw = to_string((int) htmlinfo[i]->val);
  136. }
  137. txt = "<img src=\"/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  138. httpd_resp_sendstr_chunk(req, txt.c_str());
  139. delete htmlinfo[i];
  140. }
  141. htmlinfo.clear();
  142. txt = " <p> Analog Meter: <p> ";
  143. httpd_resp_sendstr_chunk(req, txt.c_str());
  144. htmlinfo = tfliteflow.GetAllAnalog();
  145. for (int i = 0; i < htmlinfo.size(); ++i)
  146. {
  147. std::stringstream stream;
  148. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  149. zw = stream.str();
  150. txt = "<img src=\"/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  151. httpd_resp_sendstr_chunk(req, txt.c_str());
  152. delete htmlinfo[i];
  153. }
  154. htmlinfo.clear();
  155. /*
  156. for i in range(len(resultdigital)):
  157. if resultdigital[i] == 'NaN':
  158. zw = 'NaN'
  159. else:
  160. zw = str(int(resultdigital[i]))
  161. txt += '<img src=/image_tmp/'+ str(resultcut[1][i][0]) + '.jpg></img>' + zw
  162. txt = txt + '<p>'
  163. if self.AnalogReadOutEnabled:
  164. txt = txt + 'Analog Meter: <p>'
  165. for i in range(len(resultanalog)):
  166. txt += '<img src=/image_tmp/'+ str(resultcut[0][i][0]) + '.jpg></img>' + "{:.1f}".format(resultanalog[i])
  167. txt = txt + '<p>'
  168. */
  169. }
  170. /* Respond with an empty chunk to signal HTTP response completion */
  171. httpd_resp_sendstr_chunk(req, NULL);
  172. return ESP_OK;
  173. };
  174. esp_err_t handler_prevalue(httpd_req_t *req)
  175. {
  176. LogFile.WriteToFile("handler_prevalue");
  177. const char* resp_str;
  178. string zw;
  179. // printf("handler_prevalue:\n"); printf(req->uri); printf("\n");
  180. char _query[100];
  181. char _size[10] = "";
  182. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  183. {
  184. // printf("Query: "); printf(_query); printf("\n");
  185. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  186. {
  187. printf("Value: "); printf(_size); printf("\n");
  188. }
  189. }
  190. if (strlen(_size) == 0)
  191. zw = "Actual PreValue: " + tfliteflow.GetPrevalue();
  192. else
  193. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size);
  194. resp_str = zw.c_str();
  195. httpd_resp_send(req, resp_str, strlen(resp_str));
  196. /* Respond with an empty chunk to signal HTTP response completion */
  197. httpd_resp_send_chunk(req, NULL, 0);
  198. return ESP_OK;
  199. };
  200. void task_autodoFlow(void *pvParameter)
  201. {
  202. int64_t fr_start, fr_delta_ms;
  203. doInit();
  204. auto_isrunning = tfliteflow.isAutoStart(auto_intervall);
  205. while (auto_isrunning)
  206. {
  207. LogFile.WriteToFile("task_autodoFlow - next round");
  208. printf("Autoflow: start\n");
  209. fr_start = esp_timer_get_time();
  210. if (flowisrunning)
  211. {
  212. printf("Autoflow: doFLow laeuft bereits!\n");
  213. }
  214. else
  215. {
  216. printf("Autoflow: doFLow wird gestartet\n");
  217. flowisrunning = true;
  218. doflow();
  219. }
  220. LogFile.WriteToFile("task_autodoFlow - round done");
  221. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  222. const TickType_t xDelay = (auto_intervall - fr_delta_ms) / portTICK_PERIOD_MS;
  223. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  224. vTaskDelay( xDelay );
  225. }
  226. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  227. xHandletask_autodoFlow = NULL;
  228. }
  229. void TFliteDoAutoStart()
  230. {
  231. xTaskCreate(&task_autodoFlow, "task_autodoFlow", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, &xHandletask_autodoFlow);
  232. }
  233. void register_server_tflite_uri(httpd_handle_t server)
  234. {
  235. ESP_LOGI(TAGTFLITE, "server_part_camera - Registering URI handlers");
  236. httpd_uri_t camuri = { };
  237. camuri.method = HTTP_GET;
  238. camuri.uri = "/doinit";
  239. camuri.handler = handler_init;
  240. camuri.user_ctx = (void*) "Light On";
  241. httpd_register_uri_handler(server, &camuri);
  242. camuri.uri = "/setPreValue.html";
  243. camuri.handler = handler_prevalue;
  244. camuri.user_ctx = (void*) "Prevalue";
  245. httpd_register_uri_handler(server, &camuri);
  246. camuri.uri = "/doflow";
  247. camuri.handler = handler_doflow;
  248. camuri.user_ctx = (void*) "Light Off";
  249. httpd_register_uri_handler(server, &camuri);
  250. camuri.uri = "/wasserzaehler.html";
  251. camuri.handler = handler_wasserzaehler;
  252. camuri.user_ctx = (void*) "Wasserzaehler";
  253. httpd_register_uri_handler(server, &camuri);
  254. }