ClassFlowControll.cpp 7.5 KB

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