ClassFlowControll.cpp 13 KB

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