ClassFlowControll.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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("[TakeImage]") == 0) || (_stepname.compare(";[TakeImage]") == 0)){
  30. _classname = "ClassFlowTakeImage";
  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("ClassFlowTakeImage") == 0)) // if it is a TakeImage, 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("ClassFlowTakeImage") == 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. #endif
  123. #ifdef ENABLE_MQTT
  124. string ClassFlowControll::GetMQTTMainTopic()
  125. {
  126. for (int i = 0; i < FlowControll.size(); ++i)
  127. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0)
  128. return ((ClassFlowMQTT*) (FlowControll[i]))->GetMQTTMainTopic();
  129. return "";
  130. }
  131. bool ClassFlowControll::StartMQTTService() {
  132. /* Start the MQTT service */
  133. for (int i = 0; i < FlowControll.size(); ++i) {
  134. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0) {
  135. return ((ClassFlowMQTT*) (FlowControll[i]))->Start(AutoInterval);
  136. }
  137. }
  138. return false;
  139. }
  140. #endif //ENABLE_MQTT
  141. void ClassFlowControll::SetInitialParameter(void)
  142. {
  143. AutoStart = false;
  144. SetupModeActive = false;
  145. AutoInterval = 10; // Minutes
  146. flowdigit = NULL;
  147. flowanalog = NULL;
  148. flowpostprocessing = NULL;
  149. disabled = false;
  150. aktRunNr = 0;
  151. aktstatus = "Flow task not yet created";
  152. }
  153. bool ClassFlowControll::isAutoStart(long &_interval)
  154. {
  155. _interval = AutoInterval * 60 * 1000; // AutoInterval: minutes -> ms
  156. return AutoStart;
  157. }
  158. ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
  159. {
  160. ClassFlow* cfc = NULL;
  161. _type = trim(_type);
  162. if (toUpper(_type).compare("[TAKEIMAGE]") == 0)
  163. {
  164. cfc = new ClassFlowTakeImage(&FlowControll);
  165. flowtakeimage = (ClassFlowTakeImage*) cfc;
  166. }
  167. if (toUpper(_type).compare("[ALIGNMENT]") == 0)
  168. {
  169. cfc = new ClassFlowAlignment(&FlowControll);
  170. flowalignment = (ClassFlowAlignment*) cfc;
  171. }
  172. if (toUpper(_type).compare("[ANALOG]") == 0)
  173. {
  174. cfc = new ClassFlowCNNGeneral(flowalignment);
  175. flowanalog = (ClassFlowCNNGeneral*) cfc;
  176. }
  177. if (toUpper(_type).compare(0, 7, "[DIGITS") == 0)
  178. {
  179. cfc = new ClassFlowCNNGeneral(flowalignment);
  180. flowdigit = (ClassFlowCNNGeneral*) cfc;
  181. }
  182. #ifdef ENABLE_MQTT
  183. if (toUpper(_type).compare("[MQTT]") == 0)
  184. cfc = new ClassFlowMQTT(&FlowControll);
  185. #endif //ENABLE_MQTT
  186. #ifdef ENABLE_INFLUXDB
  187. if (toUpper(_type).compare("[INFLUXDB]") == 0)
  188. cfc = new ClassFlowInfluxDB(&FlowControll);
  189. #endif //ENABLE_INFLUXDB
  190. if (toUpper(_type).compare("[WRITELIST]") == 0)
  191. cfc = new ClassFlowWriteList(&FlowControll);
  192. if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
  193. {
  194. cfc = new ClassFlowPostProcessing(&FlowControll, flowanalog, flowdigit);
  195. flowpostprocessing = (ClassFlowPostProcessing*) cfc;
  196. }
  197. if (cfc) // Attached only if it is not [AutoTimer], because this is for FlowControll
  198. FlowControll.push_back(cfc);
  199. if (toUpper(_type).compare("[AUTOTIMER]") == 0)
  200. cfc = this;
  201. if (toUpper(_type).compare("[DATALOGGING]") == 0)
  202. cfc = this;
  203. if (toUpper(_type).compare("[DEBUG]") == 0)
  204. cfc = this;
  205. if (toUpper(_type).compare("[SYSTEM]") == 0)
  206. cfc = this;
  207. return cfc;
  208. }
  209. void ClassFlowControll::InitFlow(std::string config)
  210. {
  211. aktstatus = "Initialization";
  212. //#ifdef ENABLE_MQTT
  213. //MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization", false); // Right now, not possible -> MQTT Service is going to be started later
  214. //#endif //ENABLE_MQTT
  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::setActStatus(std::string _aktstatus)
  254. {
  255. aktstatus = _aktstatus;
  256. }
  257. void ClassFlowControll::doFlowTakeImageOnly(string time)
  258. {
  259. std::string zw_time;
  260. for (int i = 0; i < FlowControll.size(); ++i)
  261. {
  262. if (FlowControll[i]->name() == "ClassFlowTakeImage") {
  263. zw_time = getCurrentTimeString("%H:%M:%S");
  264. std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
  265. aktstatus = flowStatus + " (" + zw_time + ")";
  266. #ifdef ENABLE_MQTT
  267. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
  268. #endif //ENABLE_MQTT
  269. FlowControll[i]->doFlow(time);
  270. }
  271. }
  272. }
  273. bool ClassFlowControll::doFlow(string time)
  274. {
  275. bool result = true;
  276. std::string zw_time;
  277. int repeat = 0;
  278. #ifdef DEBUG_DETAIL_ON
  279. LogFile.WriteHeapInfo("ClassFlowControll::doFlow - Start");
  280. #endif
  281. /* Check if we have a valid date/time and if not restart the NTP client */
  282. /* if (! getTimeIsSet()) {
  283. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time not set, restarting NTP Client!");
  284. restartNtpClient();
  285. }*/
  286. //checkNtpStatus(0);
  287. for (int i = 0; i < FlowControll.size(); ++i)
  288. {
  289. zw_time = getCurrentTimeString("%H:%M:%S");
  290. std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
  291. aktstatus = flowStatus + " (" + zw_time + ")";
  292. //LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatus);
  293. #ifdef ENABLE_MQTT
  294. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
  295. #endif //ENABLE_MQTT
  296. #ifdef DEBUG_DETAIL_ON
  297. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  298. LogFile.WriteHeapInfo(zw);
  299. #endif
  300. if (!FlowControll[i]->doFlow(time)){
  301. repeat++;
  302. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  303. if (i) i -= 1; // vPrevious step must be repeated (probably take pictures)
  304. result = false;
  305. if (repeat > 5) {
  306. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Wiederholung 5x nicht erfolgreich --> reboot");
  307. doReboot();
  308. //Step was repeated 5x --> reboot
  309. }
  310. }
  311. else
  312. {
  313. result = true;
  314. }
  315. #ifdef DEBUG_DETAIL_ON
  316. LogFile.WriteHeapInfo("ClassFlowControll::doFlow");
  317. #endif
  318. }
  319. zw_time = getCurrentTimeString("%H:%M:%S");
  320. std::string flowStatus = "Flow finished";
  321. aktstatus = flowStatus + " (" + zw_time + ")";
  322. //LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatus);
  323. #ifdef ENABLE_MQTT
  324. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
  325. #endif //ENABLE_MQTT
  326. return result;
  327. }
  328. string ClassFlowControll::getReadoutAll(int _type)
  329. {
  330. std::string out = "";
  331. if (flowpostprocessing)
  332. {
  333. std::vector<NumberPost*> *numbers = flowpostprocessing->GetNumbers();
  334. for (int i = 0; i < (*numbers).size(); ++i)
  335. {
  336. out = out + (*numbers)[i]->name + "\t";
  337. switch (_type) {
  338. case READOUT_TYPE_VALUE:
  339. out = out + (*numbers)[i]->ReturnValue;
  340. break;
  341. case READOUT_TYPE_PREVALUE:
  342. if (flowpostprocessing->PreValueUse)
  343. {
  344. if ((*numbers)[i]->PreValueOkay)
  345. out = out + (*numbers)[i]->ReturnPreValue;
  346. else
  347. out = out + "PreValue too old";
  348. }
  349. else
  350. out = out + "PreValue deactivated";
  351. break;
  352. case READOUT_TYPE_RAWVALUE:
  353. out = out + (*numbers)[i]->ReturnRawValue;
  354. break;
  355. case READOUT_TYPE_ERROR:
  356. out = out + (*numbers)[i]->ErrorMessageText;
  357. break;
  358. }
  359. if (i < (*numbers).size()-1)
  360. out = out + "\r\n";
  361. }
  362. // ESP_LOGD(TAG, "OUT: %s", out.c_str());
  363. }
  364. return out;
  365. }
  366. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  367. {
  368. if (flowpostprocessing)
  369. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  370. string zw = "";
  371. string result = "";
  372. for (int i = 0; i < FlowControll.size(); ++i)
  373. {
  374. zw = FlowControll[i]->getReadout();
  375. if (zw.length() > 0)
  376. {
  377. if (result.length() == 0)
  378. result = zw;
  379. else
  380. result = result + "\t" + zw;
  381. }
  382. }
  383. return result;
  384. }
  385. string ClassFlowControll::GetPrevalue(std::string _number)
  386. {
  387. if (flowpostprocessing)
  388. {
  389. return flowpostprocessing->GetPreValue(_number);
  390. }
  391. return std::string("");
  392. }
  393. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern)
  394. {
  395. float zw;
  396. char* p;
  397. _newvalue = trim(_newvalue);
  398. // ESP_LOGD(TAG, "Input UpdatePreValue: %s", _newvalue.c_str());
  399. if (_newvalue.compare("0.0") == 0)
  400. {
  401. zw = 0;
  402. }
  403. else
  404. {
  405. zw = strtof(_newvalue.c_str(), &p);
  406. if (zw == 0)
  407. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  408. }
  409. if (flowpostprocessing)
  410. {
  411. flowpostprocessing->SetPreValue(zw, _numbers, _extern);
  412. return _newvalue;
  413. }
  414. return std::string();
  415. }
  416. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  417. {
  418. std::vector<string> splitted;
  419. aktparamgraph = trim(aktparamgraph);
  420. if (aktparamgraph.size() == 0)
  421. if (!this->GetNextParagraph(pfile, aktparamgraph))
  422. return false;
  423. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) &&
  424. (toUpper(aktparamgraph).compare("[SYSTEM]") != 0 && (toUpper(aktparamgraph).compare("[DATALOGGING]") != 0))) // Paragraph passt nicht zu Debug oder DataLogging
  425. return false;
  426. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  427. {
  428. splitted = ZerlegeZeile(aktparamgraph, " =");
  429. if ((toUpper(splitted[0]) == "AUTOSTART") && (splitted.size() > 1))
  430. {
  431. if (toUpper(splitted[1]) == "TRUE")
  432. {
  433. AutoStart = true;
  434. }
  435. }
  436. if ((toUpper(splitted[0]) == "INTERVAL") && (splitted.size() > 1))
  437. {
  438. AutoInterval = std::stof(splitted[1]);
  439. }
  440. if ((toUpper(splitted[0]) == "DATALOGACTIVE") && (splitted.size() > 1))
  441. {
  442. if (toUpper(splitted[1]) == "TRUE")
  443. {
  444. LogFile.SetDataLogToSD(true);
  445. }
  446. else {
  447. LogFile.SetDataLogToSD(false);
  448. }
  449. }
  450. if ((toUpper(splitted[0]) == "DATAFILESRETENTION") && (splitted.size() > 1))
  451. {
  452. LogFile.SetDataLogRetention(std::stoi(splitted[1]));
  453. }
  454. if ((toUpper(splitted[0]) == "LOGLEVEL") && (splitted.size() > 1))
  455. {
  456. /* matches esp_log_level_t */
  457. if ((toUpper(splitted[1]) == "TRUE") || (toUpper(splitted[1]) == "2"))
  458. {
  459. LogFile.setLogLevel(ESP_LOG_WARN);
  460. }
  461. else if ((toUpper(splitted[1]) == "FALSE") || (toUpper(splitted[1]) == "0") || (toUpper(splitted[1]) == "1"))
  462. {
  463. LogFile.setLogLevel(ESP_LOG_ERROR);
  464. }
  465. else if (toUpper(splitted[1]) == "3")
  466. {
  467. LogFile.setLogLevel(ESP_LOG_INFO);
  468. }
  469. else if (toUpper(splitted[1]) == "4")
  470. {
  471. LogFile.setLogLevel(ESP_LOG_DEBUG);
  472. }
  473. }
  474. if ((toUpper(splitted[0]) == "LOGFILESRETENTION") && (splitted.size() > 1))
  475. {
  476. LogFile.SetLogFileRetention(std::stoi(splitted[1]));
  477. }
  478. /* TimeServer and TimeZone got already read from the config, see setupTime () */
  479. if ((toUpper(splitted[0]) == "RSSITHRESHOLD") && (splitted.size() > 1))
  480. {
  481. if (ChangeRSSIThreshold(WLAN_CONFIG_FILE, atoi(splitted[1].c_str())))
  482. {
  483. // reboot necessary so that the new wlan.ini is also used !!!
  484. fclose(pfile);
  485. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Rebooting to activate new RSSITHRESHOLD ...");
  486. esp_restart();
  487. hard_restart();
  488. doReboot();
  489. }
  490. }
  491. if ((toUpper(splitted[0]) == "HOSTNAME") && (splitted.size() > 1))
  492. {
  493. if (ChangeHostName(WLAN_CONFIG_FILE, splitted[1]))
  494. {
  495. // reboot necessary so that the new wlan.ini is also used !!!
  496. fclose(pfile);
  497. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Rebooting to activate new HOSTNAME...");
  498. esp_restart();
  499. hard_restart();
  500. doReboot();
  501. }
  502. }
  503. if ((toUpper(splitted[0]) == "SETUPMODE") && (splitted.size() > 1))
  504. {
  505. if (toUpper(splitted[1]) == "TRUE")
  506. {
  507. SetupModeActive = true;
  508. }
  509. }
  510. }
  511. return true;
  512. }
  513. int ClassFlowControll::CleanTempFolder() {
  514. const char* folderPath = "/sdcard/img_tmp";
  515. ESP_LOGD(TAG, "Clean up temporary folder to avoid damage of sdcard sectors: %s", folderPath);
  516. DIR *dir = opendir(folderPath);
  517. if (!dir) {
  518. ESP_LOGE(TAG, "Failed to stat dir: %s", folderPath);
  519. return -1;
  520. }
  521. struct dirent *entry;
  522. int deleted = 0;
  523. while ((entry = readdir(dir)) != NULL) {
  524. std::string path = string(folderPath) + "/" + entry->d_name;
  525. if (entry->d_type == DT_REG) {
  526. if (unlink(path.c_str()) == 0) {
  527. deleted ++;
  528. } else {
  529. ESP_LOGE(TAG, "can't delete file: %s", path.c_str());
  530. }
  531. } else if (entry->d_type == DT_DIR) {
  532. deleted += removeFolder(path.c_str(), TAG);
  533. }
  534. }
  535. closedir(dir);
  536. ESP_LOGD(TAG, "%d files deleted", deleted);
  537. return 0;
  538. }
  539. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  540. {
  541. return flowtakeimage != NULL ? flowtakeimage->SendRawJPG(req) : ESP_FAIL;
  542. }
  543. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  544. {
  545. ESP_LOGD(TAG, "ClassFlowControll::GetJPGStream %s", _fn.c_str());
  546. #ifdef DEBUG_DETAIL_ON
  547. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - Start");
  548. #endif
  549. CImageBasis *_send = NULL;
  550. esp_err_t result = ESP_FAIL;
  551. bool _sendDelete = false;
  552. if (_fn == "alg.jpg") {
  553. if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
  554. _send = flowalignment->ImageBasis;
  555. }
  556. else {
  557. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: alg.jpg cannot be served");
  558. return ESP_FAIL;
  559. }
  560. }
  561. else if (_fn == "alg_roi.jpg") {
  562. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG // no CImageBasis needed to create alg_roi.jpg (ca. 790kB less RAM)
  563. if (aktstatus.find("Initialization (delayed)") != -1) {
  564. FILE* file = fopen("/sdcard/html/Flowstate_initialization_delayed.jpg", "rb");
  565. if (!file) {
  566. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File /sdcard/html/Flowstate_initialization_delayed.jpg not found");
  567. return ESP_FAIL;
  568. }
  569. fseek(file, 0, SEEK_END);
  570. long fileSize = ftell(file); /* how long is the file ? */
  571. fseek(file, 0, SEEK_SET); /* reset */
  572. unsigned char* fileBuffer = (unsigned char*) malloc(fileSize);
  573. if (!fileBuffer) {
  574. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: Not enough memory to create fileBuffer: " + std::to_string(fileSize));
  575. fclose(file);
  576. return ESP_FAIL;
  577. }
  578. fread(fileBuffer, fileSize, 1, file);
  579. fclose(file);
  580. httpd_resp_set_type(req, "image/jpeg");
  581. result = httpd_resp_send(req, (const char *)fileBuffer, fileSize);
  582. delete fileBuffer;
  583. }
  584. else if (aktstatus.find("Initialization") != -1) {
  585. FILE* file = fopen("/sdcard/html/Flowstate_initialization.jpg", "rb");
  586. if (!file) {
  587. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File /sdcard/html/Flowstate_initialization.jpg not found");
  588. return ESP_FAIL;
  589. }
  590. fseek(file, 0, SEEK_END);
  591. long fileSize = ftell(file); /* how long is the file ? */
  592. fseek(file, 0, SEEK_SET); /* reset */
  593. unsigned char* fileBuffer = (unsigned char*) malloc(fileSize);
  594. if (!fileBuffer) {
  595. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: Not enough memory to create fileBuffer: " + std::to_string(fileSize));
  596. fclose(file);
  597. return ESP_FAIL;
  598. }
  599. fread(fileBuffer, fileSize, 1, file);
  600. fclose(file);
  601. httpd_resp_set_type(req, "image/jpeg");
  602. result = httpd_resp_send(req, (const char *)fileBuffer, fileSize);
  603. delete fileBuffer;
  604. }
  605. else if (aktstatus.find("Take Image") != -1) {
  606. if (flowalignment && flowalignment->AlgROI) {
  607. FILE* file = fopen("/sdcard/html/Flowstate_take_image.jpg", "rb");
  608. if (!file) {
  609. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File /sdcard/html/Flowstate_take_image.jpg not found");
  610. return ESP_FAIL;
  611. }
  612. fseek(file, 0, SEEK_END);
  613. flowalignment->AlgROI->size = ftell(file); /* how long is the file ? */
  614. fseek(file, 0, SEEK_SET); /* reset */
  615. if (flowalignment->AlgROI->size > MAX_JPG_SIZE) {
  616. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File /sdcard/html/Flowstate_take_image.jpg (" + std::to_string(flowalignment->AlgROI->size) +
  617. ") > allocated buffer (" + std::to_string(MAX_JPG_SIZE) + ")");
  618. fclose(file);
  619. return ESP_FAIL;
  620. }
  621. fread(flowalignment->AlgROI->data, flowalignment->AlgROI->size, 1, file);
  622. fclose(file);
  623. httpd_resp_set_type(req, "image/jpeg");
  624. result = httpd_resp_send(req, (const char *)flowalignment->AlgROI->data, flowalignment->AlgROI->size);
  625. }
  626. else {
  627. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: alg_roi.jpg cannot be served -> alg.jpg is going to be served!");
  628. if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
  629. _send = flowalignment->ImageBasis;
  630. }
  631. else {
  632. httpd_resp_send(req, NULL, 0);
  633. return ESP_OK;
  634. }
  635. }
  636. }
  637. else {
  638. if (flowalignment && flowalignment->AlgROI) {
  639. httpd_resp_set_type(req, "image/jpeg");
  640. result = httpd_resp_send(req, (const char *)flowalignment->AlgROI->data, flowalignment->AlgROI->size);
  641. }
  642. else {
  643. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: alg_roi.jpg cannot be served -> alg.jpg is going to be served!");
  644. if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
  645. _send = flowalignment->ImageBasis;
  646. }
  647. else {
  648. httpd_resp_send(req, NULL, 0);
  649. return ESP_OK;
  650. }
  651. }
  652. }
  653. #else
  654. if (!flowalignment) {
  655. ESP_LOGD(TAG, "ClassFloDControll::GetJPGStream: FlowAlignment is not (yet) initialized. Interrupt serving!");
  656. httpd_resp_send(req, NULL, 0);
  657. return ESP_FAIL;
  658. }
  659. _send = new CImageBasis(flowalignment->ImageBasis);
  660. if (_send->ImageOkay()) {
  661. if (flowalignment) flowalignment->DrawRef(_send);
  662. if (flowdigit) flowdigit->DrawROI(_send);
  663. if (flowanalog) flowanalog->DrawROI(_send);
  664. _sendDelete = true; // delete temporary _send element after sending
  665. }
  666. else {
  667. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControll::GetJPGStream: Not enough memory to create alg_roi.jpg -> alg.jpg is going to be served!");
  668. if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
  669. _send = flowalignment->ImageBasis;
  670. }
  671. else {
  672. httpd_resp_send(req, NULL, 0);
  673. return ESP_OK;
  674. }
  675. }
  676. #endif
  677. }
  678. else {
  679. std::vector<HTMLInfo*> htmlinfo;
  680. htmlinfo = GetAllDigital();
  681. ESP_LOGD(TAG, "After getClassFlowControll::GetAllDigital");
  682. for (int i = 0; i < htmlinfo.size(); ++i)
  683. {
  684. if (_fn == htmlinfo[i]->filename)
  685. {
  686. if (htmlinfo[i]->image)
  687. _send = htmlinfo[i]->image;
  688. }
  689. if (_fn == htmlinfo[i]->filename_org)
  690. {
  691. if (htmlinfo[i]->image_org)
  692. _send = htmlinfo[i]->image_org;
  693. }
  694. delete htmlinfo[i];
  695. }
  696. htmlinfo.clear();
  697. if (!_send)
  698. {
  699. htmlinfo = GetAllAnalog();
  700. ESP_LOGD(TAG, "After getClassFlowControll::GetAllAnalog");
  701. for (int i = 0; i < htmlinfo.size(); ++i)
  702. {
  703. if (_fn == htmlinfo[i]->filename)
  704. {
  705. if (htmlinfo[i]->image)
  706. _send = htmlinfo[i]->image;
  707. }
  708. if (_fn == htmlinfo[i]->filename_org)
  709. {
  710. if (htmlinfo[i]->image_org)
  711. _send = htmlinfo[i]->image_org;
  712. }
  713. delete htmlinfo[i];
  714. }
  715. htmlinfo.clear();
  716. }
  717. }
  718. #ifdef DEBUG_DETAIL_ON
  719. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - before send");
  720. #endif
  721. if (_send)
  722. {
  723. ESP_LOGD(TAG, "Sending file: %s ...", _fn.c_str());
  724. set_content_type_from_file(req, _fn.c_str());
  725. result = _send->SendJPGtoHTTP(req);
  726. /* Respond with an empty chunk to signal HTTP response completion */
  727. httpd_resp_send_chunk(req, NULL, 0);
  728. ESP_LOGD(TAG, "File sending complete");
  729. if (_sendDelete)
  730. delete _send;
  731. _send = NULL;
  732. }
  733. #ifdef DEBUG_DETAIL_ON
  734. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - done");
  735. #endif
  736. return result;
  737. }
  738. string ClassFlowControll::getNumbersName()
  739. {
  740. return flowpostprocessing->getNumbersName();
  741. }
  742. string ClassFlowControll::getJSON()
  743. {
  744. return flowpostprocessing->GetJSON();
  745. }