ClassFlowControll.cpp 28 KB

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