ClassFlowControll.cpp 13 KB

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