ClassFlowControll.cpp 13 KB

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