ClassFlowControll.cpp 29 KB

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