ClassFlowControll.cpp 7.3 KB

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