ClassFlowControll.cpp 29 KB

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