ClassFlowControll.cpp 30 KB

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