server_tflite.cpp 29 KB

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