ClassFlowControll.cpp 17 KB

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