ClassFlowControll.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 = OpenFileAndWait(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. void ClassFlowControll::doFlowMakeImageOnly(string time){
  130. bool result = true;
  131. std::string zw_time;
  132. int repeat = 0;
  133. for (int i = 0; i < FlowControll.size(); ++i)
  134. {
  135. if (FlowControll[i]->name() == "ClassFlowMakeImage") {
  136. zw_time = gettimestring("%Y%m%d-%H%M%S");
  137. aktstatus = zw_time + ": " + FlowControll[i]->name();
  138. string zw = "FlowControll.doFlowMakeImageOnly - " + FlowControll[i]->name();
  139. FlowControll[i]->doFlow(time);
  140. }
  141. }
  142. }
  143. bool ClassFlowControll::doFlow(string time)
  144. {
  145. // CleanTempFolder(); // dazu muss man noch eine Rolling einführen
  146. bool result = true;
  147. std::string zw_time;
  148. int repeat = 0;
  149. for (int i = 0; i < FlowControll.size(); ++i)
  150. {
  151. zw_time = gettimestring("%Y%m%d-%H%M%S");
  152. aktstatus = zw_time + ": " + FlowControll[i]->name();
  153. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  154. LogFile.WriteToFile(zw);
  155. if (!FlowControll[i]->doFlow(time)){
  156. repeat++;
  157. LogFile.WriteToFile("Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  158. i = -1; // vorheriger Schritt muss wiederholt werden (vermutlich Bilder aufnehmen)
  159. result = false;
  160. if (repeat > 5) {
  161. LogFile.WriteToFile("Wiederholung 5x nicht erfolgreich --> reboot");
  162. doReboot();
  163. // Schritt wurde 5x wiederholt --> reboot
  164. }
  165. }
  166. else
  167. {
  168. result = true;
  169. }
  170. }
  171. zw_time = gettimestring("%Y%m%d-%H%M%S");
  172. aktstatus = zw_time + ": Flow is done";
  173. return result;
  174. }
  175. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  176. {
  177. if (flowpostprocessing)
  178. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  179. string zw = "";
  180. string result = "";
  181. for (int i = 0; i < FlowControll.size(); ++i)
  182. {
  183. zw = FlowControll[i]->getReadout();
  184. if (zw.length() > 0)
  185. {
  186. if (result.length() == 0)
  187. result = zw;
  188. else
  189. result = result + "\t" + zw;
  190. }
  191. }
  192. return result;
  193. }
  194. string ClassFlowControll::GetPrevalue()
  195. {
  196. if (flowpostprocessing)
  197. {
  198. return flowpostprocessing->GetPreValue();
  199. }
  200. return std::string();
  201. }
  202. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue)
  203. {
  204. float zw;
  205. char* p;
  206. _newvalue = trim(_newvalue);
  207. // printf("Input UpdatePreValue: %s\n", _newvalue.c_str());
  208. if (_newvalue.compare("0.0") == 0)
  209. {
  210. zw = 0;
  211. }
  212. else
  213. {
  214. zw = strtof(_newvalue.c_str(), &p);
  215. if (zw == 0)
  216. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  217. }
  218. if (flowpostprocessing)
  219. {
  220. flowpostprocessing->SavePreValue(zw);
  221. return _newvalue;
  222. }
  223. return std::string();
  224. }
  225. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  226. {
  227. std::vector<string> zerlegt;
  228. aktparamgraph = trim(aktparamgraph);
  229. if (aktparamgraph.size() == 0)
  230. if (!this->GetNextParagraph(pfile, aktparamgraph))
  231. return false;
  232. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && (toUpper(aktparamgraph).compare("[SYSTEM]") != 0)) // Paragraph passt nicht zu MakeImage
  233. return false;
  234. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  235. {
  236. zerlegt = this->ZerlegeZeile(aktparamgraph, " =");
  237. if ((toUpper(zerlegt[0]) == "AUTOSTART") && (zerlegt.size() > 1))
  238. {
  239. if (toUpper(zerlegt[1]) == "TRUE")
  240. {
  241. AutoStart = true;
  242. }
  243. }
  244. if ((toUpper(zerlegt[0]) == "INTERVALL") && (zerlegt.size() > 1))
  245. {
  246. AutoIntervall = std::stof(zerlegt[1]);
  247. }
  248. if ((toUpper(zerlegt[0]) == "LOGFILE") && (zerlegt.size() > 1))
  249. {
  250. if (toUpper(zerlegt[1]) == "TRUE")
  251. {
  252. LogFile.SwitchOnOff(true);
  253. }
  254. if (toUpper(zerlegt[1]) == "FALSE")
  255. {
  256. LogFile.SwitchOnOff(false);
  257. }
  258. }
  259. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  260. {
  261. LogFile.SetRetention(std::stoi(zerlegt[1]));
  262. }
  263. if ((toUpper(zerlegt[0]) == "TIMEZONE") && (zerlegt.size() > 1))
  264. {
  265. string zw = "Set TimeZone: " + zerlegt[1];
  266. setTimeZone(zerlegt[1]);
  267. }
  268. if ((toUpper(zerlegt[0]) == "SETUPMODE") && (zerlegt.size() > 1))
  269. {
  270. if (toUpper(zerlegt[1]) == "TRUE")
  271. {
  272. SetupModeActive = true;
  273. }
  274. }
  275. }
  276. return true;
  277. }
  278. int ClassFlowControll::CleanTempFolder() {
  279. const char* folderPath = "/sdcard/img_tmp";
  280. ESP_LOGI(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath);
  281. DIR *dir = opendir(folderPath);
  282. if (!dir) {
  283. ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath);
  284. return -1;
  285. }
  286. struct dirent *entry;
  287. int deleted = 0;
  288. while ((entry = readdir(dir)) != NULL) {
  289. std::string path = string(folderPath) + "/" + entry->d_name;
  290. if (entry->d_type == DT_REG) {
  291. if (unlink(path.c_str()) == 0) {
  292. deleted ++;
  293. } else {
  294. ESP_LOGE(TAG, "can't delete file : %s", path.c_str());
  295. }
  296. } else if (entry->d_type == DT_DIR) {
  297. deleted += removeFolder(path.c_str(), TAG);
  298. }
  299. }
  300. closedir(dir);
  301. ESP_LOGI(TAG, "%d files deleted", deleted);
  302. return 0;
  303. }