server_tflite.cpp 30 KB

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