ClassFlowControll.cpp 17 KB

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