ClassFlowControll.cpp 29 KB

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