ClassFlowControll.cpp 29 KB

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