ClassFlowControll.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. // #ifdef DEBUG_DETAIL_ON
  174. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  175. LogFile.WriteHeapInfo(zw);
  176. // #endif
  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. doReboot();
  315. }
  316. }
  317. if ((toUpper(zerlegt[0]) == "SETUPMODE") && (zerlegt.size() > 1))
  318. {
  319. if (toUpper(zerlegt[1]) == "TRUE")
  320. {
  321. SetupModeActive = true;
  322. }
  323. }
  324. }
  325. return true;
  326. }
  327. int ClassFlowControll::CleanTempFolder() {
  328. const char* folderPath = "/sdcard/img_tmp";
  329. ESP_LOGI(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath);
  330. DIR *dir = opendir(folderPath);
  331. if (!dir) {
  332. ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath);
  333. return -1;
  334. }
  335. struct dirent *entry;
  336. int deleted = 0;
  337. while ((entry = readdir(dir)) != NULL) {
  338. std::string path = string(folderPath) + "/" + entry->d_name;
  339. if (entry->d_type == DT_REG) {
  340. if (unlink(path.c_str()) == 0) {
  341. deleted ++;
  342. } else {
  343. ESP_LOGE(TAG, "can't delete file : %s", path.c_str());
  344. }
  345. } else if (entry->d_type == DT_DIR) {
  346. deleted += removeFolder(path.c_str(), TAG);
  347. }
  348. }
  349. closedir(dir);
  350. ESP_LOGI(TAG, "%d files deleted", deleted);
  351. return 0;
  352. }
  353. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  354. {
  355. return flowmakeimage->SendRawJPG(req);
  356. }
  357. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  358. {
  359. printf("ClassFlowControll::GetJPGStream %s\n", _fn.c_str());
  360. CImageBasis *_send = NULL;
  361. esp_err_t result = ESP_FAIL;
  362. bool Dodelete = false;
  363. if (_fn == "alg.jpg")
  364. {
  365. _send = flowalignment->ImageBasis;
  366. }
  367. if (_fn == "alg_roi.jpg")
  368. {
  369. CImageBasis* _imgzw = new CImageBasis(flowalignment->ImageBasis);
  370. flowalignment->DrawRef(_imgzw);
  371. if (flowdigit) flowdigit->DrawROI(_imgzw);
  372. if (flowanalog) flowanalog->DrawROI(_imgzw);
  373. _send = _imgzw;
  374. Dodelete = true;
  375. }
  376. std::vector<HTMLInfo*> htmlinfo;
  377. htmlinfo = GetAllDigital();
  378. for (int i = 0; i < htmlinfo.size(); ++i)
  379. {
  380. if (_fn == htmlinfo[i]->filename)
  381. {
  382. if (htmlinfo[i]->image)
  383. _send = htmlinfo[i]->image;
  384. }
  385. if (_fn == htmlinfo[i]->filename_org)
  386. {
  387. if (htmlinfo[i]->image_org)
  388. _send = htmlinfo[i]->image_org;
  389. }
  390. }
  391. htmlinfo = GetAllAnalog();
  392. for (int i = 0; i < htmlinfo.size(); ++i)
  393. {
  394. if (_fn == htmlinfo[i]->filename)
  395. {
  396. if (htmlinfo[i]->image)
  397. _send = htmlinfo[i]->image;
  398. }
  399. if (_fn == htmlinfo[i]->filename_org)
  400. {
  401. if (htmlinfo[i]->image_org)
  402. _send = htmlinfo[i]->image_org;
  403. }
  404. }
  405. if (_send)
  406. {
  407. ESP_LOGI(TAG, "Sending file : %s ...", _fn.c_str());
  408. set_content_type_from_file(req, _fn.c_str());
  409. result = _send->SendJPGtoHTTP(req);
  410. ESP_LOGI(TAG, "File sending complete");
  411. /* Respond with an empty chunk to signal HTTP response completion */
  412. httpd_resp_send_chunk(req, NULL, 0);
  413. }
  414. if (Dodelete)
  415. {
  416. delete _send;
  417. }
  418. return result;
  419. }