ClassFlowControll.cpp 29 KB

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