ClassFlowControll.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. if (i) 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::string out = "";
  248. if (flowpostprocessing)
  249. {
  250. std::vector<NumberPost*> *numbers = flowpostprocessing->GetNumbers();
  251. for (int i = 0; i < (*numbers).size(); ++i)
  252. {
  253. out = out + (*numbers)[i]->name + "\t";
  254. switch (_type) {
  255. case READOUT_TYPE_VALUE:
  256. out = out + (*numbers)[i]->ReturnValueNoError;
  257. break;
  258. case READOUT_TYPE_PREVALUE:
  259. if (flowpostprocessing->PreValueUse)
  260. {
  261. if ((*numbers)[i]->PreValueOkay)
  262. out = out + (*numbers)[i]->ReturnPreValue;
  263. else
  264. out = out + "PreValue too old";
  265. }
  266. else
  267. out = out + "PreValue deactivated";
  268. break;
  269. case READOUT_TYPE_RAWVALUE:
  270. out = out + (*numbers)[i]->ReturnRawValue;
  271. break;
  272. case READOUT_TYPE_ERROR:
  273. out = out + (*numbers)[i]->ErrorMessageText;
  274. break;
  275. }
  276. if (i < (*numbers).size()-1)
  277. out = out + "\r\n";
  278. }
  279. // printf("OUT: %s", out.c_str());
  280. }
  281. return out;
  282. }
  283. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  284. {
  285. if (flowpostprocessing)
  286. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  287. string zw = "";
  288. string result = "";
  289. for (int i = 0; i < FlowControll.size(); ++i)
  290. {
  291. zw = FlowControll[i]->getReadout();
  292. if (zw.length() > 0)
  293. {
  294. if (result.length() == 0)
  295. result = zw;
  296. else
  297. result = result + "\t" + zw;
  298. }
  299. }
  300. return result;
  301. }
  302. string ClassFlowControll::GetPrevalue(std::string _number)
  303. {
  304. if (flowpostprocessing)
  305. {
  306. return flowpostprocessing->GetPreValue(_number);
  307. }
  308. return std::string();
  309. }
  310. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern)
  311. {
  312. float zw;
  313. char* p;
  314. _newvalue = trim(_newvalue);
  315. // printf("Input UpdatePreValue: %s\n", _newvalue.c_str());
  316. if (_newvalue.compare("0.0") == 0)
  317. {
  318. zw = 0;
  319. }
  320. else
  321. {
  322. zw = strtof(_newvalue.c_str(), &p);
  323. if (zw == 0)
  324. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  325. }
  326. if (flowpostprocessing)
  327. {
  328. flowpostprocessing->SetPreValue(zw, _numbers, _extern);
  329. return _newvalue;
  330. }
  331. return std::string();
  332. }
  333. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  334. {
  335. std::vector<string> zerlegt;
  336. aktparamgraph = trim(aktparamgraph);
  337. if (aktparamgraph.size() == 0)
  338. if (!this->GetNextParagraph(pfile, aktparamgraph))
  339. return false;
  340. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && (toUpper(aktparamgraph).compare("[SYSTEM]") != 0)) // Paragraph passt nicht zu MakeImage
  341. return false;
  342. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  343. {
  344. zerlegt = this->ZerlegeZeile(aktparamgraph, " =");
  345. if ((toUpper(zerlegt[0]) == "AUTOSTART") && (zerlegt.size() > 1))
  346. {
  347. if (toUpper(zerlegt[1]) == "TRUE")
  348. {
  349. AutoStart = true;
  350. }
  351. }
  352. if ((toUpper(zerlegt[0]) == "INTERVALL") && (zerlegt.size() > 1))
  353. {
  354. AutoIntervall = std::stof(zerlegt[1]);
  355. }
  356. if ((toUpper(zerlegt[0]) == "LOGFILE") && (zerlegt.size() > 1))
  357. {
  358. if (toUpper(zerlegt[1]) == "TRUE")
  359. {
  360. LogFile.SwitchOnOff(true);
  361. }
  362. if (toUpper(zerlegt[1]) == "FALSE")
  363. {
  364. LogFile.SwitchOnOff(false);
  365. }
  366. }
  367. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  368. {
  369. LogFile.SetRetention(std::stoi(zerlegt[1]));
  370. }
  371. if ((toUpper(zerlegt[0]) == "TIMEZONE") && (zerlegt.size() > 1))
  372. {
  373. string zw = "Set TimeZone: " + zerlegt[1];
  374. setTimeZone(zerlegt[1]);
  375. }
  376. if ((toUpper(zerlegt[0]) == "TIMESERVER") && (zerlegt.size() > 1))
  377. {
  378. string zw = "Set TimeZone: " + zerlegt[1];
  379. reset_servername(zerlegt[1]);
  380. }
  381. if ((toUpper(zerlegt[0]) == "HOSTNAME") && (zerlegt.size() > 1))
  382. {
  383. if (ChangeHostName("/sdcard/wlan.ini", zerlegt[1]))
  384. {
  385. // reboot notwendig damit die neue wlan.ini auch benutzt wird !!!
  386. fclose(pfile);
  387. printf("do reboot\n");
  388. esp_restart();
  389. hard_restart();
  390. doReboot();
  391. }
  392. }
  393. if ((toUpper(zerlegt[0]) == "SETUPMODE") && (zerlegt.size() > 1))
  394. {
  395. if (toUpper(zerlegt[1]) == "TRUE")
  396. {
  397. SetupModeActive = true;
  398. }
  399. }
  400. if ((toUpper(zerlegt[0]) == "LOGLEVEL") && (zerlegt.size() > 1))
  401. {
  402. LogFile.setLogLevel(stoi(zerlegt[1]));
  403. }
  404. }
  405. return true;
  406. }
  407. int ClassFlowControll::CleanTempFolder() {
  408. const char* folderPath = "/sdcard/img_tmp";
  409. ESP_LOGI(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath);
  410. DIR *dir = opendir(folderPath);
  411. if (!dir) {
  412. ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath);
  413. return -1;
  414. }
  415. struct dirent *entry;
  416. int deleted = 0;
  417. while ((entry = readdir(dir)) != NULL) {
  418. std::string path = string(folderPath) + "/" + entry->d_name;
  419. if (entry->d_type == DT_REG) {
  420. if (unlink(path.c_str()) == 0) {
  421. deleted ++;
  422. } else {
  423. ESP_LOGE(TAG, "can't delete file : %s", path.c_str());
  424. }
  425. } else if (entry->d_type == DT_DIR) {
  426. deleted += removeFolder(path.c_str(), TAG);
  427. }
  428. }
  429. closedir(dir);
  430. ESP_LOGI(TAG, "%d files deleted", deleted);
  431. return 0;
  432. }
  433. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  434. {
  435. return flowmakeimage != NULL ? flowmakeimage->SendRawJPG(req) : ESP_FAIL;
  436. }
  437. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  438. {
  439. printf("ClassFlowControll::GetJPGStream %s\n", _fn.c_str());
  440. CImageBasis *_send = NULL;
  441. esp_err_t result = ESP_FAIL;
  442. bool Dodelete = false;
  443. if (flowalignment == NULL)
  444. {
  445. printf("Can't continue, flowalignment is NULL\n");
  446. return ESP_FAIL;
  447. }
  448. if (_fn == "alg.jpg")
  449. {
  450. _send = flowalignment->ImageBasis;
  451. }
  452. else
  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. _send = _imgzw;
  461. Dodelete = true;
  462. }
  463. else
  464. {
  465. std::vector<HTMLInfo*> htmlinfo;
  466. htmlinfo = GetAllDigital();
  467. for (int i = 0; i < htmlinfo.size(); ++i)
  468. {
  469. if (_fn == htmlinfo[i]->filename)
  470. {
  471. if (htmlinfo[i]->image)
  472. _send = htmlinfo[i]->image;
  473. }
  474. if (_fn == htmlinfo[i]->filename_org)
  475. {
  476. if (htmlinfo[i]->image_org)
  477. _send = htmlinfo[i]->image_org;
  478. }
  479. delete htmlinfo[i];
  480. }
  481. htmlinfo.clear();
  482. if (!_send)
  483. {
  484. htmlinfo = GetAllAnalog();
  485. for (int i = 0; i < htmlinfo.size(); ++i)
  486. {
  487. if (_fn == htmlinfo[i]->filename)
  488. {
  489. if (htmlinfo[i]->image)
  490. _send = htmlinfo[i]->image;
  491. }
  492. if (_fn == htmlinfo[i]->filename_org)
  493. {
  494. if (htmlinfo[i]->image_org)
  495. _send = htmlinfo[i]->image_org;
  496. }
  497. delete htmlinfo[i];
  498. }
  499. htmlinfo.clear();
  500. }
  501. }
  502. }
  503. if (_send)
  504. {
  505. ESP_LOGI(TAG, "Sending file : %s ...", _fn.c_str());
  506. set_content_type_from_file(req, _fn.c_str());
  507. result = _send->SendJPGtoHTTP(req);
  508. ESP_LOGI(TAG, "File sending complete");
  509. /* Respond with an empty chunk to signal HTTP response completion */
  510. httpd_resp_send_chunk(req, NULL, 0);
  511. }
  512. if (Dodelete)
  513. {
  514. delete _send;
  515. }
  516. return result;
  517. }
  518. string ClassFlowControll::getJSON()
  519. {
  520. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  521. std::string json="{\n";
  522. for (int i = 0; i < (*NUMBERS).size(); ++i)
  523. {
  524. json += "\"" + (*NUMBERS)[i]->name + "\":\n";
  525. json += " {\n";
  526. json += " \"value\": \"" + (*NUMBERS)[i]->ReturnValueNoError + "\",\n";
  527. json += " \"raw\": \"" + (*NUMBERS)[i]->ReturnRawValue + "\",\n";
  528. json += " \"error\": \"" + (*NUMBERS)[i]->ErrorMessageText + "\",\n";
  529. json += " \"rate\": \"" + std::to_string((*NUMBERS)[i]->FlowRateAct) + "\",\n";
  530. json += " \"timestamp\": \"" + (*NUMBERS)[i]->timeStamp + "\"\n";
  531. if ((i+1) < (*NUMBERS).size())
  532. json += " },\n";
  533. else
  534. json += " }\n";
  535. }
  536. json += "}";
  537. return json;
  538. }