ClassFlowControll.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. 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 to_string(zw);
  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 (aktparamgraph.compare("[AutoTimer]") != 0) // Paragraph passt nich zu MakeImage
  206. return false;
  207. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  208. {
  209. zerlegt = this->ZerlegeZeile(aktparamgraph);
  210. if ((zerlegt[0] == "AutoStart") && (zerlegt.size() > 1))
  211. {
  212. if (toUpper(zerlegt[1]) == "TRUE")
  213. {
  214. AutoStart = true;
  215. }
  216. }
  217. if ((zerlegt[0] == "Intervall") && (zerlegt.size() > 1))
  218. {
  219. AutoIntervall = std::stof(zerlegt[1]);
  220. }
  221. }
  222. return true;
  223. }