MainFlowControl.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  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. CFstatus.previewCapturePending = false;
  1060. // Kameraeinstellungen wurden verädert
  1061. CFstatus.changedCameraSettings = true;
  1062. ESP_LOGD(TAG, "Cam Settings set");
  1063. std::string _zw = "CamSettingsSet";
  1064. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1065. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1066. }
  1067. else
  1068. {
  1069. // wird aufgerufen, wenn ein neues Referenzbild erstellt oder aktualisiert wurde
  1070. // CFstatus >>> Kamera
  1071. if (setCFstatusToCam() != 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. CFstatus.changedCameraSettings = true;
  1077. CFstatus.previewCapturePending = true;
  1078. ESP_LOGD(TAG, "test_take - vor TakeImage");
  1079. std::string image_temp = flowctrl.doSingleStep("[TakeImage]", _host);
  1080. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1081. httpd_resp_send(req, image_temp.c_str(), image_temp.length());
  1082. }
  1083. }
  1084. if (_task.compare("test_align") == 0)
  1085. {
  1086. std::string _host = "";
  1087. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  1088. {
  1089. _host = std::string(_valuechar);
  1090. }
  1091. std::string zw = flowctrl.doSingleStep("[Alignment]", _host);
  1092. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1093. httpd_resp_send(req, zw.c_str(), zw.length());
  1094. }
  1095. }
  1096. else
  1097. {
  1098. std::string _zw = "DeviceIsBusy";
  1099. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1100. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1101. }
  1102. #ifdef DEBUG_DETAIL_ON
  1103. LogFile.WriteHeapInfo("handler_editflow - Done");
  1104. #endif
  1105. return ESP_OK;
  1106. }
  1107. esp_err_t handler_statusflow(httpd_req_t *req)
  1108. {
  1109. #ifdef DEBUG_DETAIL_ON
  1110. LogFile.WriteHeapInfo("handler_statusflow - Start");
  1111. #endif
  1112. const char *resp_str;
  1113. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1114. if (bTaskAutoFlowCreated)
  1115. {
  1116. #ifdef DEBUG_DETAIL_ON
  1117. ESP_LOGD(TAG, "handler_statusflow: %s", req->uri);
  1118. #endif
  1119. std::string zw = flowctrl.getActStatusWithTime();
  1120. resp_str = zw.c_str();
  1121. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1122. }
  1123. else
  1124. {
  1125. resp_str = "Flow task not yet created";
  1126. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1127. }
  1128. #ifdef DEBUG_DETAIL_ON
  1129. LogFile.WriteHeapInfo("handler_statusflow - Done");
  1130. #endif
  1131. return ESP_OK;
  1132. }
  1133. esp_err_t handler_cputemp(httpd_req_t *req)
  1134. {
  1135. #ifdef DEBUG_DETAIL_ON
  1136. LogFile.WriteHeapInfo("handler_cputemp - Start");
  1137. #endif
  1138. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1139. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  1140. #ifdef DEBUG_DETAIL_ON
  1141. LogFile.WriteHeapInfo("handler_cputemp - End");
  1142. #endif
  1143. return ESP_OK;
  1144. }
  1145. esp_err_t handler_rssi(httpd_req_t *req)
  1146. {
  1147. #ifdef DEBUG_DETAIL_ON
  1148. LogFile.WriteHeapInfo("handler_rssi - Start");
  1149. #endif
  1150. if (getWIFIisConnected())
  1151. {
  1152. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1153. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  1154. }
  1155. else
  1156. {
  1157. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  1158. return ESP_ERR_NOT_FOUND;
  1159. }
  1160. #ifdef DEBUG_DETAIL_ON
  1161. LogFile.WriteHeapInfo("handler_rssi - End");
  1162. #endif
  1163. return ESP_OK;
  1164. }
  1165. esp_err_t handler_current_date(httpd_req_t *req)
  1166. {
  1167. #ifdef DEBUG_DETAIL_ON
  1168. LogFile.WriteHeapInfo("handler_uptime - Start");
  1169. #endif
  1170. std::string formatedDateAndTime = getCurrentTimeString("%Y-%m-%d %H:%M:%S");
  1171. // std::string formatedDate = getCurrentTimeString("%Y-%m-%d");
  1172. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1173. httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
  1174. #ifdef DEBUG_DETAIL_ON
  1175. LogFile.WriteHeapInfo("handler_uptime - End");
  1176. #endif
  1177. return ESP_OK;
  1178. }
  1179. esp_err_t handler_uptime(httpd_req_t *req)
  1180. {
  1181. #ifdef DEBUG_DETAIL_ON
  1182. LogFile.WriteHeapInfo("handler_uptime - Start");
  1183. #endif
  1184. std::string formatedUptime = getFormatedUptime(false);
  1185. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1186. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  1187. #ifdef DEBUG_DETAIL_ON
  1188. LogFile.WriteHeapInfo("handler_uptime - End");
  1189. #endif
  1190. return ESP_OK;
  1191. }
  1192. esp_err_t handler_prevalue(httpd_req_t *req)
  1193. {
  1194. #ifdef DEBUG_DETAIL_ON
  1195. LogFile.WriteHeapInfo("handler_prevalue - Start");
  1196. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  1197. #endif
  1198. // Default usage message when handler gets called without any parameter
  1199. const std::string RESTUsageInfo =
  1200. "00: Handler usage:<br>"
  1201. "- To retrieve actual PreValue, please provide only a numbersname, e.g. /setPreValue?numbers=main<br>"
  1202. "- To set PreValue to a new value, please provide a numbersname and a value, e.g. /setPreValue?numbers=main&value=1234.5678<br>"
  1203. "NOTE:<br>"
  1204. "value >= 0.0: Set PreValue to provided value<br>"
  1205. "value < 0.0: Set PreValue to actual RAW value (as long RAW value is a valid number, without N)";
  1206. // Default return error message when no return is programmed
  1207. std::string sReturnMessage = "E90: Uninitialized";
  1208. char _query[100] = ""; // stays empty when the request has no query string
  1209. char _numbersname[50] = "default";
  1210. char _value[20] = "";
  1211. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1212. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  1213. {
  1214. #ifdef DEBUG_DETAIL_ON
  1215. ESP_LOGD(TAG, "Query: %s", _query);
  1216. #endif
  1217. if (httpd_query_key_value(_query, "numbers", _numbersname, 50) != ESP_OK)
  1218. {
  1219. // If request is incomplete
  1220. sReturnMessage = "E91: Query parameter incomplete or not valid!<br> "
  1221. "Call /setPreValue to show REST API usage info and/or check documentation";
  1222. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1223. return ESP_FAIL;
  1224. }
  1225. if (httpd_query_key_value(_query, "value", _value, 20) == ESP_OK)
  1226. {
  1227. #ifdef DEBUG_DETAIL_ON
  1228. ESP_LOGD(TAG, "Value: %s", _value);
  1229. #endif
  1230. }
  1231. }
  1232. else
  1233. {
  1234. // if no parameter is provided, print handler usage
  1235. httpd_resp_send(req, RESTUsageInfo.c_str(), RESTUsageInfo.length());
  1236. return ESP_OK;
  1237. }
  1238. if (strlen(_value) == 0)
  1239. {
  1240. // If no value is povided --> return actual PreValue
  1241. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1242. if (sReturnMessage.empty())
  1243. {
  1244. sReturnMessage = "E92: Numbers name not found";
  1245. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1246. return ESP_FAIL;
  1247. }
  1248. }
  1249. else
  1250. {
  1251. // New value is positive: Set PreValue to provided value and return value
  1252. // New value is negative and actual RAW value is a valid number: Set PreValue to RAW value and return value
  1253. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "REST API handler_prevalue called: numbersname: " + std::string(_numbersname) + ", value: " + std::string(_value));
  1254. if (!flowctrl.UpdatePrevalue(_value, _numbersname, true))
  1255. {
  1256. sReturnMessage = "E93: Update request rejected. Please check device logs for more details";
  1257. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1258. return ESP_FAIL;
  1259. }
  1260. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1261. if (sReturnMessage.empty())
  1262. {
  1263. sReturnMessage = "E94: Numbers name not found";
  1264. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1265. return ESP_FAIL;
  1266. }
  1267. }
  1268. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1269. #ifdef DEBUG_DETAIL_ON
  1270. LogFile.WriteHeapInfo("handler_prevalue - End");
  1271. #endif
  1272. return ESP_OK;
  1273. }
  1274. void task_autodoFlow(void *pvParameter)
  1275. {
  1276. int64_t fr_start, fr_delta_ms;
  1277. bTaskAutoFlowCreated = true;
  1278. if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
  1279. {
  1280. flowctrl.setActStatus("Initialization (delayed)");
  1281. // #ifdef ENABLE_MQTT
  1282. // MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  1283. // #endif //ENABLE_MQTT
  1284. vTaskDelay(60 * 5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
  1285. }
  1286. ESP_LOGD(TAG, "task_autodoFlow: start");
  1287. doInit();
  1288. flowctrl.setAutoStartInterval(auto_interval);
  1289. autostartIsEnabled = flowctrl.getIsAutoStart();
  1290. if (isSetupModusActive())
  1291. {
  1292. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "We are in Setup Mode -> Not starting Auto Flow!");
  1293. autostartIsEnabled = false;
  1294. // 15.7.0 Setup Wizard cannot take a Reference Picture #2953
  1295. // std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  1296. // flowctrl.doFlowTakeImageOnly(zw_time);
  1297. }
  1298. if (autostartIsEnabled)
  1299. {
  1300. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting Flow...");
  1301. }
  1302. else
  1303. {
  1304. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Autostart is not enabled -> Not starting Flow");
  1305. }
  1306. while (autostartIsEnabled)
  1307. {
  1308. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  1309. time_t roundStartTime = getUpTime();
  1310. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  1311. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  1312. fr_start = esp_timer_get_time();
  1313. if (flowisrunning)
  1314. {
  1315. #ifdef DEBUG_DETAIL_ON
  1316. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  1317. #endif
  1318. }
  1319. else
  1320. {
  1321. #ifdef DEBUG_DETAIL_ON
  1322. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  1323. #endif
  1324. flowisrunning = true;
  1325. doflow();
  1326. #ifdef DEBUG_DETAIL_ON
  1327. ESP_LOGD(TAG, "Remove older log files");
  1328. #endif
  1329. LogFile.RemoveOldLogFile();
  1330. LogFile.RemoveOldDataLog();
  1331. }
  1332. // Round finished -> Logfile
  1333. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) + " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  1334. // CPU Temp -> Logfile
  1335. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  1336. // WIFI Signal Strength (RSSI) -> Logfile
  1337. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  1338. // Check if time is synchronized (if NTP is configured)
  1339. if (getUseNtp() && !getTimeIsSet())
  1340. {
  1341. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set!");
  1342. StatusLED(TIME_CHECK, 1, false);
  1343. }
  1344. #if (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES)
  1345. wifiRoamingQuery();
  1346. #endif
  1347. // Scan channels and check if an AP with better RSSI is available, then disconnect and try to reconnect to AP with better RSSI
  1348. // NOTE: Keep this direct before the following task delay, because scan is done in blocking mode and this takes ca. 1,5 - 2s.
  1349. #ifdef WLAN_USE_ROAMING_BY_SCANNING
  1350. wifiRoamByScanning();
  1351. #endif
  1352. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  1353. if (auto_interval > fr_delta_ms)
  1354. {
  1355. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  1356. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long)xDelay);
  1357. vTaskDelay(xDelay);
  1358. }
  1359. }
  1360. while (1)
  1361. {
  1362. // Keep flow task running to handle necessary sub tasks like reboot handler, etc..
  1363. vTaskDelay(2000 / portTICK_PERIOD_MS);
  1364. }
  1365. vTaskDelete(NULL); // Delete this task if it exits from the loop above
  1366. xHandletask_autodoFlow = NULL;
  1367. ESP_LOGD(TAG, "task_autodoFlow: end");
  1368. }
  1369. void InitializeFlowTask(void)
  1370. {
  1371. BaseType_t xReturned;
  1372. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1373. uint32_t stackSize = 16 * 1024;
  1374. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", stackSize, NULL, tskIDLE_PRIORITY + 2, &xHandletask_autodoFlow, 0);
  1375. if (xReturned != pdPASS)
  1376. {
  1377. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Creation task_autodoFlow failed. Requested stack size:" + std::to_string(stackSize));
  1378. LogFile.WriteHeapInfo("Creation task_autodoFlow failed");
  1379. }
  1380. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1381. }
  1382. void register_server_main_flow_task_uri(httpd_handle_t server)
  1383. {
  1384. ESP_LOGI(TAG, "server_main_flow_task - Registering URI handlers");
  1385. httpd_uri_t camuri = {};
  1386. camuri.method = HTTP_GET;
  1387. camuri.uri = "/doinit";
  1388. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_init);
  1389. camuri.user_ctx = (void *)"Light On";
  1390. httpd_register_uri_handler(server, &camuri);
  1391. // Legacy API => New: "/setPreValue"
  1392. camuri.uri = "/setPreValue.html";
  1393. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1394. camuri.user_ctx = (void *)"Prevalue";
  1395. httpd_register_uri_handler(server, &camuri);
  1396. camuri.uri = "/setPreValue";
  1397. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1398. camuri.user_ctx = (void *)"Prevalue";
  1399. httpd_register_uri_handler(server, &camuri);
  1400. camuri.uri = "/flow_start";
  1401. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_flow_start);
  1402. camuri.user_ctx = (void *)"Flow Start";
  1403. httpd_register_uri_handler(server, &camuri);
  1404. camuri.uri = "/statusflow.html";
  1405. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1406. camuri.user_ctx = (void *)"Light Off";
  1407. httpd_register_uri_handler(server, &camuri);
  1408. camuri.uri = "/statusflow";
  1409. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1410. camuri.user_ctx = (void *)"Light Off";
  1411. httpd_register_uri_handler(server, &camuri);
  1412. // Legacy API => New: "/cpu_temperature"
  1413. camuri.uri = "/cputemp.html";
  1414. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1415. camuri.user_ctx = (void *)"Light Off";
  1416. httpd_register_uri_handler(server, &camuri);
  1417. camuri.uri = "/cpu_temperature";
  1418. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1419. camuri.user_ctx = (void *)"Light Off";
  1420. httpd_register_uri_handler(server, &camuri);
  1421. // Legacy API => New: "/rssi"
  1422. camuri.uri = "/rssi.html";
  1423. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1424. camuri.user_ctx = (void *)"Light Off";
  1425. httpd_register_uri_handler(server, &camuri);
  1426. camuri.uri = "/rssi";
  1427. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1428. camuri.user_ctx = (void *)"Light Off";
  1429. httpd_register_uri_handler(server, &camuri);
  1430. camuri.uri = "/date";
  1431. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_current_date);
  1432. camuri.user_ctx = (void *)"Light Off";
  1433. httpd_register_uri_handler(server, &camuri);
  1434. camuri.uri = "/uptime";
  1435. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_uptime);
  1436. camuri.user_ctx = (void *)"Light Off";
  1437. httpd_register_uri_handler(server, &camuri);
  1438. camuri.uri = "/editflow";
  1439. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_editflow);
  1440. camuri.user_ctx = (void *)"EditFlow";
  1441. httpd_register_uri_handler(server, &camuri);
  1442. // Legacy API => New: "/value"
  1443. camuri.uri = "/value.html";
  1444. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1445. camuri.user_ctx = (void *)"Value";
  1446. httpd_register_uri_handler(server, &camuri);
  1447. camuri.uri = "/value";
  1448. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1449. camuri.user_ctx = (void *)"Value";
  1450. httpd_register_uri_handler(server, &camuri);
  1451. // Legacy API => New: "/value"
  1452. camuri.uri = "/wasserzaehler.html";
  1453. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1454. camuri.user_ctx = (void *)"Wasserzaehler";
  1455. httpd_register_uri_handler(server, &camuri);
  1456. camuri.uri = "/json";
  1457. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_json);
  1458. camuri.user_ctx = (void *)"JSON";
  1459. httpd_register_uri_handler(server, &camuri);
  1460. camuri.uri = "/heap";
  1461. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_get_heap);
  1462. camuri.user_ctx = (void *)"Heap";
  1463. httpd_register_uri_handler(server, &camuri);
  1464. camuri.uri = "/stream";
  1465. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_stream);
  1466. camuri.user_ctx = (void *)"stream";
  1467. httpd_register_uri_handler(server, &camuri);
  1468. /** will handle metrics requests */
  1469. camuri.uri = "/metrics";
  1470. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_openmetrics);
  1471. camuri.user_ctx = (void *)"metrics";
  1472. httpd_register_uri_handler(server, &camuri);
  1473. /** when adding a new handler, make sure to increment the value for config.max_uri_handlers in `main/server_main.cpp` */
  1474. }