ClassFlowPostProcessing.cpp 8.4 KB

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