ClassFlowControll.cpp 29 KB

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