server_tflite.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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 "../../include/defines.h"
  9. #include "Helper.h"
  10. #include "statusled.h"
  11. #include "esp_camera.h"
  12. #include "time_sntp.h"
  13. #include "ClassControllCamera.h"
  14. #include "ClassFlowControll.h"
  15. #include "ClassLogFile.h"
  16. #include "server_GPIO.h"
  17. #include "server_file.h"
  18. #include "read_wlanini.h"
  19. #include "connect_wlan.h"
  20. ClassFlowControll tfliteflow;
  21. TaskHandle_t xHandletask_autodoFlow = NULL;
  22. bool bTaskAutoFlowCreated = false;
  23. bool flowisrunning = false;
  24. long auto_interval = 0;
  25. bool auto_isrunning = false;
  26. int countRounds = 0;
  27. bool isPlannedReboot = false;
  28. static const char *TAG = "TFLITE SERVER";
  29. //#define DEBUG_DETAIL_ON
  30. void CheckIsPlannedReboot()
  31. {
  32. FILE *pfile;
  33. if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL) {
  34. //LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Initial boot or not a planned reboot");
  35. isPlannedReboot = false;
  36. }
  37. else {
  38. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot");
  39. DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
  40. isPlannedReboot = true;
  41. }
  42. }
  43. bool getIsPlannedReboot()
  44. {
  45. return isPlannedReboot;
  46. }
  47. int getCountFlowRounds()
  48. {
  49. return countRounds;
  50. }
  51. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  52. {
  53. return tfliteflow.GetJPGStream(_filename, req);
  54. }
  55. esp_err_t GetRawJPG(httpd_req_t *req)
  56. {
  57. return tfliteflow.SendRawJPG(req);
  58. }
  59. bool isSetupModusActive() {
  60. return tfliteflow.getStatusSetupModus();
  61. return false;
  62. }
  63. void KillTFliteTasks()
  64. {
  65. #ifdef DEBUG_DETAIL_ON
  66. ESP_LOGD(TAG, "KillTFliteTasks: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
  67. #endif
  68. if( xHandletask_autodoFlow != NULL )
  69. {
  70. vTaskDelete(xHandletask_autodoFlow);
  71. xHandletask_autodoFlow = NULL;
  72. }
  73. #ifdef DEBUG_DETAIL_ON
  74. ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
  75. #endif
  76. }
  77. void doInit(void)
  78. {
  79. #ifdef DEBUG_DETAIL_ON
  80. ESP_LOGD(TAG, "Start tfliteflow.InitFlow(config);");
  81. #endif
  82. tfliteflow.InitFlow(CONFIG_FILE);
  83. #ifdef DEBUG_DETAIL_ON
  84. ESP_LOGD(TAG, "Finished tfliteflow.InitFlow(config);");
  85. #endif
  86. /* GPIO handler has to be initialized before MQTT init to ensure proper topic subscription */
  87. gpio_handler_init();
  88. #ifdef ENABLE_MQTT
  89. tfliteflow.StartMQTTService();
  90. #endif //ENABLE_MQTT
  91. }
  92. bool doflow(void)
  93. {
  94. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  95. ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
  96. flowisrunning = true;
  97. tfliteflow.doFlow(zw_time);
  98. flowisrunning = false;
  99. #ifdef DEBUG_DETAIL_ON
  100. ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
  101. #endif
  102. return true;
  103. }
  104. esp_err_t handler_get_heap(httpd_req_t *req)
  105. {
  106. #ifdef DEBUG_DETAIL_ON
  107. LogFile.WriteHeapInfo("handler_get_heap - Start");
  108. ESP_LOGD(TAG, "handler_get_heap uri: %s", req->uri);
  109. #endif
  110. std::string zw = "Heap info:<br>" + getESPHeapInfo();
  111. #ifdef TASK_ANALYSIS_ON
  112. char* pcTaskList = (char*) heap_caps_calloc(1, sizeof(char) * 768, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  113. if (pcTaskList) {
  114. vTaskList(pcTaskList);
  115. zw = zw + "<br><br>Task info:<br><pre>Name | State | Prio | Lowest stacksize | Creation order | CPU (-1=NoAffinity)<br>"
  116. + std::string(pcTaskList) + "</pre>";
  117. heap_caps_free(pcTaskList);
  118. }
  119. else {
  120. zw = zw + "<br><br>Task info:<br>ERROR - Allocation of TaskList buffer in PSRAM failed";
  121. }
  122. #endif
  123. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  124. if (zw.length() > 0)
  125. {
  126. httpd_resp_send(req, zw.c_str(), zw.length());
  127. }
  128. else
  129. {
  130. httpd_resp_send(req, NULL, 0);
  131. }
  132. #ifdef DEBUG_DETAIL_ON
  133. LogFile.WriteHeapInfo("handler_get_heap - Done");
  134. #endif
  135. return ESP_OK;
  136. }
  137. esp_err_t handler_init(httpd_req_t *req)
  138. {
  139. #ifdef DEBUG_DETAIL_ON
  140. LogFile.WriteHeapInfo("handler_init - Start");
  141. ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
  142. #endif
  143. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  144. const char* resp_str = "Init started<br>";
  145. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  146. doInit();
  147. resp_str = "Init done<br>";
  148. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  149. #ifdef DEBUG_DETAIL_ON
  150. LogFile.WriteHeapInfo("handler_init - Done");
  151. #endif
  152. return ESP_OK;
  153. }
  154. esp_err_t handler_flow_start(httpd_req_t *req) {
  155. #ifdef DEBUG_DETAIL_ON
  156. LogFile.WriteHeapInfo("handler_flow_start - Start");
  157. #endif
  158. ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
  159. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  160. if (auto_isrunning) {
  161. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  162. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
  163. const char* resp_str = "The flow is going to be started immediately or is already running";
  164. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  165. }
  166. else {
  167. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
  168. const char* resp_str = "WARNING: Flow start triggered by REST API, but flow is not active";
  169. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  170. }
  171. #ifdef DEBUG_DETAIL_ON
  172. LogFile.WriteHeapInfo("handler_flow_start - Done");
  173. #endif
  174. return ESP_OK;
  175. }
  176. #ifdef ENABLE_MQTT
  177. esp_err_t MQTTCtrlFlowStart(std::string _topic) {
  178. #ifdef DEBUG_DETAIL_ON
  179. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Start");
  180. #endif
  181. ESP_LOGD(TAG, "MQTTCtrlFlowStart: topic %s", _topic.c_str());
  182. if (auto_isrunning)
  183. {
  184. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  185. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by MQTT topic " + _topic);
  186. }
  187. else
  188. {
  189. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by MQTT topic " + _topic + ", but flow is not active!");
  190. }
  191. #ifdef DEBUG_DETAIL_ON
  192. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Done");
  193. #endif
  194. return ESP_OK;
  195. }
  196. #endif //ENABLE_MQTT
  197. esp_err_t handler_json(httpd_req_t *req)
  198. {
  199. #ifdef DEBUG_DETAIL_ON
  200. LogFile.WriteHeapInfo("handler_json - Start");
  201. #endif
  202. ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
  203. if (bTaskAutoFlowCreated)
  204. {
  205. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  206. httpd_resp_set_type(req, "application/json");
  207. std::string zw = tfliteflow.getJSON();
  208. if (zw.length() > 0)
  209. {
  210. httpd_resp_send(req, zw.c_str(), zw.length());
  211. }
  212. else
  213. {
  214. httpd_resp_send(req, NULL, 0);
  215. }
  216. }
  217. else
  218. {
  219. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!");
  220. return ESP_ERR_NOT_FOUND;
  221. }
  222. #ifdef DEBUG_DETAIL_ON
  223. LogFile.WriteHeapInfo("handler_JSON - Done");
  224. #endif
  225. return ESP_OK;
  226. }
  227. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  228. {
  229. #ifdef DEBUG_DETAIL_ON
  230. LogFile.WriteHeapInfo("handler water counter - Start");
  231. #endif
  232. if (bTaskAutoFlowCreated)
  233. {
  234. bool _rawValue = false;
  235. bool _noerror = false;
  236. bool _all = false;
  237. std::string _type = "value";
  238. string zw;
  239. ESP_LOGD(TAG, "handler water counter uri: %s", req->uri);
  240. char _query[100];
  241. char _size[10];
  242. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  243. {
  244. // ESP_LOGD(TAG, "Query: %s", _query);
  245. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  246. {
  247. #ifdef DEBUG_DETAIL_ON
  248. ESP_LOGD(TAG, "all is found%s", _size);
  249. #endif
  250. _all = true;
  251. }
  252. if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
  253. {
  254. #ifdef DEBUG_DETAIL_ON
  255. ESP_LOGD(TAG, "all is found: %s", _size);
  256. #endif
  257. _type = std::string(_size);
  258. }
  259. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  260. {
  261. #ifdef DEBUG_DETAIL_ON
  262. ESP_LOGD(TAG, "rawvalue is found: %s", _size);
  263. #endif
  264. _rawValue = true;
  265. }
  266. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  267. {
  268. #ifdef DEBUG_DETAIL_ON
  269. ESP_LOGD(TAG, "noerror is found: %s", _size);
  270. #endif
  271. _noerror = true;
  272. }
  273. }
  274. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  275. if (_all)
  276. {
  277. httpd_resp_set_type(req, "text/plain");
  278. ESP_LOGD(TAG, "TYPE: %s", _type.c_str());
  279. int _intype = READOUT_TYPE_VALUE;
  280. if (_type == "prevalue")
  281. _intype = READOUT_TYPE_PREVALUE;
  282. if (_type == "raw")
  283. _intype = READOUT_TYPE_RAWVALUE;
  284. if (_type == "error")
  285. _intype = READOUT_TYPE_ERROR;
  286. zw = tfliteflow.getReadoutAll(_intype);
  287. ESP_LOGD(TAG, "ZW: %s", zw.c_str());
  288. if (zw.length() > 0)
  289. httpd_resp_send(req, zw.c_str(), zw.length());
  290. return ESP_OK;
  291. }
  292. std::string *status = tfliteflow.getActStatus();
  293. string query = std::string(_query);
  294. // ESP_LOGD(TAG, "Query: %s, query.c_str());
  295. if (query.find("full") != std::string::npos)
  296. {
  297. string txt;
  298. txt = "<body style=\"font-family: arial\">";
  299. if ((countRounds <= 1) && (*status != std::string("Flow finished"))) { // First round not completed yet
  300. txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + *status + "</h3>\n";
  301. }
  302. else {
  303. txt += "<h3>Value</h3>";
  304. }
  305. httpd_resp_sendstr_chunk(req, txt.c_str());
  306. }
  307. zw = tfliteflow.getReadout(_rawValue, _noerror);
  308. if (zw.length() > 0)
  309. httpd_resp_sendstr_chunk(req, zw.c_str());
  310. if (query.find("full") != std::string::npos)
  311. {
  312. string txt, zw;
  313. if ((countRounds <= 1) && (*status != std::string("Flow finished"))) { // First round not completed yet
  314. // Nothing to do
  315. }
  316. else {
  317. /* Digital ROIs */
  318. txt = "<body style=\"font-family: arial\">";
  319. txt += "<h3>Recognized Digit ROIs (previous round)</h3>\n";
  320. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  321. std::vector<HTMLInfo*> htmlinfodig;
  322. htmlinfodig = tfliteflow.GetAllDigital();
  323. for (int i = 0; i < htmlinfodig.size(); ++i)
  324. {
  325. if (tfliteflow.GetTypeDigital() == Digital)
  326. {
  327. if (htmlinfodig[i]->val == 10)
  328. zw = "NaN";
  329. else
  330. zw = to_string((int) htmlinfodig[i]->val);
  331. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  332. }
  333. else
  334. {
  335. std::stringstream stream;
  336. stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
  337. zw = stream.str();
  338. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  339. }
  340. delete htmlinfodig[i];
  341. }
  342. htmlinfodig.clear();
  343. txt += "</tr></table>\n";
  344. httpd_resp_sendstr_chunk(req, txt.c_str());
  345. /* Analog ROIs */
  346. txt = "<h3>Recognized Analog ROIs (previous round)</h3>\n";
  347. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  348. std::vector<HTMLInfo*> htmlinfoana;
  349. htmlinfoana = tfliteflow.GetAllAnalog();
  350. for (int i = 0; i < htmlinfoana.size(); ++i)
  351. {
  352. std::stringstream stream;
  353. stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
  354. zw = stream.str();
  355. txt += "<td style=\"width: 150px;\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"></p></td>\n";
  356. delete htmlinfoana[i];
  357. }
  358. htmlinfoana.clear();
  359. txt += "</tr>\n</table>\n";
  360. httpd_resp_sendstr_chunk(req, txt.c_str());
  361. /* Full Image
  362. * Only show it after the image got taken and aligned */
  363. txt = "<h3>Aligned Image (current round)</h3>\n";
  364. if ((*status == std::string("Initialization")) ||
  365. (*status == std::string("Initialization (delayed)")) ||
  366. (*status == std::string("Take Image"))) {
  367. txt += "<p>Current state: " + *status + "</p>\n";
  368. }
  369. else {
  370. txt += "<img src=\"/img_tmp/alg_roi.jpg\">\n";
  371. }
  372. httpd_resp_sendstr_chunk(req, txt.c_str());
  373. }
  374. }
  375. /* Respond with an empty chunk to signal HTTP response completion */
  376. httpd_resp_sendstr_chunk(req, NULL);
  377. }
  378. else
  379. {
  380. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!");
  381. return ESP_ERR_NOT_FOUND;
  382. }
  383. #ifdef DEBUG_DETAIL_ON
  384. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  385. #endif
  386. return ESP_OK;
  387. }
  388. esp_err_t handler_editflow(httpd_req_t *req)
  389. {
  390. #ifdef DEBUG_DETAIL_ON
  391. LogFile.WriteHeapInfo("handler_editflow - Start");
  392. #endif
  393. ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri);
  394. char _query[200];
  395. char _valuechar[30];
  396. string _task;
  397. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  398. {
  399. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  400. {
  401. #ifdef DEBUG_DETAIL_ON
  402. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  403. #endif
  404. _task = string(_valuechar);
  405. }
  406. }
  407. if (_task.compare("namenumbers") == 0)
  408. {
  409. ESP_LOGD(TAG, "Get NUMBER list");
  410. return get_numbers_file_handler(req);
  411. }
  412. if (_task.compare("data") == 0)
  413. {
  414. ESP_LOGD(TAG, "Get data list");
  415. return get_data_file_handler(req);
  416. }
  417. if (_task.compare("tflite") == 0)
  418. {
  419. ESP_LOGD(TAG, "Get tflite list");
  420. return get_tflite_file_handler(req);
  421. }
  422. if (_task.compare("copy") == 0)
  423. {
  424. string in, out, zw;
  425. httpd_query_key_value(_query, "in", _valuechar, 30);
  426. in = string(_valuechar);
  427. httpd_query_key_value(_query, "out", _valuechar, 30);
  428. out = string(_valuechar);
  429. #ifdef DEBUG_DETAIL_ON
  430. ESP_LOGD(TAG, "in: %s", in.c_str());
  431. ESP_LOGD(TAG, "out: %s", out.c_str());
  432. #endif
  433. in = "/sdcard" + in;
  434. out = "/sdcard" + out;
  435. CopyFile(in, out);
  436. zw = "Copy Done";
  437. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  438. httpd_resp_send(req, zw.c_str(), zw.length());
  439. }
  440. if (_task.compare("cutref") == 0)
  441. {
  442. string in, out, zw;
  443. int x, y, dx, dy;
  444. bool enhance = false;
  445. httpd_query_key_value(_query, "in", _valuechar, 30);
  446. in = string(_valuechar);
  447. httpd_query_key_value(_query, "out", _valuechar, 30);
  448. out = string(_valuechar);
  449. httpd_query_key_value(_query, "x", _valuechar, 30);
  450. zw = string(_valuechar);
  451. x = stoi(zw);
  452. httpd_query_key_value(_query, "y", _valuechar, 30);
  453. zw = string(_valuechar);
  454. y = stoi(zw);
  455. httpd_query_key_value(_query, "dx", _valuechar, 30);
  456. zw = string(_valuechar);
  457. dx = stoi(zw);
  458. httpd_query_key_value(_query, "dy", _valuechar, 30);
  459. zw = string(_valuechar);
  460. dy = stoi(zw);
  461. #ifdef DEBUG_DETAIL_ON
  462. ESP_LOGD(TAG, "in: %s", in.c_str());
  463. ESP_LOGD(TAG, "out: %s", out.c_str());
  464. ESP_LOGD(TAG, "x: %s", zw.c_str());
  465. ESP_LOGD(TAG, "y: %s", zw.c_str());
  466. ESP_LOGD(TAG, "dx: %s", zw.c_str());
  467. ESP_LOGD(TAG, "dy: %s", zw.c_str());
  468. #endif
  469. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  470. {
  471. zw = string(_valuechar);
  472. if (zw.compare("true") == 0)
  473. {
  474. enhance = true;
  475. }
  476. }
  477. in = "/sdcard" + in;
  478. out = "/sdcard" + out;
  479. string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  480. CAlignAndCutImage *caic = new CAlignAndCutImage(in);
  481. caic->CutAndSave(out2, x, y, dx, dy);
  482. delete caic;
  483. CImageBasis *cim = new CImageBasis(out2);
  484. if (enhance)
  485. {
  486. cim->Contrast(90);
  487. }
  488. cim->SaveToFile(out);
  489. delete cim;
  490. zw = "CutImage Done";
  491. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  492. httpd_resp_send(req, zw.c_str(), zw.length());
  493. }
  494. if (_task.compare("test_take") == 0)
  495. {
  496. std::string _host = "";
  497. std::string _bri = "";
  498. std::string _con = "";
  499. std::string _sat = "";
  500. std::string _int = "";
  501. int bri = -100;
  502. int sat = -100;
  503. int con = -100;
  504. int intens = -100;
  505. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  506. _host = std::string(_valuechar);
  507. }
  508. if (httpd_query_key_value(_query, "int", _valuechar, 30) == ESP_OK) {
  509. _int = std::string(_valuechar);
  510. intens = stoi(_int);
  511. }
  512. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK) {
  513. _bri = std::string(_valuechar);
  514. bri = stoi(_bri);
  515. }
  516. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK) {
  517. _con = std::string(_valuechar);
  518. con = stoi(_con);
  519. }
  520. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK) {
  521. _sat = std::string(_valuechar);
  522. sat = stoi(_sat);
  523. }
  524. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  525. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  526. Camera.SetBrightnessContrastSaturation(bri, con, sat);
  527. Camera.SetLEDIntensity(intens);
  528. ESP_LOGD(TAG, "test_take - vor TakeImage");
  529. std::string zw = tfliteflow.doSingleStep("[TakeImage]", _host);
  530. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  531. httpd_resp_send(req, zw.c_str(), zw.length());
  532. }
  533. if (_task.compare("test_align") == 0)
  534. {
  535. std::string _host = "";
  536. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  537. _host = std::string(_valuechar);
  538. }
  539. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  540. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  541. std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
  542. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  543. httpd_resp_send(req, zw.c_str(), zw.length());
  544. }
  545. #ifdef DEBUG_DETAIL_ON
  546. LogFile.WriteHeapInfo("handler_editflow - Done");
  547. #endif
  548. return ESP_OK;
  549. }
  550. esp_err_t handler_statusflow(httpd_req_t *req)
  551. {
  552. #ifdef DEBUG_DETAIL_ON
  553. LogFile.WriteHeapInfo("handler_prevalue - Start");
  554. #endif
  555. const char* resp_str;
  556. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  557. if (bTaskAutoFlowCreated)
  558. {
  559. #ifdef DEBUG_DETAIL_ON
  560. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  561. #endif
  562. string* zw = tfliteflow.getActStatusWithTime();
  563. resp_str = zw->c_str();
  564. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  565. }
  566. else
  567. {
  568. resp_str = "Flow task not yet created";
  569. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  570. }
  571. #ifdef DEBUG_DETAIL_ON
  572. LogFile.WriteHeapInfo("handler_prevalue - Done");
  573. #endif
  574. return ESP_OK;
  575. }
  576. esp_err_t handler_cputemp(httpd_req_t *req)
  577. {
  578. #ifdef DEBUG_DETAIL_ON
  579. LogFile.WriteHeapInfo("handler_cputemp - Start");
  580. #endif
  581. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  582. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  583. #ifdef DEBUG_DETAIL_ON
  584. LogFile.WriteHeapInfo("handler_cputemp - End");
  585. #endif
  586. return ESP_OK;
  587. }
  588. esp_err_t handler_rssi(httpd_req_t *req)
  589. {
  590. #ifdef DEBUG_DETAIL_ON
  591. LogFile.WriteHeapInfo("handler_rssi - Start");
  592. #endif
  593. if (getWIFIisConnected())
  594. {
  595. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  596. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  597. }
  598. else
  599. {
  600. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  601. return ESP_ERR_NOT_FOUND;
  602. }
  603. #ifdef DEBUG_DETAIL_ON
  604. LogFile.WriteHeapInfo("handler_rssi - End");
  605. #endif
  606. return ESP_OK;
  607. }
  608. esp_err_t handler_uptime(httpd_req_t *req)
  609. {
  610. #ifdef DEBUG_DETAIL_ON
  611. LogFile.WriteHeapInfo("handler_uptime - Start");
  612. #endif
  613. std::string formatedUptime = getFormatedUptime(false);
  614. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  615. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  616. #ifdef DEBUG_DETAIL_ON
  617. LogFile.WriteHeapInfo("handler_uptime - End");
  618. #endif
  619. return ESP_OK;
  620. }
  621. esp_err_t handler_prevalue(httpd_req_t *req)
  622. {
  623. #ifdef DEBUG_DETAIL_ON
  624. LogFile.WriteHeapInfo("handler_prevalue - Start");
  625. #endif
  626. const char* resp_str;
  627. string zw;
  628. #ifdef DEBUG_DETAIL_ON
  629. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  630. #endif
  631. char _query[100];
  632. char _size[10] = "";
  633. char _numbers[50] = "default";
  634. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  635. {
  636. #ifdef DEBUG_DETAIL_ON
  637. ESP_LOGD(TAG, "Query: %s", _query);
  638. #endif
  639. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  640. {
  641. #ifdef DEBUG_DETAIL_ON
  642. ESP_LOGD(TAG, "Value: %s", _size);
  643. #endif
  644. }
  645. httpd_query_key_value(_query, "numbers", _numbers, 50);
  646. }
  647. if (strlen(_size) == 0)
  648. {
  649. zw = tfliteflow.GetPrevalue(std::string(_numbers));
  650. }
  651. else
  652. {
  653. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size, _numbers, true);
  654. }
  655. resp_str = zw.c_str();
  656. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  657. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  658. #ifdef DEBUG_DETAIL_ON
  659. LogFile.WriteHeapInfo("handler_prevalue - End");
  660. #endif
  661. return ESP_OK;
  662. }
  663. void task_autodoFlow(void *pvParameter)
  664. {
  665. int64_t fr_start, fr_delta_ms;
  666. bTaskAutoFlowCreated = true;
  667. if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
  668. {
  669. tfliteflow.setActStatus("Initialization (delayed)");
  670. //#ifdef ENABLE_MQTT
  671. //MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  672. //#endif //ENABLE_MQTT
  673. vTaskDelay(60*5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
  674. }
  675. ESP_LOGD(TAG, "task_autodoFlow: start");
  676. doInit();
  677. auto_isrunning = tfliteflow.isAutoStart(auto_interval);
  678. if (isSetupModusActive())
  679. {
  680. auto_isrunning = false;
  681. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  682. tfliteflow.doFlowTakeImageOnly(zw_time);
  683. }
  684. while (auto_isrunning)
  685. {
  686. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  687. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  688. time_t roundStartTime = getUpTime();
  689. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  690. fr_start = esp_timer_get_time();
  691. if (flowisrunning)
  692. {
  693. #ifdef DEBUG_DETAIL_ON
  694. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  695. #endif
  696. }
  697. else
  698. {
  699. #ifdef DEBUG_DETAIL_ON
  700. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  701. #endif
  702. flowisrunning = true;
  703. doflow();
  704. #ifdef DEBUG_DETAIL_ON
  705. ESP_LOGD(TAG, "Remove older log files");
  706. #endif
  707. LogFile.RemoveOldLogFile();
  708. LogFile.RemoveOldDataLog();
  709. }
  710. //Round finished -> Logfile
  711. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) +
  712. " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  713. //CPU Temp -> Logfile
  714. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  715. // WIFI Signal Strength (RSSI) -> Logfile
  716. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  717. // Check if time is synchronized (if NTP is configured)
  718. if (getUseNtp() && !getTimeIsSet()) {
  719. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set. Check configuration");
  720. StatusLED(TIME_CHECK, 1, false);
  721. }
  722. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  723. if (auto_interval > fr_delta_ms)
  724. {
  725. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  726. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long) xDelay);
  727. vTaskDelay( xDelay );
  728. }
  729. }
  730. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  731. xHandletask_autodoFlow = NULL;
  732. ESP_LOGD(TAG, "task_autodoFlow: end");
  733. }
  734. void TFliteDoAutoStart()
  735. {
  736. BaseType_t xReturned;
  737. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  738. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow, 0);
  739. //xReturned = xTaskCreate(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow);
  740. if( xReturned != pdPASS )
  741. {
  742. ESP_LOGD(TAG, "ERROR task_autodoFlow konnte nicht erzeugt werden!");
  743. }
  744. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  745. }
  746. #ifdef ENABLE_MQTT
  747. std::string GetMQTTMainTopic()
  748. {
  749. return tfliteflow.GetMQTTMainTopic();
  750. }
  751. #endif//ENABLE_MQTT
  752. void register_server_tflite_uri(httpd_handle_t server)
  753. {
  754. ESP_LOGI(TAG, "server_part_camera - Registering URI handlers");
  755. httpd_uri_t camuri = { };
  756. camuri.method = HTTP_GET;
  757. camuri.uri = "/doinit";
  758. camuri.handler = handler_init;
  759. camuri.user_ctx = (void*) "Light On";
  760. httpd_register_uri_handler(server, &camuri);
  761. // Legacy API => New: "/setPreValue"
  762. camuri.uri = "/setPreValue.html";
  763. camuri.handler = handler_prevalue;
  764. camuri.user_ctx = (void*) "Prevalue";
  765. httpd_register_uri_handler(server, &camuri);
  766. camuri.uri = "/setPreValue";
  767. camuri.handler = handler_prevalue;
  768. camuri.user_ctx = (void*) "Prevalue";
  769. httpd_register_uri_handler(server, &camuri);
  770. camuri.uri = "/flow_start";
  771. camuri.handler = handler_flow_start;
  772. camuri.user_ctx = (void*) "Flow Start";
  773. httpd_register_uri_handler(server, &camuri);
  774. camuri.uri = "/statusflow.html";
  775. camuri.handler = handler_statusflow;
  776. camuri.user_ctx = (void*) "Light Off";
  777. httpd_register_uri_handler(server, &camuri);
  778. camuri.uri = "/statusflow";
  779. camuri.handler = handler_statusflow;
  780. camuri.user_ctx = (void*) "Light Off";
  781. httpd_register_uri_handler(server, &camuri);
  782. // Legacy API => New: "/cpu_temperature"
  783. camuri.uri = "/cputemp.html";
  784. camuri.handler = handler_cputemp;
  785. camuri.user_ctx = (void*) "Light Off";
  786. httpd_register_uri_handler(server, &camuri);
  787. camuri.uri = "/cpu_temperature";
  788. camuri.handler = handler_cputemp;
  789. camuri.user_ctx = (void*) "Light Off";
  790. httpd_register_uri_handler(server, &camuri);
  791. // Legacy API => New: "/rssi"
  792. camuri.uri = "/rssi.html";
  793. camuri.handler = handler_rssi;
  794. camuri.user_ctx = (void*) "Light Off";
  795. httpd_register_uri_handler(server, &camuri);
  796. camuri.uri = "/rssi";
  797. camuri.handler = handler_rssi;
  798. camuri.user_ctx = (void*) "Light Off";
  799. httpd_register_uri_handler(server, &camuri);
  800. camuri.uri = "/uptime";
  801. camuri.handler = handler_uptime;
  802. camuri.user_ctx = (void*) "Light Off";
  803. httpd_register_uri_handler(server, &camuri);
  804. camuri.uri = "/editflow";
  805. camuri.handler = handler_editflow;
  806. camuri.user_ctx = (void*) "EditFlow";
  807. httpd_register_uri_handler(server, &camuri);
  808. // Legacy API => New: "/value"
  809. camuri.uri = "/value.html";
  810. camuri.handler = handler_wasserzaehler;
  811. camuri.user_ctx = (void*) "Value";
  812. httpd_register_uri_handler(server, &camuri);
  813. camuri.uri = "/value";
  814. camuri.handler = handler_wasserzaehler;
  815. camuri.user_ctx = (void*) "Value";
  816. httpd_register_uri_handler(server, &camuri);
  817. // Legacy API => New: "/value"
  818. camuri.uri = "/wasserzaehler.html";
  819. camuri.handler = handler_wasserzaehler;
  820. camuri.user_ctx = (void*) "Wasserzaehler";
  821. httpd_register_uri_handler(server, &camuri);
  822. camuri.uri = "/json";
  823. camuri.handler = handler_json;
  824. camuri.user_ctx = (void*) "JSON";
  825. httpd_register_uri_handler(server, &camuri);
  826. camuri.uri = "/heap";
  827. camuri.handler = handler_get_heap;
  828. camuri.user_ctx = (void*) "Heap";
  829. httpd_register_uri_handler(server, &camuri);
  830. }