ClassFlowPostProcessing.cpp 10 KB

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