ClassFlowControll.cpp 31 KB

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