server_tflite.cpp 28 KB

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