ClassFlowControll.cpp 17 KB

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