ClassFlowPostProcessing.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include "ClassFlowPostProcessing.h"
  2. #include "Helper.h"
  3. #include "ClassFlowAnalog.h"
  4. #include "ClassFlowDigit.h"
  5. #include "ClassFlowMakeImage.h"
  6. #include <time.h>
  7. string ClassFlowPostProcessing::GetPreValue()
  8. {
  9. return to_string(PreValue);
  10. }
  11. bool ClassFlowPostProcessing::LoadPreValue(void)
  12. {
  13. FILE* pFile;
  14. char zw[1024];
  15. string zwtime, zwvalue;
  16. pFile = fopen(FilePreValue.c_str(), "r");
  17. if (pFile == NULL)
  18. return false;
  19. fgets(zw, 1024, pFile);
  20. printf("%s", zw);
  21. zwtime = trim(std::string(zw));
  22. fgets(zw, 1024, pFile);
  23. printf("%s", zw);
  24. zwvalue = trim(std::string(zw));
  25. PreValue = stof(zwvalue.c_str());
  26. time_t tStart;
  27. int yy, month, dd, hh, mm, ss;
  28. struct tm whenStart;
  29. sscanf(zwtime.c_str(), "%d-%d-%d_%d-%d-%d", &yy, &month, &dd, &hh, &mm, &ss);
  30. whenStart.tm_year = yy - 1900;
  31. whenStart.tm_mon = month - 1;
  32. whenStart.tm_mday = dd;
  33. whenStart.tm_hour = hh;
  34. whenStart.tm_min = mm;
  35. whenStart.tm_sec = ss;
  36. whenStart.tm_isdst = -1;
  37. tStart = mktime(&whenStart);
  38. time_t now;
  39. time(&now);
  40. localtime(&now);
  41. double difference = difftime(now, tStart);
  42. difference /= 60;
  43. if (difference > PreValueAgeStartup)
  44. return false;
  45. return true;
  46. }
  47. void ClassFlowPostProcessing::SavePreValue(float value, string zwtime)
  48. {
  49. FILE* pFile;
  50. PreValue = value;
  51. pFile = fopen(FilePreValue.c_str(), "w");
  52. if (strlen(zwtime.c_str()) == 0)
  53. {
  54. time_t rawtime;
  55. struct tm* timeinfo;
  56. char buffer[80];
  57. time(&rawtime);
  58. timeinfo = localtime(&rawtime);
  59. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  60. zwtime = std::string(buffer);
  61. }
  62. fputs(zwtime.c_str(), pFile);
  63. fputs("\n", pFile);
  64. fputs(to_string(value).c_str(), pFile);
  65. fputs("\n", pFile);
  66. fclose(pFile);
  67. }
  68. ClassFlowPostProcessing::ClassFlowPostProcessing()
  69. {
  70. PreValueUse = false;
  71. PreValueAgeStartup = 30;
  72. AllowNegativeRates = false;
  73. MaxRateValue = 0.1;
  74. ErrorMessage = false;
  75. ListFlowControll = NULL;
  76. PreValueOkay = false;
  77. useMaxRateValue = false;
  78. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  79. }
  80. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc)
  81. {
  82. PreValueUse = false;
  83. PreValueAgeStartup = 30;
  84. AllowNegativeRates = false;
  85. MaxRateValue = 0.1;
  86. ErrorMessage = false;
  87. ListFlowControll = NULL;
  88. PreValueOkay = false;
  89. useMaxRateValue = false;
  90. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  91. ListFlowControll = lfc;
  92. }
  93. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  94. {
  95. std::vector<string> zerlegt;
  96. aktparamgraph = trim(aktparamgraph);
  97. if (aktparamgraph.size() == 0)
  98. if (!this->GetNextParagraph(pfile, aktparamgraph))
  99. return false;
  100. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  101. return false;
  102. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  103. {
  104. zerlegt = this->ZerlegeZeile(aktparamgraph);
  105. if ((zerlegt[0] == "PreValueUse") && (zerlegt.size() > 1))
  106. {
  107. if ((zerlegt[1] == "True") || (zerlegt[1] == "true"))
  108. {
  109. PreValueUse = true;
  110. PreValueOkay = LoadPreValue();
  111. }
  112. }
  113. if ((zerlegt[0] == "AllowNegativeRates") && (zerlegt.size() > 1))
  114. {
  115. if ((zerlegt[1] == "True") || (zerlegt[1] == "true"))
  116. AllowNegativeRates = true;
  117. }
  118. if ((zerlegt[0] == "ErrorMessage") && (zerlegt.size() > 1))
  119. {
  120. if ((zerlegt[1] == "True") || (zerlegt[1] == "true"))
  121. ErrorMessage = true;
  122. }
  123. if ((zerlegt[0] == "PreValueAgeStartup") && (zerlegt.size() > 1))
  124. {
  125. PreValueAgeStartup = std::stoi(zerlegt[1]);
  126. }
  127. if ((zerlegt[0] == "MaxRateValue") && (zerlegt.size() > 1))
  128. {
  129. useMaxRateValue = true;
  130. MaxRateValue = std::stof(zerlegt[1]);
  131. }
  132. }
  133. return true;
  134. }
  135. bool ClassFlowPostProcessing::doFlow(string zwtime)
  136. {
  137. string result = "";
  138. string digit = "";
  139. string analog = "";
  140. bool isdigit = false;
  141. bool isanalog = false;
  142. string zw;
  143. string error = "";
  144. time_t imagetime = 0;
  145. for (int i = 0; i < ListFlowControll->size(); ++i)
  146. {
  147. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  148. {
  149. imagetime = ((ClassFlowMakeImage*)(*ListFlowControll)[i])->getTimeImageTaken();
  150. }
  151. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  152. {
  153. isdigit = true;
  154. digit = (*ListFlowControll)[i]->getReadout();
  155. }
  156. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  157. {
  158. isanalog = true;
  159. analog = (*ListFlowControll)[i]->getReadout();
  160. }
  161. }
  162. if (imagetime == 0)
  163. time(&imagetime);
  164. struct tm* timeinfo;
  165. timeinfo = localtime(&imagetime);
  166. char strftime_buf[64];
  167. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H-%M-%S", timeinfo);
  168. zwtime = std::string(strftime_buf);
  169. // // TESTING ONLY////////////////////
  170. // isdigit = true; digit = "12N";
  171. // isanalog = true; analog = "456";
  172. if (!PreValueUse || !PreValueOkay)
  173. {
  174. if (isdigit)
  175. ReturnValue = digit;
  176. if (isdigit && isanalog)
  177. ReturnValue = ReturnValue + ".";
  178. if (isanalog)
  179. ReturnValue = ReturnValue + analog;
  180. if ((findDelimiterPos(ReturnValue, "N") == std::string::npos) && (ReturnValue.length() > 0))
  181. {
  182. while ((ReturnValue.length() > 1) && (ReturnValue[0] == '0'))
  183. {
  184. ReturnValue.erase(0, 1);
  185. }
  186. ReturnRawValue = ReturnValue;
  187. Value = std::stof(ReturnValue);
  188. SavePreValue(Value, zwtime);
  189. }
  190. return true;
  191. }
  192. if (isdigit)
  193. {
  194. digit = ErsetzteN(digit);
  195. zw = zw + digit;
  196. }
  197. if (isdigit && isanalog)
  198. zw = zw + ".";
  199. if (isanalog)
  200. zw = zw + analog;
  201. ReturnRawValue = zw;
  202. Value = std::stof(zw);
  203. if ((!AllowNegativeRates) && (Value < PreValue))
  204. {
  205. error = "Negative Rate - Return old value - " + std::to_string(Value);
  206. Value = PreValue;
  207. }
  208. if (useMaxRateValue && ((Value - PreValue) > MaxRateValue))
  209. {
  210. error = "Negative Rate - Return old value - " + std::to_string(Value);
  211. Value = PreValue;
  212. }
  213. ReturnValue = std::to_string(Value);
  214. if (ErrorMessage && (error.length() > 0))
  215. ReturnValue = ReturnValue + "\t" + error;
  216. if (error.length() == 0)
  217. SavePreValue(Value, zwtime);
  218. return true;
  219. }
  220. string ClassFlowPostProcessing::getReadout()
  221. {
  222. return ReturnValue;
  223. }
  224. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue)
  225. {
  226. if (_rawValue)
  227. return ReturnRawValue;
  228. return ReturnValue;
  229. }
  230. string ClassFlowPostProcessing::ErsetzteN(string input)
  231. {
  232. int posN, posPunkt;
  233. int pot, ziffer;
  234. float zw;
  235. posN = findDelimiterPos(input, "N");
  236. posPunkt = input.length();
  237. while (posN != std::string::npos)
  238. {
  239. pot = posPunkt - posN - 1;
  240. zw = PreValue / pow(10, pot);
  241. ziffer = ((int) zw) % 10;
  242. input[posN] = ziffer + 48;
  243. posN = findDelimiterPos(input, "N");
  244. }
  245. return input;
  246. }