ClassFlowControll.cpp 13 KB

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