server_tflite.cpp 29 KB

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