ClassFlowControll.cpp 18 KB

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