ClassFlowControll.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. #include "ClassFlowControll.h"
  2. #include "connect_wlan.h"
  3. #include "read_wlanini.h"
  4. #include "freertos/task.h"
  5. #include <sys/stat.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <dirent.h>
  10. #ifdef __cplusplus
  11. }
  12. #endif
  13. #include "ClassLogFile.h"
  14. #include "time_sntp.h"
  15. #include "Helper.h"
  16. #include "server_ota.h"
  17. #ifdef ENABLE_MQTT
  18. #include "interface_mqtt.h"
  19. #include "server_mqtt.h"
  20. #endif //ENABLE_MQTT
  21. #include "server_help.h"
  22. #include "../../include/defines.h"
  23. static const char* TAG = "CTRL";
  24. //#define DEBUG_DETAIL_ON
  25. std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host){
  26. std::string _classname = "";
  27. std::string result = "";
  28. ESP_LOGD(TAG, "Step %s start", _stepname.c_str());
  29. if ((_stepname.compare("[MakeImage]") == 0) || (_stepname.compare(";[MakeImage]") == 0)){
  30. _classname = "ClassFlowMakeImage";
  31. }
  32. if ((_stepname.compare("[Alignment]") == 0) || (_stepname.compare(";[Alignment]") == 0)){
  33. _classname = "ClassFlowAlignment";
  34. }
  35. if ((_stepname.compare(0, 7, "[Digits") == 0) || (_stepname.compare(0, 8, ";[Digits") == 0)) {
  36. _classname = "ClassFlowCNNGeneral";
  37. }
  38. if ((_stepname.compare("[Analog]") == 0) || (_stepname.compare(";[Analog]") == 0)){
  39. _classname = "ClassFlowCNNGeneral";
  40. }
  41. #ifdef ENABLE_MQTT
  42. if ((_stepname.compare("[MQTT]") == 0) || (_stepname.compare(";[MQTT]") == 0)){
  43. _classname = "ClassFlowMQTT";
  44. }
  45. #endif //ENABLE_MQTT
  46. #ifdef ENABLE_INFLUXDB
  47. if ((_stepname.compare("[InfluxDB]") == 0) || (_stepname.compare(";[InfluxDB]") == 0)){
  48. _classname = "ClassFlowInfluxDB";
  49. }
  50. #endif //ENABLE_INFLUXDB
  51. for (int i = 0; i < FlowControll.size(); ++i)
  52. if (FlowControll[i]->name().compare(_classname) == 0){
  53. if (!(FlowControll[i]->name().compare("ClassFlowMakeImage") == 0)) // if it is a MakeImage, the image does not need to be included, this happens automatically with the html query.
  54. FlowControll[i]->doFlow("");
  55. result = FlowControll[i]->getHTMLSingleStep(_host);
  56. }
  57. ESP_LOGD(TAG, "Step %s end", _stepname.c_str());
  58. return result;
  59. }
  60. std::string ClassFlowControll::TranslateAktstatus(std::string _input)
  61. {
  62. if (_input.compare("ClassFlowMakeImage") == 0)
  63. return ("Take Image");
  64. if (_input.compare("ClassFlowAlignment") == 0)
  65. return ("Aligning");
  66. if (_input.compare("ClassFlowCNNGeneral") == 0)
  67. return ("Digitalization of ROIs");
  68. #ifdef ENABLE_MQTT
  69. if (_input.compare("ClassFlowMQTT") == 0)
  70. return ("Sending MQTT");
  71. #endif //ENABLE_MQTT
  72. #ifdef ENABLE_INFLUXDB
  73. if (_input.compare("ClassFlowInfluxDB") == 0)
  74. return ("Sending InfluxDB");
  75. #endif //ENABLE_INFLUXDB
  76. if (_input.compare("ClassFlowPostProcessing") == 0)
  77. return ("Post-Processing");
  78. if (_input.compare("ClassFlowWriteList") == 0)
  79. return ("Writing List");
  80. return "Unkown Status";
  81. }
  82. std::vector<HTMLInfo*> ClassFlowControll::GetAllDigital()
  83. {
  84. if (flowdigit)
  85. {
  86. ESP_LOGD(TAG, "ClassFlowControll::GetAllDigital - flowdigit != NULL");
  87. return flowdigit->GetHTMLInfo();
  88. }
  89. std::vector<HTMLInfo*> empty;
  90. return empty;
  91. }
  92. std::vector<HTMLInfo*> ClassFlowControll::GetAllAnalog()
  93. {
  94. if (flowanalog)
  95. return flowanalog->GetHTMLInfo();
  96. std::vector<HTMLInfo*> empty;
  97. return empty;
  98. }
  99. t_CNNType ClassFlowControll::GetTypeDigital()
  100. {
  101. if (flowdigit)
  102. return flowdigit->getCNNType();
  103. return t_CNNType::None;
  104. }
  105. t_CNNType ClassFlowControll::GetTypeAnalog()
  106. {
  107. if (flowanalog)
  108. return flowanalog->getCNNType();
  109. return t_CNNType::None;
  110. }
  111. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  112. void ClassFlowControll::DigitalDrawROI(CImageBasis *_zw)
  113. {
  114. if (flowdigit)
  115. flowdigit->DrawROI(_zw);
  116. }
  117. void ClassFlowControll::AnalogDrawROI(CImageBasis *_zw)
  118. {
  119. if (flowanalog)
  120. flowanalog->DrawROI(_zw);
  121. }
  122. void ClassFlowControll::SetNewAlgROI(bool _value)
  123. {
  124. bNewAlgROI = _value;
  125. }
  126. #endif
  127. #ifdef ENABLE_MQTT
  128. string ClassFlowControll::GetMQTTMainTopic()
  129. {
  130. for (int i = 0; i < FlowControll.size(); ++i)
  131. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0)
  132. return ((ClassFlowMQTT*) (FlowControll[i]))->GetMQTTMainTopic();
  133. return "";
  134. }
  135. bool ClassFlowControll::StartMQTTService() {
  136. /* Start the MQTT service */
  137. for (int i = 0; i < FlowControll.size(); ++i) {
  138. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0) {
  139. return ((ClassFlowMQTT*) (FlowControll[i]))->Start(AutoIntervall);
  140. }
  141. }
  142. return false;
  143. }
  144. #endif //ENABLE_MQTT
  145. void ClassFlowControll::SetInitialParameter(void)
  146. {
  147. AutoStart = false;
  148. SetupModeActive = false;
  149. AutoIntervall = 10; // Minutes
  150. flowdigit = NULL;
  151. flowanalog = NULL;
  152. flowpostprocessing = NULL;
  153. disabled = false;
  154. aktRunNr = 0;
  155. aktstatus = "Booting ...";
  156. }
  157. bool ClassFlowControll::isAutoStart(long &_intervall)
  158. {
  159. _intervall = AutoIntervall * 60 * 1000; // AutoInterval: minutes -> ms
  160. return AutoStart;
  161. }
  162. ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
  163. {
  164. ClassFlow* cfc = NULL;
  165. _type = trim(_type);
  166. if (toUpper(_type).compare("[MAKEIMAGE]") == 0)
  167. {
  168. cfc = new ClassFlowMakeImage(&FlowControll);
  169. flowmakeimage = (ClassFlowMakeImage*) cfc;
  170. }
  171. if (toUpper(_type).compare("[ALIGNMENT]") == 0)
  172. {
  173. cfc = new ClassFlowAlignment(&FlowControll);
  174. flowalignment = (ClassFlowAlignment*) cfc;
  175. }
  176. if (toUpper(_type).compare("[ANALOG]") == 0)
  177. {
  178. cfc = new ClassFlowCNNGeneral(flowalignment);
  179. flowanalog = (ClassFlowCNNGeneral*) cfc;
  180. }
  181. if (toUpper(_type).compare(0, 7, "[DIGITS") == 0)
  182. {
  183. cfc = new ClassFlowCNNGeneral(flowalignment);
  184. flowdigit = (ClassFlowCNNGeneral*) cfc;
  185. }
  186. #ifdef ENABLE_MQTT
  187. if (toUpper(_type).compare("[MQTT]") == 0)
  188. cfc = new ClassFlowMQTT(&FlowControll);
  189. #endif //ENABLE_MQTT
  190. #ifdef ENABLE_INFLUXDB
  191. if (toUpper(_type).compare("[INFLUXDB]") == 0)
  192. cfc = new ClassFlowInfluxDB(&FlowControll);
  193. #endif //ENABLE_INFLUXDB
  194. if (toUpper(_type).compare("[WRITELIST]") == 0)
  195. cfc = new ClassFlowWriteList(&FlowControll);
  196. if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
  197. {
  198. cfc = new ClassFlowPostProcessing(&FlowControll, flowanalog, flowdigit);
  199. flowpostprocessing = (ClassFlowPostProcessing*) cfc;
  200. }
  201. if (cfc) // Attached only if it is not [AutoTimer], because this is for FlowControll
  202. FlowControll.push_back(cfc);
  203. if (toUpper(_type).compare("[AUTOTIMER]") == 0)
  204. cfc = this;
  205. if (toUpper(_type).compare("[DATALOGGING]") == 0)
  206. cfc = this;
  207. if (toUpper(_type).compare("[DEBUG]") == 0)
  208. cfc = this;
  209. if (toUpper(_type).compare("[SYSTEM]") == 0)
  210. cfc = this;
  211. return cfc;
  212. }
  213. void ClassFlowControll::InitFlow(std::string config)
  214. {
  215. string line;
  216. flowpostprocessing = NULL;
  217. ClassFlow* cfc;
  218. FILE* pFile;
  219. config = FormatFileName(config);
  220. pFile = fopen(config.c_str(), "r");
  221. line = "";
  222. char zw[1024];
  223. if (pFile != NULL)
  224. {
  225. fgets(zw, 1024, pFile);
  226. ESP_LOGD(TAG, "%s", zw);
  227. line = std::string(zw);
  228. }
  229. while ((line.size() > 0) && !(feof(pFile)))
  230. {
  231. cfc = CreateClassFlow(line);
  232. if (cfc)
  233. {
  234. ESP_LOGD(TAG, "Start ReadParameter (%s)", line.c_str());
  235. cfc->ReadParameter(pFile, line);
  236. }
  237. else
  238. {
  239. line = "";
  240. if (fgets(zw, 1024, pFile) && !feof(pFile))
  241. {
  242. ESP_LOGD(TAG, "Read: %s", zw);
  243. line = std::string(zw);
  244. }
  245. }
  246. }
  247. fclose(pFile);
  248. }
  249. std::string* ClassFlowControll::getActStatus()
  250. {
  251. return &aktstatus;
  252. }
  253. void ClassFlowControll::doFlowMakeImageOnly(string time)
  254. {
  255. std::string zw_time;
  256. for (int i = 0; i < FlowControll.size(); ++i)
  257. {
  258. if (FlowControll[i]->name() == "ClassFlowMakeImage") {
  259. zw_time = getCurrentTimeString("%H:%M:%S");
  260. std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
  261. aktstatus = flowStatus + " (" + zw_time + ")";
  262. #ifdef ENABLE_MQTT
  263. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
  264. #endif //ENABLE_MQTT
  265. FlowControll[i]->doFlow(time);
  266. }
  267. }
  268. }
  269. bool ClassFlowControll::doFlow(string time)
  270. {
  271. bool result = true;
  272. std::string zw_time;
  273. int repeat = 0;
  274. #ifdef DEBUG_DETAIL_ON
  275. LogFile.WriteHeapInfo("ClassFlowControll::doFlow - Start");
  276. #endif
  277. /* Check if we have a valid date/time and if not restart the NTP client */
  278. /* if (! getTimeIsSet()) {
  279. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time not set, restarting NTP Client!");
  280. restartNtpClient();
  281. }*/
  282. //checkNtpStatus(0);
  283. for (int i = 0; i < FlowControll.size(); ++i)
  284. {
  285. zw_time = getCurrentTimeString("%H:%M:%S");
  286. std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
  287. aktstatus = flowStatus + " (" + zw_time + ")";
  288. //LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatus);
  289. #ifdef ENABLE_MQTT
  290. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
  291. #endif //ENABLE_MQTT
  292. #ifdef DEBUG_DETAIL_ON
  293. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  294. LogFile.WriteHeapInfo(zw);
  295. #endif
  296. if (!FlowControll[i]->doFlow(time)){
  297. repeat++;
  298. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  299. if (i) i -= 1; // vPrevious step must be repeated (probably take pictures)
  300. result = false;
  301. if (repeat > 5) {
  302. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Wiederholung 5x nicht erfolgreich --> reboot");
  303. doReboot();
  304. //Step was repeated 5x --> reboot
  305. }
  306. }
  307. else
  308. {
  309. result = true;
  310. }
  311. #ifdef DEBUG_DETAIL_ON
  312. LogFile.WriteHeapInfo("ClassFlowControll::doFlow");
  313. #endif
  314. }
  315. zw_time = getCurrentTimeString("%H:%M:%S");
  316. std::string flowStatus = "Flow finished";
  317. aktstatus = flowStatus + " (" + zw_time + ")";
  318. //LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatus);
  319. #ifdef ENABLE_MQTT
  320. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
  321. #endif //ENABLE_MQTT
  322. return result;
  323. }
  324. string ClassFlowControll::getReadoutAll(int _type)
  325. {
  326. std::string out = "";
  327. if (flowpostprocessing)
  328. {
  329. std::vector<NumberPost*> *numbers = flowpostprocessing->GetNumbers();
  330. for (int i = 0; i < (*numbers).size(); ++i)
  331. {
  332. out = out + (*numbers)[i]->name + "\t";
  333. switch (_type) {
  334. case READOUT_TYPE_VALUE:
  335. out = out + (*numbers)[i]->ReturnValue;
  336. break;
  337. case READOUT_TYPE_PREVALUE:
  338. if (flowpostprocessing->PreValueUse)
  339. {
  340. if ((*numbers)[i]->PreValueOkay)
  341. out = out + (*numbers)[i]->ReturnPreValue;
  342. else
  343. out = out + "PreValue too old";
  344. }
  345. else
  346. out = out + "PreValue deactivated";
  347. break;
  348. case READOUT_TYPE_RAWVALUE:
  349. out = out + (*numbers)[i]->ReturnRawValue;
  350. break;
  351. case READOUT_TYPE_ERROR:
  352. out = out + (*numbers)[i]->ErrorMessageText;
  353. break;
  354. }
  355. if (i < (*numbers).size()-1)
  356. out = out + "\r\n";
  357. }
  358. // ESP_LOGD(TAG, "OUT: %s", out.c_str());
  359. }
  360. return out;
  361. }
  362. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  363. {
  364. if (flowpostprocessing)
  365. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  366. string zw = "";
  367. string result = "";
  368. for (int i = 0; i < FlowControll.size(); ++i)
  369. {
  370. zw = FlowControll[i]->getReadout();
  371. if (zw.length() > 0)
  372. {
  373. if (result.length() == 0)
  374. result = zw;
  375. else
  376. result = result + "\t" + zw;
  377. }
  378. }
  379. return result;
  380. }
  381. string ClassFlowControll::GetPrevalue(std::string _number)
  382. {
  383. if (flowpostprocessing)
  384. {
  385. return flowpostprocessing->GetPreValue(_number);
  386. }
  387. return std::string("");
  388. }
  389. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern)
  390. {
  391. float zw;
  392. char* p;
  393. _newvalue = trim(_newvalue);
  394. // ESP_LOGD(TAG, "Input UpdatePreValue: %s", _newvalue.c_str());
  395. if (_newvalue.compare("0.0") == 0)
  396. {
  397. zw = 0;
  398. }
  399. else
  400. {
  401. zw = strtof(_newvalue.c_str(), &p);
  402. if (zw == 0)
  403. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  404. }
  405. if (flowpostprocessing)
  406. {
  407. flowpostprocessing->SetPreValue(zw, _numbers, _extern);
  408. return _newvalue;
  409. }
  410. return std::string();
  411. }
  412. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  413. {
  414. std::vector<string> splitted;
  415. aktparamgraph = trim(aktparamgraph);
  416. if (aktparamgraph.size() == 0)
  417. if (!this->GetNextParagraph(pfile, aktparamgraph))
  418. return false;
  419. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) &&
  420. (toUpper(aktparamgraph).compare("[SYSTEM]") != 0 && (toUpper(aktparamgraph).compare("[DATALOGGING]") != 0))) // Paragraph passt nicht zu MakeImage
  421. return false;
  422. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  423. {
  424. splitted = ZerlegeZeile(aktparamgraph, " =");
  425. if ((toUpper(splitted[0]) == "AUTOSTART") && (splitted.size() > 1))
  426. {
  427. if (toUpper(splitted[1]) == "TRUE")
  428. {
  429. AutoStart = true;
  430. }
  431. }
  432. if ((toUpper(splitted[0]) == "INTERVALL") && (splitted.size() > 1))
  433. {
  434. AutoIntervall = std::stof(splitted[1]);
  435. }
  436. if ((toUpper(splitted[0]) == "DATALOGACTIVE") && (splitted.size() > 1))
  437. {
  438. if (toUpper(splitted[1]) == "TRUE")
  439. {
  440. LogFile.SetDataLogToSD(true);
  441. }
  442. else {
  443. LogFile.SetDataLogToSD(false);
  444. }
  445. }
  446. if ((toUpper(splitted[0]) == "DATALOGRETENTIONINDAYS") && (splitted.size() > 1))
  447. {
  448. LogFile.SetDataLogRetention(std::stoi(splitted[1]));
  449. }
  450. if ((toUpper(splitted[0]) == "LOGFILE") && (splitted.size() > 1))
  451. {
  452. /* matches esp_log_level_t */
  453. if ((toUpper(splitted[1]) == "TRUE") || (toUpper(splitted[1]) == "2"))
  454. {
  455. LogFile.setLogLevel(ESP_LOG_WARN);
  456. }
  457. else if ((toUpper(splitted[1]) == "FALSE") || (toUpper(splitted[1]) == "0") || (toUpper(splitted[1]) == "1"))
  458. {
  459. LogFile.setLogLevel(ESP_LOG_ERROR);
  460. }
  461. else if (toUpper(splitted[1]) == "3")
  462. {
  463. LogFile.setLogLevel(ESP_LOG_INFO);
  464. }
  465. else if (toUpper(splitted[1]) == "4")
  466. {
  467. LogFile.setLogLevel(ESP_LOG_DEBUG);
  468. }
  469. }
  470. if ((toUpper(splitted[0]) == "LOGFILERETENTIONINDAYS") && (splitted.size() > 1))
  471. {
  472. LogFile.SetLogFileRetention(std::stoi(splitted[1]));
  473. }
  474. /* TimeServer and TimeZone got already read from the config, see setupTime () */
  475. if ((toUpper(splitted[0]) == "RSSITHREASHOLD") && (splitted.size() > 1))
  476. {
  477. if (ChangeRSSIThreashold(WLAN_CONFIG_FILE, atoi(splitted[1].c_str())))
  478. {
  479. // reboot necessary so that the new wlan.ini is also used !!!
  480. fclose(pfile);
  481. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Rebooting to activate new RSSITHREASHOLD ...");
  482. esp_restart();
  483. hard_restart();
  484. doReboot();
  485. }
  486. }
  487. if ((toUpper(splitted[0]) == "HOSTNAME") && (splitted.size() > 1))
  488. {
  489. if (ChangeHostName(WLAN_CONFIG_FILE, splitted[1]))
  490. {
  491. // reboot necessary so that the new wlan.ini is also used !!!
  492. fclose(pfile);
  493. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Rebooting to activate new HOSTNAME...");
  494. esp_restart();
  495. hard_restart();
  496. doReboot();
  497. }
  498. }
  499. if ((toUpper(splitted[0]) == "SETUPMODE") && (splitted.size() > 1))
  500. {
  501. if (toUpper(splitted[1]) == "TRUE")
  502. {
  503. SetupModeActive = true;
  504. }
  505. }
  506. }
  507. return true;
  508. }
  509. int ClassFlowControll::CleanTempFolder() {
  510. const char* folderPath = "/sdcard/img_tmp";
  511. ESP_LOGD(TAG, "Clean up temporary folder to avoid damage of sdcard sectors: %s", folderPath);
  512. DIR *dir = opendir(folderPath);
  513. if (!dir) {
  514. ESP_LOGE(TAG, "Failed to stat dir: %s", folderPath);
  515. return -1;
  516. }
  517. struct dirent *entry;
  518. int deleted = 0;
  519. while ((entry = readdir(dir)) != NULL) {
  520. std::string path = string(folderPath) + "/" + entry->d_name;
  521. if (entry->d_type == DT_REG) {
  522. if (unlink(path.c_str()) == 0) {
  523. deleted ++;
  524. } else {
  525. ESP_LOGE(TAG, "can't delete file: %s", path.c_str());
  526. }
  527. } else if (entry->d_type == DT_DIR) {
  528. deleted += removeFolder(path.c_str(), TAG);
  529. }
  530. }
  531. closedir(dir);
  532. ESP_LOGD(TAG, "%d files deleted", deleted);
  533. return 0;
  534. }
  535. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  536. {
  537. return flowmakeimage != NULL ? flowmakeimage->SendRawJPG(req) : ESP_FAIL;
  538. }
  539. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  540. {
  541. ESP_LOGD(TAG, "ClassFlowControll::GetJPGStream %s", _fn.c_str());
  542. #ifdef DEBUG_DETAIL_ON
  543. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - Start");
  544. #endif
  545. CImageBasis *_send = NULL;
  546. esp_err_t result = ESP_FAIL;
  547. bool _sendDelete = false;
  548. if (flowalignment == NULL)
  549. {
  550. ESP_LOGD(TAG, "ClassFloDControll::GetJPGStream: FloDalignment is not (yet) initialized. Interrupt serving!");
  551. return ESP_OK;
  552. }
  553. if (_fn == "alg.jpg") {
  554. if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
  555. _send = flowalignment->ImageBasis;
  556. }
  557. else {
  558. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: alg.jpg cannot be served");
  559. return ESP_FAIL;
  560. }
  561. }
  562. else if (_fn == "alg_roi.jpg") {
  563. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG // no CImageBasis needed to create alg_roi.jpg (ca. 790kB less RAM)
  564. if (bNewAlgROI) {
  565. if (flowalignment && flowalignment->AlgROI) {
  566. httpd_resp_set_type(req, "image/jpeg");
  567. result = httpd_resp_send(req, (const char *)flowalignment->AlgROI->data, flowalignment->AlgROI->size);
  568. }
  569. else {
  570. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: alg_roi.jpg cannot be served");
  571. return ESP_FAIL;
  572. }
  573. }
  574. else {
  575. if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
  576. _send = flowalignment->ImageBasis;
  577. }
  578. else {
  579. httpd_resp_send(req, NULL, 0);
  580. return ESP_OK;
  581. }
  582. }
  583. #else
  584. _send = new CImageBasis(flowalignment->ImageBasis);
  585. if (_send->ImageOkay()) {
  586. if (flowalignment) flowalignment->DrawRef(_send);
  587. if (flowdigit) flowdigit->DrawROI(_send);
  588. if (flowanalog) flowanalog->DrawROI(_send);
  589. _sendDelete = true; // delete temporary _send element after sending
  590. }
  591. else {
  592. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControll::GetJPGStream: Not enough memory to create alg_roi.jpg -> alg.jpg is going to be served!");
  593. if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
  594. _send = flowalignment->ImageBasis;
  595. }
  596. else {
  597. httpd_resp_send(req, NULL, 0);
  598. return ESP_OK;
  599. }
  600. }
  601. #endif
  602. }
  603. else {
  604. std::vector<HTMLInfo*> htmlinfo;
  605. htmlinfo = GetAllDigital();
  606. ESP_LOGD(TAG, "After getClassFlowControll::GetAllDigital");
  607. for (int i = 0; i < htmlinfo.size(); ++i)
  608. {
  609. if (_fn == htmlinfo[i]->filename)
  610. {
  611. if (htmlinfo[i]->image)
  612. _send = htmlinfo[i]->image;
  613. }
  614. if (_fn == htmlinfo[i]->filename_org)
  615. {
  616. if (htmlinfo[i]->image_org)
  617. _send = htmlinfo[i]->image_org;
  618. }
  619. delete htmlinfo[i];
  620. }
  621. htmlinfo.clear();
  622. if (!_send)
  623. {
  624. htmlinfo = GetAllAnalog();
  625. ESP_LOGD(TAG, "After getClassFlowControll::GetAllAnalog");
  626. for (int i = 0; i < htmlinfo.size(); ++i)
  627. {
  628. if (_fn == htmlinfo[i]->filename)
  629. {
  630. if (htmlinfo[i]->image)
  631. _send = htmlinfo[i]->image;
  632. }
  633. if (_fn == htmlinfo[i]->filename_org)
  634. {
  635. if (htmlinfo[i]->image_org)
  636. _send = htmlinfo[i]->image_org;
  637. }
  638. delete htmlinfo[i];
  639. }
  640. htmlinfo.clear();
  641. }
  642. }
  643. #ifdef DEBUG_DETAIL_ON
  644. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - before send");
  645. #endif
  646. if (_send)
  647. {
  648. ESP_LOGD(TAG, "Sending file: %s ...", _fn.c_str());
  649. set_content_type_from_file(req, _fn.c_str());
  650. result = _send->SendJPGtoHTTP(req);
  651. /* Respond with an empty chunk to signal HTTP response completion */
  652. httpd_resp_send_chunk(req, NULL, 0);
  653. ESP_LOGD(TAG, "File sending complete");
  654. if (_sendDelete)
  655. delete _send;
  656. _send = NULL;
  657. }
  658. #ifdef DEBUG_DETAIL_ON
  659. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - done");
  660. #endif
  661. return result;
  662. }
  663. string ClassFlowControll::getNumbersName()
  664. {
  665. return flowpostprocessing->getNumbersName();
  666. }
  667. string ClassFlowControll::getJSON()
  668. {
  669. return flowpostprocessing->GetJSON();
  670. }