ClassFlowControll.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. #include "defines.h"
  2. #include "ClassFlowControll.h"
  3. #include "connect_wifi_sta.h"
  4. #include "read_network_config.h"
  5. #include "freertos/task.h"
  6. #include <sys/stat.h>
  7. #include <dirent.h>
  8. #include "ClassLogFile.h"
  9. #include "time_sntp.h"
  10. #include "Helper.h"
  11. #include "server_ota.h"
  12. #include "interface_mqtt.h"
  13. #include "server_mqtt.h"
  14. #include "server_help.h"
  15. #include "MainFlowControl.h"
  16. #include "basic_auth.h"
  17. static const char *TAG = "FLOWCTRL";
  18. std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host)
  19. {
  20. std::string _classname = "";
  21. std::string result = "";
  22. ESP_LOGD(TAG, "Step %s start", _stepname.c_str());
  23. if ((_stepname.compare("[TakeImage]") == 0) || (_stepname.compare(";[TakeImage]") == 0))
  24. {
  25. _classname = "ClassFlowTakeImage";
  26. }
  27. else if ((_stepname.compare("[Alignment]") == 0) || (_stepname.compare(";[Alignment]") == 0))
  28. {
  29. _classname = "ClassFlowAlignment";
  30. }
  31. else if ((_stepname.compare(0, 7, "[Digits") == 0) || (_stepname.compare(0, 8, ";[Digits") == 0))
  32. {
  33. _classname = "ClassFlowCNNGeneral";
  34. }
  35. else if ((_stepname.compare("[Analog]") == 0) || (_stepname.compare(";[Analog]") == 0))
  36. {
  37. _classname = "ClassFlowCNNGeneral";
  38. }
  39. else if ((_stepname.compare("[MQTT]") == 0) || (_stepname.compare(";[MQTT]") == 0))
  40. {
  41. _classname = "ClassFlowMQTT";
  42. }
  43. else if ((_stepname.compare("[InfluxDB]") == 0) || (_stepname.compare(";[InfluxDB]") == 0))
  44. {
  45. _classname = "ClassFlowInfluxDB";
  46. }
  47. else if ((_stepname.compare("[InfluxDBv2]") == 0) || (_stepname.compare(";[InfluxDBv2]") == 0))
  48. {
  49. _classname = "ClassFlowInfluxDBv2";
  50. }
  51. else if ((_stepname.compare("[Webhook]") == 0) || (_stepname.compare(";[Webhook]") == 0))
  52. {
  53. _classname = "ClassFlowWebhook";
  54. }
  55. for (int i = 0; i < FlowControll.size(); ++i)
  56. {
  57. if (FlowControll[i]->name().compare(_classname) == 0)
  58. {
  59. if (!(FlowControll[i]->name().compare("ClassFlowTakeImage") == 0))
  60. {
  61. // if it is a TakeImage, the image does not need to be included, this happens automatically with the html query.
  62. FlowControll[i]->doFlow("");
  63. }
  64. result = FlowControll[i]->getHTMLSingleStep(_host);
  65. }
  66. }
  67. ESP_LOGD(TAG, "Step %s end", _stepname.c_str());
  68. return result;
  69. }
  70. std::string ClassFlowControll::TranslateAktstatus(std::string _input)
  71. {
  72. if (_input.compare("ClassFlowTakeImage") == 0)
  73. {
  74. return ("Take Image");
  75. }
  76. else if (_input.compare("ClassFlowAlignment") == 0)
  77. {
  78. return ("Aligning");
  79. }
  80. else if (_input.compare("ClassFlowCNNGeneral") == 0)
  81. {
  82. return ("Digitization of ROIs");
  83. }
  84. else if (_input.compare("ClassFlowMQTT") == 0)
  85. {
  86. return ("Sending MQTT");
  87. }
  88. else if (_input.compare("ClassFlowInfluxDB") == 0)
  89. {
  90. return ("Sending InfluxDB");
  91. }
  92. else if (_input.compare("ClassFlowInfluxDBv2") == 0)
  93. {
  94. return ("Sending InfluxDBv2");
  95. }
  96. else if (_input.compare("ClassFlowWebhook") == 0)
  97. {
  98. return ("Sending Webhook");
  99. }
  100. else if (_input.compare("ClassFlowPostProcessing") == 0)
  101. {
  102. return ("Post-Processing");
  103. }
  104. return "Unkown Status";
  105. }
  106. std::vector<HTMLInfo *> ClassFlowControll::GetAllDigit()
  107. {
  108. if (flowdigit)
  109. {
  110. ESP_LOGD(TAG, "ClassFlowControll::GetAllDigit - flowdigit != NULL");
  111. return flowdigit->GetHTMLInfo();
  112. }
  113. std::vector<HTMLInfo *> empty;
  114. return empty;
  115. }
  116. std::vector<HTMLInfo *> ClassFlowControll::GetAllAnalog()
  117. {
  118. if (flowanalog)
  119. {
  120. return flowanalog->GetHTMLInfo();
  121. }
  122. std::vector<HTMLInfo *> empty;
  123. return empty;
  124. }
  125. t_CNNType ClassFlowControll::GetTypeDigit()
  126. {
  127. if (flowdigit)
  128. {
  129. return flowdigit->getCNNType();
  130. }
  131. return t_CNNType::None;
  132. }
  133. t_CNNType ClassFlowControll::GetTypeAnalog()
  134. {
  135. if (flowanalog)
  136. {
  137. return flowanalog->getCNNType();
  138. }
  139. return t_CNNType::None;
  140. }
  141. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  142. void ClassFlowControll::DigitDrawROI(CImageBasis *TempImage)
  143. {
  144. if (flowdigit)
  145. {
  146. flowdigit->DrawROI(TempImage);
  147. }
  148. }
  149. void ClassFlowControll::AnalogDrawROI(CImageBasis *TempImage)
  150. {
  151. if (flowanalog)
  152. {
  153. flowanalog->DrawROI(TempImage);
  154. }
  155. }
  156. #endif
  157. bool ClassFlowControll::StartMQTTService()
  158. {
  159. /* Start the MQTT service */
  160. for (int i = 0; i < FlowControll.size(); ++i)
  161. {
  162. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0)
  163. {
  164. return ((ClassFlowMQTT *)(FlowControll[i]))->Start(AutoInterval);
  165. }
  166. }
  167. return false;
  168. }
  169. void ClassFlowControll::SetInitialParameter(void)
  170. {
  171. AutoStart = true;
  172. SetupModeActive = false;
  173. AutoInterval = 10; // Minutes
  174. flowdigit = NULL;
  175. flowanalog = NULL;
  176. flowpostprocessing = NULL;
  177. aktRunNr = 0;
  178. aktstatus = "Flow task not yet created";
  179. aktstatusWithTime = aktstatus;
  180. disabled = false;
  181. }
  182. bool ClassFlowControll::getIsAutoStart(void)
  183. {
  184. // return AutoStart;
  185. return true; // Flow must always be enabled, else the manual trigger (REST, MQTT) will not work!
  186. }
  187. void ClassFlowControll::setAutoStartInterval(long &_interval)
  188. {
  189. _interval = AutoInterval * 60 * 1000; // AutoInterval: minutes -> ms
  190. }
  191. ClassFlow *ClassFlowControll::CreateClassFlow(std::string _type)
  192. {
  193. ClassFlow *cfc = NULL;
  194. _type = to_upper(trim_string_left_right(_type));
  195. if (_type.compare("[TAKEIMAGE]") == 0)
  196. {
  197. cfc = new ClassFlowTakeImage(&FlowControll);
  198. flowtakeimage = (ClassFlowTakeImage *)cfc;
  199. }
  200. else if (_type.compare("[ALIGNMENT]") == 0)
  201. {
  202. cfc = new ClassFlowAlignment(&FlowControll);
  203. flowalignment = (ClassFlowAlignment *)cfc;
  204. }
  205. else if (_type.compare("[ANALOG]") == 0)
  206. {
  207. cfc = new ClassFlowCNNGeneral(flowalignment);
  208. flowanalog = (ClassFlowCNNGeneral *)cfc;
  209. }
  210. else if (_type.compare(0, 7, "[DIGITS") == 0)
  211. {
  212. cfc = new ClassFlowCNNGeneral(flowalignment);
  213. flowdigit = (ClassFlowCNNGeneral *)cfc;
  214. }
  215. else if (_type.compare("[MQTT]") == 0)
  216. {
  217. cfc = new ClassFlowMQTT(&FlowControll);
  218. }
  219. else if (_type.compare("[INFLUXDB]") == 0)
  220. {
  221. cfc = new ClassFlowInfluxDB(&FlowControll);
  222. }
  223. else if (_type.compare("[INFLUXDBV2]") == 0)
  224. {
  225. cfc = new ClassFlowInfluxDBv2(&FlowControll);
  226. }
  227. else if (_type.compare("[WEBHOOK]") == 0)
  228. {
  229. cfc = new ClassFlowWebhook(&FlowControll);
  230. }
  231. else if (_type.compare("[POSTPROCESSING]") == 0)
  232. {
  233. cfc = new ClassFlowPostProcessing(&FlowControll, flowanalog, flowdigit);
  234. flowpostprocessing = (ClassFlowPostProcessing *)cfc;
  235. }
  236. if (cfc)
  237. {
  238. // Attached only if it is not [AutoTimer], because this is for FlowControll
  239. FlowControll.push_back(cfc);
  240. }
  241. if (_type.compare("[AUTOTIMER]") == 0)
  242. {
  243. cfc = this;
  244. }
  245. else if (_type.compare("[DATALOGGING]") == 0)
  246. {
  247. cfc = this;
  248. }
  249. else if (_type.compare("[DEBUG]") == 0)
  250. {
  251. cfc = this;
  252. }
  253. else if (_type.compare("[SYSTEM]") == 0)
  254. {
  255. cfc = this;
  256. }
  257. return cfc;
  258. }
  259. void ClassFlowControll::InitFlow(std::string config)
  260. {
  261. aktstatus = "Initialization";
  262. aktstatusWithTime = aktstatus;
  263. flowpostprocessing = NULL;
  264. ClassFlow *cfc;
  265. config = format_filename(config);
  266. FILE *pFile = fopen(config.c_str(), "r");
  267. std::string line = "";
  268. char temp_char[1024];
  269. if (pFile != NULL)
  270. {
  271. fgets(temp_char, 1024, pFile);
  272. ESP_LOGD(TAG, "%s", temp_char);
  273. line = std::string(temp_char);
  274. }
  275. while ((line.size() > 0) && !(feof(pFile)))
  276. {
  277. cfc = CreateClassFlow(line);
  278. if (cfc)
  279. {
  280. ESP_LOGE(TAG, "Start ReadParameter (%s)", line.c_str());
  281. cfc->ReadParameter(pFile, line);
  282. }
  283. else
  284. {
  285. line = "";
  286. if (fgets(temp_char, 1024, pFile) && !feof(pFile))
  287. {
  288. ESP_LOGD(TAG, "Read: %s", temp_char);
  289. line = std::string(temp_char);
  290. }
  291. }
  292. }
  293. fclose(pFile);
  294. }
  295. std::string *ClassFlowControll::getActStatusWithTime()
  296. {
  297. return &aktstatusWithTime;
  298. }
  299. std::string *ClassFlowControll::getActStatus()
  300. {
  301. return &aktstatus;
  302. }
  303. void ClassFlowControll::setActStatus(std::string _aktstatus)
  304. {
  305. aktstatus = _aktstatus;
  306. aktstatusWithTime = aktstatus;
  307. }
  308. void ClassFlowControll::doFlowTakeImageOnly(std::string time)
  309. {
  310. std::string temp_time;
  311. for (int i = 0; i < FlowControll.size(); ++i)
  312. {
  313. if (FlowControll[i]->name() == "ClassFlowTakeImage")
  314. {
  315. temp_time = getCurrentTimeString("%H:%M:%S");
  316. aktstatus = TranslateAktstatus(FlowControll[i]->name());
  317. aktstatusWithTime = aktstatus + " (" + temp_time + ")";
  318. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, 1, false);
  319. FlowControll[i]->doFlow(time);
  320. }
  321. }
  322. }
  323. bool ClassFlowControll::doFlow(std::string time)
  324. {
  325. bool result = true;
  326. std::string temp_time;
  327. int repeat = 0;
  328. int qos = 1;
  329. for (int i = 0; i < FlowControll.size(); ++i)
  330. {
  331. temp_time = getCurrentTimeString("%H:%M:%S");
  332. aktstatus = TranslateAktstatus(FlowControll[i]->name());
  333. aktstatusWithTime = aktstatus + " (" + temp_time + ")";
  334. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Status: " + aktstatusWithTime);
  335. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
  336. if (!FlowControll[i]->doFlow(time))
  337. {
  338. repeat++;
  339. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Fehler beim " + aktstatus + " Schritt - wird zum " + std::to_string(repeat) + ". Mal wiederholt");
  340. if (i)
  341. {
  342. i -= 1;
  343. } // vPrevious step must be repeated (probably take pictures)
  344. result = false;
  345. if (repeat > 5)
  346. {
  347. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Wiederholung 5x nicht erfolgreich --> reboot");
  348. doReboot();
  349. // Step was repeated 5x --> reboot
  350. }
  351. }
  352. else
  353. {
  354. result = true;
  355. }
  356. }
  357. temp_time = getCurrentTimeString("%H:%M:%S");
  358. aktstatus = "Flow finished";
  359. aktstatusWithTime = aktstatus + " (" + temp_time + ")";
  360. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatusWithTime);
  361. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
  362. return result;
  363. }
  364. std::string ClassFlowControll::getReadoutAll(int _type)
  365. {
  366. std::string out = "";
  367. if (flowpostprocessing)
  368. {
  369. std::vector<NumberPost *> *numbers = flowpostprocessing->GetNumbers();
  370. for (int i = 0; i < (*numbers).size(); ++i)
  371. {
  372. out = out + (*numbers)[i]->name + "\t";
  373. switch (_type)
  374. {
  375. case READOUT_TYPE_VALUE:
  376. out = out + (*numbers)[i]->ReturnValue;
  377. break;
  378. case READOUT_TYPE_PREVALUE:
  379. if (flowpostprocessing->PreValueUse)
  380. {
  381. if ((*numbers)[i]->PreValueValid)
  382. {
  383. out = out + (*numbers)[i]->ReturnPreValue;
  384. }
  385. else
  386. {
  387. out = out + "PreValue too old";
  388. }
  389. }
  390. else
  391. {
  392. out = out + "PreValue deactivated";
  393. }
  394. break;
  395. case READOUT_TYPE_RAWVALUE:
  396. out = out + (*numbers)[i]->ReturnRawValue;
  397. break;
  398. case READOUT_TYPE_ERROR:
  399. out = out + (*numbers)[i]->ErrorMessageText;
  400. break;
  401. }
  402. if (i < (*numbers).size() - 1)
  403. {
  404. out = out + "\r\n";
  405. }
  406. }
  407. // ESP_LOGD(TAG, "OUT: %s", out.c_str());
  408. }
  409. return out;
  410. }
  411. std::string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false, int _number = 0)
  412. {
  413. if (flowpostprocessing)
  414. {
  415. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror, _number);
  416. }
  417. return std::string("");
  418. }
  419. std::string ClassFlowControll::GetPrevalue(std::string _number)
  420. {
  421. if (flowpostprocessing)
  422. {
  423. return flowpostprocessing->GetPreValue(_number);
  424. }
  425. return std::string("");
  426. }
  427. bool ClassFlowControll::UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern)
  428. {
  429. double newvalueAsDouble;
  430. char *p;
  431. _newvalue = trim_string_left_right(_newvalue);
  432. // ESP_LOGD(TAG, "Input UpdatePreValue: %s", _newvalue.c_str());
  433. if (_newvalue.substr(0, 8).compare("0.000000") == 0 || _newvalue.compare("0.0") == 0 || _newvalue.compare("0") == 0)
  434. {
  435. newvalueAsDouble = 0; // preset to value = 0
  436. }
  437. else
  438. {
  439. newvalueAsDouble = strtod(_newvalue.c_str(), &p);
  440. if (newvalueAsDouble == 0)
  441. {
  442. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "UpdatePrevalue: No valid value for processing: " + _newvalue);
  443. return false;
  444. }
  445. }
  446. if (flowpostprocessing)
  447. {
  448. if (flowpostprocessing->SetPreValue(newvalueAsDouble, _numbers, _extern))
  449. {
  450. return true;
  451. }
  452. else
  453. {
  454. return false;
  455. }
  456. }
  457. else
  458. {
  459. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "UpdatePrevalue: ERROR - Class Post-Processing not initialized");
  460. return false;
  461. }
  462. }
  463. bool ClassFlowControll::ReadParameter(FILE *pFile, std::string &aktparamgraph)
  464. {
  465. aktparamgraph = trim_string_left_right(aktparamgraph);
  466. if (aktparamgraph.size() == 0)
  467. {
  468. if (!GetNextParagraph(pFile, aktparamgraph))
  469. {
  470. return false;
  471. }
  472. }
  473. if ((to_upper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (to_upper(aktparamgraph).compare("[DEBUG]") != 0) &&
  474. (to_upper(aktparamgraph).compare("[SYSTEM]") != 0 && (to_upper(aktparamgraph).compare("[DATALOGGING]") != 0)))
  475. {
  476. // Paragraph passt nicht zu Debug oder DataLogging
  477. return false;
  478. }
  479. std::vector<std::string> splitted;
  480. while (getNextLine(pFile, &aktparamgraph) && !isNewParagraph(aktparamgraph))
  481. {
  482. splitted = split_line(aktparamgraph);
  483. if (splitted.size() > 1)
  484. {
  485. std::string _param = to_upper(splitted[0]);
  486. if (_param == "INTERVAL")
  487. {
  488. if (is_string_numeric(splitted[1]))
  489. {
  490. AutoInterval = std::stof(splitted[1]);
  491. }
  492. }
  493. else if (_param == "DATALOGACTIVE")
  494. {
  495. LogFile.SetDataLogToSD(alphanumeric_to_boolean(splitted[1]));
  496. }
  497. else if (_param == "DATAFILESRETENTION")
  498. {
  499. if (is_string_numeric(splitted[1]))
  500. {
  501. LogFile.SetDataLogRetention(std::stoi(splitted[1]));
  502. }
  503. }
  504. else if (_param == "LOGLEVEL")
  505. {
  506. /* matches esp_log_level_t */
  507. if ((to_upper(splitted[1]) == "TRUE") || (to_upper(splitted[1]) == "2"))
  508. {
  509. LogFile.setLogLevel(ESP_LOG_WARN);
  510. }
  511. else if ((to_upper(splitted[1]) == "FALSE") || (to_upper(splitted[1]) == "0") || (to_upper(splitted[1]) == "1"))
  512. {
  513. LogFile.setLogLevel(ESP_LOG_ERROR);
  514. }
  515. else if (to_upper(splitted[1]) == "3")
  516. {
  517. LogFile.setLogLevel(ESP_LOG_INFO);
  518. }
  519. else if (to_upper(splitted[1]) == "4")
  520. {
  521. LogFile.setLogLevel(ESP_LOG_DEBUG);
  522. }
  523. /* If system reboot was not triggered by user and reboot was caused by execption -> keep log level to DEBUG */
  524. if (!getIsPlannedReboot() && (esp_reset_reason() == ESP_RST_PANIC))
  525. {
  526. LogFile.setLogLevel(ESP_LOG_DEBUG);
  527. }
  528. }
  529. else if (_param == "LOGFILESRETENTION")
  530. {
  531. if (is_string_numeric(splitted[1]))
  532. {
  533. LogFile.SetLogFileRetention(std::stoi(splitted[1]));
  534. }
  535. }
  536. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  537. else if (_param == "RSSITHRESHOLD")
  538. {
  539. int RSSIThresholdTMP = atoi(splitted[1].c_str());
  540. RSSIThresholdTMP = min(0, max(-100, RSSIThresholdTMP)); // Verify input limits (-100 - 0)
  541. if (ChangeRSSIThreshold(NETWORK_CONFIG_FILE, RSSIThresholdTMP))
  542. {
  543. // reboot necessary so that the new wlan.ini is also used !!!
  544. fclose(pFile);
  545. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new RSSITHRESHOLD ...");
  546. doReboot();
  547. }
  548. }
  549. #endif
  550. else if (_param == "HOSTNAME")
  551. {
  552. if (ChangeHostName(NETWORK_CONFIG_FILE, splitted[1]))
  553. {
  554. // reboot necessary so that the new wlan.ini is also used !!!
  555. fclose(pFile);
  556. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new HOSTNAME...");
  557. doReboot();
  558. }
  559. }
  560. else if (_param == "SETUPMODE")
  561. {
  562. SetupModeActive = alphanumeric_to_boolean(splitted[1]);
  563. }
  564. }
  565. }
  566. return true;
  567. }
  568. int ClassFlowControll::CleanTempFolder()
  569. {
  570. const char *folderPath = "/sdcard/img_tmp";
  571. ESP_LOGD(TAG, "Clean up temporary folder to avoid damage of sdcard sectors: %s", folderPath);
  572. DIR *dir = opendir(folderPath);
  573. if (!dir)
  574. {
  575. ESP_LOGE(TAG, "Failed to stat dir: %s", folderPath);
  576. return -1;
  577. }
  578. struct dirent *entry;
  579. int deleted = 0;
  580. while ((entry = readdir(dir)) != NULL)
  581. {
  582. std::string path = std::string(folderPath) + "/" + entry->d_name;
  583. if (entry->d_type == DT_REG)
  584. {
  585. if (unlink(path.c_str()) == 0)
  586. {
  587. deleted++;
  588. }
  589. else
  590. {
  591. ESP_LOGE(TAG, "can't delete file: %s", path.c_str());
  592. }
  593. }
  594. else if (entry->d_type == DT_DIR)
  595. {
  596. deleted += remove_folder(path.c_str(), TAG);
  597. }
  598. }
  599. closedir(dir);
  600. ESP_LOGD(TAG, "%d files deleted", deleted);
  601. return 0;
  602. }
  603. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  604. {
  605. return flowtakeimage != NULL ? flowtakeimage->SendRawJPG(req) : ESP_FAIL;
  606. }
  607. esp_err_t ClassFlowControll::GetJPGStream(std::string file_name, httpd_req_t *req)
  608. {
  609. ESP_LOGD(TAG, "ClassFlowControl::GetJPGStream %s", file_name.c_str());
  610. CImageBasis *image_to_send = NULL;
  611. esp_err_t result = ESP_FAIL;
  612. bool delete_image_to_send = false;
  613. if (file_name == "alg.jpg")
  614. {
  615. if (flowalignment && flowalignment->ImageBasis->ImageOkay())
  616. {
  617. image_to_send = flowalignment->ImageBasis;
  618. }
  619. else
  620. {
  621. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControl::GetJPGStream: alg.jpg cannot be served");
  622. result = send_file(req, "/sdcard/html/Flowstate_at_work.jpg");
  623. httpd_resp_send_chunk(req, NULL, 0);
  624. }
  625. }
  626. else if (file_name == "alg_roi.jpg")
  627. {
  628. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG // no CImageBasis needed to create alg_roi.jpg (ca. 790kB less RAM)
  629. if (aktstatus.find("Initialization (delayed)") != -1)
  630. {
  631. result = send_file(req, "/sdcard/html/Flowstate_init_delayed.jpg");
  632. }
  633. else if (aktstatus.find("Initialization") != -1)
  634. {
  635. result = send_file(req, "/sdcard/html/Flowstate_init.jpg");
  636. }
  637. else if (aktstatus.find("Take Image") != -1)
  638. {
  639. if (flowalignment && flowalignment->AlgROI)
  640. {
  641. result = send_file(req, "/sdcard/html/Flowstate_take_image.jpg");
  642. }
  643. else
  644. {
  645. if (flowalignment && flowalignment->ImageBasis->ImageOkay())
  646. {
  647. image_to_send = flowalignment->ImageBasis;
  648. }
  649. else
  650. {
  651. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControl::GetJPGStream: alg_roi.jpg cannot be served -> alg.jpg is going to be served!");
  652. result = send_file(req, "/sdcard/html/Flowstate_at_work.jpg");
  653. httpd_resp_send_chunk(req, NULL, 0);
  654. }
  655. }
  656. }
  657. else
  658. {
  659. if (flowalignment && flowalignment->AlgROI)
  660. {
  661. httpd_resp_set_type(req, "image/jpeg");
  662. result = httpd_resp_send(req, (const char *)flowalignment->AlgROI->data, flowalignment->AlgROI->size);
  663. }
  664. else
  665. {
  666. if (flowalignment && flowalignment->ImageBasis->ImageOkay())
  667. {
  668. image_to_send = flowalignment->ImageBasis;
  669. }
  670. else
  671. {
  672. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControl::GetJPGStream: alg_roi.jpg cannot be served -> alg.jpg is going to be served!");
  673. result = send_file(req, "/sdcard/html/Flowstate_at_work.jpg");
  674. httpd_resp_send_chunk(req, NULL, 0);
  675. }
  676. }
  677. }
  678. #else
  679. if (!flowalignment)
  680. {
  681. ESP_LOGD(TAG, "ClassFloDControl::GetJPGStream: FlowAlignment is not (yet) initialized. Interrupt serving!");
  682. httpd_resp_send(req, NULL, 0);
  683. return ESP_FAIL;
  684. }
  685. image_to_send = new CImageBasis("alg_roi", flowalignment->ImageBasis);
  686. if (image_to_send->ImageOkay())
  687. {
  688. if (flowalignment)
  689. {
  690. flowalignment->DrawRef(image_to_send);
  691. }
  692. if (flowdigit)
  693. {
  694. flowdigit->DrawROI(image_to_send);
  695. }
  696. if (flowanalog)
  697. {
  698. flowanalog->DrawROI(image_to_send);
  699. }
  700. delete_image_to_send = true; // delete temporary image_to_send element after sending
  701. }
  702. else
  703. {
  704. if (flowalignment && flowalignment->ImageBasis->ImageOkay())
  705. {
  706. image_to_send = flowalignment->ImageBasis;
  707. }
  708. else
  709. {
  710. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControl::GetJPGStream: Not enough memory to create alg_roi.jpg -> alg.jpg is going to be served!");
  711. result = send_file(req, "/sdcard/html/Flowstate_at_work.jpg");
  712. httpd_resp_send_chunk(req, NULL, 0);
  713. }
  714. }
  715. #endif
  716. }
  717. else
  718. {
  719. std::vector<HTMLInfo *> htmlinfo = GetAllDigit();
  720. ESP_LOGD(TAG, "After getClassFlowControl::GetAllDigit");
  721. for (int i = 0; i < htmlinfo.size(); ++i)
  722. {
  723. if (file_name == htmlinfo[i]->filename)
  724. {
  725. if (htmlinfo[i]->image)
  726. {
  727. image_to_send = htmlinfo[i]->image;
  728. }
  729. }
  730. if (file_name == htmlinfo[i]->filename_org)
  731. {
  732. if (htmlinfo[i]->image_org)
  733. {
  734. image_to_send = htmlinfo[i]->image_org;
  735. }
  736. }
  737. delete htmlinfo[i];
  738. }
  739. htmlinfo.clear();
  740. if (!image_to_send)
  741. {
  742. htmlinfo = GetAllAnalog();
  743. ESP_LOGD(TAG, "After getClassFlowControl::GetAllAnalog");
  744. for (int i = 0; i < htmlinfo.size(); ++i)
  745. {
  746. if (file_name == htmlinfo[i]->filename)
  747. {
  748. if (htmlinfo[i]->image)
  749. {
  750. image_to_send = htmlinfo[i]->image;
  751. }
  752. }
  753. if (file_name == htmlinfo[i]->filename_org)
  754. {
  755. if (htmlinfo[i]->image_org)
  756. {
  757. image_to_send = htmlinfo[i]->image_org;
  758. }
  759. }
  760. delete htmlinfo[i];
  761. }
  762. htmlinfo.clear();
  763. }
  764. }
  765. if (image_to_send)
  766. {
  767. ESP_LOGD(TAG, "Sending file: %s ...", file_name.c_str());
  768. set_content_type_from_file(req, file_name.c_str());
  769. result = image_to_send->SendJPGtoHTTP(req);
  770. /* Respond with an empty chunk to signal HTTP response completion */
  771. httpd_resp_send_chunk(req, NULL, 0);
  772. ESP_LOGD(TAG, "File sending complete");
  773. if (delete_image_to_send)
  774. {
  775. delete image_to_send;
  776. }
  777. image_to_send = NULL;
  778. }
  779. return result;
  780. }
  781. std::string ClassFlowControll::getNumbersName()
  782. {
  783. return flowpostprocessing->getNumbersName();
  784. }
  785. std::string ClassFlowControll::getJSON()
  786. {
  787. return flowpostprocessing->GetJSON();
  788. }
  789. /**
  790. * @returns a vector of all current sequences
  791. **/
  792. const std::vector<NumberPost *> &ClassFlowControll::getNumbers()
  793. {
  794. return *flowpostprocessing->GetNumbers();
  795. }