ClassFlowControll.cpp 14 KB

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