ClassFlowControll.cpp 28 KB

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