ClassFlowControll.cpp 15 KB

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