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