ClassFlowControll.cpp 13 KB

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