ClassFlowControll.cpp 29 KB

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