ClassFlowControll.cpp 29 KB

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