MainFlowControl.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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, "xclk", _valuechar, 30) == ESP_OK)
  692. {
  693. std::string _xclk = std::string(_valuechar);
  694. if (isStringNumeric(_xclk))
  695. {
  696. CFstatus.CamXclkFreqMhz = clipInt(std::stoi(_xclk), 20, 1);
  697. }
  698. }
  699. if (httpd_query_key_value(_query, "waitb", _valuechar, 30) == ESP_OK)
  700. {
  701. std::string _waitb = std::string(_valuechar);
  702. if (isStringNumeric(_waitb))
  703. {
  704. CFstatus.WaitBeforePicture = std::max(0, std::stoi(_valuechar));
  705. }
  706. }
  707. if (httpd_query_key_value(_query, "aecgc", _valuechar, 30) == ESP_OK)
  708. {
  709. std::string _aecgc = std::string(_valuechar);
  710. if (isStringNumeric(_aecgc))
  711. {
  712. int _aecgc_ = std::stoi(_valuechar);
  713. switch (_aecgc_)
  714. {
  715. case 1:
  716. CFstatus.ImageGainceiling = GAINCEILING_4X;
  717. break;
  718. case 2:
  719. CFstatus.ImageGainceiling = GAINCEILING_8X;
  720. break;
  721. case 3:
  722. CFstatus.ImageGainceiling = GAINCEILING_16X;
  723. break;
  724. case 4:
  725. CFstatus.ImageGainceiling = GAINCEILING_32X;
  726. break;
  727. case 5:
  728. CFstatus.ImageGainceiling = GAINCEILING_64X;
  729. break;
  730. case 6:
  731. CFstatus.ImageGainceiling = GAINCEILING_128X;
  732. break;
  733. default:
  734. CFstatus.ImageGainceiling = GAINCEILING_2X;
  735. }
  736. }
  737. else
  738. {
  739. if (_aecgc == "X4") {
  740. CFstatus.ImageGainceiling = GAINCEILING_4X;
  741. }
  742. else if (_aecgc == "X8") {
  743. CFstatus.ImageGainceiling = GAINCEILING_8X;
  744. }
  745. else if (_aecgc == "X16") {
  746. CFstatus.ImageGainceiling = GAINCEILING_16X;
  747. }
  748. else if (_aecgc == "X32") {
  749. CFstatus.ImageGainceiling = GAINCEILING_32X;
  750. }
  751. else if (_aecgc == "X64") {
  752. CFstatus.ImageGainceiling = GAINCEILING_64X;
  753. }
  754. else if (_aecgc == "X128") {
  755. CFstatus.ImageGainceiling = GAINCEILING_128X;
  756. }
  757. else {
  758. CFstatus.ImageGainceiling = GAINCEILING_2X;
  759. }
  760. }
  761. }
  762. if (httpd_query_key_value(_query, "qual", _valuechar, 30) == ESP_OK)
  763. {
  764. std::string _qual = std::string(_valuechar);
  765. if (isStringNumeric(_qual))
  766. {
  767. int _qual_ = std::stoi(_valuechar);
  768. CFstatus.ImageQuality = clipInt(_qual_, 63, 6);
  769. }
  770. }
  771. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK)
  772. {
  773. std::string _bri = std::string(_valuechar);
  774. if (isStringNumeric(_bri))
  775. {
  776. int _bri_ = std::stoi(_valuechar);
  777. CFstatus.ImageBrightness = clipInt(_bri_, 2, -2);
  778. }
  779. }
  780. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK)
  781. {
  782. std::string _con = std::string(_valuechar);
  783. if (isStringNumeric(_con))
  784. {
  785. int _con_ = std::stoi(_valuechar);
  786. CFstatus.ImageContrast = clipInt(_con_, 2, -2);
  787. }
  788. }
  789. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK)
  790. {
  791. std::string _sat = std::string(_valuechar);
  792. if (isStringNumeric(_sat))
  793. {
  794. int _sat_ = std::stoi(_valuechar);
  795. CFstatus.ImageSaturation = clipInt(_sat_, 2, -2);
  796. }
  797. }
  798. if (httpd_query_key_value(_query, "shp", _valuechar, 30) == ESP_OK)
  799. {
  800. std::string _shp = std::string(_valuechar);
  801. if (isStringNumeric(_shp))
  802. {
  803. int _shp_ = std::stoi(_valuechar);
  804. if (CCstatus.CamSensor_id == OV2640_PID)
  805. {
  806. CFstatus.ImageSharpness = clipInt(_shp_, 2, -2);
  807. }
  808. else
  809. {
  810. CFstatus.ImageSharpness = clipInt(_shp_, 3, -3);
  811. }
  812. }
  813. }
  814. if (httpd_query_key_value(_query, "ashp", _valuechar, 30) == ESP_OK)
  815. {
  816. std::string _ashp = std::string(_valuechar);
  817. CFstatus.ImageAutoSharpness = alphanumericToBoolean(_ashp);
  818. }
  819. if (httpd_query_key_value(_query, "spe", _valuechar, 30) == ESP_OK)
  820. {
  821. std::string _spe = std::string(_valuechar);
  822. if (isStringNumeric(_spe))
  823. {
  824. int _spe_ = std::stoi(_valuechar);
  825. CFstatus.ImageSpecialEffect = clipInt(_spe_, 6, 0);
  826. }
  827. else
  828. {
  829. if (_spe == "negative") {
  830. CFstatus.ImageSpecialEffect = 1;
  831. }
  832. else if (_spe == "grayscale") {
  833. CFstatus.ImageSpecialEffect = 2;
  834. }
  835. else if (_spe == "red") {
  836. CFstatus.ImageSpecialEffect = 3;
  837. }
  838. else if (_spe == "green") {
  839. CFstatus.ImageSpecialEffect = 4;
  840. }
  841. else if (_spe == "blue") {
  842. CFstatus.ImageSpecialEffect = 5;
  843. }
  844. else if (_spe == "retro") {
  845. CFstatus.ImageSpecialEffect = 6;
  846. }
  847. else {
  848. CFstatus.ImageSpecialEffect = 0;
  849. }
  850. }
  851. }
  852. if (httpd_query_key_value(_query, "wbm", _valuechar, 30) == ESP_OK)
  853. {
  854. std::string _wbm = std::string(_valuechar);
  855. if (isStringNumeric(_wbm))
  856. {
  857. int _wbm_ = std::stoi(_valuechar);
  858. CFstatus.ImageWbMode = clipInt(_wbm_, 4, 0);
  859. }
  860. else
  861. {
  862. if (_wbm == "sunny") {
  863. CFstatus.ImageWbMode = 1;
  864. }
  865. else if (_wbm == "cloudy") {
  866. CFstatus.ImageWbMode = 2;
  867. }
  868. else if (_wbm == "office") {
  869. CFstatus.ImageWbMode = 3;
  870. }
  871. else if (_wbm == "home") {
  872. CFstatus.ImageWbMode = 4;
  873. }
  874. else {
  875. CFstatus.ImageWbMode = 0;
  876. }
  877. }
  878. }
  879. if (httpd_query_key_value(_query, "awb", _valuechar, 30) == ESP_OK)
  880. {
  881. std::string _awb = std::string(_valuechar);
  882. CFstatus.ImageAwb = alphanumericToBoolean(_awb);
  883. }
  884. if (httpd_query_key_value(_query, "awbg", _valuechar, 30) == ESP_OK)
  885. {
  886. std::string _awbg = std::string(_valuechar);
  887. CFstatus.ImageAwbGain = alphanumericToBoolean(_awbg);
  888. }
  889. if (httpd_query_key_value(_query, "aec", _valuechar, 30) == ESP_OK)
  890. {
  891. std::string _aec = std::string(_valuechar);
  892. CFstatus.ImageAec = alphanumericToBoolean(_aec);
  893. }
  894. if (httpd_query_key_value(_query, "aec2", _valuechar, 30) == ESP_OK)
  895. {
  896. std::string _aec2 = std::string(_valuechar);
  897. CFstatus.ImageAec2 = alphanumericToBoolean(_aec2);
  898. }
  899. if (httpd_query_key_value(_query, "ael", _valuechar, 30) == ESP_OK)
  900. {
  901. std::string _ael = std::string(_valuechar);
  902. if (isStringNumeric(_ael))
  903. {
  904. int _ael_ = std::stoi(_valuechar);
  905. if (CCstatus.CamSensor_id == OV2640_PID)
  906. {
  907. CFstatus.ImageAeLevel = clipInt(_ael_, 2, -2);
  908. }
  909. else
  910. {
  911. CFstatus.ImageAeLevel = clipInt(_ael_, 5, -5);
  912. }
  913. }
  914. }
  915. if (httpd_query_key_value(_query, "aecv", _valuechar, 30) == ESP_OK)
  916. {
  917. std::string _aecv = std::string(_valuechar);
  918. if (isStringNumeric(_aecv))
  919. {
  920. int _aecv_ = std::stoi(_valuechar);
  921. CFstatus.ImageAecValue = clipInt(_aecv_, 1200, 0);
  922. }
  923. }
  924. if (httpd_query_key_value(_query, "agc", _valuechar, 30) == ESP_OK)
  925. {
  926. std::string _agc = std::string(_valuechar);
  927. CFstatus.ImageAgc = alphanumericToBoolean(_agc);
  928. }
  929. if (httpd_query_key_value(_query, "agcg", _valuechar, 30) == ESP_OK)
  930. {
  931. std::string _agcg = std::string(_valuechar);
  932. if (isStringNumeric(_agcg))
  933. {
  934. int _agcg_ = std::stoi(_valuechar);
  935. CFstatus.ImageAgcGain = clipInt(_agcg_, 30, 0);
  936. }
  937. }
  938. if (httpd_query_key_value(_query, "bpc", _valuechar, 30) == ESP_OK)
  939. {
  940. std::string _bpc = std::string(_valuechar);
  941. CFstatus.ImageBpc = alphanumericToBoolean(_bpc);
  942. }
  943. if (httpd_query_key_value(_query, "wpc", _valuechar, 30) == ESP_OK)
  944. {
  945. std::string _wpc = std::string(_valuechar);
  946. CFstatus.ImageWpc = alphanumericToBoolean(_wpc);
  947. }
  948. if (httpd_query_key_value(_query, "rgma", _valuechar, 30) == ESP_OK)
  949. {
  950. std::string _rgma = std::string(_valuechar);
  951. CFstatus.ImageRawGma = alphanumericToBoolean(_rgma);
  952. }
  953. if (httpd_query_key_value(_query, "lenc", _valuechar, 30) == ESP_OK)
  954. {
  955. std::string _lenc = std::string(_valuechar);
  956. CFstatus.ImageLenc = alphanumericToBoolean(_lenc);
  957. }
  958. if (httpd_query_key_value(_query, "mirror", _valuechar, 30) == ESP_OK)
  959. {
  960. std::string _mirror = std::string(_valuechar);
  961. CFstatus.ImageHmirror = alphanumericToBoolean(_mirror);
  962. }
  963. if (httpd_query_key_value(_query, "flip", _valuechar, 30) == ESP_OK)
  964. {
  965. std::string _flip = std::string(_valuechar);
  966. CFstatus.ImageVflip = alphanumericToBoolean(_flip);
  967. }
  968. if (httpd_query_key_value(_query, "dcw", _valuechar, 30) == ESP_OK)
  969. {
  970. std::string _dcw = std::string(_valuechar);
  971. CFstatus.ImageDcw = alphanumericToBoolean(_dcw);
  972. }
  973. if (httpd_query_key_value(_query, "den", _valuechar, 30) == ESP_OK)
  974. {
  975. std::string _idlv = std::string(_valuechar);
  976. if (isStringNumeric(_idlv))
  977. {
  978. int _ImageDenoiseLevel = std::stoi(_valuechar);
  979. if (CCstatus.CamSensor_id == OV2640_PID)
  980. {
  981. CFstatus.ImageDenoiseLevel = 0;
  982. }
  983. else
  984. {
  985. CFstatus.ImageDenoiseLevel = clipInt(_ImageDenoiseLevel, 8, 0);
  986. }
  987. }
  988. }
  989. if (httpd_query_key_value(_query, "zoom", _valuechar, 30) == ESP_OK)
  990. {
  991. std::string _zoom = std::string(_valuechar);
  992. CFstatus.ImageZoomEnabled = alphanumericToBoolean(_zoom);
  993. }
  994. if (httpd_query_key_value(_query, "zoomx", _valuechar, 30) == ESP_OK)
  995. {
  996. std::string _zoomx = std::string(_valuechar);
  997. if (isStringNumeric(_zoomx))
  998. {
  999. int _ImageZoomOffsetX = std::stoi(_valuechar);
  1000. if (CCstatus.CamSensor_id == OV2640_PID)
  1001. {
  1002. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 480, -480);
  1003. }
  1004. else if (CCstatus.CamSensor_id == OV3660_PID)
  1005. {
  1006. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 704, -704);
  1007. }
  1008. else if (CCstatus.CamSensor_id == OV5640_PID)
  1009. {
  1010. CFstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 960, -960);
  1011. }
  1012. }
  1013. }
  1014. if (httpd_query_key_value(_query, "zoomy", _valuechar, 30) == ESP_OK)
  1015. {
  1016. std::string _zoomy = std::string(_valuechar);
  1017. if (isStringNumeric(_zoomy))
  1018. {
  1019. int _ImageZoomOffsetY = std::stoi(_valuechar);
  1020. if (CCstatus.CamSensor_id == OV2640_PID)
  1021. {
  1022. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 360, -360);
  1023. }
  1024. else if (CCstatus.CamSensor_id == OV3660_PID)
  1025. {
  1026. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 528, -528);
  1027. }
  1028. else if (CCstatus.CamSensor_id == OV5640_PID)
  1029. {
  1030. CFstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 720, -720);
  1031. }
  1032. }
  1033. }
  1034. if (httpd_query_key_value(_query, "zooms", _valuechar, 30) == ESP_OK)
  1035. {
  1036. std::string _zooms = std::string(_valuechar);
  1037. if (isStringNumeric(_zooms))
  1038. {
  1039. int _ImageZoomSize = std::stoi(_valuechar);
  1040. if (CCstatus.CamSensor_id == OV2640_PID)
  1041. {
  1042. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 29, 0);
  1043. }
  1044. else if (CCstatus.CamSensor_id == OV3660_PID)
  1045. {
  1046. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 43, 0);
  1047. }
  1048. else if (CCstatus.CamSensor_id == OV5640_PID)
  1049. {
  1050. CFstatus.ImageZoomSize = clipInt(_ImageZoomSize, 59, 0);
  1051. }
  1052. }
  1053. }
  1054. if (httpd_query_key_value(_query, "ledi", _valuechar, 30) == ESP_OK)
  1055. {
  1056. std::string _ledi = std::string(_valuechar);
  1057. if (isStringNumeric(_ledi))
  1058. {
  1059. int _ImageLedIntensity = std::stoi(_valuechar);
  1060. CFstatus.ImageLedIntensity = Camera.SetLEDIntensity(_ImageLedIntensity);
  1061. }
  1062. }
  1063. if (_task.compare("cam_settings") == 0)
  1064. {
  1065. // wird aufgerufen, wenn das Referenzbild + Kameraeinstellungen gespeichert wurden
  1066. setCFstatusToCCstatus(); // CFstatus >>> CCstatus
  1067. CFstatus.previewCapturePending = false;
  1068. // Kameraeinstellungen wurden verädert
  1069. CFstatus.changedCameraSettings = true;
  1070. ESP_LOGD(TAG, "Cam Settings set");
  1071. std::string _zw = "CamSettingsSet";
  1072. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1073. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1074. }
  1075. else
  1076. {
  1077. // wird aufgerufen, wenn ein neues Referenzbild erstellt oder aktualisiert wurde
  1078. // CFstatus >>> Kamera
  1079. if (setCFstatusToCam() != ESP_OK)
  1080. {
  1081. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Could not apply temporary camera settings");
  1082. return ESP_FAIL;
  1083. }
  1084. CFstatus.changedCameraSettings = true;
  1085. CFstatus.previewCapturePending = true;
  1086. ESP_LOGD(TAG, "test_take - vor TakeImage");
  1087. std::string image_temp = flowctrl.doSingleStep("[TakeImage]", _host);
  1088. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1089. httpd_resp_send(req, image_temp.c_str(), image_temp.length());
  1090. }
  1091. }
  1092. if (_task.compare("test_align") == 0)
  1093. {
  1094. std::string _host = "";
  1095. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK)
  1096. {
  1097. _host = std::string(_valuechar);
  1098. }
  1099. std::string zw = flowctrl.doSingleStep("[Alignment]", _host);
  1100. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1101. httpd_resp_send(req, zw.c_str(), zw.length());
  1102. }
  1103. }
  1104. else
  1105. {
  1106. std::string _zw = "DeviceIsBusy";
  1107. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1108. httpd_resp_send(req, _zw.c_str(), _zw.length());
  1109. }
  1110. #ifdef DEBUG_DETAIL_ON
  1111. LogFile.WriteHeapInfo("handler_editflow - Done");
  1112. #endif
  1113. return ESP_OK;
  1114. }
  1115. esp_err_t handler_statusflow(httpd_req_t *req)
  1116. {
  1117. #ifdef DEBUG_DETAIL_ON
  1118. LogFile.WriteHeapInfo("handler_statusflow - Start");
  1119. #endif
  1120. const char *resp_str;
  1121. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1122. if (bTaskAutoFlowCreated)
  1123. {
  1124. #ifdef DEBUG_DETAIL_ON
  1125. ESP_LOGD(TAG, "handler_statusflow: %s", req->uri);
  1126. #endif
  1127. std::string zw = flowctrl.getActStatusWithTime();
  1128. resp_str = zw.c_str();
  1129. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1130. }
  1131. else
  1132. {
  1133. resp_str = "Flow task not yet created";
  1134. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  1135. }
  1136. #ifdef DEBUG_DETAIL_ON
  1137. LogFile.WriteHeapInfo("handler_statusflow - Done");
  1138. #endif
  1139. return ESP_OK;
  1140. }
  1141. esp_err_t handler_cputemp(httpd_req_t *req)
  1142. {
  1143. #ifdef DEBUG_DETAIL_ON
  1144. LogFile.WriteHeapInfo("handler_cputemp - Start");
  1145. #endif
  1146. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1147. httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
  1148. #ifdef DEBUG_DETAIL_ON
  1149. LogFile.WriteHeapInfo("handler_cputemp - End");
  1150. #endif
  1151. return ESP_OK;
  1152. }
  1153. esp_err_t handler_rssi(httpd_req_t *req)
  1154. {
  1155. #ifdef DEBUG_DETAIL_ON
  1156. LogFile.WriteHeapInfo("handler_rssi - Start");
  1157. #endif
  1158. if (getWIFIisConnected())
  1159. {
  1160. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1161. httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
  1162. }
  1163. else
  1164. {
  1165. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  1166. return ESP_ERR_NOT_FOUND;
  1167. }
  1168. #ifdef DEBUG_DETAIL_ON
  1169. LogFile.WriteHeapInfo("handler_rssi - End");
  1170. #endif
  1171. return ESP_OK;
  1172. }
  1173. esp_err_t handler_current_date(httpd_req_t *req)
  1174. {
  1175. #ifdef DEBUG_DETAIL_ON
  1176. LogFile.WriteHeapInfo("handler_uptime - Start");
  1177. #endif
  1178. std::string formatedDateAndTime = getCurrentTimeString("%Y-%m-%d %H:%M:%S");
  1179. // std::string formatedDate = getCurrentTimeString("%Y-%m-%d");
  1180. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1181. httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
  1182. #ifdef DEBUG_DETAIL_ON
  1183. LogFile.WriteHeapInfo("handler_uptime - End");
  1184. #endif
  1185. return ESP_OK;
  1186. }
  1187. esp_err_t handler_uptime(httpd_req_t *req)
  1188. {
  1189. #ifdef DEBUG_DETAIL_ON
  1190. LogFile.WriteHeapInfo("handler_uptime - Start");
  1191. #endif
  1192. std::string formatedUptime = getFormatedUptime(false);
  1193. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1194. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  1195. #ifdef DEBUG_DETAIL_ON
  1196. LogFile.WriteHeapInfo("handler_uptime - End");
  1197. #endif
  1198. return ESP_OK;
  1199. }
  1200. esp_err_t handler_prevalue(httpd_req_t *req)
  1201. {
  1202. #ifdef DEBUG_DETAIL_ON
  1203. LogFile.WriteHeapInfo("handler_prevalue - Start");
  1204. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  1205. #endif
  1206. // Default usage message when handler gets called without any parameter
  1207. const std::string RESTUsageInfo =
  1208. "00: Handler usage:<br>"
  1209. "- To retrieve actual PreValue, please provide only a numbersname, e.g. /setPreValue?numbers=main<br>"
  1210. "- To set PreValue to a new value, please provide a numbersname and a value, e.g. /setPreValue?numbers=main&value=1234.5678<br>"
  1211. "NOTE:<br>"
  1212. "value >= 0.0: Set PreValue to provided value<br>"
  1213. "value < 0.0: Set PreValue to actual RAW value (as long RAW value is a valid number, without N)";
  1214. // Default return error message when no return is programmed
  1215. std::string sReturnMessage = "E90: Uninitialized";
  1216. char _query[100] = ""; // stays empty when the request has no query string
  1217. char _numbersname[50] = "default";
  1218. char _value[20] = "";
  1219. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  1220. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  1221. {
  1222. #ifdef DEBUG_DETAIL_ON
  1223. ESP_LOGD(TAG, "Query: %s", _query);
  1224. #endif
  1225. if (httpd_query_key_value(_query, "numbers", _numbersname, 50) != ESP_OK)
  1226. {
  1227. // If request is incomplete
  1228. sReturnMessage = "E91: Query parameter incomplete or not valid!<br> "
  1229. "Call /setPreValue to show REST API usage info and/or check documentation";
  1230. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1231. return ESP_FAIL;
  1232. }
  1233. if (httpd_query_key_value(_query, "value", _value, 20) == ESP_OK)
  1234. {
  1235. #ifdef DEBUG_DETAIL_ON
  1236. ESP_LOGD(TAG, "Value: %s", _value);
  1237. #endif
  1238. }
  1239. }
  1240. else
  1241. {
  1242. // if no parameter is provided, print handler usage
  1243. httpd_resp_send(req, RESTUsageInfo.c_str(), RESTUsageInfo.length());
  1244. return ESP_OK;
  1245. }
  1246. if (strlen(_value) == 0)
  1247. {
  1248. // If no value is povided --> return actual PreValue
  1249. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1250. if (sReturnMessage.empty())
  1251. {
  1252. sReturnMessage = "E92: Numbers name not found";
  1253. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1254. return ESP_FAIL;
  1255. }
  1256. }
  1257. else
  1258. {
  1259. // New value is positive: Set PreValue to provided value and return value
  1260. // New value is negative and actual RAW value is a valid number: Set PreValue to RAW value and return value
  1261. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "REST API handler_prevalue called: numbersname: " + std::string(_numbersname) + ", value: " + std::string(_value));
  1262. if (!flowctrl.UpdatePrevalue(_value, _numbersname, true))
  1263. {
  1264. sReturnMessage = "E93: Update request rejected. Please check device logs for more details";
  1265. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1266. return ESP_FAIL;
  1267. }
  1268. sReturnMessage = flowctrl.GetPrevalue(std::string(_numbersname));
  1269. if (sReturnMessage.empty())
  1270. {
  1271. sReturnMessage = "E94: Numbers name not found";
  1272. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1273. return ESP_FAIL;
  1274. }
  1275. }
  1276. httpd_resp_send(req, sReturnMessage.c_str(), sReturnMessage.length());
  1277. #ifdef DEBUG_DETAIL_ON
  1278. LogFile.WriteHeapInfo("handler_prevalue - End");
  1279. #endif
  1280. return ESP_OK;
  1281. }
  1282. void task_autodoFlow(void *pvParameter)
  1283. {
  1284. int64_t fr_start, fr_delta_ms;
  1285. bTaskAutoFlowCreated = true;
  1286. if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
  1287. {
  1288. flowctrl.setActStatus("Initialization (delayed)");
  1289. // #ifdef ENABLE_MQTT
  1290. // MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
  1291. // #endif //ENABLE_MQTT
  1292. vTaskDelay(60 * 5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
  1293. }
  1294. ESP_LOGD(TAG, "task_autodoFlow: start");
  1295. doInit();
  1296. flowctrl.setAutoStartInterval(auto_interval);
  1297. autostartIsEnabled = flowctrl.getIsAutoStart();
  1298. if (isSetupModusActive())
  1299. {
  1300. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "We are in Setup Mode -> Not starting Auto Flow!");
  1301. autostartIsEnabled = false;
  1302. // 15.7.0 Setup Wizard cannot take a Reference Picture #2953
  1303. // std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  1304. // flowctrl.doFlowTakeImageOnly(zw_time);
  1305. }
  1306. if (autostartIsEnabled)
  1307. {
  1308. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting Flow...");
  1309. }
  1310. else
  1311. {
  1312. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Autostart is not enabled -> Not starting Flow");
  1313. }
  1314. while (autostartIsEnabled)
  1315. {
  1316. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  1317. time_t roundStartTime = getUpTime();
  1318. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  1319. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  1320. fr_start = esp_timer_get_time();
  1321. if (flowisrunning)
  1322. {
  1323. #ifdef DEBUG_DETAIL_ON
  1324. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  1325. #endif
  1326. }
  1327. else
  1328. {
  1329. #ifdef DEBUG_DETAIL_ON
  1330. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  1331. #endif
  1332. flowisrunning = true;
  1333. doflow();
  1334. #ifdef DEBUG_DETAIL_ON
  1335. ESP_LOGD(TAG, "Remove older log files");
  1336. #endif
  1337. LogFile.RemoveOldLogFile();
  1338. LogFile.RemoveOldDataLog();
  1339. }
  1340. // Round finished -> Logfile
  1341. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) + " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  1342. // CPU Temp -> Logfile
  1343. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
  1344. // WIFI Signal Strength (RSSI) -> Logfile
  1345. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
  1346. // Check if time is synchronized (if NTP is configured)
  1347. if (getUseNtp() && !getTimeIsSet())
  1348. {
  1349. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set!");
  1350. StatusLED(TIME_CHECK, 1, false);
  1351. }
  1352. #if (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES)
  1353. wifiRoamingQuery();
  1354. #endif
  1355. // Scan channels and check if an AP with better RSSI is available, then disconnect and try to reconnect to AP with better RSSI
  1356. // NOTE: Keep this direct before the following task delay, because scan is done in blocking mode and this takes ca. 1,5 - 2s.
  1357. #ifdef WLAN_USE_ROAMING_BY_SCANNING
  1358. wifiRoamByScanning();
  1359. #endif
  1360. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  1361. if (auto_interval > fr_delta_ms)
  1362. {
  1363. const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
  1364. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long)xDelay);
  1365. vTaskDelay(xDelay);
  1366. }
  1367. }
  1368. while (1)
  1369. {
  1370. // Keep flow task running to handle necessary sub tasks like reboot handler, etc..
  1371. vTaskDelay(2000 / portTICK_PERIOD_MS);
  1372. }
  1373. vTaskDelete(NULL); // Delete this task if it exits from the loop above
  1374. xHandletask_autodoFlow = NULL;
  1375. ESP_LOGD(TAG, "task_autodoFlow: end");
  1376. }
  1377. void InitializeFlowTask(void)
  1378. {
  1379. BaseType_t xReturned;
  1380. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1381. uint32_t stackSize = 16 * 1024;
  1382. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", stackSize, NULL, tskIDLE_PRIORITY + 2, &xHandletask_autodoFlow, 0);
  1383. if (xReturned != pdPASS)
  1384. {
  1385. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Creation task_autodoFlow failed. Requested stack size:" + std::to_string(stackSize));
  1386. LogFile.WriteHeapInfo("Creation task_autodoFlow failed");
  1387. }
  1388. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  1389. }
  1390. void register_server_main_flow_task_uri(httpd_handle_t server)
  1391. {
  1392. ESP_LOGI(TAG, "server_main_flow_task - Registering URI handlers");
  1393. httpd_uri_t camuri = {};
  1394. camuri.method = HTTP_GET;
  1395. camuri.uri = "/doinit";
  1396. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_init);
  1397. camuri.user_ctx = (void *)"Light On";
  1398. httpd_register_uri_handler(server, &camuri);
  1399. // Legacy API => New: "/setPreValue"
  1400. camuri.uri = "/setPreValue.html";
  1401. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1402. camuri.user_ctx = (void *)"Prevalue";
  1403. httpd_register_uri_handler(server, &camuri);
  1404. camuri.uri = "/setPreValue";
  1405. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_prevalue);
  1406. camuri.user_ctx = (void *)"Prevalue";
  1407. httpd_register_uri_handler(server, &camuri);
  1408. camuri.uri = "/flow_start";
  1409. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_flow_start);
  1410. camuri.user_ctx = (void *)"Flow Start";
  1411. httpd_register_uri_handler(server, &camuri);
  1412. camuri.uri = "/statusflow.html";
  1413. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1414. camuri.user_ctx = (void *)"Light Off";
  1415. httpd_register_uri_handler(server, &camuri);
  1416. camuri.uri = "/statusflow";
  1417. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_statusflow);
  1418. camuri.user_ctx = (void *)"Light Off";
  1419. httpd_register_uri_handler(server, &camuri);
  1420. // Legacy API => New: "/cpu_temperature"
  1421. camuri.uri = "/cputemp.html";
  1422. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1423. camuri.user_ctx = (void *)"Light Off";
  1424. httpd_register_uri_handler(server, &camuri);
  1425. camuri.uri = "/cpu_temperature";
  1426. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_cputemp);
  1427. camuri.user_ctx = (void *)"Light Off";
  1428. httpd_register_uri_handler(server, &camuri);
  1429. // Legacy API => New: "/rssi"
  1430. camuri.uri = "/rssi.html";
  1431. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1432. camuri.user_ctx = (void *)"Light Off";
  1433. httpd_register_uri_handler(server, &camuri);
  1434. camuri.uri = "/rssi";
  1435. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_rssi);
  1436. camuri.user_ctx = (void *)"Light Off";
  1437. httpd_register_uri_handler(server, &camuri);
  1438. camuri.uri = "/date";
  1439. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_current_date);
  1440. camuri.user_ctx = (void *)"Light Off";
  1441. httpd_register_uri_handler(server, &camuri);
  1442. camuri.uri = "/uptime";
  1443. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_uptime);
  1444. camuri.user_ctx = (void *)"Light Off";
  1445. httpd_register_uri_handler(server, &camuri);
  1446. camuri.uri = "/editflow";
  1447. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_editflow);
  1448. camuri.user_ctx = (void *)"EditFlow";
  1449. httpd_register_uri_handler(server, &camuri);
  1450. // Legacy API => New: "/value"
  1451. camuri.uri = "/value.html";
  1452. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1453. camuri.user_ctx = (void *)"Value";
  1454. httpd_register_uri_handler(server, &camuri);
  1455. camuri.uri = "/value";
  1456. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1457. camuri.user_ctx = (void *)"Value";
  1458. httpd_register_uri_handler(server, &camuri);
  1459. // Legacy API => New: "/value"
  1460. camuri.uri = "/wasserzaehler.html";
  1461. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_wasserzaehler);
  1462. camuri.user_ctx = (void *)"Wasserzaehler";
  1463. httpd_register_uri_handler(server, &camuri);
  1464. camuri.uri = "/json";
  1465. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_json);
  1466. camuri.user_ctx = (void *)"JSON";
  1467. httpd_register_uri_handler(server, &camuri);
  1468. camuri.uri = "/heap";
  1469. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_get_heap);
  1470. camuri.user_ctx = (void *)"Heap";
  1471. httpd_register_uri_handler(server, &camuri);
  1472. camuri.uri = "/stream";
  1473. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_stream);
  1474. camuri.user_ctx = (void *)"stream";
  1475. httpd_register_uri_handler(server, &camuri);
  1476. /** will handle metrics requests */
  1477. camuri.uri = "/metrics";
  1478. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_openmetrics);
  1479. camuri.user_ctx = (void *)"metrics";
  1480. httpd_register_uri_handler(server, &camuri);
  1481. /** when adding a new handler, make sure to increment the value for config.max_uri_handlers in `main/server_main.cpp` */
  1482. }