ClassFlowPostProcessing.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. checkDigitIncreaseConsistency = false;
  81. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  82. }
  83. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc)
  84. {
  85. PreValueUse = false;
  86. PreValueAgeStartup = 30;
  87. AllowNegativeRates = false;
  88. MaxRateValue = 0.1;
  89. ErrorMessage = false;
  90. ListFlowControll = NULL;
  91. PreValueOkay = false;
  92. useMaxRateValue = false;
  93. checkDigitIncreaseConsistency = false;
  94. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  95. ListFlowControll = lfc;
  96. }
  97. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  98. {
  99. std::vector<string> zerlegt;
  100. aktparamgraph = trim(aktparamgraph);
  101. if (aktparamgraph.size() == 0)
  102. if (!this->GetNextParagraph(pfile, aktparamgraph))
  103. return false;
  104. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  105. return false;
  106. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  107. {
  108. zerlegt = this->ZerlegeZeile(aktparamgraph);
  109. if ((zerlegt[0] == "PreValueUse") && (zerlegt.size() > 1))
  110. {
  111. if ((zerlegt[1] == "True") || (zerlegt[1] == "true"))
  112. {
  113. PreValueUse = true;
  114. PreValueOkay = LoadPreValue();
  115. if (PreValueOkay)
  116. {
  117. Value = PreValue;
  118. for (int i = 0; i < ListFlowControll->size(); ++i)
  119. {
  120. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  121. {
  122. int AnzahlNachkomma = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  123. std::stringstream stream;
  124. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  125. ReturnValue = stream.str();
  126. }
  127. }
  128. }
  129. }
  130. }
  131. if ((zerlegt[0] == "CheckDigitIncreaseConsistency") && (zerlegt.size() > 1))
  132. {
  133. if (toUpper(zerlegt[1]) == "TRUE")
  134. checkDigitIncreaseConsistency = true;
  135. }
  136. if ((zerlegt[0] == "AllowNegativeRates") && (zerlegt.size() > 1))
  137. {
  138. if (toUpper(zerlegt[1]) == "TRUE")
  139. AllowNegativeRates = true;
  140. }
  141. if ((zerlegt[0] == "ErrorMessage") && (zerlegt.size() > 1))
  142. {
  143. if (toUpper(zerlegt[1]) == "TRUE")
  144. ErrorMessage = true;
  145. }
  146. if ((zerlegt[0] == "PreValueAgeStartup") && (zerlegt.size() > 1))
  147. {
  148. PreValueAgeStartup = std::stoi(zerlegt[1]);
  149. }
  150. if ((zerlegt[0] == "MaxRateValue") && (zerlegt.size() > 1))
  151. {
  152. useMaxRateValue = true;
  153. MaxRateValue = std::stof(zerlegt[1]);
  154. }
  155. }
  156. return true;
  157. }
  158. bool ClassFlowPostProcessing::doFlow(string zwtime)
  159. {
  160. string result = "";
  161. string digit = "";
  162. string analog = "";
  163. string zwvalue;
  164. bool isdigit = false;
  165. bool isanalog = false;
  166. int AnzahlNachkomma = 0;
  167. string zw;
  168. string error = "";
  169. time_t imagetime = 0;
  170. for (int i = 0; i < ListFlowControll->size(); ++i)
  171. {
  172. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  173. {
  174. imagetime = ((ClassFlowMakeImage*)(*ListFlowControll)[i])->getTimeImageTaken();
  175. }
  176. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  177. {
  178. isdigit = true;
  179. digit = (*ListFlowControll)[i]->getReadout();
  180. }
  181. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  182. {
  183. isanalog = true;
  184. analog = (*ListFlowControll)[i]->getReadout();
  185. AnzahlNachkomma = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  186. }
  187. }
  188. if (imagetime == 0)
  189. time(&imagetime);
  190. struct tm* timeinfo;
  191. timeinfo = localtime(&imagetime);
  192. char strftime_buf[64];
  193. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H-%M-%S", timeinfo);
  194. zwtime = std::string(strftime_buf);
  195. // // TESTING ONLY////////////////////
  196. // isdigit = true; digit = "12N";
  197. // isanalog = true; analog = "456";
  198. if (!PreValueUse || !PreValueOkay)
  199. {
  200. if (isdigit)
  201. ReturnValue = digit;
  202. if (isdigit && isanalog)
  203. ReturnValue = ReturnValue + ".";
  204. if (isanalog)
  205. ReturnValue = ReturnValue + analog;
  206. ReturnRawValue = ReturnValue;
  207. if ((findDelimiterPos(ReturnValue, "N") == std::string::npos) && (ReturnValue.length() > 0))
  208. {
  209. while ((ReturnValue.length() > 1) && (ReturnValue[0] == '0'))
  210. {
  211. ReturnValue.erase(0, 1);
  212. }
  213. Value = std::stof(ReturnValue);
  214. SavePreValue(Value, zwtime);
  215. }
  216. return true;
  217. }
  218. if (isdigit)
  219. ReturnRawValue = digit;
  220. if (isdigit && isanalog)
  221. ReturnRawValue = ReturnRawValue + ".";
  222. if (isanalog)
  223. ReturnRawValue = ReturnRawValue + analog;
  224. if (isdigit)
  225. {
  226. int lastanalog = -1;
  227. if (isanalog)
  228. lastanalog = analog[0] - 48;
  229. digit = ErsetzteN(digit, lastanalog);
  230. zw = digit;
  231. }
  232. if (isdigit && isanalog)
  233. zw = zw + ".";
  234. if (isanalog)
  235. zw = zw + analog;
  236. Value = std::stof(zw);
  237. std::stringstream stream;
  238. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  239. zwvalue = stream.str();
  240. if ((!AllowNegativeRates) && (Value < PreValue))
  241. {
  242. error = "Negative Rate - Returned old value - read value: " + zwvalue;
  243. Value = PreValue;
  244. stream.str("");
  245. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  246. zwvalue = stream.str();
  247. }
  248. if (useMaxRateValue && (abs(Value - PreValue) > MaxRateValue))
  249. {
  250. error = "Rate too high - Returned old value - read value: " + zwvalue;
  251. Value = PreValue;
  252. stream.str("");
  253. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  254. zwvalue = stream.str();
  255. }
  256. ReturnValue = zwvalue;
  257. if (ErrorMessage && (error.length() > 0))
  258. ReturnValue = ReturnValue + "\t" + error;
  259. if (error.length() == 0)
  260. SavePreValue(Value, zwtime);
  261. return true;
  262. }
  263. string ClassFlowPostProcessing::getReadout()
  264. {
  265. return ReturnValue;
  266. }
  267. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue)
  268. {
  269. if (_rawValue)
  270. return ReturnRawValue;
  271. return ReturnValue;
  272. }
  273. string ClassFlowPostProcessing::ErsetzteN(string input, int lastvalueanalog = -1)
  274. {
  275. int posN, posPunkt;
  276. int pot, ziffer;
  277. float zw;
  278. posN = findDelimiterPos(input, "N");
  279. posPunkt = input.length();
  280. while (posN != std::string::npos)
  281. {
  282. pot = posPunkt - posN - 1;
  283. zw = PreValue / pow(10, pot);
  284. ziffer = ((int) zw) % 10;
  285. input[posN] = ziffer + 48;
  286. posN = findDelimiterPos(input, "N");
  287. }
  288. ///////////////////////////// TestCode
  289. /*
  290. input = "10";
  291. posPunkt = input.length();
  292. PreValue = 9.5;
  293. lastvalueanalog = 7;
  294. */
  295. if (checkDigitIncreaseConsistency && lastvalueanalog > -1)
  296. {
  297. int zifferIST;
  298. int substrakt = 0;
  299. bool lastcorrected = false;
  300. for (int i = input.length() - 1; i >= 0; --i)
  301. {
  302. zifferIST = input[i] - 48; //std::stoi(std::string(input[i]));
  303. if (lastcorrected)
  304. {
  305. zifferIST--;
  306. input[i] = zifferIST + 48;
  307. lastcorrected = false;
  308. }
  309. pot = posPunkt - i - 1;
  310. zw = PreValue / pow(10, pot);
  311. ziffer = ((int) zw) % 10;
  312. if (zifferIST < ziffer)
  313. {
  314. if (lastvalueanalog >= 7)
  315. {
  316. input[i] = ziffer + 48;
  317. lastvalueanalog = ziffer;
  318. lastcorrected = true;
  319. }
  320. }
  321. }
  322. }
  323. return input;
  324. }