ClassFlowControll.cpp 14 KB

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