ClassFlowControll.cpp 30 KB

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