ClassFlowControll.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. //#include "CImg.h"
  18. #include "server_help.h"
  19. //#define DEBUG_DETAIL_ON
  20. static const char* TAG = "flow_controll";
  21. std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host){
  22. std::string _classname = "";
  23. std::string result = "";
  24. // ESP_LOGD(TAG, "_stepname: %s", _stepname.c_str());
  25. if ((_stepname.compare("[MakeImage]") == 0) || (_stepname.compare(";[MakeImage]") == 0)){
  26. _classname = "ClassFlowMakeImage";
  27. }
  28. if ((_stepname.compare("[Alignment]") == 0) || (_stepname.compare(";[Alignment]") == 0)){
  29. _classname = "ClassFlowAlignment";
  30. }
  31. if ((_stepname.compare(0, 7, "[Digits") == 0) || (_stepname.compare(0, 8, ";[Digits") == 0)) {
  32. _classname = "ClassFlowCNNGeneral";
  33. }
  34. if ((_stepname.compare("[Analog]") == 0) || (_stepname.compare(";[Analog]") == 0)){
  35. _classname = "ClassFlowCNNGeneral";
  36. }
  37. if ((_stepname.compare("[MQTT]") == 0) || (_stepname.compare(";[MQTT]") == 0)){
  38. _classname = "ClassFlowMQTT";
  39. }
  40. if ((_stepname.compare("[InfluxDB]") == 0) || (_stepname.compare(";[InfluxDB]") == 0)){
  41. _classname = "ClassFlowInfluxDB";
  42. }
  43. for (int i = 0; i < FlowControll.size(); ++i)
  44. if (FlowControll[i]->name().compare(_classname) == 0){
  45. if (!(FlowControll[i]->name().compare("ClassFlowMakeImage") == 0)) // falls es ein MakeImage ist, braucht das Bild nicht extra aufgenommen zu werden, dass passiert bei html-Abfrage automatisch
  46. FlowControll[i]->doFlow("");
  47. result = FlowControll[i]->getHTMLSingleStep(_host);
  48. }
  49. return result;
  50. }
  51. std::string ClassFlowControll::TranslateAktstatus(std::string _input)
  52. {
  53. if (_input.compare("ClassFlowMakeImage") == 0)
  54. return ("Take Image");
  55. if (_input.compare("ClassFlowAlignment") == 0)
  56. return ("Aligning");
  57. if (_input.compare("ClassFlowCNNGeneral") == 0)
  58. return ("Digitalization of ROIs");
  59. if (_input.compare("ClassFlowMQTT") == 0)
  60. return ("Sending MQTT");
  61. if (_input.compare("ClassFlowInfluxDB") == 0)
  62. return ("Sending InfluxDB");
  63. if (_input.compare("ClassFlowPostProcessing") == 0)
  64. return ("Processing");
  65. if (_input.compare("ClassFlowWriteList") == 0)
  66. return ("Processing");
  67. return "Unkown Status";
  68. }
  69. std::vector<HTMLInfo*> ClassFlowControll::GetAllDigital()
  70. {
  71. if (flowdigit)
  72. {
  73. ESP_LOGD(TAG, "ClassFlowControll::GetAllDigital - flowdigit != NULL");
  74. return flowdigit->GetHTMLInfo();
  75. }
  76. std::vector<HTMLInfo*> empty;
  77. return empty;
  78. }
  79. std::vector<HTMLInfo*> ClassFlowControll::GetAllAnalog()
  80. {
  81. if (flowanalog)
  82. return flowanalog->GetHTMLInfo();
  83. std::vector<HTMLInfo*> empty;
  84. return empty;
  85. }
  86. t_CNNType ClassFlowControll::GetTypeDigital()
  87. {
  88. if (flowdigit)
  89. return flowdigit->getCNNType();
  90. return t_CNNType::None;
  91. }
  92. t_CNNType ClassFlowControll::GetTypeAnalog()
  93. {
  94. if (flowanalog)
  95. return flowanalog->getCNNType();
  96. return t_CNNType::None;
  97. }
  98. string ClassFlowControll::GetMQTTMainTopic()
  99. {
  100. for (int i = 0; i < FlowControll.size(); ++i)
  101. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0)
  102. return ((ClassFlowMQTT*) (FlowControll[i]))->GetMQTTMainTopic();
  103. return "";
  104. }
  105. void ClassFlowControll::SetInitialParameter(void)
  106. {
  107. AutoStart = false;
  108. SetupModeActive = false;
  109. AutoIntervall = 10;
  110. flowdigit = NULL;
  111. flowanalog = NULL;
  112. flowpostprocessing = NULL;
  113. disabled = false;
  114. aktRunNr = 0;
  115. aktstatus = "Booting ...";
  116. }
  117. bool ClassFlowControll::isAutoStart(long &_intervall)
  118. {
  119. _intervall = AutoIntervall * 60 * 1000; // AutoIntervall: Minuten -> ms
  120. return AutoStart;
  121. }
  122. int ClassFlowControll::getAutoInterval()
  123. {
  124. return AutoIntervall * 60; // AutoIntervall: Minuten -> seconds
  125. }
  126. ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
  127. {
  128. ClassFlow* cfc = NULL;
  129. _type = trim(_type);
  130. if (toUpper(_type).compare("[MAKEIMAGE]") == 0)
  131. {
  132. cfc = new ClassFlowMakeImage(&FlowControll);
  133. flowmakeimage = (ClassFlowMakeImage*) cfc;
  134. }
  135. if (toUpper(_type).compare("[ALIGNMENT]") == 0)
  136. {
  137. cfc = new ClassFlowAlignment(&FlowControll);
  138. flowalignment = (ClassFlowAlignment*) cfc;
  139. }
  140. if (toUpper(_type).compare("[ANALOG]") == 0)
  141. {
  142. cfc = new ClassFlowCNNGeneral(flowalignment);
  143. flowanalog = (ClassFlowCNNGeneral*) cfc;
  144. }
  145. if (toUpper(_type).compare(0, 7, "[DIGITS") == 0)
  146. {
  147. cfc = new ClassFlowCNNGeneral(flowalignment);
  148. flowdigit = (ClassFlowCNNGeneral*) cfc;
  149. }
  150. if (toUpper(_type).compare("[MQTT]") == 0)
  151. cfc = new ClassFlowMQTT(&FlowControll);
  152. if (toUpper(_type).compare("[INFLUXDB]") == 0)
  153. cfc = new ClassFlowInfluxDB(&FlowControll);
  154. if (toUpper(_type).compare("[WRITELIST]") == 0)
  155. cfc = new ClassFlowWriteList(&FlowControll);
  156. if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
  157. {
  158. cfc = new ClassFlowPostProcessing(&FlowControll, flowanalog, flowdigit);
  159. flowpostprocessing = (ClassFlowPostProcessing*) cfc;
  160. }
  161. if (cfc) // Wird nur angehangen, falls es nicht [AutoTimer] ist, denn dieses ist für FlowControll
  162. FlowControll.push_back(cfc);
  163. if (toUpper(_type).compare("[AUTOTIMER]") == 0)
  164. cfc = this;
  165. if (toUpper(_type).compare("[DEBUG]") == 0)
  166. cfc = this;
  167. if (toUpper(_type).compare("[SYSTEM]") == 0)
  168. cfc = this;
  169. return cfc;
  170. }
  171. void ClassFlowControll::InitFlow(std::string config)
  172. {
  173. string line;
  174. flowpostprocessing = NULL;
  175. ClassFlow* cfc;
  176. FILE* pFile;
  177. config = FormatFileName(config);
  178. pFile = OpenFileAndWait(config.c_str(), "r");
  179. line = "";
  180. char zw[1024];
  181. if (pFile != NULL)
  182. {
  183. fgets(zw, 1024, pFile);
  184. ESP_LOGD(TAG, "%s", zw);
  185. line = std::string(zw);
  186. }
  187. while ((line.size() > 0) && !(feof(pFile)))
  188. {
  189. cfc = CreateClassFlow(line);
  190. if (cfc)
  191. {
  192. ESP_LOGD(TAG, "Start ReadParameter (%s)", line.c_str());
  193. cfc->ReadParameter(pFile, line);
  194. }
  195. else
  196. {
  197. line = "";
  198. if (fgets(zw, 1024, pFile) && !feof(pFile))
  199. {
  200. ESP_LOGD(TAG, "Read: %s", zw);
  201. line = std::string(zw);
  202. }
  203. }
  204. }
  205. fclose(pFile);
  206. }
  207. std::string* ClassFlowControll::getActStatus(){
  208. return &aktstatus;
  209. }
  210. void ClassFlowControll::doFlowMakeImageOnly(string time){
  211. std::string zw_time;
  212. for (int i = 0; i < FlowControll.size(); ++i)
  213. {
  214. if (FlowControll[i]->name() == "ClassFlowMakeImage") {
  215. // zw_time = gettimestring("%Y%m%d-%H%M%S");
  216. zw_time = gettimestring("%H:%M:%S");
  217. aktstatus = TranslateAktstatus(FlowControll[i]->name()) + " (" + zw_time + ")";
  218. FlowControll[i]->doFlow(time);
  219. }
  220. }
  221. }
  222. bool ClassFlowControll::doFlow(string time)
  223. {
  224. // CleanTempFolder(); // dazu muss man noch eine Rolling einführen
  225. bool result = true;
  226. std::string zw_time;
  227. int repeat = 0;
  228. #ifdef DEBUG_DETAIL_ON
  229. LogFile.WriteHeapInfo("ClassFlowControll::doFlow - Start");
  230. #endif
  231. for (int i = 0; i < FlowControll.size(); ++i)
  232. {
  233. zw_time = gettimestring("%H:%M:%S");
  234. aktstatus = TranslateAktstatus(FlowControll[i]->name()) + " (" + zw_time + ")";
  235. // zw_time = gettimestring("%Y%m%d-%H%M%S");
  236. // aktstatus = zw_time + ": " + FlowControll[i]->name();
  237. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  238. LogFile.WriteHeapInfo(zw);
  239. if (!FlowControll[i]->doFlow(time)){
  240. repeat++;
  241. LogFile.WriteToFile(ESP_LOG_WARN, "Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  242. if (i) i -= 1; // vorheriger Schritt muss wiederholt werden (vermutlich Bilder aufnehmen)
  243. result = false;
  244. if (repeat > 5) {
  245. LogFile.WriteToFile(ESP_LOG_ERROR, "Wiederholung 5x nicht erfolgreich --> reboot");
  246. doReboot();
  247. // Schritt wurde 5x wiederholt --> reboot
  248. }
  249. }
  250. else
  251. {
  252. result = true;
  253. }
  254. #ifdef DEBUG_DETAIL_ON
  255. LogFile.WriteHeapInfo("ClassFlowControll::doFlow");
  256. #endif
  257. }
  258. zw_time = gettimestring("%H:%M:%S");
  259. aktstatus = "Flow finished (" + zw_time + ")";
  260. return result;
  261. }
  262. string ClassFlowControll::getReadoutAll(int _type)
  263. {
  264. std::string out = "";
  265. if (flowpostprocessing)
  266. {
  267. std::vector<NumberPost*> *numbers = flowpostprocessing->GetNumbers();
  268. for (int i = 0; i < (*numbers).size(); ++i)
  269. {
  270. out = out + (*numbers)[i]->name + "\t";
  271. switch (_type) {
  272. case READOUT_TYPE_VALUE:
  273. out = out + (*numbers)[i]->ReturnValue;
  274. break;
  275. case READOUT_TYPE_PREVALUE:
  276. if (flowpostprocessing->PreValueUse)
  277. {
  278. if ((*numbers)[i]->PreValueOkay)
  279. out = out + (*numbers)[i]->ReturnPreValue;
  280. else
  281. out = out + "PreValue too old";
  282. }
  283. else
  284. out = out + "PreValue deactivated";
  285. break;
  286. case READOUT_TYPE_RAWVALUE:
  287. out = out + (*numbers)[i]->ReturnRawValue;
  288. break;
  289. case READOUT_TYPE_ERROR:
  290. out = out + (*numbers)[i]->ErrorMessageText;
  291. break;
  292. }
  293. if (i < (*numbers).size()-1)
  294. out = out + "\r\n";
  295. }
  296. // ESP_LOGD(TAG, "OUT: %s", out.c_str());
  297. }
  298. return out;
  299. }
  300. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  301. {
  302. if (flowpostprocessing)
  303. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  304. string zw = "";
  305. string result = "";
  306. for (int i = 0; i < FlowControll.size(); ++i)
  307. {
  308. zw = FlowControll[i]->getReadout();
  309. if (zw.length() > 0)
  310. {
  311. if (result.length() == 0)
  312. result = zw;
  313. else
  314. result = result + "\t" + zw;
  315. }
  316. }
  317. return result;
  318. }
  319. string ClassFlowControll::GetPrevalue(std::string _number)
  320. {
  321. if (flowpostprocessing)
  322. {
  323. return flowpostprocessing->GetPreValue(_number);
  324. }
  325. return std::string();
  326. }
  327. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern)
  328. {
  329. double zw;
  330. char* p;
  331. _newvalue = trim(_newvalue);
  332. // ESP_LOGD(TAG, "Input UpdatePreValue: %s", _newvalue.c_str());
  333. if (_newvalue.compare("0.0") == 0)
  334. {
  335. zw = 0;
  336. }
  337. else
  338. {
  339. zw = strtod(_newvalue.c_str(), &p);
  340. if (zw == 0)
  341. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  342. }
  343. if (flowpostprocessing)
  344. {
  345. flowpostprocessing->SetPreValue(zw, _numbers, _extern);
  346. return _newvalue;
  347. }
  348. return std::string();
  349. }
  350. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  351. {
  352. std::vector<string> zerlegt;
  353. aktparamgraph = trim(aktparamgraph);
  354. if (aktparamgraph.size() == 0)
  355. if (!this->GetNextParagraph(pfile, aktparamgraph))
  356. return false;
  357. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && (toUpper(aktparamgraph).compare("[SYSTEM]") != 0)) // Paragraph passt nicht zu MakeImage
  358. return false;
  359. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  360. {
  361. zerlegt = this->ZerlegeZeile(aktparamgraph, " =");
  362. if ((toUpper(zerlegt[0]) == "AUTOSTART") && (zerlegt.size() > 1))
  363. {
  364. if (toUpper(zerlegt[1]) == "TRUE")
  365. {
  366. AutoStart = true;
  367. }
  368. }
  369. if ((toUpper(zerlegt[0]) == "INTERVALL") && (zerlegt.size() > 1))
  370. {
  371. AutoIntervall = std::stof(zerlegt[1]);
  372. }
  373. if ((toUpper(zerlegt[0]) == "LOGFILE") && (zerlegt.size() > 1))
  374. {
  375. if (toUpper(zerlegt[1]) == "TRUE")
  376. {
  377. LogFile.SwitchOnOff(true);
  378. }
  379. if (toUpper(zerlegt[1]) == "FALSE")
  380. {
  381. LogFile.SwitchOnOff(false);
  382. }
  383. }
  384. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  385. {
  386. LogFile.SetRetention(std::stoi(zerlegt[1]));
  387. }
  388. if ((toUpper(zerlegt[0]) == "TIMEZONE") && (zerlegt.size() > 1))
  389. {
  390. string zw = "Set TimeZone: " + zerlegt[1];
  391. setTimeZone(zerlegt[1]);
  392. }
  393. if ((toUpper(zerlegt[0]) == "TIMESERVER") && (zerlegt.size() > 1))
  394. {
  395. string zw = "Set TimeZone: " + zerlegt[1];
  396. reset_servername(zerlegt[1]);
  397. }
  398. if ((toUpper(zerlegt[0]) == "HOSTNAME") && (zerlegt.size() > 1))
  399. {
  400. if (ChangeHostName("/sdcard/wlan.ini", zerlegt[1]))
  401. {
  402. // reboot notwendig damit die neue wlan.ini auch benutzt wird !!!
  403. fclose(pfile);
  404. LogFile.WriteToFile(ESP_LOG_ERROR, "Rebooting to activate new HOSTNAME...");
  405. esp_restart();
  406. hard_restart();
  407. doReboot();
  408. }
  409. }
  410. if ((toUpper(zerlegt[0]) == "SETUPMODE") && (zerlegt.size() > 1))
  411. {
  412. if (toUpper(zerlegt[1]) == "TRUE")
  413. {
  414. SetupModeActive = true;
  415. }
  416. }
  417. if ((toUpper(zerlegt[0]) == "LOGLEVEL") && (zerlegt.size() > 1)) // TODO there seems to be no such parameter in the config, but there is one called "Debug_Logfile_value1"!
  418. {
  419. LogFile.setLogLevel((esp_log_level_t)(stoi(zerlegt[1]))); // Gets mapped to esp_log_level_t
  420. }
  421. }
  422. return true;
  423. }
  424. int ClassFlowControll::CleanTempFolder() {
  425. const char* folderPath = "/sdcard/img_tmp";
  426. ESP_LOGI(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath);
  427. DIR *dir = opendir(folderPath);
  428. if (!dir) {
  429. ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath);
  430. return -1;
  431. }
  432. struct dirent *entry;
  433. int deleted = 0;
  434. while ((entry = readdir(dir)) != NULL) {
  435. std::string path = string(folderPath) + "/" + entry->d_name;
  436. if (entry->d_type == DT_REG) {
  437. if (unlink(path.c_str()) == 0) {
  438. deleted ++;
  439. } else {
  440. ESP_LOGE(TAG, "can't delete file : %s", path.c_str());
  441. }
  442. } else if (entry->d_type == DT_DIR) {
  443. deleted += removeFolder(path.c_str(), TAG);
  444. }
  445. }
  446. closedir(dir);
  447. ESP_LOGI(TAG, "%d files deleted", deleted);
  448. return 0;
  449. }
  450. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  451. {
  452. return flowmakeimage != NULL ? flowmakeimage->SendRawJPG(req) : ESP_FAIL;
  453. }
  454. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  455. {
  456. ESP_LOGD(TAG, "ClassFlowControll::GetJPGStream %s", _fn.c_str());
  457. CImageBasis *_send = NULL;
  458. esp_err_t result = ESP_FAIL;
  459. bool Dodelete = false;
  460. if (flowalignment == NULL)
  461. {
  462. ESP_LOGD(TAG, "Can't continue, flowalignment is NULL");
  463. return ESP_FAIL;
  464. }
  465. if (_fn == "alg.jpg")
  466. {
  467. _send = flowalignment->ImageBasis;
  468. }
  469. else
  470. {
  471. if (_fn == "alg_roi.jpg")
  472. {
  473. CImageBasis* _imgzw = new CImageBasis(flowalignment->ImageBasis);
  474. flowalignment->DrawRef(_imgzw);
  475. if (flowdigit) flowdigit->DrawROI(_imgzw);
  476. if (flowanalog) flowanalog->DrawROI(_imgzw);
  477. _send = _imgzw;
  478. Dodelete = true;
  479. }
  480. else
  481. {
  482. std::vector<HTMLInfo*> htmlinfo;
  483. htmlinfo = GetAllDigital();
  484. ESP_LOGD(TAG, "After getClassFlowControll::GetAllDigital");
  485. for (int i = 0; i < htmlinfo.size(); ++i)
  486. {
  487. if (_fn == htmlinfo[i]->filename)
  488. {
  489. if (htmlinfo[i]->image)
  490. _send = htmlinfo[i]->image;
  491. }
  492. if (_fn == htmlinfo[i]->filename_org)
  493. {
  494. if (htmlinfo[i]->image_org)
  495. _send = htmlinfo[i]->image_org;
  496. }
  497. delete htmlinfo[i];
  498. }
  499. htmlinfo.clear();
  500. if (!_send)
  501. {
  502. htmlinfo = GetAllAnalog();
  503. for (int i = 0; i < htmlinfo.size(); ++i)
  504. {
  505. if (_fn == htmlinfo[i]->filename)
  506. {
  507. if (htmlinfo[i]->image)
  508. _send = htmlinfo[i]->image;
  509. }
  510. if (_fn == htmlinfo[i]->filename_org)
  511. {
  512. if (htmlinfo[i]->image_org)
  513. _send = htmlinfo[i]->image_org;
  514. }
  515. delete htmlinfo[i];
  516. }
  517. htmlinfo.clear();
  518. }
  519. }
  520. }
  521. if (_send)
  522. {
  523. ESP_LOGI(TAG, "Sending file : %s ...", _fn.c_str());
  524. set_content_type_from_file(req, _fn.c_str());
  525. result = _send->SendJPGtoHTTP(req);
  526. ESP_LOGI(TAG, "File sending complete");
  527. /* Respond with an empty chunk to signal HTTP response completion */
  528. httpd_resp_send_chunk(req, NULL, 0);
  529. }
  530. if (Dodelete)
  531. {
  532. delete _send;
  533. }
  534. return result;
  535. }
  536. string ClassFlowControll::getNumbersName()
  537. {
  538. return flowpostprocessing->getNumbersName();
  539. }
  540. string ClassFlowControll::getJSON(std::string _id, std::string _mac)
  541. {
  542. return flowpostprocessing->GetJSON(_id, _mac);
  543. }