ClassFlowControll.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. #include "ClassFlowControll.h"
  2. #include "connect_wlan.h"
  3. #include "read_wlanini.h"
  4. #include "freertos/task.h"
  5. #include <sys/stat.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <dirent.h>
  10. #ifdef __cplusplus
  11. }
  12. #endif
  13. #include "ClassLogFile.h"
  14. #include "time_sntp.h"
  15. #include "Helper.h"
  16. #include "server_ota.h"
  17. #ifdef ENABLE_MQTT
  18. #include "interface_mqtt.h"
  19. #include "server_mqtt.h"
  20. #endif //ENABLE_MQTT
  21. #include "server_help.h"
  22. #include "MainFlowControl.h"
  23. #include "../../include/defines.h"
  24. static const char* TAG = "FLOWCTRL";
  25. //#define DEBUG_DETAIL_ON
  26. std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host){
  27. std::string _classname = "";
  28. std::string result = "";
  29. ESP_LOGD(TAG, "Step %s start", _stepname.c_str());
  30. if ((_stepname.compare("[TakeImage]") == 0) || (_stepname.compare(";[TakeImage]") == 0)){
  31. _classname = "ClassFlowTakeImage";
  32. }
  33. if ((_stepname.compare("[Alignment]") == 0) || (_stepname.compare(";[Alignment]") == 0)){
  34. _classname = "ClassFlowAlignment";
  35. }
  36. if ((_stepname.compare(0, 7, "[Digits") == 0) || (_stepname.compare(0, 8, ";[Digits") == 0)) {
  37. _classname = "ClassFlowCNNGeneral";
  38. }
  39. if ((_stepname.compare("[Analog]") == 0) || (_stepname.compare(";[Analog]") == 0)){
  40. _classname = "ClassFlowCNNGeneral";
  41. }
  42. #ifdef ENABLE_MQTT
  43. if ((_stepname.compare("[MQTT]") == 0) || (_stepname.compare(";[MQTT]") == 0)){
  44. _classname = "ClassFlowMQTT";
  45. }
  46. #endif //ENABLE_MQTT
  47. #ifdef ENABLE_INFLUXDB
  48. if ((_stepname.compare("[InfluxDB]") == 0) || (_stepname.compare(";[InfluxDB]") == 0)){
  49. _classname = "ClassFlowInfluxDB";
  50. }
  51. if ((_stepname.compare("[InfluxDBv2]") == 0) || (_stepname.compare(";[InfluxDBv2]") == 0)){
  52. _classname = "ClassFlowInfluxDBv2";
  53. }
  54. #endif //ENABLE_INFLUXDB
  55. for (int i = 0; i < FlowControll.size(); ++i)
  56. if (FlowControll[i]->name().compare(_classname) == 0){
  57. 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.
  58. FlowControll[i]->doFlow("");
  59. result = FlowControll[i]->getHTMLSingleStep(_host);
  60. }
  61. ESP_LOGD(TAG, "Step %s end", _stepname.c_str());
  62. return result;
  63. }
  64. std::string ClassFlowControll::TranslateAktstatus(std::string _input)
  65. {
  66. if (_input.compare("ClassFlowTakeImage") == 0)
  67. return ("Take Image");
  68. if (_input.compare("ClassFlowAlignment") == 0)
  69. return ("Aligning");
  70. if (_input.compare("ClassFlowCNNGeneral") == 0)
  71. return ("Digitalization of ROIs");
  72. #ifdef ENABLE_MQTT
  73. if (_input.compare("ClassFlowMQTT") == 0)
  74. return ("Sending MQTT");
  75. #endif //ENABLE_MQTT
  76. #ifdef ENABLE_INFLUXDB
  77. if (_input.compare("ClassFlowInfluxDB") == 0)
  78. return ("Sending InfluxDB");
  79. if (_input.compare("ClassFlowInfluxDBv2") == 0)
  80. return ("Sending InfluxDBv2");
  81. #endif //ENABLE_INFLUXDB
  82. if (_input.compare("ClassFlowPostProcessing") == 0)
  83. return ("Post-Processing");
  84. return "Unkown Status";
  85. }
  86. std::vector<HTMLInfo*> ClassFlowControll::GetAllDigital()
  87. {
  88. if (flowdigit)
  89. {
  90. ESP_LOGD(TAG, "ClassFlowControll::GetAllDigital - flowdigit != NULL");
  91. return flowdigit->GetHTMLInfo();
  92. }
  93. std::vector<HTMLInfo*> empty;
  94. return empty;
  95. }
  96. std::vector<HTMLInfo*> ClassFlowControll::GetAllAnalog()
  97. {
  98. if (flowanalog)
  99. return flowanalog->GetHTMLInfo();
  100. std::vector<HTMLInfo*> empty;
  101. return empty;
  102. }
  103. t_CNNType ClassFlowControll::GetTypeDigital()
  104. {
  105. if (flowdigit)
  106. return flowdigit->getCNNType();
  107. return t_CNNType::None;
  108. }
  109. t_CNNType ClassFlowControll::GetTypeAnalog()
  110. {
  111. if (flowanalog)
  112. return flowanalog->getCNNType();
  113. return t_CNNType::None;
  114. }
  115. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  116. void ClassFlowControll::DigitalDrawROI(CImageBasis *_zw)
  117. {
  118. if (flowdigit)
  119. flowdigit->DrawROI(_zw);
  120. }
  121. void ClassFlowControll::AnalogDrawROI(CImageBasis *_zw)
  122. {
  123. if (flowanalog)
  124. flowanalog->DrawROI(_zw);
  125. }
  126. #endif
  127. #ifdef ENABLE_MQTT
  128. bool ClassFlowControll::StartMQTTService()
  129. {
  130. /* Start the MQTT service */
  131. for (int i = 0; i < FlowControll.size(); ++i) {
  132. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0) {
  133. return ((ClassFlowMQTT*) (FlowControll[i]))->Start(AutoInterval);
  134. }
  135. }
  136. return false;
  137. }
  138. #endif //ENABLE_MQTT
  139. void ClassFlowControll::SetInitialParameter(void)
  140. {
  141. AutoStart = false;
  142. SetupModeActive = false;
  143. AutoInterval = 10; // Minutes
  144. flowdigit = NULL;
  145. flowanalog = NULL;
  146. flowpostprocessing = NULL;
  147. disabled = false;
  148. aktRunNr = 0;
  149. aktstatus = "Flow task not yet created";
  150. aktstatusWithTime = aktstatus;
  151. }
  152. bool ClassFlowControll::getIsAutoStart(void)
  153. {
  154. return AutoStart;
  155. }
  156. void ClassFlowControll::setAutoStartInterval(long &_interval)
  157. {
  158. _interval = AutoInterval * 60 * 1000; // AutoInterval: minutes -> ms
  159. }
  160. ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
  161. {
  162. ClassFlow* cfc = NULL;
  163. _type = trim(_type);
  164. if (toUpper(_type).compare("[TAKEIMAGE]") == 0)
  165. {
  166. cfc = new ClassFlowTakeImage(&FlowControll);
  167. flowtakeimage = (ClassFlowTakeImage*) cfc;
  168. }
  169. if (toUpper(_type).compare("[ALIGNMENT]") == 0)
  170. {
  171. cfc = new ClassFlowAlignment(&FlowControll);
  172. flowalignment = (ClassFlowAlignment*) cfc;
  173. }
  174. if (toUpper(_type).compare("[ANALOG]") == 0)
  175. {
  176. cfc = new ClassFlowCNNGeneral(flowalignment);
  177. flowanalog = (ClassFlowCNNGeneral*) cfc;
  178. }
  179. if (toUpper(_type).compare(0, 7, "[DIGITS") == 0)
  180. {
  181. cfc = new ClassFlowCNNGeneral(flowalignment);
  182. flowdigit = (ClassFlowCNNGeneral*) cfc;
  183. }
  184. #ifdef ENABLE_MQTT
  185. if (toUpper(_type).compare("[MQTT]") == 0)
  186. cfc = new ClassFlowMQTT(&FlowControll);
  187. #endif //ENABLE_MQTT
  188. #ifdef ENABLE_INFLUXDB
  189. if (toUpper(_type).compare("[INFLUXDB]") == 0)
  190. cfc = new ClassFlowInfluxDB(&FlowControll);
  191. if (toUpper(_type).compare("[INFLUXDBV2]") == 0)
  192. cfc = new ClassFlowInfluxDBv2(&FlowControll);
  193. #endif //ENABLE_INFLUXDB
  194. if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
  195. {
  196. cfc = new ClassFlowPostProcessing(&FlowControll, flowanalog, flowdigit);
  197. flowpostprocessing = (ClassFlowPostProcessing*) cfc;
  198. }
  199. if (cfc) // Attached only if it is not [AutoTimer], because this is for FlowControll
  200. FlowControll.push_back(cfc);
  201. if (toUpper(_type).compare("[AUTOTIMER]") == 0)
  202. cfc = this;
  203. if (toUpper(_type).compare("[DATALOGGING]") == 0)
  204. cfc = this;
  205. if (toUpper(_type).compare("[DEBUG]") == 0)
  206. cfc = this;
  207. if (toUpper(_type).compare("[SYSTEM]") == 0)
  208. cfc = this;
  209. return cfc;
  210. }
  211. void ClassFlowControll::InitFlow(std::string config)
  212. {
  213. aktstatus = "Initialization";
  214. aktstatusWithTime = aktstatus;
  215. //#ifdef ENABLE_MQTT
  216. //MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization", 1, false); // Right now, not possible -> MQTT Service is going to be started later
  217. //#endif //ENABLE_MQTT
  218. string line;
  219. flowpostprocessing = NULL;
  220. ClassFlow* cfc;
  221. FILE* pFile;
  222. config = FormatFileName(config);
  223. pFile = fopen(config.c_str(), "r");
  224. line = "";
  225. char zw[1024];
  226. if (pFile != NULL)
  227. {
  228. fgets(zw, 1024, pFile);
  229. ESP_LOGD(TAG, "%s", zw);
  230. line = std::string(zw);
  231. }
  232. while ((line.size() > 0) && !(feof(pFile)))
  233. {
  234. cfc = CreateClassFlow(line);
  235. // printf("Name: %s\n", cfc->name().c_str());
  236. if (cfc)
  237. {
  238. ESP_LOGD(TAG, "Start ReadParameter (%s)", line.c_str());
  239. cfc->ReadParameter(pFile, line);
  240. }
  241. else
  242. {
  243. line = "";
  244. if (fgets(zw, 1024, pFile) && !feof(pFile))
  245. {
  246. ESP_LOGD(TAG, "Read: %s", zw);
  247. line = std::string(zw);
  248. }
  249. }
  250. }
  251. fclose(pFile);
  252. }
  253. std::string* ClassFlowControll::getActStatusWithTime()
  254. {
  255. return &aktstatusWithTime;
  256. }
  257. std::string* ClassFlowControll::getActStatus()
  258. {
  259. return &aktstatus;
  260. }
  261. void ClassFlowControll::setActStatus(std::string _aktstatus)
  262. {
  263. aktstatus = _aktstatus;
  264. aktstatusWithTime = aktstatus;
  265. }
  266. void ClassFlowControll::doFlowTakeImageOnly(string time)
  267. {
  268. std::string zw_time;
  269. for (int i = 0; i < FlowControll.size(); ++i)
  270. {
  271. if (FlowControll[i]->name() == "ClassFlowTakeImage") {
  272. zw_time = getCurrentTimeString("%H:%M:%S");
  273. aktstatus = TranslateAktstatus(FlowControll[i]->name());
  274. aktstatusWithTime = aktstatus + " (" + zw_time + ")";
  275. #ifdef ENABLE_MQTT
  276. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, 1, false);
  277. #endif //ENABLE_MQTT
  278. FlowControll[i]->doFlow(time);
  279. }
  280. }
  281. }
  282. bool ClassFlowControll::doFlow(string time)
  283. {
  284. bool result = true;
  285. std::string zw_time;
  286. int repeat = 0;
  287. int qos = 1;
  288. #ifdef DEBUG_DETAIL_ON
  289. LogFile.WriteHeapInfo("ClassFlowControll::doFlow - Start");
  290. #endif
  291. /* Check if we have a valid date/time and if not restart the NTP client */
  292. /* if (! getTimeIsSet()) {
  293. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time not set, restarting NTP Client!");
  294. restartNtpClient();
  295. }*/
  296. //checkNtpStatus(0);
  297. for (int i = 0; i < FlowControll.size(); ++i)
  298. {
  299. zw_time = getCurrentTimeString("%H:%M:%S");
  300. aktstatus = TranslateAktstatus(FlowControll[i]->name());
  301. aktstatusWithTime = aktstatus + " (" + zw_time + ")";
  302. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Status: " + aktstatusWithTime);
  303. #ifdef ENABLE_MQTT
  304. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
  305. #endif //ENABLE_MQTT
  306. #ifdef DEBUG_DETAIL_ON
  307. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  308. LogFile.WriteHeapInfo(zw);
  309. #endif
  310. if (!FlowControll[i]->doFlow(time)){
  311. repeat++;
  312. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  313. if (i) i -= 1; // vPrevious step must be repeated (probably take pictures)
  314. result = false;
  315. if (repeat > 5) {
  316. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Wiederholung 5x nicht erfolgreich --> reboot");
  317. doReboot();
  318. //Step was repeated 5x --> reboot
  319. }
  320. }
  321. else
  322. {
  323. result = true;
  324. }
  325. #ifdef DEBUG_DETAIL_ON
  326. LogFile.WriteHeapInfo("ClassFlowControll::doFlow");
  327. #endif
  328. }
  329. zw_time = getCurrentTimeString("%H:%M:%S");
  330. aktstatus = "Flow finished";
  331. aktstatusWithTime = aktstatus + " (" + zw_time + ")";
  332. //LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatusWithTime);
  333. #ifdef ENABLE_MQTT
  334. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
  335. #endif //ENABLE_MQTT
  336. return result;
  337. }
  338. string ClassFlowControll::getReadoutAll(int _type)
  339. {
  340. std::string out = "";
  341. if (flowpostprocessing)
  342. {
  343. std::vector<NumberPost*> *numbers = flowpostprocessing->GetNumbers();
  344. for (int i = 0; i < (*numbers).size(); ++i)
  345. {
  346. out = out + (*numbers)[i]->name + "\t";
  347. switch (_type) {
  348. case READOUT_TYPE_VALUE:
  349. out = out + (*numbers)[i]->ReturnValue;
  350. break;
  351. case READOUT_TYPE_PREVALUE:
  352. if (flowpostprocessing->PreValueUse)
  353. {
  354. if ((*numbers)[i]->PreValueOkay)
  355. out = out + (*numbers)[i]->ReturnPreValue;
  356. else
  357. out = out + "PreValue too old";
  358. }
  359. else
  360. out = out + "PreValue deactivated";
  361. break;
  362. case READOUT_TYPE_RAWVALUE:
  363. out = out + (*numbers)[i]->ReturnRawValue;
  364. break;
  365. case READOUT_TYPE_ERROR:
  366. out = out + (*numbers)[i]->ErrorMessageText;
  367. break;
  368. }
  369. if (i < (*numbers).size()-1)
  370. out = out + "\r\n";
  371. }
  372. // ESP_LOGD(TAG, "OUT: %s", out.c_str());
  373. }
  374. return out;
  375. }
  376. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false, int _number = 0)
  377. {
  378. if (flowpostprocessing)
  379. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror, _number);
  380. string zw = "";
  381. string result = "";
  382. for (int i = 0; i < FlowControll.size(); ++i)
  383. {
  384. zw = FlowControll[i]->getReadout();
  385. if (zw.length() > 0)
  386. {
  387. if (result.length() == 0)
  388. result = zw;
  389. else
  390. result = result + "\t" + zw;
  391. }
  392. }
  393. return result;
  394. }
  395. string ClassFlowControll::GetPrevalue(std::string _number)
  396. {
  397. if (flowpostprocessing)
  398. {
  399. return flowpostprocessing->GetPreValue(_number);
  400. }
  401. return std::string("");
  402. }
  403. bool ClassFlowControll::UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern)
  404. {
  405. double newvalueAsDouble;
  406. char* p;
  407. _newvalue = trim(_newvalue);
  408. //ESP_LOGD(TAG, "Input UpdatePreValue: %s", _newvalue.c_str());
  409. if (_newvalue.substr(0,8).compare("0.000000") == 0 || _newvalue.compare("0.0") == 0 || _newvalue.compare("0") == 0) {
  410. newvalueAsDouble = 0; // preset to value = 0
  411. }
  412. else {
  413. newvalueAsDouble = strtod(_newvalue.c_str(), &p);
  414. if (newvalueAsDouble == 0) {
  415. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "UpdatePrevalue: No valid value for processing: " + _newvalue);
  416. return false;
  417. }
  418. }
  419. if (flowpostprocessing) {
  420. if (flowpostprocessing->SetPreValue(newvalueAsDouble, _numbers, _extern))
  421. return true;
  422. else
  423. return false;
  424. }
  425. else {
  426. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "UpdatePrevalue: ERROR - Class Post-Processing not initialized");
  427. return false;
  428. }
  429. }
  430. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  431. {
  432. std::vector<string> splitted;
  433. aktparamgraph = trim(aktparamgraph);
  434. if (aktparamgraph.size() == 0)
  435. if (!this->GetNextParagraph(pfile, aktparamgraph))
  436. return false;
  437. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) &&
  438. (toUpper(aktparamgraph).compare("[SYSTEM]") != 0 && (toUpper(aktparamgraph).compare("[DATALOGGING]") != 0))) // Paragraph passt nicht zu Debug oder DataLogging
  439. return false;
  440. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  441. {
  442. splitted = ZerlegeZeile(aktparamgraph, " =");
  443. if ((toUpper(splitted[0]) == "AUTOSTART") && (splitted.size() > 1))
  444. {
  445. if (toUpper(splitted[1]) == "TRUE")
  446. {
  447. AutoStart = true;
  448. }
  449. }
  450. if ((toUpper(splitted[0]) == "INTERVAL") && (splitted.size() > 1))
  451. {
  452. AutoInterval = std::stof(splitted[1]);
  453. }
  454. if ((toUpper(splitted[0]) == "DATALOGACTIVE") && (splitted.size() > 1))
  455. {
  456. if (toUpper(splitted[1]) == "TRUE")
  457. {
  458. LogFile.SetDataLogToSD(true);
  459. }
  460. else {
  461. LogFile.SetDataLogToSD(false);
  462. }
  463. }
  464. if ((toUpper(splitted[0]) == "DATAFILESRETENTION") && (splitted.size() > 1))
  465. {
  466. LogFile.SetDataLogRetention(std::stoi(splitted[1]));
  467. }
  468. if ((toUpper(splitted[0]) == "LOGLEVEL") && (splitted.size() > 1))
  469. {
  470. /* matches esp_log_level_t */
  471. if ((toUpper(splitted[1]) == "TRUE") || (toUpper(splitted[1]) == "2"))
  472. {
  473. LogFile.setLogLevel(ESP_LOG_WARN);
  474. }
  475. else if ((toUpper(splitted[1]) == "FALSE") || (toUpper(splitted[1]) == "0") || (toUpper(splitted[1]) == "1"))
  476. {
  477. LogFile.setLogLevel(ESP_LOG_ERROR);
  478. }
  479. else if (toUpper(splitted[1]) == "3")
  480. {
  481. LogFile.setLogLevel(ESP_LOG_INFO);
  482. }
  483. else if (toUpper(splitted[1]) == "4")
  484. {
  485. LogFile.setLogLevel(ESP_LOG_DEBUG);
  486. }
  487. /* If system reboot was not triggered by user and reboot was caused by execption -> keep log level to DEBUG */
  488. if (!getIsPlannedReboot() && (esp_reset_reason() == ESP_RST_PANIC))
  489. LogFile.setLogLevel(ESP_LOG_DEBUG);
  490. }
  491. if ((toUpper(splitted[0]) == "LOGFILESRETENTION") && (splitted.size() > 1))
  492. {
  493. LogFile.SetLogFileRetention(std::stoi(splitted[1]));
  494. }
  495. /* TimeServer and TimeZone got already read from the config, see setupTime () */
  496. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  497. if ((toUpper(splitted[0]) == "RSSITHRESHOLD") && (splitted.size() > 1))
  498. {
  499. int RSSIThresholdTMP = atoi(splitted[1].c_str());
  500. RSSIThresholdTMP = min(0, max(-100, RSSIThresholdTMP)); // Verify input limits (-100 - 0)
  501. if (ChangeRSSIThreshold(WLAN_CONFIG_FILE, RSSIThresholdTMP))
  502. {
  503. // reboot necessary so that the new wlan.ini is also used !!!
  504. fclose(pfile);
  505. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new RSSITHRESHOLD ...");
  506. doReboot();
  507. }
  508. }
  509. #endif
  510. if ((toUpper(splitted[0]) == "HOSTNAME") && (splitted.size() > 1))
  511. {
  512. if (ChangeHostName(WLAN_CONFIG_FILE, splitted[1]))
  513. {
  514. // reboot necessary so that the new wlan.ini is also used !!!
  515. fclose(pfile);
  516. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new HOSTNAME...");
  517. doReboot();
  518. }
  519. }
  520. if ((toUpper(splitted[0]) == "SETUPMODE") && (splitted.size() > 1))
  521. {
  522. if (toUpper(splitted[1]) == "TRUE")
  523. {
  524. SetupModeActive = true;
  525. }
  526. }
  527. }
  528. return true;
  529. }
  530. int ClassFlowControll::CleanTempFolder() {
  531. const char* folderPath = "/sdcard/img_tmp";
  532. ESP_LOGD(TAG, "Clean up temporary folder to avoid damage of sdcard sectors: %s", folderPath);
  533. DIR *dir = opendir(folderPath);
  534. if (!dir) {
  535. ESP_LOGE(TAG, "Failed to stat dir: %s", folderPath);
  536. return -1;
  537. }
  538. struct dirent *entry;
  539. int deleted = 0;
  540. while ((entry = readdir(dir)) != NULL) {
  541. std::string path = string(folderPath) + "/" + entry->d_name;
  542. if (entry->d_type == DT_REG) {
  543. if (unlink(path.c_str()) == 0) {
  544. deleted ++;
  545. } else {
  546. ESP_LOGE(TAG, "can't delete file: %s", path.c_str());
  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 = GetAllDigital();
  698. ESP_LOGD(TAG, "After getClassFlowControll::GetAllDigital");
  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. }