ClassFlowControll.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. fgets(zw, 1024, pFile);
  144. printf("%s", zw);
  145. line = std::string(zw);
  146. }
  147. }
  148. fclose(pFile);
  149. }
  150. std::string ClassFlowControll::getActStatus(){
  151. return aktstatus;
  152. }
  153. void ClassFlowControll::doFlowMakeImageOnly(string time){
  154. std::string zw_time;
  155. for (int i = 0; i < FlowControll.size(); ++i)
  156. {
  157. if (FlowControll[i]->name() == "ClassFlowMakeImage") {
  158. zw_time = gettimestring("%Y%m%d-%H%M%S");
  159. aktstatus = zw_time + ": " + FlowControll[i]->name();
  160. string zw = "FlowControll.doFlowMakeImageOnly - " + FlowControll[i]->name();
  161. FlowControll[i]->doFlow(time);
  162. }
  163. }
  164. }
  165. bool ClassFlowControll::doFlow(string time)
  166. {
  167. // CleanTempFolder(); // dazu muss man noch eine Rolling einführen
  168. bool result = true;
  169. std::string zw_time;
  170. int repeat = 0;
  171. #ifdef DEBUG_DETAIL_ON
  172. LogFile.WriteHeapInfo("ClassFlowAnalog::doFlow - Start");
  173. #endif
  174. for (int i = 0; i < FlowControll.size(); ++i)
  175. {
  176. zw_time = gettimestring("%Y%m%d-%H%M%S");
  177. aktstatus = zw_time + ": " + FlowControll[i]->name();
  178. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  179. LogFile.WriteHeapInfo(zw);
  180. if (!FlowControll[i]->doFlow(time)){
  181. repeat++;
  182. LogFile.WriteToFile("Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  183. i = -1; // vorheriger Schritt muss wiederholt werden (vermutlich Bilder aufnehmen)
  184. result = false;
  185. if (repeat > 5) {
  186. LogFile.WriteToFile("Wiederholung 5x nicht erfolgreich --> reboot");
  187. doReboot();
  188. // Schritt wurde 5x wiederholt --> reboot
  189. }
  190. }
  191. else
  192. {
  193. result = true;
  194. }
  195. #ifdef DEBUG_DETAIL_ON
  196. LogFile.WriteHeapInfo("ClassFlowAnalog::doFlow");
  197. #endif
  198. }
  199. zw_time = gettimestring("%Y%m%d-%H%M%S");
  200. aktstatus = zw_time + ": Flow is done";
  201. return result;
  202. }
  203. void ClassFlowControll::UpdateAktStatus(std::string _flow)
  204. {
  205. aktstatus = gettimestring("%Y%m%d-%H%M%S");
  206. aktstatus = aktstatus + "\t" + std::to_string(aktRunNr) + "\t";
  207. if (_flow == "ClassFlowMakeImage")
  208. aktstatus = aktstatus + "Taking Raw Image";
  209. else
  210. if (_flow == "ClassFlowAlignment")
  211. aktstatus = aktstatus + "Aligning Image";
  212. }
  213. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  214. {
  215. if (flowpostprocessing)
  216. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  217. string zw = "";
  218. string result = "";
  219. for (int i = 0; i < FlowControll.size(); ++i)
  220. {
  221. zw = FlowControll[i]->getReadout();
  222. if (zw.length() > 0)
  223. {
  224. if (result.length() == 0)
  225. result = zw;
  226. else
  227. result = result + "\t" + zw;
  228. }
  229. }
  230. return result;
  231. }
  232. string ClassFlowControll::GetPrevalue()
  233. {
  234. if (flowpostprocessing)
  235. {
  236. return flowpostprocessing->GetPreValue();
  237. }
  238. return std::string();
  239. }
  240. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue)
  241. {
  242. float zw;
  243. char* p;
  244. _newvalue = trim(_newvalue);
  245. // printf("Input UpdatePreValue: %s\n", _newvalue.c_str());
  246. if (_newvalue.compare("0.0") == 0)
  247. {
  248. zw = 0;
  249. }
  250. else
  251. {
  252. zw = strtof(_newvalue.c_str(), &p);
  253. if (zw == 0)
  254. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  255. }
  256. if (flowpostprocessing)
  257. {
  258. flowpostprocessing->SavePreValue(zw);
  259. return _newvalue;
  260. }
  261. return std::string();
  262. }
  263. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  264. {
  265. std::vector<string> zerlegt;
  266. aktparamgraph = trim(aktparamgraph);
  267. if (aktparamgraph.size() == 0)
  268. if (!this->GetNextParagraph(pfile, aktparamgraph))
  269. return false;
  270. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && (toUpper(aktparamgraph).compare("[SYSTEM]") != 0)) // Paragraph passt nicht zu MakeImage
  271. return false;
  272. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  273. {
  274. zerlegt = this->ZerlegeZeile(aktparamgraph, " =");
  275. if ((toUpper(zerlegt[0]) == "AUTOSTART") && (zerlegt.size() > 1))
  276. {
  277. if (toUpper(zerlegt[1]) == "TRUE")
  278. {
  279. AutoStart = true;
  280. }
  281. }
  282. if ((toUpper(zerlegt[0]) == "INTERVALL") && (zerlegt.size() > 1))
  283. {
  284. AutoIntervall = std::stof(zerlegt[1]);
  285. }
  286. if ((toUpper(zerlegt[0]) == "LOGFILE") && (zerlegt.size() > 1))
  287. {
  288. if (toUpper(zerlegt[1]) == "TRUE")
  289. {
  290. LogFile.SwitchOnOff(true);
  291. }
  292. if (toUpper(zerlegt[1]) == "FALSE")
  293. {
  294. LogFile.SwitchOnOff(false);
  295. }
  296. }
  297. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  298. {
  299. LogFile.SetRetention(std::stoi(zerlegt[1]));
  300. }
  301. if ((toUpper(zerlegt[0]) == "TIMEZONE") && (zerlegt.size() > 1))
  302. {
  303. string zw = "Set TimeZone: " + zerlegt[1];
  304. setTimeZone(zerlegt[1]);
  305. }
  306. if ((toUpper(zerlegt[0]) == "TIMESERVER") && (zerlegt.size() > 1))
  307. {
  308. string zw = "Set TimeZone: " + zerlegt[1];
  309. reset_servername(zerlegt[1]);
  310. }
  311. if ((toUpper(zerlegt[0]) == "HOSTNAME") && (zerlegt.size() > 1))
  312. {
  313. if (ChangeHostName("/sdcard/wlan.ini", zerlegt[1]))
  314. {
  315. // reboot notwendig damit die neue wlan.ini auch benutzt wird !!!
  316. fclose(pfile);
  317. printf("do reboot\n");
  318. esp_restart();
  319. hard_restart();
  320. doReboot();
  321. }
  322. }
  323. if ((toUpper(zerlegt[0]) == "SETUPMODE") && (zerlegt.size() > 1))
  324. {
  325. if (toUpper(zerlegt[1]) == "TRUE")
  326. {
  327. SetupModeActive = true;
  328. }
  329. }
  330. if ((toUpper(zerlegt[0]) == "LOGLEVEL") && (zerlegt.size() > 1))
  331. {
  332. LogFile.setLogLevel(stoi(zerlegt[1]));
  333. }
  334. }
  335. return true;
  336. }
  337. int ClassFlowControll::CleanTempFolder() {
  338. const char* folderPath = "/sdcard/img_tmp";
  339. ESP_LOGI(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath);
  340. DIR *dir = opendir(folderPath);
  341. if (!dir) {
  342. ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath);
  343. return -1;
  344. }
  345. struct dirent *entry;
  346. int deleted = 0;
  347. while ((entry = readdir(dir)) != NULL) {
  348. std::string path = string(folderPath) + "/" + entry->d_name;
  349. if (entry->d_type == DT_REG) {
  350. if (unlink(path.c_str()) == 0) {
  351. deleted ++;
  352. } else {
  353. ESP_LOGE(TAG, "can't delete file : %s", path.c_str());
  354. }
  355. } else if (entry->d_type == DT_DIR) {
  356. deleted += removeFolder(path.c_str(), TAG);
  357. }
  358. }
  359. closedir(dir);
  360. ESP_LOGI(TAG, "%d files deleted", deleted);
  361. return 0;
  362. }
  363. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  364. {
  365. return flowmakeimage != NULL ? flowmakeimage->SendRawJPG(req) : ESP_FAIL;
  366. }
  367. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  368. {
  369. printf("ClassFlowControll::GetJPGStream %s\n", _fn.c_str());
  370. CImageBasis *_send = NULL;
  371. esp_err_t result = ESP_FAIL;
  372. bool Dodelete = false;
  373. if (flowalignment == NULL)
  374. {
  375. printf("Can't continue, flowalignment is NULL\n");
  376. return ESP_FAIL;
  377. }
  378. if (_fn == "alg.jpg")
  379. {
  380. _send = flowalignment->ImageBasis;
  381. }
  382. if (_fn == "alg_roi.jpg")
  383. {
  384. CImageBasis* _imgzw = new CImageBasis(flowalignment->ImageBasis);
  385. flowalignment->DrawRef(_imgzw);
  386. if (flowdigit) flowdigit->DrawROI(_imgzw);
  387. if (flowanalog) flowanalog->DrawROI(_imgzw);
  388. _send = _imgzw;
  389. Dodelete = true;
  390. }
  391. std::vector<HTMLInfo*> htmlinfo;
  392. htmlinfo = GetAllDigital();
  393. for (int i = 0; i < htmlinfo.size(); ++i)
  394. {
  395. if (_fn == htmlinfo[i]->filename)
  396. {
  397. if (htmlinfo[i]->image)
  398. _send = htmlinfo[i]->image;
  399. }
  400. if (_fn == htmlinfo[i]->filename_org)
  401. {
  402. if (htmlinfo[i]->image_org)
  403. _send = htmlinfo[i]->image_org;
  404. }
  405. delete htmlinfo[i];
  406. }
  407. htmlinfo.clear();
  408. htmlinfo = GetAllAnalog();
  409. for (int i = 0; i < htmlinfo.size(); ++i)
  410. {
  411. if (_fn == htmlinfo[i]->filename)
  412. {
  413. if (htmlinfo[i]->image)
  414. _send = htmlinfo[i]->image;
  415. }
  416. if (_fn == htmlinfo[i]->filename_org)
  417. {
  418. if (htmlinfo[i]->image_org)
  419. _send = htmlinfo[i]->image_org;
  420. }
  421. delete htmlinfo[i];
  422. }
  423. htmlinfo.clear();
  424. if (_send)
  425. {
  426. ESP_LOGI(TAG, "Sending file : %s ...", _fn.c_str());
  427. set_content_type_from_file(req, _fn.c_str());
  428. result = _send->SendJPGtoHTTP(req);
  429. ESP_LOGI(TAG, "File sending complete");
  430. /* Respond with an empty chunk to signal HTTP response completion */
  431. httpd_resp_send_chunk(req, NULL, 0);
  432. }
  433. if (Dodelete)
  434. {
  435. delete _send;
  436. }
  437. return result;
  438. }