MainFlowControl.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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)
  72. {
  73. return flowctrl.SendRawJPG(req);
  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 setCCstatusToCFstatus(void)
  123. {
  124. static_cast<CameraSettings &>(CFstatus) =
  125. static_cast<const CameraSettings &>(CCstatus);
  126. return ESP_OK;
  127. }
  128. esp_err_t setCFstatusToCCstatus(void)
  129. {
  130. static_cast<CameraSettings &>(CCstatus) =
  131. static_cast<const CameraSettings &>(CFstatus);
  132. return ESP_OK;
  133. }
  134. esp_err_t setCFstatusToCam(void)
  135. {
  136. return Camera.ApplySettings(CFstatus);
  137. }
  138. esp_err_t handler_get_heap(httpd_req_t *req)
  139. {
  140. #ifdef DEBUG_DETAIL_ON
  141. LogFile.WriteHeapInfo("handler_get_heap - Start");
  142. ESP_LOGD(TAG, "handler_get_heap uri: %s", req->uri);
  143. #endif
  144. std::string zw = "Heap info:<br>" + getESPHeapInfo();
  145. #ifdef TASK_ANALYSIS_ON
  146. char *pcTaskList = (char *)calloc_psram_heap(std::string(TAG) + "->pcTaskList", 1, sizeof(char) * 768, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  147. if (pcTaskList)
  148. {
  149. vTaskList(pcTaskList);
  150. zw = zw + "<br><br>Task info:<br><pre>Name | State | Prio | Lowest stacksize | Creation order | CPU (-1=NoAffinity)<br>" + std::string(pcTaskList) + "</pre>";
  151. free_psram_heap(std::string(TAG) + "->pcTaskList", pcTaskList);
  152. }
  153. else
  154. {
  155. zw = zw + "<br><br>Task info:<br>ERROR - Allocation of TaskList buffer in PSRAM failed";
  156. }
  157. #endif
  158. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  159. if (zw.length() > 0)
  160. {
  161. httpd_resp_send(req, zw.c_str(), zw.length());
  162. }
  163. else
  164. {
  165. httpd_resp_send(req, NULL, 0);
  166. }
  167. #ifdef DEBUG_DETAIL_ON
  168. LogFile.WriteHeapInfo("handler_get_heap - Done");
  169. #endif
  170. return ESP_OK;
  171. }
  172. esp_err_t handler_init(httpd_req_t *req)
  173. {
  174. #ifdef DEBUG_DETAIL_ON
  175. LogFile.WriteHeapInfo("handler_init - Start");
  176. ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
  177. #endif
  178. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  179. // Rebuilding the flow at runtime is not safe: InitFlow() keeps the old ClassFlow objects (a second full
  180. // pipeline, ~1.2 MB PSRAM, until allocations fail and the device panics) and races with a running round.
  181. // Apply the configuration with a reboot instead.
  182. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "/doinit requested -> rebooting to apply the configuration");
  183. const char *resp_str = "Reboot to apply the configuration<br>";
  184. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  185. doReboot();
  186. #ifdef DEBUG_DETAIL_ON
  187. LogFile.WriteHeapInfo("handler_init - Done");
  188. #endif
  189. return ESP_OK;
  190. }
  191. esp_err_t handler_stream(httpd_req_t *req)
  192. {
  193. #ifdef DEBUG_DETAIL_ON
  194. LogFile.WriteHeapInfo("handler_stream - Start");
  195. ESP_LOGD(TAG, "handler_stream uri: %s", req->uri);
  196. #endif
  197. char _query[50] = ""; // stays empty when the request has no query string
  198. char _value[10];
  199. bool flashlightOn = false;
  200. if (httpd_req_get_url_query_str(req, _query, 50) == ESP_OK)
  201. {
  202. // ESP_LOGD(TAG, "Query: %s", _query);
  203. if (httpd_query_key_value(_query, "flashlight", _value, 10) == ESP_OK)
  204. {
  205. #ifdef DEBUG_DETAIL_ON
  206. ESP_LOGD(TAG, "flashlight is found%s", _value);
  207. #endif
  208. if (strlen(_value) > 0)
  209. {
  210. flashlightOn = true;
  211. }
  212. }
  213. }
  214. Camera.CaptureToStream(req, flashlightOn);
  215. #ifdef DEBUG_DETAIL_ON
  216. LogFile.WriteHeapInfo("handler_stream - Done");
  217. #endif
  218. return ESP_OK;
  219. }
  220. esp_err_t handler_flow_start(httpd_req_t *req)
  221. {
  222. #ifdef DEBUG_DETAIL_ON
  223. LogFile.WriteHeapInfo("handler_flow_start - Start");
  224. #endif
  225. ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
  226. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  227. if (autostartIsEnabled)
  228. {
  229. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  230. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
  231. const char *resp_str = "The flow is going to be started immediately or is already running";
  232. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  233. }
  234. else
  235. {
  236. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
  237. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow start triggered by REST API, but flow is not active");
  238. }
  239. #ifdef DEBUG_DETAIL_ON
  240. LogFile.WriteHeapInfo("handler_flow_start - Done");
  241. #endif
  242. return ESP_OK;
  243. }
  244. #ifdef ENABLE_MQTT
  245. esp_err_t MQTTCtrlFlowStart(std::string _topic)
  246. {
  247. #ifdef DEBUG_DETAIL_ON
  248. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Start");
  249. #endif
  250. ESP_LOGD(TAG, "MQTTCtrlFlowStart: topic %s", _topic.c_str());
  251. if (autostartIsEnabled)
  252. {
  253. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  254. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by MQTT topic " + _topic);
  255. }
  256. else
  257. {
  258. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by MQTT topic " + _topic + ", but flow is not active!");
  259. }
  260. #ifdef DEBUG_DETAIL_ON
  261. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Done");
  262. #endif
  263. return ESP_OK;
  264. }
  265. #endif // ENABLE_MQTT
  266. esp_err_t handler_json(httpd_req_t *req)
  267. {
  268. #ifdef DEBUG_DETAIL_ON
  269. LogFile.WriteHeapInfo("handler_json - Start");
  270. #endif
  271. ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
  272. if (bTaskAutoFlowCreated && bFlowInitialized)
  273. {
  274. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  275. httpd_resp_set_type(req, "application/json");
  276. std::string zw = flowctrl.getJSON();
  277. if (zw.length() > 0)
  278. {
  279. httpd_resp_send(req, zw.c_str(), zw.length());
  280. }
  281. else
  282. {
  283. httpd_resp_send(req, NULL, 0);
  284. }
  285. }
  286. else
  287. {
  288. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!");
  289. return ESP_ERR_NOT_FOUND;
  290. }
  291. #ifdef DEBUG_DETAIL_ON
  292. LogFile.WriteHeapInfo("handler_JSON - Done");
  293. #endif
  294. return ESP_OK;
  295. }
  296. /**
  297. * Generates a http response containing the OpenMetrics (https://openmetrics.io/) text wire format
  298. * according to https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#text-format.
  299. *
  300. * A MetricFamily with a Metric for each Sequence is provided. If no valid value is available, the metric is not provided.
  301. * MetricPoints are provided without a timestamp. Additional metrics with some device information is also provided.
  302. *
  303. * The metric name prefix is 'ai_on_the_edge_device_'.
  304. *
  305. * example configuration for Prometheus (`prometheus.yml`):
  306. *
  307. * - job_name: watermeter
  308. * static_configs:
  309. * - targets: ['watermeter.fritz.box']
  310. *
  311. */
  312. esp_err_t handler_openmetrics(httpd_req_t *req)
  313. {
  314. #ifdef DEBUG_DETAIL_ON
  315. LogFile.WriteHeapInfo("handler_openmetrics - Start");
  316. #endif
  317. ESP_LOGD(TAG, "handler_openmetrics uri: %s", req->uri);
  318. if (bTaskAutoFlowCreated && bFlowInitialized)
  319. {
  320. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  321. httpd_resp_set_type(req, "text/plain"); // application/openmetrics-text is not yet supported by prometheus so we use text/plain for now
  322. const string metricNamePrefix = "ai_on_the_edge_device";
  323. // get current measurement (flow)
  324. string response = createSequenceMetrics(metricNamePrefix, flowctrl.getNumbers());
  325. // CPU Temperature
  326. response += createMetric(metricNamePrefix + "_cpu_temperature_celsius", "current cpu temperature in celsius", "gauge", std::to_string((int)temperatureRead()));
  327. // WiFi signal strength
  328. response += createMetric(metricNamePrefix + "_rssi_dbm", "current WiFi signal strength in dBm", "gauge", std::to_string(get_WIFI_RSSI()));
  329. // memory info
  330. response += createMetric(metricNamePrefix + "_memory_heap_free_bytes", "available heap memory", "gauge", std::to_string(getESPHeapSize()));
  331. // device uptime
  332. response += createMetric(metricNamePrefix + "_uptime_seconds", "device uptime in seconds", "gauge", std::to_string((long)getUpTime()));
  333. // data aquisition round
  334. response += createMetric(metricNamePrefix + "_rounds_total", "data aquisition rounds since device startup", "counter", std::to_string(countRounds));
  335. // the response always contains at least the metadata (HELP, TYPE) for the MetricFamily so no length check is needed
  336. httpd_resp_send(req, response.c_str(), response.length());
  337. }
  338. else
  339. {
  340. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /metrics not yet available!");
  341. return ESP_ERR_NOT_FOUND;
  342. }
  343. #ifdef DEBUG_DETAIL_ON
  344. LogFile.WriteHeapInfo("handler_openmetrics - Done");
  345. #endif
  346. return ESP_OK;
  347. }
  348. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  349. {
  350. #ifdef DEBUG_DETAIL_ON
  351. LogFile.WriteHeapInfo("handler water counter - Start");
  352. #endif
  353. if (bTaskAutoFlowCreated)
  354. {
  355. bool _rawValue = false;
  356. bool _noerror = false;
  357. bool _all = false;
  358. std::string _type = "value";
  359. std::string zw;
  360. ESP_LOGD(TAG, "handler water counter uri: %s", req->uri);
  361. char _query[100] = ""; // stays empty when the request has no query string
  362. char _size[10];
  363. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  364. {
  365. // ESP_LOGD(TAG, "Query: %s", _query);
  366. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  367. {
  368. #ifdef DEBUG_DETAIL_ON
  369. ESP_LOGD(TAG, "all is found%s", _size);
  370. #endif
  371. _all = true;
  372. }
  373. if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
  374. {
  375. #ifdef DEBUG_DETAIL_ON
  376. ESP_LOGD(TAG, "all is found: %s", _size);
  377. #endif
  378. _type = std::string(_size);
  379. }
  380. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  381. {
  382. #ifdef DEBUG_DETAIL_ON
  383. ESP_LOGD(TAG, "rawvalue is found: %s", _size);
  384. #endif
  385. _rawValue = true;
  386. }
  387. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  388. {
  389. #ifdef DEBUG_DETAIL_ON
  390. ESP_LOGD(TAG, "noerror is found: %s", _size);
  391. #endif
  392. _noerror = true;
  393. }
  394. }
  395. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  396. if (_all)
  397. {
  398. httpd_resp_set_type(req, "text/plain");
  399. ESP_LOGD(TAG, "TYPE: %s", _type.c_str());
  400. int _intype = READOUT_TYPE_VALUE;
  401. if (_type == "prevalue")
  402. {
  403. _intype = READOUT_TYPE_PREVALUE;
  404. }
  405. if (_type == "raw")
  406. {
  407. _intype = READOUT_TYPE_RAWVALUE;
  408. }
  409. if (_type == "error")
  410. {
  411. _intype = READOUT_TYPE_ERROR;
  412. }
  413. zw = flowctrl.getReadoutAll(_intype);
  414. ESP_LOGD(TAG, "ZW: %s", zw.c_str());
  415. if (zw.length() > 0)
  416. {
  417. httpd_resp_send(req, zw.c_str(), zw.length());
  418. }
  419. return ESP_OK;
  420. }
  421. std::string status = flowctrl.getActStatus();
  422. std::string query = std::string(_query);
  423. // ESP_LOGD(TAG, "Query: %s, query.c_str());
  424. if (query.find("full") != std::string::npos)
  425. {
  426. std::string txt;
  427. txt = "<body style=\"font-family: arial\">";
  428. if ((countRounds <= 1) && (status != std::string("Flow finished")))
  429. {
  430. // First round not completed yet
  431. txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + status + "</h3>\n";
  432. }
  433. else
  434. {
  435. txt += "<h3>Value</h3>";
  436. }
  437. httpd_resp_sendstr_chunk(req, txt.c_str());
  438. }
  439. zw = flowctrl.getReadout(_rawValue, _noerror, 0);
  440. if (zw.length() > 0)
  441. {
  442. httpd_resp_sendstr_chunk(req, zw.c_str());
  443. }
  444. if (query.find("full") != std::string::npos)
  445. {
  446. std::string txt, zw;
  447. if ((countRounds <= 1) && (status != std::string("Flow finished")))
  448. {
  449. // First round not completed yet
  450. // Nothing to do
  451. }
  452. else
  453. {
  454. /* Digit ROIs */
  455. txt = "<body style=\"font-family: arial\">";
  456. txt += "<hr><h3>Recognized Digit ROIs (previous round)</h3>\n";
  457. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  458. std::vector<HTMLInfo *> htmlinfodig;
  459. htmlinfodig = flowctrl.GetAllDigit();
  460. for (int i = 0; i < htmlinfodig.size(); ++i)
  461. {
  462. if (flowctrl.GetTypeDigit() == Digit)
  463. {
  464. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  465. if ((htmlinfodig[i]->val >= 10) || (htmlinfodig[i]->val < 0))
  466. {
  467. zw = "NaN";
  468. }
  469. else
  470. {
  471. zw = std::to_string((int)htmlinfodig[i]->val);
  472. }
  473. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  474. }
  475. else
  476. {
  477. std::stringstream stream;
  478. stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
  479. zw = stream.str();
  480. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  481. if ((std::stod(zw) >= 10) || (std::stod(zw) < 0))
  482. {
  483. zw = "NaN";
  484. }
  485. txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
  486. }
  487. delete htmlinfodig[i];
  488. }
  489. htmlinfodig.clear();
  490. txt += "</tr></table>\n";
  491. httpd_resp_sendstr_chunk(req, txt.c_str());
  492. /* Analog ROIs */
  493. txt = "<hr><h3>Recognized Analog ROIs (previous round)</h3>\n";
  494. txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
  495. std::vector<HTMLInfo *> htmlinfoana;
  496. htmlinfoana = flowctrl.GetAllAnalog();
  497. for (int i = 0; i < htmlinfoana.size(); ++i)
  498. {
  499. std::stringstream stream;
  500. stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
  501. zw = stream.str();
  502. // Numbers greater than 10 and less than 0 indicate NaN, since a Roi can only have values ​​from 0 to 9.
  503. if ((std::stod(zw) >= 10) || (std::stod(zw) < 0))
  504. {
  505. zw = "NaN";
  506. }
  507. txt += "<td style=\"width: 150px;\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"></p></td>\n";
  508. delete htmlinfoana[i];
  509. }
  510. htmlinfoana.clear();
  511. txt += "</tr>\n</table>\n";
  512. httpd_resp_sendstr_chunk(req, txt.c_str());
  513. /* Full Image
  514. * Only show it after the image got taken */
  515. txt = "<hr><h3>Full Image (current round)</h3>\n";
  516. if ((status == std::string("Initialization")) ||
  517. (status == std::string("Initialization (delayed)")) ||
  518. (status == std::string("Take Image")))
  519. {
  520. txt += "<p>Current state: " + status + "</p>\n";
  521. }
  522. else
  523. {
  524. txt += "<img src=\"/img_tmp/alg_roi.jpg\">\n";
  525. }
  526. httpd_resp_sendstr_chunk(req, txt.c_str());
  527. }
  528. }
  529. /* Respond with an empty chunk to signal HTTP response completion */
  530. httpd_resp_sendstr_chunk(req, NULL);
  531. }
  532. else
  533. {
  534. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!");
  535. return ESP_ERR_NOT_FOUND;
  536. }
  537. #ifdef DEBUG_DETAIL_ON
  538. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  539. #endif
  540. return ESP_OK;
  541. }
  542. esp_err_t handler_editflow(httpd_req_t *req)
  543. {
  544. #ifdef DEBUG_DETAIL_ON
  545. LogFile.WriteHeapInfo("handler_editflow - Start");
  546. #endif
  547. ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri);
  548. char _query[512] = ""; // stays empty when the request has no query string
  549. char _valuechar[30];
  550. std::string _task;
  551. if (httpd_req_get_url_query_str(req, _query, 512) == ESP_OK)
  552. {
  553. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  554. {
  555. #ifdef DEBUG_DETAIL_ON
  556. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  557. #endif
  558. _task = std::string(_valuechar);
  559. }
  560. }
  561. if (_task.compare("namenumbers") == 0)
  562. {
  563. ESP_LOGD(TAG, "Get NUMBER list");
  564. return get_numbers_file_handler(req);
  565. }
  566. if (_task.compare("data") == 0)
  567. {
  568. ESP_LOGD(TAG, "Get data list");
  569. return get_data_file_handler(req);
  570. }
  571. if (_task.compare("tflite") == 0)
  572. {
  573. ESP_LOGD(TAG, "Get tflite list");
  574. return get_tflite_file_handler(req);
  575. }
  576. if (_task.compare("copy") == 0)
  577. {
  578. std::string in, out, zw;
  579. httpd_query_key_value(_query, "in", _valuechar, 30);
  580. in = std::string(_valuechar);
  581. httpd_query_key_value(_query, "out", _valuechar, 30);
  582. out = std::string(_valuechar);
  583. #ifdef DEBUG_DETAIL_ON
  584. ESP_LOGD(TAG, "in: %s", in.c_str());
  585. ESP_LOGD(TAG, "out: %s", out.c_str());
  586. #endif
  587. in = "/sdcard" + in;
  588. out = "/sdcard" + out;
  589. CopyFile(in, out);
  590. zw = "Copy Done";
  591. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  592. httpd_resp_send(req, zw.c_str(), zw.length());
  593. }
  594. if (_task.compare("cutref") == 0)
  595. {
  596. std::string in, out, zw;
  597. int x = 0, y = 0, dx = 20, dy = 20;
  598. bool enhance = false;
  599. httpd_query_key_value(_query, "in", _valuechar, 30);
  600. in = std::string(_valuechar);
  601. httpd_query_key_value(_query, "out", _valuechar, 30);
  602. out = std::string(_valuechar);
  603. httpd_query_key_value(_query, "x", _valuechar, 30);
  604. std::string _x = std::string(_valuechar);
  605. if (isStringNumeric(_x))
  606. {
  607. x = std::stoi(_x);
  608. }
  609. httpd_query_key_value(_query, "y", _valuechar, 30);
  610. std::string _y = std::string(_valuechar);
  611. if (isStringNumeric(_y))
  612. {
  613. y = std::stoi(_y);
  614. }
  615. httpd_query_key_value(_query, "dx", _valuechar, 30);
  616. std::string _dx = std::string(_valuechar);
  617. if (isStringNumeric(_dx))
  618. {
  619. dx = std::stoi(_dx);
  620. }
  621. httpd_query_key_value(_query, "dy", _valuechar, 30);
  622. std::string _dy = std::string(_valuechar);
  623. if (isStringNumeric(_dy))
  624. {
  625. dy = std::stoi(_dy);
  626. }
  627. #ifdef DEBUG_DETAIL_ON
  628. ESP_LOGD(TAG, "in: %s", in.c_str());
  629. ESP_LOGD(TAG, "out: %s", out.c_str());
  630. ESP_LOGD(TAG, "x: %s", _x.c_str());
  631. ESP_LOGD(TAG, "y: %s", _y.c_str());
  632. ESP_LOGD(TAG, "dx: %s", _dx.c_str());
  633. ESP_LOGD(TAG, "dy: %s", _dy.c_str());
  634. #endif
  635. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  636. {
  637. string _enhance = std::string(_valuechar);
  638. if (_enhance.compare("true") == 0)
  639. {
  640. enhance = true;
  641. }
  642. }
  643. in = "/sdcard" + in;
  644. out = "/sdcard" + out;
  645. std::string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  646. if ((flowctrl.SetupModeActive || (flowctrl.getActStatus() == std::string("Flow finished"))) && psram_init_shared_memory_for_take_image_step())
  647. {
  648. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Taking image for Alignment Mark Update...");
  649. bool operationOk = false;
  650. CAlignAndCutImage *caic = new (std::nothrow) CAlignAndCutImage("cutref", in);
  651. if (caic != NULL) {
  652. caic->CutAndSave(out2, x, y, dx, dy);
  653. delete caic;
  654. CImageBasis *cim = new (std::nothrow) CImageBasis("cutref", out2);
  655. if (cim != NULL && cim->ImageOkay()) {
  656. if (enhance) cim->Contrast(90);
  657. cim->SaveToFile(out);
  658. operationOk = true;
  659. }
  660. delete cim;
  661. }
  662. if (operationOk) zw = "CutImage Done";
  663. else {
  664. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Alignment mark update failed due to an allocation or image I/O error");
  665. zw = "CutImage Failed";
  666. }
  667. // Release only after every STBI-backed temporary image has been destroyed.
  668. psram_deinit_shared_memory_for_take_image_step();
  669. }
  670. else
  671. {
  672. 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() + "')!");
  673. zw = "Device Busy";
  674. }
  675. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  676. httpd_resp_send(req, zw.c_str(), zw.length());
  677. }
  678. // wird beim Erstellen eines neuen Referenzbildes aufgerufen
  679. std::string sys_status = flowctrl.getActStatus();
  680. if ((sys_status != "Take Image") && (sys_status != "Aligning"))
  681. {
  682. if ((_task.compare("test_take") == 0) || (_task.compare("cam_settings") == 0))
  683. {
  684. std::string _host = "";
  685. // laden der aktuellen Kameraeinstellungen(CCstatus) in den Zwischenspeicher(CFstatus)
  686. setCCstatusToCFstatus(); // CCstatus >>> CFstatus
  687. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  688. {
  689. _host = std::string(_valuechar);
  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. CFstatus.WaitBeforePicture = 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. CFstatus.ImageGainceiling = GAINCEILING_4X;
  709. break;
  710. case 2:
  711. CFstatus.ImageGainceiling = GAINCEILING_8X;
  712. break;
  713. case 3:
  714. CFstatus.ImageGainceiling = GAINCEILING_16X;
  715. break;
  716. case 4:
  717. CFstatus.ImageGainceiling = GAINCEILING_32X;
  718. break;
  719. case 5:
  720. CFstatus.ImageGainceiling = GAINCEILING_64X;
  721. break;
  722. case 6:
  723. CFstatus.ImageGainceiling = GAINCEILING_128X;
  724. break;
  725. default:
  726. CFstatus.ImageGainceiling = GAINCEILING_2X;
  727. }
  728. }
  729. else
  730. {
  731. if (_aecgc == "X4") {
  732. CFstatus.ImageGainceiling = GAINCEILING_4X;
  733. }
  734. else if (_aecgc == "X8") {
  735. CFstatus.ImageGainceiling = GAINCEILING_8X;
  736. }
  737. else if (_aecgc == "X16") {
  738. CFstatus.ImageGainceiling = GAINCEILING_16X;
  739. }
  740. else if (_aecgc == "X32") {
  741. CFstatus.ImageGainceiling = GAINCEILING_32X;
  742. }
  743. else if (_aecgc == "X64") {
  744. CFstatus.ImageGainceiling = GAINCEILING_64X;
  745. }
  746. else if (_aecgc == "X128") {
  747. CFstatus.ImageGainceiling = GAINCEILING_128X;
  748. }
  749. else {
  750. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.ImageSharpness = clipInt(_shp_, 2, -2);
  799. }
  800. else
  801. {
  802. CFstatus.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. CFstatus.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. CFstatus.ImageSpecialEffect = clipInt(_spe_, 6, 0);
  818. }
  819. else
  820. {
  821. if (_spe == "negative") {
  822. CFstatus.ImageSpecialEffect = 1;
  823. }
  824. else if (_spe == "grayscale") {
  825. CFstatus.ImageSpecialEffect = 2;
  826. }
  827. else if (_spe == "red") {
  828. CFstatus.ImageSpecialEffect = 3;
  829. }
  830. else if (_spe == "green") {
  831. CFstatus.ImageSpecialEffect = 4;
  832. }
  833. else if (_spe == "blue") {
  834. CFstatus.ImageSpecialEffect = 5;
  835. }
  836. else if (_spe == "retro") {
  837. CFstatus.ImageSpecialEffect = 6;
  838. }
  839. else {
  840. CFstatus.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. CFstatus.ImageWbMode = clipInt(_wbm_, 4, 0);
  851. }
  852. else
  853. {
  854. if (_wbm == "sunny") {
  855. CFstatus.ImageWbMode = 1;
  856. }
  857. else if (_wbm == "cloudy") {
  858. CFstatus.ImageWbMode = 2;
  859. }
  860. else if (_wbm == "office") {
  861. CFstatus.ImageWbMode = 3;
  862. }
  863. else if (_wbm == "home") {
  864. CFstatus.ImageWbMode = 4;
  865. }
  866. else {
  867. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.ImageAeLevel = clipInt(_ael_, 2, -2);
  900. }
  901. else
  902. {
  903. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.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. CFstatus.ImageDenoiseLevel = 0;
  974. }
  975. else
  976. {
  977. CFstatus.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. CFstatus.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. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 480, -480);
  995. }
  996. else if (CCstatus.CamSensor_id == OV3660_PID)
  997. {
  998. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 704, -704);
  999. }
  1000. else if (CCstatus.CamSensor_id == OV5640_PID)
  1001. {
  1002. CFstatus.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. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 360, -360);
  1015. }
  1016. else if (CCstatus.CamSensor_id == OV3660_PID)
  1017. {
  1018. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 528, -528);
  1019. }
  1020. else if (CCstatus.CamSensor_id == OV5640_PID)
  1021. {
  1022. CFstatus.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. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 29, 0);
  1035. }
  1036. else if (CCstatus.CamSensor_id == OV3660_PID)
  1037. {
  1038. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 43, 0);
  1039. }
  1040. else if (CCstatus.CamSensor_id == OV5640_PID)
  1041. {
  1042. CFstatus.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. CFstatus.ImageLedIntensity = Camera.SetLEDIntensity(_ImageLedIntensity);
  1053. }
  1054. }
  1055. if (_task.compare("cam_settings") == 0)
  1056. {
  1057. // wird aufgerufen, wenn das Referenzbild + Kameraeinstellungen gespeichert wurden
  1058. setCFstatusToCCstatus(); // CFstatus >>> CCstatus
  1059. // Kameraeinstellungen wurden verädert
  1060. CFstatus.changedCameraSettings = true;
  1061. ESP_LOGD(TAG, "Cam Settings set");
  1062. std::string _zw = "CamSettingsSet";
  1063. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1064. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1065. }
  1066. else
  1067. {
  1068. // wird aufgerufen, wenn ein neues Referenzbild erstellt oder aktualisiert wurde
  1069. // CFstatus >>> Kamera
  1070. setCFstatusToCam();
  1071. Camera.SetQualityZoomSize(CFstatus.ImageQuality, CFstatus.ImageFrameSize, CFstatus.ImageZoomEnabled, CFstatus.ImageZoomOffsetX, CFstatus.ImageZoomOffsetY, CFstatus.ImageZoomSize, CFstatus.ImageVflip);
  1072. // Camera.SetZoomSize(CFstatus.ImageZoomEnabled, CFstatus.ImageZoomOffsetX, CFstatus.ImageZoomOffsetY, CFstatus.ImageZoomSize, CFstatus.ImageVflip);
  1073. // Kameraeinstellungen wurden verädert
  1074. CFstatus.changedCameraSettings = true;
  1075. ESP_LOGD(TAG, "test_take - vor TakeImage");
  1076. std::string image_temp = flowctrl.doSingleStep("[TakeImage]", _host);
  1077. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1078. httpd_resp_send(req, image_temp.c_str(), image_temp.length());
  1079. }
  1080. }
  1081. if (_task.compare("test_align") == 0)
  1082. {
  1083. std::string _host = "";
  1084. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  1085. {
  1086. _host = std::string(_valuechar);
  1087. }
  1088. std::string zw = flowctrl.doSingleStep("[Alignment]", _host);
  1089. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1090. httpd_resp_send(req, zw.c_str(), zw.length());
  1091. }
  1092. }
  1093. else
  1094. {
  1095. std::string _zw = "DeviceIsBusy";
  1096. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1097. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1098. }
  1099. #ifdef DEBUG_DETAIL_ON
  1100. LogFile.WriteHeapInfo("handler_editflow - Done");
  1101. #endif
  1102. return ESP_OK;
  1103. }
  1104. esp_err_t handler_statusflow(httpd_req_t *req)
  1105. {
  1106. #ifdef DEBUG_DETAIL_ON
  1107. LogFile.WriteHeapInfo("handler_statusflow - Start");
  1108. #endif
  1109. const char *resp_str;
  1110. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1111. if (bTaskAutoFlowCreated)
  1112. {
  1113. #ifdef DEBUG_DETAIL_ON
  1114. ESP_LOGD(TAG, "handler_statusflow: %s", req->uri);
  1115. #endif
  1116. std::string zw = flowctrl.getActStatusWithTime();
  1117. resp_str = zw.c_str();
  1118. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1119. }
  1120. else
  1121. {
  1122. resp_str = "Flow task not yet created";
  1123. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1124. }
  1125. #ifdef DEBUG_DETAIL_ON
  1126. LogFile.WriteHeapInfo("handler_statusflow - Done");
  1127. #endif
  1128. return ESP_OK;
  1129. }
  1130. esp_err_t handler_cputemp(httpd_req_t *req)
  1131. {
  1132. #ifdef DEBUG_DETAIL_ON
  1133. LogFile.WriteHeapInfo("handler_cputemp - Start");
  1134. #endif
  1135. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1136. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  1137. #ifdef DEBUG_DETAIL_ON
  1138. LogFile.WriteHeapInfo("handler_cputemp - End");
  1139. #endif
  1140. return ESP_OK;
  1141. }
  1142. esp_err_t handler_rssi(httpd_req_t *req)
  1143. {
  1144. #ifdef DEBUG_DETAIL_ON
  1145. LogFile.WriteHeapInfo("handler_rssi - Start");
  1146. #endif
  1147. if (getWIFIisConnected())
  1148. {
  1149. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1150. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  1151. }
  1152. else
  1153. {
  1154. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  1155. return ESP_ERR_NOT_FOUND;
  1156. }
  1157. #ifdef DEBUG_DETAIL_ON
  1158. LogFile.WriteHeapInfo("handler_rssi - End");
  1159. #endif
  1160. return ESP_OK;
  1161. }
  1162. esp_err_t handler_current_date(httpd_req_t *req)
  1163. {
  1164. #ifdef DEBUG_DETAIL_ON
  1165. LogFile.WriteHeapInfo("handler_uptime - Start");
  1166. #endif
  1167. std::string formatedDateAndTime = getCurrentTimeString("%Y-%m-%d %H:%M:%S");
  1168. // std::string formatedDate = getCurrentTimeString("%Y-%m-%d");
  1169. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1170. httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
  1171. #ifdef DEBUG_DETAIL_ON
  1172. LogFile.WriteHeapInfo("handler_uptime - End");
  1173. #endif
  1174. return ESP_OK;
  1175. }
  1176. esp_err_t handler_uptime(httpd_req_t *req)
  1177. {
  1178. #ifdef DEBUG_DETAIL_ON
  1179. LogFile.WriteHeapInfo("handler_uptime - Start");
  1180. #endif
  1181. std::string formatedUptime = getFormatedUptime(false);
  1182. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1183. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  1184. #ifdef DEBUG_DETAIL_ON
  1185. LogFile.WriteHeapInfo("handler_uptime - End");
  1186. #endif
  1187. return ESP_OK;
  1188. }
  1189. esp_err_t handler_prevalue(httpd_req_t *req)
  1190. {
  1191. #ifdef DEBUG_DETAIL_ON
  1192. LogFile.WriteHeapInfo("handler_prevalue - Start");
  1193. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  1194. #endif
  1195. // Default usage message when handler gets called without any parameter
  1196. const std::string RESTUsageInfo =
  1197. "00: Handler usage:<br>"
  1198. "- To retrieve actual PreValue, please provide only a numbersname, e.g. /setPreValue?numbers=main<br>"
  1199. "- To set PreValue to a new value, please provide a numbersname and a value, e.g. /setPreValue?numbers=main&value=1234.5678<br>"
  1200. "NOTE:<br>"
  1201. "value >= 0.0: Set PreValue to provided value<br>"
  1202. "value < 0.0: Set PreValue to actual RAW value (as long RAW value is a valid number, without N)";
  1203. // Default return error message when no return is programmed
  1204. std::string sReturnMessage = "E90: Uninitialized";
  1205. char _query[100] = ""; // stays empty when the request has no query string
  1206. char _numbersname[50] = "default";
  1207. char _value[20] = "";
  1208. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1209. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  1210. {
  1211. #ifdef DEBUG_DETAIL_ON
  1212. ESP_LOGD(TAG, "Query: %s", _query);
  1213. #endif
  1214. if (httpd_query_key_value(_query, "numbers", _numbersname, 50) != ESP_OK)
  1215. {
  1216. // If request is incomplete
  1217. sReturnMessage = "E91: Query parameter incomplete or not valid!<br> "
  1218. "Call /setPreValue to show REST API usage info and/or check documentation";
  1219. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1220. return ESP_FAIL;
  1221. }
  1222. if (httpd_query_key_value(_query, "value", _value, 20) == ESP_OK)
  1223. {
  1224. #ifdef DEBUG_DETAIL_ON
  1225. ESP_LOGD(TAG, "Value: %s", _value);
  1226. #endif
  1227. }
  1228. }
  1229. else
  1230. {
  1231. // if no parameter is provided, print handler usage
  1232. httpd_resp_send(req, RESTUsageInfo.c_str(), RESTUsageInfo.length());
  1233. return ESP_OK;
  1234. }
  1235. if (strlen(_value) == 0)
  1236. {
  1237. // If no value is povided --> return actual PreValue
  1238. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1239. if (sReturnMessage.empty())
  1240. {
  1241. sReturnMessage = "E92: Numbers name not found";
  1242. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1243. return ESP_FAIL;
  1244. }
  1245. }
  1246. else
  1247. {
  1248. // New value is positive: Set PreValue to provided value and return value
  1249. // New value is negative and actual RAW value is a valid number: Set PreValue to RAW value and return value
  1250. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "REST API handler_prevalue called: numbersname: " + std::string(_numbersname) + ", value: " + std::string(_value));
  1251. if (!flowctrl.UpdatePrevalue(_value, _numbersname, true))
  1252. {
  1253. sReturnMessage = "E93: Update request rejected. Please check device logs for more details";
  1254. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1255. return ESP_FAIL;
  1256. }
  1257. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1258. if (sReturnMessage.empty())
  1259. {
  1260. sReturnMessage = "E94: Numbers name not found";
  1261. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1262. return ESP_FAIL;
  1263. }
  1264. }
  1265. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1266. #ifdef DEBUG_DETAIL_ON
  1267. LogFile.WriteHeapInfo("handler_prevalue - End");
  1268. #endif
  1269. return ESP_OK;
  1270. }
  1271. void task_autodoFlow(void *pvParameter)
  1272. {
  1273. int64_t fr_start, fr_delta_ms;
  1274. bTaskAutoFlowCreated = true;
  1275. if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
  1276. {
  1277. flowctrl.setActStatus("Initialization (delayed)");
  1278. // #ifdef ENABLE_MQTT
  1279. // MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  1280. // #endif //ENABLE_MQTT
  1281. vTaskDelay(60 * 5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
  1282. }
  1283. ESP_LOGD(TAG, "task_autodoFlow: start");
  1284. doInit();
  1285. flowctrl.setAutoStartInterval(auto_interval);
  1286. autostartIsEnabled = flowctrl.getIsAutoStart();
  1287. if (isSetupModusActive())
  1288. {
  1289. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "We are in Setup Mode -> Not starting Auto Flow!");
  1290. autostartIsEnabled = false;
  1291. // 15.7.0 Setup Wizard cannot take a Reference Picture #2953
  1292. // std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  1293. // flowctrl.doFlowTakeImageOnly(zw_time);
  1294. }
  1295. if (autostartIsEnabled)
  1296. {
  1297. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting Flow...");
  1298. }
  1299. else
  1300. {
  1301. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Autostart is not enabled -> Not starting Flow");
  1302. }
  1303. while (autostartIsEnabled)
  1304. {
  1305. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  1306. time_t roundStartTime = getUpTime();
  1307. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  1308. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  1309. fr_start = esp_timer_get_time();
  1310. if (flowisrunning)
  1311. {
  1312. #ifdef DEBUG_DETAIL_ON
  1313. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  1314. #endif
  1315. }
  1316. else
  1317. {
  1318. #ifdef DEBUG_DETAIL_ON
  1319. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  1320. #endif
  1321. flowisrunning = true;
  1322. doflow();
  1323. #ifdef DEBUG_DETAIL_ON
  1324. ESP_LOGD(TAG, "Remove older log files");
  1325. #endif
  1326. LogFile.RemoveOldLogFile();
  1327. LogFile.RemoveOldDataLog();
  1328. }
  1329. // Round finished -> Logfile
  1330. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) + " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  1331. // CPU Temp -> Logfile
  1332. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  1333. // WIFI Signal Strength (RSSI) -> Logfile
  1334. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  1335. // Check if time is synchronized (if NTP is configured)
  1336. if (getUseNtp() && !getTimeIsSet())
  1337. {
  1338. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set!");
  1339. StatusLED(TIME_CHECK, 1, false);
  1340. }
  1341. #if (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES)
  1342. wifiRoamingQuery();
  1343. #endif
  1344. // Scan channels and check if an AP with better RSSI is available, then disconnect and try to reconnect to AP with better RSSI
  1345. // NOTE: Keep this direct before the following task delay, because scan is done in blocking mode and this takes ca. 1,5 - 2s.
  1346. #ifdef WLAN_USE_ROAMING_BY_SCANNING
  1347. wifiRoamByScanning();
  1348. #endif
  1349. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  1350. if (auto_interval > fr_delta_ms)
  1351. {
  1352. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  1353. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long)xDelay);
  1354. vTaskDelay(xDelay);
  1355. }
  1356. }
  1357. while (1)
  1358. {
  1359. // Keep flow task running to handle necessary sub tasks like reboot handler, etc..
  1360. vTaskDelay(2000 / portTICK_PERIOD_MS);
  1361. }
  1362. vTaskDelete(NULL); // Delete this task if it exits from the loop above
  1363. xHandletask_autodoFlow = NULL;
  1364. ESP_LOGD(TAG, "task_autodoFlow: end");
  1365. }
  1366. void InitializeFlowTask(void)
  1367. {
  1368. BaseType_t xReturned;
  1369. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1370. uint32_t stackSize = 16 * 1024;
  1371. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", stackSize, NULL, tskIDLE_PRIORITY + 2, &xHandletask_autodoFlow, 0);
  1372. if (xReturned != pdPASS)
  1373. {
  1374. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Creation task_autodoFlow failed. Requested stack size:" + std::to_string(stackSize));
  1375. LogFile.WriteHeapInfo("Creation task_autodoFlow failed");
  1376. }
  1377. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1378. }
  1379. void register_server_main_flow_task_uri(httpd_handle_t server)
  1380. {
  1381. ESP_LOGI(TAG, "server_main_flow_task - Registering URI handlers");
  1382. httpd_uri_t camuri = {};
  1383. camuri.method = HTTP_GET;
  1384. camuri.uri = "/doinit";
  1385. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_init);
  1386. camuri.user_ctx = (void *)"Light On";
  1387. httpd_register_uri_handler(server, &camuri);
  1388. // Legacy API => New: "/setPreValue"
  1389. camuri.uri = "/setPreValue.html";
  1390. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1391. camuri.user_ctx = (void *)"Prevalue";
  1392. httpd_register_uri_handler(server, &camuri);
  1393. camuri.uri = "/setPreValue";
  1394. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1395. camuri.user_ctx = (void *)"Prevalue";
  1396. httpd_register_uri_handler(server, &camuri);
  1397. camuri.uri = "/flow_start";
  1398. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_flow_start);
  1399. camuri.user_ctx = (void *)"Flow Start";
  1400. httpd_register_uri_handler(server, &camuri);
  1401. camuri.uri = "/statusflow.html";
  1402. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1403. camuri.user_ctx = (void *)"Light Off";
  1404. httpd_register_uri_handler(server, &camuri);
  1405. camuri.uri = "/statusflow";
  1406. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1407. camuri.user_ctx = (void *)"Light Off";
  1408. httpd_register_uri_handler(server, &camuri);
  1409. // Legacy API => New: "/cpu_temperature"
  1410. camuri.uri = "/cputemp.html";
  1411. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1412. camuri.user_ctx = (void *)"Light Off";
  1413. httpd_register_uri_handler(server, &camuri);
  1414. camuri.uri = "/cpu_temperature";
  1415. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1416. camuri.user_ctx = (void *)"Light Off";
  1417. httpd_register_uri_handler(server, &camuri);
  1418. // Legacy API => New: "/rssi"
  1419. camuri.uri = "/rssi.html";
  1420. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1421. camuri.user_ctx = (void *)"Light Off";
  1422. httpd_register_uri_handler(server, &camuri);
  1423. camuri.uri = "/rssi";
  1424. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1425. camuri.user_ctx = (void *)"Light Off";
  1426. httpd_register_uri_handler(server, &camuri);
  1427. camuri.uri = "/date";
  1428. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_current_date);
  1429. camuri.user_ctx = (void *)"Light Off";
  1430. httpd_register_uri_handler(server, &camuri);
  1431. camuri.uri = "/uptime";
  1432. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_uptime);
  1433. camuri.user_ctx = (void *)"Light Off";
  1434. httpd_register_uri_handler(server, &camuri);
  1435. camuri.uri = "/editflow";
  1436. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_editflow);
  1437. camuri.user_ctx = (void *)"EditFlow";
  1438. httpd_register_uri_handler(server, &camuri);
  1439. // Legacy API => New: "/value"
  1440. camuri.uri = "/value.html";
  1441. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1442. camuri.user_ctx = (void *)"Value";
  1443. httpd_register_uri_handler(server, &camuri);
  1444. camuri.uri = "/value";
  1445. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1446. camuri.user_ctx = (void *)"Value";
  1447. httpd_register_uri_handler(server, &camuri);
  1448. // Legacy API => New: "/value"
  1449. camuri.uri = "/wasserzaehler.html";
  1450. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1451. camuri.user_ctx = (void *)"Wasserzaehler";
  1452. httpd_register_uri_handler(server, &camuri);
  1453. camuri.uri = "/json";
  1454. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_json);
  1455. camuri.user_ctx = (void *)"JSON";
  1456. httpd_register_uri_handler(server, &camuri);
  1457. camuri.uri = "/heap";
  1458. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_get_heap);
  1459. camuri.user_ctx = (void *)"Heap";
  1460. httpd_register_uri_handler(server, &camuri);
  1461. camuri.uri = "/stream";
  1462. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_stream);
  1463. camuri.user_ctx = (void *)"stream";
  1464. httpd_register_uri_handler(server, &camuri);
  1465. /** will handle metrics requests */
  1466. camuri.uri = "/metrics";
  1467. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_openmetrics);
  1468. camuri.user_ctx = (void *)"metrics";
  1469. httpd_register_uri_handler(server, &camuri);
  1470. /** when adding a new handler, make sure to increment the value for config.max_uri_handlers in `main/server_main.cpp` */
  1471. }