ClassFlowControll.cpp 13 KB

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