ClassFlowControll._c_pp 7.7 KB

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