ClassFlowControll.cpp 22 KB

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