ClassFlowControll.cpp 8.6 KB

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