ClassFlowControll.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host){
  11. std::string _classname = "";
  12. std::string result = "";
  13. if (_stepname.compare("[MakeImage]") == 0){
  14. _classname = "ClassFlowMakeImage";
  15. }
  16. if (_stepname.compare("[Alignment]") == 0){
  17. _classname = "ClassFlowAlignment";
  18. }
  19. if (_stepname.compare("[Digits]") == 0){
  20. _classname = "ClassFlowDigit";
  21. }
  22. if (_stepname.compare("[Analog]") == 0){
  23. _classname = "ClassFlowAnalog";
  24. }
  25. if (_stepname.compare("[MQTT]") == 0){
  26. _classname = "ClassFlowMQTT";
  27. }
  28. // std::string zw = "Classname: " + _classname + "\n";
  29. // printf(zw.c_str());
  30. for (int i = 0; i < FlowControll.size(); ++i)
  31. if (FlowControll[i]->name().compare(_classname) == 0){
  32. // printf(FlowControll[i]->name().c_str()); printf("\n");
  33. FlowControll[i]->doFlow("");
  34. result = FlowControll[i]->getHTMLSingleStep(_host);
  35. }
  36. return result;
  37. }
  38. std::vector<HTMLInfo*> ClassFlowControll::GetAllDigital()
  39. {
  40. for (int i = 0; i < FlowControll.size(); ++i)
  41. if (FlowControll[i]->name().compare("ClassFlowDigit") == 0)
  42. return ((ClassFlowDigit*) (FlowControll[i]))->GetHTMLInfo();
  43. std::vector<HTMLInfo*> empty;
  44. return empty;
  45. }
  46. std::vector<HTMLInfo*> ClassFlowControll::GetAllAnalog()
  47. {
  48. for (int i = 0; i < FlowControll.size(); ++i)
  49. if (FlowControll[i]->name().compare("ClassFlowAnalog") == 0)
  50. return ((ClassFlowAnalog*) (FlowControll[i]))->GetHTMLInfo();
  51. std::vector<HTMLInfo*> empty;
  52. return empty;
  53. }
  54. void ClassFlowControll::SetInitialParameter(void)
  55. {
  56. AutoStart = false;
  57. SetupModeActive = false;
  58. AutoIntervall = 10;
  59. }
  60. bool ClassFlowControll::isAutoStart(long &_intervall)
  61. {
  62. _intervall = AutoIntervall * 60 * 1000; // AutoIntervall: Minuten -> ms
  63. return AutoStart;
  64. }
  65. ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
  66. {
  67. ClassFlow* cfc = NULL;
  68. _type = trim(_type);
  69. if (toUpper(_type).compare("[MAKEIMAGE]") == 0)
  70. cfc = new ClassFlowMakeImage(&FlowControll);
  71. if (toUpper(_type).compare("[ALIGNMENT]") == 0)
  72. cfc = new ClassFlowAlignment(&FlowControll);
  73. if (toUpper(_type).compare("[ANALOG]") == 0)
  74. cfc = new ClassFlowAnalog(&FlowControll);
  75. if (toUpper(_type).compare("[DIGITS]") == 0)
  76. cfc = new ClassFlowDigit(&FlowControll);
  77. if (toUpper(_type).compare("[MQTT]") == 0)
  78. cfc = new ClassFlowMQTT(&FlowControll);
  79. if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
  80. {
  81. cfc = new ClassFlowPostProcessing(&FlowControll);
  82. flowpostprocessing = (ClassFlowPostProcessing*) cfc;
  83. }
  84. if (cfc) // Wird nur angehangen, falls es nicht [AutoTimer] ist, denn dieses ist für FlowControll
  85. FlowControll.push_back(cfc);
  86. if (toUpper(_type).compare("[AUTOTIMER]") == 0)
  87. cfc = this;
  88. if (toUpper(_type).compare("[DEBUG]") == 0)
  89. cfc = this;
  90. if (toUpper(_type).compare("[SYSTEM]") == 0)
  91. cfc = this;
  92. return cfc;
  93. }
  94. void ClassFlowControll::InitFlow(std::string config)
  95. {
  96. string line;
  97. flowpostprocessing = NULL;
  98. ClassFlow* cfc;
  99. FILE* pFile;
  100. config = FormatFileName(config);
  101. pFile = fopen(config.c_str(), "r");
  102. line = "";
  103. char zw[1024];
  104. if (pFile != NULL)
  105. {
  106. fgets(zw, 1024, pFile);
  107. printf("%s", zw);
  108. line = std::string(zw);
  109. }
  110. while ((line.size() > 0) && !(feof(pFile)))
  111. {
  112. cfc = CreateClassFlow(line);
  113. if (cfc)
  114. {
  115. cfc->ReadParameter(pFile, line);
  116. }
  117. else
  118. {
  119. fgets(zw, 1024, pFile);
  120. printf("%s", zw);
  121. line = std::string(zw);
  122. }
  123. }
  124. fclose(pFile);
  125. }
  126. std::string ClassFlowControll::getActStatus(){
  127. return aktstatus;
  128. }
  129. bool ClassFlowControll::doFlow(string time)
  130. {
  131. // CleanTempFolder(); // dazu muss man noch eine Rolling einführen
  132. bool result = true;
  133. std::string zw_time;
  134. int repeat = 0;
  135. for (int i = 0; i < FlowControll.size(); ++i)
  136. {
  137. zw_time = gettimestring("%Y%m%d-%H%M%S");
  138. aktstatus = zw_time + ": " + FlowControll[i]->name();
  139. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  140. LogFile.WriteToFile(zw);
  141. if (!FlowControll[i]->doFlow(time)){
  142. repeat++;
  143. LogFile.WriteToFile("Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  144. i = -1; // vorheriger Schritt muss wiederholt werden (vermutlich Bilder aufnehmen)
  145. result = false;
  146. if (repeat > 5) {
  147. LogFile.WriteToFile("Wiederholung 5x nicht erfolgreich --> reboot");
  148. doReboot();
  149. // Schritt wurde 5x wiederholt --> reboot
  150. }
  151. }
  152. else
  153. {
  154. result = true;
  155. }
  156. }
  157. zw_time = gettimestring("%Y%m%d-%H%M%S");
  158. aktstatus = zw_time + ": Flow is done";
  159. return result;
  160. }
  161. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  162. {
  163. if (flowpostprocessing)
  164. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  165. string zw = "";
  166. string result = "";
  167. for (int i = 0; i < FlowControll.size(); ++i)
  168. {
  169. zw = FlowControll[i]->getReadout();
  170. if (zw.length() > 0)
  171. {
  172. if (result.length() == 0)
  173. result = zw;
  174. else
  175. result = result + "\t" + zw;
  176. }
  177. }
  178. return result;
  179. }
  180. string ClassFlowControll::GetPrevalue()
  181. {
  182. if (flowpostprocessing)
  183. {
  184. return flowpostprocessing->GetPreValue();
  185. }
  186. return std::string();
  187. }
  188. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue)
  189. {
  190. float zw;
  191. char* p;
  192. _newvalue = trim(_newvalue);
  193. // printf("Input UpdatePreValue: %s\n", _newvalue.c_str());
  194. if (_newvalue.compare("0.0") == 0)
  195. {
  196. zw = 0;
  197. }
  198. else
  199. {
  200. zw = strtof(_newvalue.c_str(), &p);
  201. if (zw == 0)
  202. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  203. }
  204. if (flowpostprocessing)
  205. {
  206. flowpostprocessing->SavePreValue(zw);
  207. return _newvalue;
  208. }
  209. return std::string();
  210. }
  211. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  212. {
  213. std::vector<string> zerlegt;
  214. aktparamgraph = trim(aktparamgraph);
  215. if (aktparamgraph.size() == 0)
  216. if (!this->GetNextParagraph(pfile, aktparamgraph))
  217. return false;
  218. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && (toUpper(aktparamgraph).compare("[SYSTEM]") != 0)) // Paragraph passt nicht zu MakeImage
  219. return false;
  220. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  221. {
  222. zerlegt = this->ZerlegeZeile(aktparamgraph, " =");
  223. if ((toUpper(zerlegt[0]) == "AUTOSTART") && (zerlegt.size() > 1))
  224. {
  225. if (toUpper(zerlegt[1]) == "TRUE")
  226. {
  227. AutoStart = true;
  228. }
  229. }
  230. if ((toUpper(zerlegt[0]) == "INTERVALL") && (zerlegt.size() > 1))
  231. {
  232. AutoIntervall = std::stof(zerlegt[1]);
  233. }
  234. if ((toUpper(zerlegt[0]) == "LOGFILE") && (zerlegt.size() > 1))
  235. {
  236. if (toUpper(zerlegt[1]) == "TRUE")
  237. {
  238. LogFile.SwitchOnOff(true);
  239. }
  240. if (toUpper(zerlegt[1]) == "FALSE")
  241. {
  242. LogFile.SwitchOnOff(false);
  243. }
  244. }
  245. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  246. {
  247. LogFile.SetRetention(std::stoi(zerlegt[1]));
  248. }
  249. if ((toUpper(zerlegt[0]) == "TIMEZONE") && (zerlegt.size() > 1))
  250. {
  251. string zw = "Set TimeZone: " + zerlegt[1];
  252. setTimeZone(zerlegt[1]);
  253. }
  254. if ((toUpper(zerlegt[0]) == "TIMEUPDATEINTERVALL") && (zerlegt.size() > 1))
  255. {
  256. TimeUpdateIntervall = stof(zerlegt[1]);
  257. xTaskCreate(&task_doTimeSync, "update_time", configMINIMAL_STACK_SIZE * 16, &TimeUpdateIntervall, tskIDLE_PRIORITY, NULL);
  258. }
  259. if ((toUpper(zerlegt[0]) == "SETUPMODE") && (zerlegt.size() > 1))
  260. {
  261. if (toUpper(zerlegt[1]) == "TRUE")
  262. {
  263. SetupModeActive = true;
  264. }
  265. }
  266. }
  267. return true;
  268. }
  269. int ClassFlowControll::CleanTempFolder() {
  270. const char* folderPath = "/sdcard/img_tmp";
  271. ESP_LOGI(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath);
  272. DIR *dir = opendir(folderPath);
  273. if (!dir) {
  274. ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath);
  275. return -1;
  276. }
  277. struct dirent *entry;
  278. int deleted = 0;
  279. while ((entry = readdir(dir)) != NULL) {
  280. std::string path = string(folderPath) + "/" + entry->d_name;
  281. if (entry->d_type == DT_REG) {
  282. if (unlink(path.c_str()) == 0) {
  283. deleted ++;
  284. } else {
  285. ESP_LOGE(TAG, "can't delete file : %s", path.c_str());
  286. }
  287. } else if (entry->d_type == DT_DIR) {
  288. deleted += removeFolder(path.c_str(), TAG);
  289. }
  290. }
  291. closedir(dir);
  292. ESP_LOGI(TAG, "%d files deleted", deleted);
  293. return 0;
  294. }