ClassFlowControll.cpp 28 KB

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