ClassFlowControll.cpp 18 KB

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