ClassFlowControll.cpp 9.3 KB

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