ClassFlowControll.cpp 28 KB

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