MainFlowControl.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. #include "MainFlowControl.h"
  2. #include <new>
  3. #include <string>
  4. #include <vector>
  5. #include "string.h"
  6. #include "esp_log.h"
  7. #include <esp_timer.h>
  8. #include <iomanip>
  9. #include <sstream>
  10. #include "../../include/defines.h"
  11. #include "Helper.h"
  12. #include "statusled.h"
  13. #include "esp_camera.h"
  14. #include "time_sntp.h"
  15. #include "ClassControllCamera.h"
  16. #include "ClassFlowControll.h"
  17. #include "ClassLogFile.h"
  18. #include "server_GPIO.h"
  19. #include "server_file.h"
  20. #include "server_ota.h"
  21. #include "read_wlanini.h"
  22. #include "connect_wlan.h"
  23. #include "psram.h"
  24. #include "basic_auth.h"
  25. // support IDF 5.x
  26. #ifndef portTICK_RATE_MS
  27. #define portTICK_RATE_MS portTICK_PERIOD_MS
  28. #endif
  29. ClassFlowControll flowctrl;
  30. camera_flow_config_temp_t CFstatus;
  31. TaskHandle_t xHandletask_autodoFlow = NULL;
  32. bool bTaskAutoFlowCreated = false;
  33. // True only while the flow objects are fully built. bTaskAutoFlowCreated is already true during the
  34. // post-panic init delay and while doInit() rebuilds the flow, when flowctrl's post-processing is NULL
  35. // or half-built, so REST handlers that read the results must check this flag instead.
  36. bool bFlowInitialized = false;
  37. bool flowisrunning = false;
  38. long auto_interval = 0;
  39. bool autostartIsEnabled = false;
  40. int countRounds = 0;
  41. bool isPlannedReboot = false;
  42. static const char *TAG = "MAINCTRL";
  43. // #define DEBUG_DETAIL_ON
  44. void CheckIsPlannedReboot(void)
  45. {
  46. FILE *pfile;
  47. if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL)
  48. {
  49. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Initial boot or not a planned reboot");
  50. isPlannedReboot = false;
  51. }
  52. else
  53. {
  54. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot");
  55. DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
  56. isPlannedReboot = true;
  57. }
  58. }
  59. bool getIsPlannedReboot(void)
  60. {
  61. return isPlannedReboot;
  62. }
  63. int getCountFlowRounds(void)
  64. {
  65. return countRounds;
  66. }
  67. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  68. {
  69. return flowctrl.GetJPGStream(_filename, req);
  70. }
  71. esp_err_t GetRawJPG(httpd_req_t *req, bool previewCapture)
  72. {
  73. return flowctrl.SendRawJPG(req, previewCapture);
  74. }
  75. bool isSetupModusActive(void)
  76. {
  77. return flowctrl.getStatusSetupModus();
  78. }
  79. void DeleteMainFlowTask(void)
  80. {
  81. #ifdef DEBUG_DETAIL_ON
  82. ESP_LOGD(TAG, "DeleteMainFlowTask: xHandletask_autodoFlow: %ld", (long)xHandletask_autodoFlow);
  83. #endif
  84. if (xHandletask_autodoFlow != NULL)
  85. {
  86. vTaskDelete(xHandletask_autodoFlow);
  87. xHandletask_autodoFlow = NULL;
  88. }
  89. #ifdef DEBUG_DETAIL_ON
  90. ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
  91. #endif
  92. }
  93. void doInit(void)
  94. {
  95. bFlowInitialized = false;
  96. #ifdef DEBUG_DETAIL_ON
  97. ESP_LOGD(TAG, "Start flowctrl.InitFlow(config);");
  98. #endif
  99. flowctrl.InitFlow(CONFIG_FILE);
  100. bFlowInitialized = true;
  101. #ifdef DEBUG_DETAIL_ON
  102. ESP_LOGD(TAG, "Finished flowctrl.InitFlow(config);");
  103. #endif
  104. /* GPIO handler has to be initialized before MQTT init to ensure proper topic subscription */
  105. gpio_handler_init();
  106. #ifdef ENABLE_MQTT
  107. flowctrl.StartMQTTService();
  108. #endif // ENABLE_MQTT
  109. }
  110. bool doflow(void)
  111. {
  112. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  113. ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
  114. flowisrunning = true;
  115. flowctrl.doFlow(zw_time);
  116. flowisrunning = false;
  117. #ifdef DEBUG_DETAIL_ON
  118. ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
  119. #endif
  120. return true;
  121. }
  122. esp_err_t handler_get_heap(httpd_req_t *req)
  123. {
  124. #ifdef DEBUG_DETAIL_ON
  125. LogFile.WriteHeapInfo("handler_get_heap - Start");
  126. ESP_LOGD(TAG, "handler_get_heap uri: %s", req->uri);
  127. #endif
  128. std::string zw = "Heap info:<br>" + getESPHeapInfo();
  129. #ifdef TASK_ANALYSIS_ON
  130. char *pcTaskList = (char *)calloc_psram_heap(std::string(TAG) + "->pcTaskList", 1, sizeof(char) * 768, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  131. if (pcTaskList)
  132. {
  133. vTaskList(pcTaskList);
  134. zw = zw + "<br><br>Task info:<br><pre>Name | State | Prio | Lowest stacksize | Creation order | CPU (-1=NoAffinity)<br>" + std::string(pcTaskList) + "</pre>";
  135. free_psram_heap(std::string(TAG) + "->pcTaskList", pcTaskList);
  136. }
  137. else
  138. {
  139. zw = zw + "<br><br>Task info:<br>ERROR - Allocation of TaskList buffer in PSRAM failed";
  140. }
  141. #endif
  142. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  143. if (zw.length() > 0)
  144. {
  145. httpd_resp_send(req, zw.c_str(), zw.length());
  146. }
  147. else
  148. {
  149. httpd_resp_send(req, NULL, 0);
  150. }
  151. #ifdef DEBUG_DETAIL_ON
  152. LogFile.WriteHeapInfo("handler_get_heap - Done");
  153. #endif
  154. return ESP_OK;
  155. }
  156. esp_err_t handler_init(httpd_req_t *req)
  157. {
  158. #ifdef DEBUG_DETAIL_ON
  159. LogFile.WriteHeapInfo("handler_init - Start");
  160. ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
  161. #endif
  162. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  163. // Rebuilding the flow at runtime is not safe: InitFlow() keeps the old ClassFlow objects (a second full
  164. // pipeline, ~1.2 MB PSRAM, until allocations fail and the device panics) and races with a running round.
  165. // Apply the configuration with a reboot instead.
  166. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "/doinit requested -> rebooting to apply the configuration");
  167. const char *resp_str = "Reboot to apply the configuration<br>";
  168. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  169. doReboot();
  170. #ifdef DEBUG_DETAIL_ON
  171. LogFile.WriteHeapInfo("handler_init - Done");
  172. #endif
  173. return ESP_OK;
  174. }
  175. esp_err_t handler_stream(httpd_req_t *req)
  176. {
  177. #ifdef DEBUG_DETAIL_ON
  178. LogFile.WriteHeapInfo("handler_stream - Start");
  179. ESP_LOGD(TAG, "handler_stream uri: %s", req->uri);
  180. #endif
  181. char _query[50] = ""; // stays empty when the request has no query string
  182. char _value[10];
  183. bool flashlightOn = false;
  184. if (httpd_req_get_url_query_str(req, _query, 50) == ESP_OK)
  185. {
  186. // ESP_LOGD(TAG, "Query: %s", _query);
  187. if (httpd_query_key_value(_query, "flashlight", _value, 10) == ESP_OK)
  188. {
  189. #ifdef DEBUG_DETAIL_ON
  190. ESP_LOGD(TAG, "flashlight is found%s", _value);
  191. #endif
  192. if (strlen(_value) > 0)
  193. {
  194. flashlightOn = true;
  195. }
  196. }
  197. }
  198. Camera.CaptureToStream(req, flashlightOn);
  199. #ifdef DEBUG_DETAIL_ON
  200. LogFile.WriteHeapInfo("handler_stream - Done");
  201. #endif
  202. return ESP_OK;
  203. }
  204. esp_err_t handler_flow_start(httpd_req_t *req)
  205. {
  206. #ifdef DEBUG_DETAIL_ON
  207. LogFile.WriteHeapInfo("handler_flow_start - Start");
  208. #endif
  209. ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
  210. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  211. if (autostartIsEnabled)
  212. {
  213. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  214. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
  215. const char *resp_str = "The flow is going to be started immediately or is already running";
  216. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  217. }
  218. else
  219. {
  220. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
  221. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow start triggered by REST API, but flow is not active");
  222. }
  223. #ifdef DEBUG_DETAIL_ON
  224. LogFile.WriteHeapInfo("handler_flow_start - Done");
  225. #endif
  226. return ESP_OK;
  227. }
  228. #ifdef ENABLE_MQTT
  229. esp_err_t MQTTCtrlFlowStart(std::string _topic)
  230. {
  231. #ifdef DEBUG_DETAIL_ON
  232. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Start");
  233. #endif
  234. ESP_LOGD(TAG, "MQTTCtrlFlowStart: topic %s", _topic.c_str());
  235. if (autostartIsEnabled)
  236. {
  237. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  238. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by MQTT topic " + _topic);
  239. }
  240. else
  241. {
  242. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by MQTT topic " + _topic + ", but flow is not active!");
  243. }
  244. #ifdef DEBUG_DETAIL_ON
  245. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Done");
  246. #endif
  247. return ESP_OK;
  248. }
  249. #endif // ENABLE_MQTT
  250. esp_err_t handler_json(httpd_req_t *req)
  251. {
  252. #ifdef DEBUG_DETAIL_ON
  253. LogFile.WriteHeapInfo("handler_json - Start");
  254. #endif
  255. ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
  256. if (bTaskAutoFlowCreated && bFlowInitialized)
  257. {
  258. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  259. httpd_resp_set_type(req, "application/json");
  260. std::string zw = flowctrl.getJSON();
  261. if (zw.length() > 0)
  262. {
  263. httpd_resp_send(req, zw.c_str(), zw.length());
  264. }
  265. else
  266. {
  267. httpd_resp_send(req, NULL, 0);
  268. }
  269. }
  270. else
  271. {
  272. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!");
  273. return ESP_ERR_NOT_FOUND;
  274. }
  275. #ifdef DEBUG_DETAIL_ON
  276. LogFile.WriteHeapInfo("handler_JSON - Done");
  277. #endif
  278. return ESP_OK;
  279. }
  280. /**
  281. * Generates a http response containing the OpenMetrics (https://openmetrics.io/) text wire format
  282. * according to https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#text-format.
  283. *
  284. * A MetricFamily with a Metric for each Sequence is provided. If no valid value is available, the metric is not provided.
  285. * MetricPoints are provided without a timestamp. Additional metrics with some device information is also provided.
  286. *
  287. * The metric name prefix is 'ai_on_the_edge_device_'.
  288. *
  289. * example configuration for Prometheus (`prometheus.yml`):
  290. *
  291. * - job_name: watermeter
  292. * static_configs:
  293. * - targets: ['watermeter.fritz.box']
  294. *
  295. */
  296. esp_err_t handler_openmetrics(httpd_req_t *req)
  297. {
  298. #ifdef DEBUG_DETAIL_ON
  299. LogFile.WriteHeapInfo("handler_openmetrics - Start");
  300. #endif
  301. ESP_LOGD(TAG, "handler_openmetrics uri: %s", req->uri);
  302. if (bTaskAutoFlowCreated && bFlowInitialized)
  303. {
  304. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  305. httpd_resp_set_type(req, "text/plain"); // application/openmetrics-text is not yet supported by prometheus so we use text/plain for now
  306. const string metricNamePrefix = "ai_on_the_edge_device";
  307. // get current measurement (flow)
  308. string response = createSequenceMetrics(metricNamePrefix, flowctrl.getNumbers());
  309. // CPU Temperature
  310. response += createMetric(metricNamePrefix + "_cpu_temperature_celsius", "current cpu temperature in celsius", "gauge", std::to_string((int)temperatureRead()));
  311. // WiFi signal strength
  312. response += createMetric(metricNamePrefix + "_rssi_dbm", "current WiFi signal strength in dBm", "gauge", std::to_string(get_WIFI_RSSI()));
  313. // memory info
  314. response += createMetric(metricNamePrefix + "_memory_heap_free_bytes", "available heap memory", "gauge", std::to_string(getESPHeapSize()));
  315. // device uptime
  316. response += createMetric(metricNamePrefix + "_uptime_seconds", "device uptime in seconds", "gauge", std::to_string((long)getUpTime()));
  317. // data aquisition round
  318. response += createMetric(metricNamePrefix + "_rounds_total", "data aquisition rounds since device startup", "counter", std::to_string(countRounds));
  319. // the response always contains at least the metadata (HELP, TYPE) for the MetricFamily so no length check is needed
  320. httpd_resp_send(req, response.c_str(), response.length());
  321. }
  322. else
  323. {
  324. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /metrics not yet available!");
  325. return ESP_ERR_NOT_FOUND;
  326. }
  327. #ifdef DEBUG_DETAIL_ON
  328. LogFile.WriteHeapInfo("handler_openmetrics - Done");
  329. #endif
  330. return ESP_OK;
  331. }
  332. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  333. {
  334. #ifdef DEBUG_DETAIL_ON
  335. LogFile.WriteHeapInfo("handler water counter - Start");
  336. #endif
  337. if (bTaskAutoFlowCreated)
  338. {
  339. bool _rawValue = false;
  340. bool _noerror = false;
  341. bool _all = false;
  342. std::string _type = "value";
  343. std::string zw;
  344. ESP_LOGD(TAG, "handler water counter uri: %s", req->uri);
  345. char _query[100] = ""; // stays empty when the request has no query string
  346. char _size[10];
  347. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  348. {
  349. // ESP_LOGD(TAG, "Query: %s", _query);
  350. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  351. {
  352. #ifdef DEBUG_DETAIL_ON
  353. ESP_LOGD(TAG, "all is found%s", _size);
  354. #endif
  355. _all = true;
  356. }
  357. if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
  358. {
  359. #ifdef DEBUG_DETAIL_ON
  360. ESP_LOGD(TAG, "all is found: %s", _size);
  361. #endif
  362. _type = std::string(_size);
  363. }
  364. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  365. {
  366. #ifdef DEBUG_DETAIL_ON
  367. ESP_LOGD(TAG, "rawvalue is found: %s", _size);
  368. #endif
  369. _rawValue = true;
  370. }
  371. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  372. {
  373. #ifdef DEBUG_DETAIL_ON
  374. ESP_LOGD(TAG, "noerror is found: %s", _size);
  375. #endif
  376. _noerror = true;
  377. }
  378. }
  379. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  380. if (_all)
  381. {
  382. httpd_resp_set_type(req, "text/plain");
  383. ESP_LOGD(TAG, "TYPE: %s", _type.c_str());
  384. int _intype = READOUT_TYPE_VALUE;
  385. if (_type == "prevalue")
  386. {
  387. _intype = READOUT_TYPE_PREVALUE;
  388. }
  389. if (_type == "raw")
  390. {
  391. _intype = READOUT_TYPE_RAWVALUE;
  392. }
  393. if (_type == "error")
  394. {
  395. _intype = READOUT_TYPE_ERROR;
  396. }
  397. zw = flowctrl.getReadoutAll(_intype);
  398. ESP_LOGD(TAG, "ZW: %s", zw.c_str());
  399. if (zw.length() > 0)
  400. {
  401. httpd_resp_send(req, zw.c_str(), zw.length());
  402. }
  403. return ESP_OK;
  404. }
  405. std::string status = flowctrl.getActStatus();
  406. std::string query = std::string(_query);
  407. // ESP_LOGD(TAG, "Query: %s, query.c_str());
  408. if (query.find("full") != std::string::npos)
  409. {
  410. std::string txt;
  411. txt = "<body style=\"font-family: arial\">";
  412. if ((countRounds <= 1) && (status != std::string("Flow finished")))
  413. {
  414. // First round not completed yet
  415. txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + status + "</h3>\n";
  416. }
  417. else
  418. {
  419. txt += "<h3>Value</h3>";
  420. }
  421. httpd_resp_sendstr_chunk(req, txt.c_str());
  422. }
  423. zw = flowctrl.getReadout(_rawValue, _noerror, 0);
  424. if (zw.length() > 0)
  425. {
  426. httpd_resp_sendstr_chunk(req, zw.c_str());
  427. }
  428. if (query.find("full") != std::string::npos)
  429. {
  430. std::string txt, zw;
  431. if ((countRounds <= 1) && (status != std::string("Flow finished")))
  432. {
  433. // First round not completed yet
  434. // Nothing to do
  435. }
  436. else
  437. {
  438. /* Digit ROIs */
  439. txt = "<body style=\"font-family: arial\">";
  440. txt += "<hr><h3>Recognized Digit ROIs (previous round)</h3>\n";
  441. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  442. std::vector<HTMLInfo *> htmlinfodig;
  443. htmlinfodig = flowctrl.GetAllDigit();
  444. for (int i = 0; i < htmlinfodig.size(); ++i)
  445. {
  446. if (flowctrl.GetTypeDigit() == Digit)
  447. {
  448. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  449. if ((htmlinfodig[i]->val >= 10) || (htmlinfodig[i]->val < 0))
  450. {
  451. zw = "NaN";
  452. }
  453. else
  454. {
  455. zw = std::to_string((int)htmlinfodig[i]->val);
  456. }
  457. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  458. }
  459. else
  460. {
  461. std::stringstream stream;
  462. stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
  463. zw = stream.str();
  464. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  465. if ((std::stod(zw) >= 10) || (std::stod(zw) < 0))
  466. {
  467. zw = "NaN";
  468. }
  469. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  470. }
  471. delete htmlinfodig[i];
  472. }
  473. htmlinfodig.clear();
  474. txt += "</tr></table>\n";
  475. httpd_resp_sendstr_chunk(req, txt.c_str());
  476. /* Analog ROIs */
  477. txt = "<hr><h3>Recognized Analog ROIs (previous round)</h3>\n";
  478. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  479. std::vector<HTMLInfo *> htmlinfoana;
  480. htmlinfoana = flowctrl.GetAllAnalog();
  481. for (int i = 0; i < htmlinfoana.size(); ++i)
  482. {
  483. std::stringstream stream;
  484. stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
  485. zw = stream.str();
  486. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  487. if ((std::stod(zw) >= 10) || (std::stod(zw) < 0))
  488. {
  489. zw = "NaN";
  490. }
  491. txt += "<td style=\"width: 150px;\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"></p></td>\n";
  492. delete htmlinfoana[i];
  493. }
  494. htmlinfoana.clear();
  495. txt += "</tr>\n</table>\n";
  496. httpd_resp_sendstr_chunk(req, txt.c_str());
  497. /* Full Image
  498. * Only show it after the image got taken */
  499. txt = "<hr><h3>Full Image (current round)</h3>\n";
  500. if ((status == std::string("Initialization")) ||
  501. (status == std::string("Initialization (delayed)")) ||
  502. (status == std::string("Take Image")))
  503. {
  504. txt += "<p>Current state: " + status + "</p>\n";
  505. }
  506. else
  507. {
  508. txt += "<img src=\"/img_tmp/alg_roi.jpg\">\n";
  509. }
  510. httpd_resp_sendstr_chunk(req, txt.c_str());
  511. }
  512. }
  513. /* Respond with an empty chunk to signal HTTP response completion */
  514. httpd_resp_sendstr_chunk(req, NULL);
  515. }
  516. else
  517. {
  518. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!");
  519. return ESP_ERR_NOT_FOUND;
  520. }
  521. #ifdef DEBUG_DETAIL_ON
  522. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  523. #endif
  524. return ESP_OK;
  525. }
  526. esp_err_t handler_editflow(httpd_req_t *req)
  527. {
  528. #ifdef DEBUG_DETAIL_ON
  529. LogFile.WriteHeapInfo("handler_editflow - Start");
  530. #endif
  531. ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri);
  532. char _query[512] = ""; // stays empty when the request has no query string
  533. char _valuechar[30];
  534. std::string _task;
  535. if (httpd_req_get_url_query_str(req, _query, 512) == ESP_OK)
  536. {
  537. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  538. {
  539. #ifdef DEBUG_DETAIL_ON
  540. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  541. #endif
  542. _task = std::string(_valuechar);
  543. }
  544. }
  545. if (_task.compare("namenumbers") == 0)
  546. {
  547. ESP_LOGD(TAG, "Get NUMBER list");
  548. return get_numbers_file_handler(req);
  549. }
  550. if (_task.compare("data") == 0)
  551. {
  552. ESP_LOGD(TAG, "Get data list");
  553. return get_data_file_handler(req);
  554. }
  555. if (_task.compare("tflite") == 0)
  556. {
  557. ESP_LOGD(TAG, "Get tflite list");
  558. return get_tflite_file_handler(req);
  559. }
  560. if (_task.compare("copy") == 0)
  561. {
  562. std::string in, out, zw;
  563. httpd_query_key_value(_query, "in", _valuechar, 30);
  564. in = std::string(_valuechar);
  565. httpd_query_key_value(_query, "out", _valuechar, 30);
  566. out = std::string(_valuechar);
  567. #ifdef DEBUG_DETAIL_ON
  568. ESP_LOGD(TAG, "in: %s", in.c_str());
  569. ESP_LOGD(TAG, "out: %s", out.c_str());
  570. #endif
  571. in = "/sdcard" + in;
  572. out = "/sdcard" + out;
  573. CopyFile(in, out);
  574. zw = "Copy Done";
  575. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  576. httpd_resp_send(req, zw.c_str(), zw.length());
  577. }
  578. if (_task.compare("cutref") == 0)
  579. {
  580. std::string in, out, zw;
  581. int x = 0, y = 0, dx = 20, dy = 20;
  582. bool enhance = false;
  583. httpd_query_key_value(_query, "in", _valuechar, 30);
  584. in = std::string(_valuechar);
  585. httpd_query_key_value(_query, "out", _valuechar, 30);
  586. out = std::string(_valuechar);
  587. httpd_query_key_value(_query, "x", _valuechar, 30);
  588. std::string _x = std::string(_valuechar);
  589. if (isStringNumeric(_x))
  590. {
  591. x = std::stoi(_x);
  592. }
  593. httpd_query_key_value(_query, "y", _valuechar, 30);
  594. std::string _y = std::string(_valuechar);
  595. if (isStringNumeric(_y))
  596. {
  597. y = std::stoi(_y);
  598. }
  599. httpd_query_key_value(_query, "dx", _valuechar, 30);
  600. std::string _dx = std::string(_valuechar);
  601. if (isStringNumeric(_dx))
  602. {
  603. dx = std::stoi(_dx);
  604. }
  605. httpd_query_key_value(_query, "dy", _valuechar, 30);
  606. std::string _dy = std::string(_valuechar);
  607. if (isStringNumeric(_dy))
  608. {
  609. dy = std::stoi(_dy);
  610. }
  611. #ifdef DEBUG_DETAIL_ON
  612. ESP_LOGD(TAG, "in: %s", in.c_str());
  613. ESP_LOGD(TAG, "out: %s", out.c_str());
  614. ESP_LOGD(TAG, "x: %s", _x.c_str());
  615. ESP_LOGD(TAG, "y: %s", _y.c_str());
  616. ESP_LOGD(TAG, "dx: %s", _dx.c_str());
  617. ESP_LOGD(TAG, "dy: %s", _dy.c_str());
  618. #endif
  619. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  620. {
  621. string _enhance = std::string(_valuechar);
  622. if (_enhance.compare("true") == 0)
  623. {
  624. enhance = true;
  625. }
  626. }
  627. in = "/sdcard" + in;
  628. out = "/sdcard" + out;
  629. std::string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  630. if ((flowctrl.SetupModeActive || (flowctrl.getActStatus() == std::string("Flow finished"))) && psram_init_shared_memory_for_take_image_step())
  631. {
  632. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Taking image for Alignment Mark Update...");
  633. bool operationOk = false;
  634. CAlignAndCutImage *caic = new (std::nothrow) CAlignAndCutImage("cutref", in);
  635. if (caic != NULL) {
  636. caic->CutAndSave(out2, x, y, dx, dy);
  637. delete caic;
  638. CImageBasis *cim = new (std::nothrow) CImageBasis("cutref", out2);
  639. if (cim != NULL && cim->ImageOkay()) {
  640. if (enhance) cim->Contrast(90);
  641. cim->SaveToFile(out);
  642. operationOk = true;
  643. }
  644. delete cim;
  645. }
  646. if (operationOk) zw = "CutImage Done";
  647. else {
  648. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Alignment mark update failed due to an allocation or image I/O error");
  649. zw = "CutImage Failed";
  650. }
  651. // Release only after every STBI-backed temporary image has been destroyed.
  652. psram_deinit_shared_memory_for_take_image_step();
  653. }
  654. else
  655. {
  656. LogFile.WriteToFile(ESP_LOG_WARN, TAG, std::string("Taking image for Alignment Mark not possible while device") + " is busy with a round (Current State: '" + flowctrl.getActStatus() + "')!");
  657. zw = "Device Busy";
  658. }
  659. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  660. httpd_resp_send(req, zw.c_str(), zw.length());
  661. }
  662. // wird beim Erstellen eines neuen Referenzbildes aufgerufen
  663. std::string sys_status = flowctrl.getActStatus();
  664. if ((sys_status != "Take Image") && (sys_status != "Aligning"))
  665. {
  666. if ((_task.compare("test_take") == 0) || (_task.compare("cam_settings") == 0))
  667. {
  668. std::string _host = "";
  669. // CFstatus is only used as the temporary settings copy for test_take.
  670. const bool isTestTake = (_task.compare("test_take") == 0);
  671. CameraSettings *settingsTarget = &static_cast<CameraSettings &>(CCstatus);
  672. if (isTestTake)
  673. {
  674. static_cast<CameraSettings &>(CFstatus) =
  675. static_cast<const CameraSettings &>(CCstatus);
  676. settingsTarget = &static_cast<CameraSettings &>(CFstatus);
  677. }
  678. CameraSettings &settings = *settingsTarget;
  679. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  680. {
  681. _host = std::string(_valuechar);
  682. }
  683. if (httpd_query_key_value(_query, "xclk", _valuechar, 30) == ESP_OK)
  684. {
  685. std::string _xclk = std::string(_valuechar);
  686. if (isStringNumeric(_xclk))
  687. {
  688. settings.CamXclkFreqMhz = clipInt(std::stoi(_xclk), 20, 1);
  689. }
  690. }
  691. if (httpd_query_key_value(_query, "waitb", _valuechar, 30) == ESP_OK)
  692. {
  693. std::string _waitb = std::string(_valuechar);
  694. if (isStringNumeric(_waitb))
  695. {
  696. settings.WaitBeforePicture = std::max(0, std::stoi(_valuechar));
  697. }
  698. }
  699. if (httpd_query_key_value(_query, "aecgc", _valuechar, 30) == ESP_OK)
  700. {
  701. std::string _aecgc = std::string(_valuechar);
  702. if (isStringNumeric(_aecgc))
  703. {
  704. int _aecgc_ = std::stoi(_valuechar);
  705. switch (_aecgc_)
  706. {
  707. case 1:
  708. settings.ImageGainceiling = GAINCEILING_4X;
  709. break;
  710. case 2:
  711. settings.ImageGainceiling = GAINCEILING_8X;
  712. break;
  713. case 3:
  714. settings.ImageGainceiling = GAINCEILING_16X;
  715. break;
  716. case 4:
  717. settings.ImageGainceiling = GAINCEILING_32X;
  718. break;
  719. case 5:
  720. settings.ImageGainceiling = GAINCEILING_64X;
  721. break;
  722. case 6:
  723. settings.ImageGainceiling = GAINCEILING_128X;
  724. break;
  725. default:
  726. settings.ImageGainceiling = GAINCEILING_2X;
  727. }
  728. }
  729. else
  730. {
  731. if (_aecgc == "X4") {
  732. settings.ImageGainceiling = GAINCEILING_4X;
  733. }
  734. else if (_aecgc == "X8") {
  735. settings.ImageGainceiling = GAINCEILING_8X;
  736. }
  737. else if (_aecgc == "X16") {
  738. settings.ImageGainceiling = GAINCEILING_16X;
  739. }
  740. else if (_aecgc == "X32") {
  741. settings.ImageGainceiling = GAINCEILING_32X;
  742. }
  743. else if (_aecgc == "X64") {
  744. settings.ImageGainceiling = GAINCEILING_64X;
  745. }
  746. else if (_aecgc == "X128") {
  747. settings.ImageGainceiling = GAINCEILING_128X;
  748. }
  749. else {
  750. settings.ImageGainceiling = GAINCEILING_2X;
  751. }
  752. }
  753. }
  754. if (httpd_query_key_value(_query, "qual", _valuechar, 30) == ESP_OK)
  755. {
  756. std::string _qual = std::string(_valuechar);
  757. if (isStringNumeric(_qual))
  758. {
  759. int _qual_ = std::stoi(_valuechar);
  760. settings.ImageQuality = clipInt(_qual_, 63, 6);
  761. }
  762. }
  763. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK)
  764. {
  765. std::string _bri = std::string(_valuechar);
  766. if (isStringNumeric(_bri))
  767. {
  768. int _bri_ = std::stoi(_valuechar);
  769. settings.ImageBrightness = clipInt(_bri_, 2, -2);
  770. }
  771. }
  772. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK)
  773. {
  774. std::string _con = std::string(_valuechar);
  775. if (isStringNumeric(_con))
  776. {
  777. int _con_ = std::stoi(_valuechar);
  778. settings.ImageContrast = clipInt(_con_, 2, -2);
  779. }
  780. }
  781. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK)
  782. {
  783. std::string _sat = std::string(_valuechar);
  784. if (isStringNumeric(_sat))
  785. {
  786. int _sat_ = std::stoi(_valuechar);
  787. settings.ImageSaturation = clipInt(_sat_, 2, -2);
  788. }
  789. }
  790. if (httpd_query_key_value(_query, "shp", _valuechar, 30) == ESP_OK)
  791. {
  792. std::string _shp = std::string(_valuechar);
  793. if (isStringNumeric(_shp))
  794. {
  795. int _shp_ = std::stoi(_valuechar);
  796. if (CCstatus.CamSensor_id == OV2640_PID)
  797. {
  798. settings.ImageSharpness = clipInt(_shp_, 2, -2);
  799. }
  800. else
  801. {
  802. settings.ImageSharpness = clipInt(_shp_, 3, -3);
  803. }
  804. }
  805. }
  806. if (httpd_query_key_value(_query, "ashp", _valuechar, 30) == ESP_OK)
  807. {
  808. std::string _ashp = std::string(_valuechar);
  809. settings.ImageAutoSharpness = alphanumericToBoolean(_ashp);
  810. }
  811. if (httpd_query_key_value(_query, "spe", _valuechar, 30) == ESP_OK)
  812. {
  813. std::string _spe = std::string(_valuechar);
  814. if (isStringNumeric(_spe))
  815. {
  816. int _spe_ = std::stoi(_valuechar);
  817. settings.ImageSpecialEffect = clipInt(_spe_, 6, 0);
  818. }
  819. else
  820. {
  821. if (_spe == "negative") {
  822. settings.ImageSpecialEffect = 1;
  823. }
  824. else if (_spe == "grayscale") {
  825. settings.ImageSpecialEffect = 2;
  826. }
  827. else if (_spe == "red") {
  828. settings.ImageSpecialEffect = 3;
  829. }
  830. else if (_spe == "green") {
  831. settings.ImageSpecialEffect = 4;
  832. }
  833. else if (_spe == "blue") {
  834. settings.ImageSpecialEffect = 5;
  835. }
  836. else if (_spe == "retro") {
  837. settings.ImageSpecialEffect = 6;
  838. }
  839. else {
  840. settings.ImageSpecialEffect = 0;
  841. }
  842. }
  843. }
  844. if (httpd_query_key_value(_query, "wbm", _valuechar, 30) == ESP_OK)
  845. {
  846. std::string _wbm = std::string(_valuechar);
  847. if (isStringNumeric(_wbm))
  848. {
  849. int _wbm_ = std::stoi(_valuechar);
  850. settings.ImageWbMode = clipInt(_wbm_, 4, 0);
  851. }
  852. else
  853. {
  854. if (_wbm == "sunny") {
  855. settings.ImageWbMode = 1;
  856. }
  857. else if (_wbm == "cloudy") {
  858. settings.ImageWbMode = 2;
  859. }
  860. else if (_wbm == "office") {
  861. settings.ImageWbMode = 3;
  862. }
  863. else if (_wbm == "home") {
  864. settings.ImageWbMode = 4;
  865. }
  866. else {
  867. settings.ImageWbMode = 0;
  868. }
  869. }
  870. }
  871. if (httpd_query_key_value(_query, "awb", _valuechar, 30) == ESP_OK)
  872. {
  873. std::string _awb = std::string(_valuechar);
  874. settings.ImageAwb = alphanumericToBoolean(_awb);
  875. }
  876. if (httpd_query_key_value(_query, "awbg", _valuechar, 30) == ESP_OK)
  877. {
  878. std::string _awbg = std::string(_valuechar);
  879. settings.ImageAwbGain = alphanumericToBoolean(_awbg);
  880. }
  881. if (httpd_query_key_value(_query, "aec", _valuechar, 30) == ESP_OK)
  882. {
  883. std::string _aec = std::string(_valuechar);
  884. settings.ImageAec = alphanumericToBoolean(_aec);
  885. }
  886. if (httpd_query_key_value(_query, "aec2", _valuechar, 30) == ESP_OK)
  887. {
  888. std::string _aec2 = std::string(_valuechar);
  889. settings.ImageAec2 = alphanumericToBoolean(_aec2);
  890. }
  891. if (httpd_query_key_value(_query, "ael", _valuechar, 30) == ESP_OK)
  892. {
  893. std::string _ael = std::string(_valuechar);
  894. if (isStringNumeric(_ael))
  895. {
  896. int _ael_ = std::stoi(_valuechar);
  897. if (CCstatus.CamSensor_id == OV2640_PID)
  898. {
  899. settings.ImageAeLevel = clipInt(_ael_, 2, -2);
  900. }
  901. else
  902. {
  903. settings.ImageAeLevel = clipInt(_ael_, 5, -5);
  904. }
  905. }
  906. }
  907. if (httpd_query_key_value(_query, "aecv", _valuechar, 30) == ESP_OK)
  908. {
  909. std::string _aecv = std::string(_valuechar);
  910. if (isStringNumeric(_aecv))
  911. {
  912. int _aecv_ = std::stoi(_valuechar);
  913. settings.ImageAecValue = clipInt(_aecv_, 1200, 0);
  914. }
  915. }
  916. if (httpd_query_key_value(_query, "agc", _valuechar, 30) == ESP_OK)
  917. {
  918. std::string _agc = std::string(_valuechar);
  919. settings.ImageAgc = alphanumericToBoolean(_agc);
  920. }
  921. if (httpd_query_key_value(_query, "agcg", _valuechar, 30) == ESP_OK)
  922. {
  923. std::string _agcg = std::string(_valuechar);
  924. if (isStringNumeric(_agcg))
  925. {
  926. int _agcg_ = std::stoi(_valuechar);
  927. settings.ImageAgcGain = clipInt(_agcg_, 30, 0);
  928. }
  929. }
  930. if (httpd_query_key_value(_query, "bpc", _valuechar, 30) == ESP_OK)
  931. {
  932. std::string _bpc = std::string(_valuechar);
  933. settings.ImageBpc = alphanumericToBoolean(_bpc);
  934. }
  935. if (httpd_query_key_value(_query, "wpc", _valuechar, 30) == ESP_OK)
  936. {
  937. std::string _wpc = std::string(_valuechar);
  938. settings.ImageWpc = alphanumericToBoolean(_wpc);
  939. }
  940. if (httpd_query_key_value(_query, "rgma", _valuechar, 30) == ESP_OK)
  941. {
  942. std::string _rgma = std::string(_valuechar);
  943. settings.ImageRawGma = alphanumericToBoolean(_rgma);
  944. }
  945. if (httpd_query_key_value(_query, "lenc", _valuechar, 30) == ESP_OK)
  946. {
  947. std::string _lenc = std::string(_valuechar);
  948. settings.ImageLenc = alphanumericToBoolean(_lenc);
  949. }
  950. if (httpd_query_key_value(_query, "mirror", _valuechar, 30) == ESP_OK)
  951. {
  952. std::string _mirror = std::string(_valuechar);
  953. settings.ImageHmirror = alphanumericToBoolean(_mirror);
  954. }
  955. if (httpd_query_key_value(_query, "flip", _valuechar, 30) == ESP_OK)
  956. {
  957. std::string _flip = std::string(_valuechar);
  958. settings.ImageVflip = alphanumericToBoolean(_flip);
  959. }
  960. if (httpd_query_key_value(_query, "dcw", _valuechar, 30) == ESP_OK)
  961. {
  962. std::string _dcw = std::string(_valuechar);
  963. settings.ImageDcw = alphanumericToBoolean(_dcw);
  964. }
  965. if (httpd_query_key_value(_query, "den", _valuechar, 30) == ESP_OK)
  966. {
  967. std::string _idlv = std::string(_valuechar);
  968. if (isStringNumeric(_idlv))
  969. {
  970. int _ImageDenoiseLevel = std::stoi(_valuechar);
  971. if (CCstatus.CamSensor_id == OV2640_PID)
  972. {
  973. settings.ImageDenoiseLevel = 0;
  974. }
  975. else
  976. {
  977. settings.ImageDenoiseLevel = clipInt(_ImageDenoiseLevel, 8, 0);
  978. }
  979. }
  980. }
  981. if (httpd_query_key_value(_query, "zoom", _valuechar, 30) == ESP_OK)
  982. {
  983. std::string _zoom = std::string(_valuechar);
  984. settings.ImageZoomEnabled = alphanumericToBoolean(_zoom);
  985. }
  986. if (httpd_query_key_value(_query, "zoomx", _valuechar, 30) == ESP_OK)
  987. {
  988. std::string _zoomx = std::string(_valuechar);
  989. if (isStringNumeric(_zoomx))
  990. {
  991. int _ImageZoomOffsetX = std::stoi(_valuechar);
  992. if (CCstatus.CamSensor_id == OV2640_PID)
  993. {
  994. settings.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 480, -480);
  995. }
  996. else if (CCstatus.CamSensor_id == OV3660_PID)
  997. {
  998. settings.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 704, -704);
  999. }
  1000. else if (CCstatus.CamSensor_id == OV5640_PID)
  1001. {
  1002. settings.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 960, -960);
  1003. }
  1004. }
  1005. }
  1006. if (httpd_query_key_value(_query, "zoomy", _valuechar, 30) == ESP_OK)
  1007. {
  1008. std::string _zoomy = std::string(_valuechar);
  1009. if (isStringNumeric(_zoomy))
  1010. {
  1011. int _ImageZoomOffsetY = std::stoi(_valuechar);
  1012. if (CCstatus.CamSensor_id == OV2640_PID)
  1013. {
  1014. settings.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 360, -360);
  1015. }
  1016. else if (CCstatus.CamSensor_id == OV3660_PID)
  1017. {
  1018. settings.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 528, -528);
  1019. }
  1020. else if (CCstatus.CamSensor_id == OV5640_PID)
  1021. {
  1022. settings.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 720, -720);
  1023. }
  1024. }
  1025. }
  1026. if (httpd_query_key_value(_query, "zooms", _valuechar, 30) == ESP_OK)
  1027. {
  1028. std::string _zooms = std::string(_valuechar);
  1029. if (isStringNumeric(_zooms))
  1030. {
  1031. int _ImageZoomSize = std::stoi(_valuechar);
  1032. if (CCstatus.CamSensor_id == OV2640_PID)
  1033. {
  1034. settings.ImageZoomSize = clipInt(_ImageZoomSize, 29, 0);
  1035. }
  1036. else if (CCstatus.CamSensor_id == OV3660_PID)
  1037. {
  1038. settings.ImageZoomSize = clipInt(_ImageZoomSize, 43, 0);
  1039. }
  1040. else if (CCstatus.CamSensor_id == OV5640_PID)
  1041. {
  1042. settings.ImageZoomSize = clipInt(_ImageZoomSize, 59, 0);
  1043. }
  1044. }
  1045. }
  1046. if (httpd_query_key_value(_query, "ledi", _valuechar, 30) == ESP_OK)
  1047. {
  1048. std::string _ledi = std::string(_valuechar);
  1049. if (isStringNumeric(_ledi))
  1050. {
  1051. int _ImageLedIntensity = std::stoi(_valuechar);
  1052. settings.ImageLedIntensity = Camera.SetLEDIntensity(_ImageLedIntensity);
  1053. }
  1054. }
  1055. if (!isTestTake)
  1056. {
  1057. // Saved camera settings are committed in CCstatus.
  1058. if (Camera.setSensorDatenFromCCstatus() != ESP_OK)
  1059. {
  1060. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Could not apply committed camera settings");
  1061. return ESP_FAIL;
  1062. }
  1063. ESP_LOGD(TAG, "Cam Settings set");
  1064. std::string _zw = "CamSettingsSet";
  1065. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1066. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1067. }
  1068. else
  1069. {
  1070. // Only test_take applies temporary camera settings.
  1071. if (Camera.ApplyPreviewSettings(settings) != ESP_OK)
  1072. {
  1073. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Could not apply temporary camera settings");
  1074. return ESP_FAIL;
  1075. }
  1076. ESP_LOGD(TAG, "test_take - vor TakeImage");
  1077. std::string image_temp = flowctrl.doSingleStep("[TakeImage]", _host);
  1078. const std::string rawImagePath = "/img_tmp/raw.jpg";
  1079. const std::size_t rawImagePosition = image_temp.find(rawImagePath);
  1080. if (rawImagePosition != std::string::npos)
  1081. {
  1082. image_temp.replace(rawImagePosition, rawImagePath.length(),
  1083. rawImagePath + "?preview=1");
  1084. }
  1085. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1086. httpd_resp_send(req, image_temp.c_str(), image_temp.length());
  1087. }
  1088. }
  1089. if (_task.compare("test_align") == 0)
  1090. {
  1091. std::string _host = "";
  1092. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  1093. {
  1094. _host = std::string(_valuechar);
  1095. }
  1096. std::string zw = flowctrl.doSingleStep("[Alignment]", _host);
  1097. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1098. httpd_resp_send(req, zw.c_str(), zw.length());
  1099. }
  1100. }
  1101. else
  1102. {
  1103. std::string _zw = "DeviceIsBusy";
  1104. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1105. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1106. }
  1107. #ifdef DEBUG_DETAIL_ON
  1108. LogFile.WriteHeapInfo("handler_editflow - Done");
  1109. #endif
  1110. return ESP_OK;
  1111. }
  1112. esp_err_t handler_statusflow(httpd_req_t *req)
  1113. {
  1114. #ifdef DEBUG_DETAIL_ON
  1115. LogFile.WriteHeapInfo("handler_statusflow - Start");
  1116. #endif
  1117. const char *resp_str;
  1118. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1119. if (bTaskAutoFlowCreated)
  1120. {
  1121. #ifdef DEBUG_DETAIL_ON
  1122. ESP_LOGD(TAG, "handler_statusflow: %s", req->uri);
  1123. #endif
  1124. std::string zw = flowctrl.getActStatusWithTime();
  1125. resp_str = zw.c_str();
  1126. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1127. }
  1128. else
  1129. {
  1130. resp_str = "Flow task not yet created";
  1131. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1132. }
  1133. #ifdef DEBUG_DETAIL_ON
  1134. LogFile.WriteHeapInfo("handler_statusflow - Done");
  1135. #endif
  1136. return ESP_OK;
  1137. }
  1138. esp_err_t handler_cputemp(httpd_req_t *req)
  1139. {
  1140. #ifdef DEBUG_DETAIL_ON
  1141. LogFile.WriteHeapInfo("handler_cputemp - Start");
  1142. #endif
  1143. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1144. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  1145. #ifdef DEBUG_DETAIL_ON
  1146. LogFile.WriteHeapInfo("handler_cputemp - End");
  1147. #endif
  1148. return ESP_OK;
  1149. }
  1150. esp_err_t handler_rssi(httpd_req_t *req)
  1151. {
  1152. #ifdef DEBUG_DETAIL_ON
  1153. LogFile.WriteHeapInfo("handler_rssi - Start");
  1154. #endif
  1155. if (getWIFIisConnected())
  1156. {
  1157. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1158. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  1159. }
  1160. else
  1161. {
  1162. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  1163. return ESP_ERR_NOT_FOUND;
  1164. }
  1165. #ifdef DEBUG_DETAIL_ON
  1166. LogFile.WriteHeapInfo("handler_rssi - End");
  1167. #endif
  1168. return ESP_OK;
  1169. }
  1170. esp_err_t handler_current_date(httpd_req_t *req)
  1171. {
  1172. #ifdef DEBUG_DETAIL_ON
  1173. LogFile.WriteHeapInfo("handler_uptime - Start");
  1174. #endif
  1175. std::string formatedDateAndTime = getCurrentTimeString("%Y-%m-%d %H:%M:%S");
  1176. // std::string formatedDate = getCurrentTimeString("%Y-%m-%d");
  1177. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1178. httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
  1179. #ifdef DEBUG_DETAIL_ON
  1180. LogFile.WriteHeapInfo("handler_uptime - End");
  1181. #endif
  1182. return ESP_OK;
  1183. }
  1184. esp_err_t handler_uptime(httpd_req_t *req)
  1185. {
  1186. #ifdef DEBUG_DETAIL_ON
  1187. LogFile.WriteHeapInfo("handler_uptime - Start");
  1188. #endif
  1189. std::string formatedUptime = getFormatedUptime(false);
  1190. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1191. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  1192. #ifdef DEBUG_DETAIL_ON
  1193. LogFile.WriteHeapInfo("handler_uptime - End");
  1194. #endif
  1195. return ESP_OK;
  1196. }
  1197. esp_err_t handler_prevalue(httpd_req_t *req)
  1198. {
  1199. #ifdef DEBUG_DETAIL_ON
  1200. LogFile.WriteHeapInfo("handler_prevalue - Start");
  1201. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  1202. #endif
  1203. // Default usage message when handler gets called without any parameter
  1204. const std::string RESTUsageInfo =
  1205. "00: Handler usage:<br>"
  1206. "- To retrieve actual PreValue, please provide only a numbersname, e.g. /setPreValue?numbers=main<br>"
  1207. "- To set PreValue to a new value, please provide a numbersname and a value, e.g. /setPreValue?numbers=main&value=1234.5678<br>"
  1208. "NOTE:<br>"
  1209. "value >= 0.0: Set PreValue to provided value<br>"
  1210. "value < 0.0: Set PreValue to actual RAW value (as long RAW value is a valid number, without N)";
  1211. // Default return error message when no return is programmed
  1212. std::string sReturnMessage = "E90: Uninitialized";
  1213. char _query[100] = ""; // stays empty when the request has no query string
  1214. char _numbersname[50] = "default";
  1215. char _value[20] = "";
  1216. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1217. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  1218. {
  1219. #ifdef DEBUG_DETAIL_ON
  1220. ESP_LOGD(TAG, "Query: %s", _query);
  1221. #endif
  1222. if (httpd_query_key_value(_query, "numbers", _numbersname, 50) != ESP_OK)
  1223. {
  1224. // If request is incomplete
  1225. sReturnMessage = "E91: Query parameter incomplete or not valid!<br> "
  1226. "Call /setPreValue to show REST API usage info and/or check documentation";
  1227. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1228. return ESP_FAIL;
  1229. }
  1230. if (httpd_query_key_value(_query, "value", _value, 20) == ESP_OK)
  1231. {
  1232. #ifdef DEBUG_DETAIL_ON
  1233. ESP_LOGD(TAG, "Value: %s", _value);
  1234. #endif
  1235. }
  1236. }
  1237. else
  1238. {
  1239. // if no parameter is provided, print handler usage
  1240. httpd_resp_send(req, RESTUsageInfo.c_str(), RESTUsageInfo.length());
  1241. return ESP_OK;
  1242. }
  1243. if (strlen(_value) == 0)
  1244. {
  1245. // If no value is povided --> return actual PreValue
  1246. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1247. if (sReturnMessage.empty())
  1248. {
  1249. sReturnMessage = "E92: Numbers name not found";
  1250. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1251. return ESP_FAIL;
  1252. }
  1253. }
  1254. else
  1255. {
  1256. // New value is positive: Set PreValue to provided value and return value
  1257. // New value is negative and actual RAW value is a valid number: Set PreValue to RAW value and return value
  1258. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "REST API handler_prevalue called: numbersname: " + std::string(_numbersname) + ", value: " + std::string(_value));
  1259. if (!flowctrl.UpdatePrevalue(_value, _numbersname, true))
  1260. {
  1261. sReturnMessage = "E93: Update request rejected. Please check device logs for more details";
  1262. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1263. return ESP_FAIL;
  1264. }
  1265. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1266. if (sReturnMessage.empty())
  1267. {
  1268. sReturnMessage = "E94: Numbers name not found";
  1269. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1270. return ESP_FAIL;
  1271. }
  1272. }
  1273. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1274. #ifdef DEBUG_DETAIL_ON
  1275. LogFile.WriteHeapInfo("handler_prevalue - End");
  1276. #endif
  1277. return ESP_OK;
  1278. }
  1279. void task_autodoFlow(void *pvParameter)
  1280. {
  1281. int64_t fr_start, fr_delta_ms;
  1282. bTaskAutoFlowCreated = true;
  1283. if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
  1284. {
  1285. flowctrl.setActStatus("Initialization (delayed)");
  1286. // #ifdef ENABLE_MQTT
  1287. // MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  1288. // #endif //ENABLE_MQTT
  1289. vTaskDelay(60 * 5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
  1290. }
  1291. ESP_LOGD(TAG, "task_autodoFlow: start");
  1292. doInit();
  1293. flowctrl.setAutoStartInterval(auto_interval);
  1294. autostartIsEnabled = flowctrl.getIsAutoStart();
  1295. if (isSetupModusActive())
  1296. {
  1297. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "We are in Setup Mode -> Not starting Auto Flow!");
  1298. autostartIsEnabled = false;
  1299. // 15.7.0 Setup Wizard cannot take a Reference Picture #2953
  1300. // std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  1301. // flowctrl.doFlowTakeImageOnly(zw_time);
  1302. }
  1303. if (autostartIsEnabled)
  1304. {
  1305. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting Flow...");
  1306. }
  1307. else
  1308. {
  1309. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Autostart is not enabled -> Not starting Flow");
  1310. }
  1311. while (autostartIsEnabled)
  1312. {
  1313. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  1314. time_t roundStartTime = getUpTime();
  1315. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  1316. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  1317. fr_start = esp_timer_get_time();
  1318. if (flowisrunning)
  1319. {
  1320. #ifdef DEBUG_DETAIL_ON
  1321. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  1322. #endif
  1323. }
  1324. else
  1325. {
  1326. #ifdef DEBUG_DETAIL_ON
  1327. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  1328. #endif
  1329. flowisrunning = true;
  1330. doflow();
  1331. #ifdef DEBUG_DETAIL_ON
  1332. ESP_LOGD(TAG, "Remove older log files");
  1333. #endif
  1334. LogFile.RemoveOldLogFile();
  1335. LogFile.RemoveOldDataLog();
  1336. }
  1337. // Round finished -> Logfile
  1338. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) + " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  1339. // CPU Temp -> Logfile
  1340. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  1341. // WIFI Signal Strength (RSSI) -> Logfile
  1342. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  1343. // Check if time is synchronized (if NTP is configured)
  1344. if (getUseNtp() && !getTimeIsSet())
  1345. {
  1346. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set!");
  1347. StatusLED(TIME_CHECK, 1, false);
  1348. }
  1349. #if (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES)
  1350. wifiRoamingQuery();
  1351. #endif
  1352. // Scan channels and check if an AP with better RSSI is available, then disconnect and try to reconnect to AP with better RSSI
  1353. // NOTE: Keep this direct before the following task delay, because scan is done in blocking mode and this takes ca. 1,5 - 2s.
  1354. #ifdef WLAN_USE_ROAMING_BY_SCANNING
  1355. wifiRoamByScanning();
  1356. #endif
  1357. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  1358. if (auto_interval > fr_delta_ms)
  1359. {
  1360. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  1361. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long)xDelay);
  1362. vTaskDelay(xDelay);
  1363. }
  1364. }
  1365. while (1)
  1366. {
  1367. // Keep flow task running to handle necessary sub tasks like reboot handler, etc..
  1368. vTaskDelay(2000 / portTICK_PERIOD_MS);
  1369. }
  1370. vTaskDelete(NULL); // Delete this task if it exits from the loop above
  1371. xHandletask_autodoFlow = NULL;
  1372. ESP_LOGD(TAG, "task_autodoFlow: end");
  1373. }
  1374. void InitializeFlowTask(void)
  1375. {
  1376. BaseType_t xReturned;
  1377. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1378. uint32_t stackSize = 16 * 1024;
  1379. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", stackSize, NULL, tskIDLE_PRIORITY + 2, &xHandletask_autodoFlow, 0);
  1380. if (xReturned != pdPASS)
  1381. {
  1382. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Creation task_autodoFlow failed. Requested stack size:" + std::to_string(stackSize));
  1383. LogFile.WriteHeapInfo("Creation task_autodoFlow failed");
  1384. }
  1385. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1386. }
  1387. void register_server_main_flow_task_uri(httpd_handle_t server)
  1388. {
  1389. ESP_LOGI(TAG, "server_main_flow_task - Registering URI handlers");
  1390. httpd_uri_t camuri = {};
  1391. camuri.method = HTTP_GET;
  1392. camuri.uri = "/doinit";
  1393. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_init);
  1394. camuri.user_ctx = (void *)"Light On";
  1395. httpd_register_uri_handler(server, &camuri);
  1396. // Legacy API => New: "/setPreValue"
  1397. camuri.uri = "/setPreValue.html";
  1398. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1399. camuri.user_ctx = (void *)"Prevalue";
  1400. httpd_register_uri_handler(server, &camuri);
  1401. camuri.uri = "/setPreValue";
  1402. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1403. camuri.user_ctx = (void *)"Prevalue";
  1404. httpd_register_uri_handler(server, &camuri);
  1405. camuri.uri = "/flow_start";
  1406. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_flow_start);
  1407. camuri.user_ctx = (void *)"Flow Start";
  1408. httpd_register_uri_handler(server, &camuri);
  1409. camuri.uri = "/statusflow.html";
  1410. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1411. camuri.user_ctx = (void *)"Light Off";
  1412. httpd_register_uri_handler(server, &camuri);
  1413. camuri.uri = "/statusflow";
  1414. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1415. camuri.user_ctx = (void *)"Light Off";
  1416. httpd_register_uri_handler(server, &camuri);
  1417. // Legacy API => New: "/cpu_temperature"
  1418. camuri.uri = "/cputemp.html";
  1419. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1420. camuri.user_ctx = (void *)"Light Off";
  1421. httpd_register_uri_handler(server, &camuri);
  1422. camuri.uri = "/cpu_temperature";
  1423. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1424. camuri.user_ctx = (void *)"Light Off";
  1425. httpd_register_uri_handler(server, &camuri);
  1426. // Legacy API => New: "/rssi"
  1427. camuri.uri = "/rssi.html";
  1428. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1429. camuri.user_ctx = (void *)"Light Off";
  1430. httpd_register_uri_handler(server, &camuri);
  1431. camuri.uri = "/rssi";
  1432. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1433. camuri.user_ctx = (void *)"Light Off";
  1434. httpd_register_uri_handler(server, &camuri);
  1435. camuri.uri = "/date";
  1436. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_current_date);
  1437. camuri.user_ctx = (void *)"Light Off";
  1438. httpd_register_uri_handler(server, &camuri);
  1439. camuri.uri = "/uptime";
  1440. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_uptime);
  1441. camuri.user_ctx = (void *)"Light Off";
  1442. httpd_register_uri_handler(server, &camuri);
  1443. camuri.uri = "/editflow";
  1444. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_editflow);
  1445. camuri.user_ctx = (void *)"EditFlow";
  1446. httpd_register_uri_handler(server, &camuri);
  1447. // Legacy API => New: "/value"
  1448. camuri.uri = "/value.html";
  1449. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1450. camuri.user_ctx = (void *)"Value";
  1451. httpd_register_uri_handler(server, &camuri);
  1452. camuri.uri = "/value";
  1453. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1454. camuri.user_ctx = (void *)"Value";
  1455. httpd_register_uri_handler(server, &camuri);
  1456. // Legacy API => New: "/value"
  1457. camuri.uri = "/wasserzaehler.html";
  1458. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1459. camuri.user_ctx = (void *)"Wasserzaehler";
  1460. httpd_register_uri_handler(server, &camuri);
  1461. camuri.uri = "/json";
  1462. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_json);
  1463. camuri.user_ctx = (void *)"JSON";
  1464. httpd_register_uri_handler(server, &camuri);
  1465. camuri.uri = "/heap";
  1466. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_get_heap);
  1467. camuri.user_ctx = (void *)"Heap";
  1468. httpd_register_uri_handler(server, &camuri);
  1469. camuri.uri = "/stream";
  1470. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_stream);
  1471. camuri.user_ctx = (void *)"stream";
  1472. httpd_register_uri_handler(server, &camuri);
  1473. /** will handle metrics requests */
  1474. camuri.uri = "/metrics";
  1475. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_openmetrics);
  1476. camuri.user_ctx = (void *)"metrics";
  1477. httpd_register_uri_handler(server, &camuri);
  1478. /** when adding a new handler, make sure to increment the value for config.max_uri_handlers in `main/server_main.cpp` */
  1479. }