ClassFlowControll.cpp 18 KB

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