ClassFlowControll.cpp 17 KB

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