ClassFlowPostProcessing.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include "ClassFlowPostProcessing.h"
  2. #include "Helper.h"
  3. #include "ClassFlowAnalog.h"
  4. #include "ClassFlowDigit.h"
  5. #include "ClassFlowMakeImage.h"
  6. #include "ClassLogFile.h"
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <time.h>
  10. string ClassFlowPostProcessing::GetPreValue()
  11. {
  12. std::string result;
  13. bool isAnalog = false;
  14. bool isDigit = false;
  15. int AnzahlAnalog = 0;
  16. result = RundeOutput(PreValue, -DecimalShift);
  17. for (int i = 0; i < ListFlowControll->size(); ++i)
  18. {
  19. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  20. {
  21. isAnalog = true;
  22. AnzahlAnalog = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  23. }
  24. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  25. {
  26. isDigit = true;
  27. }
  28. }
  29. if (isDigit && isAnalog)
  30. result = RundeOutput(PreValue, AnzahlAnalog - DecimalShift);
  31. return result;
  32. }
  33. bool ClassFlowPostProcessing::LoadPreValue(void)
  34. {
  35. FILE* pFile;
  36. char zw[1024];
  37. string zwtime, zwvalue;
  38. pFile = fopen(FilePreValue.c_str(), "r");
  39. if (pFile == NULL)
  40. return false;
  41. fgets(zw, 1024, pFile);
  42. printf("%s", zw);
  43. zwtime = trim(std::string(zw));
  44. fgets(zw, 1024, pFile);
  45. fclose(pFile);
  46. printf("%s", zw);
  47. zwvalue = trim(std::string(zw));
  48. PreValue = stof(zwvalue.c_str());
  49. time_t tStart;
  50. int yy, month, dd, hh, mm, ss;
  51. struct tm whenStart;
  52. sscanf(zwtime.c_str(), "%d-%d-%d_%d-%d-%d", &yy, &month, &dd, &hh, &mm, &ss);
  53. whenStart.tm_year = yy - 1900;
  54. whenStart.tm_mon = month - 1;
  55. whenStart.tm_mday = dd;
  56. whenStart.tm_hour = hh;
  57. whenStart.tm_min = mm;
  58. whenStart.tm_sec = ss;
  59. whenStart.tm_isdst = -1;
  60. tStart = mktime(&whenStart);
  61. time_t now;
  62. time(&now);
  63. localtime(&now);
  64. double difference = difftime(now, tStart);
  65. difference /= 60;
  66. if (difference > PreValueAgeStartup)
  67. return false;
  68. Value = PreValue;
  69. ReturnValue = to_string(Value);
  70. ReturnValueNoError = ReturnValue;
  71. bool isAnalog = false;
  72. bool isDigit = false;
  73. int AnzahlAnalog = 0;
  74. for (int i = 0; i < ListFlowControll->size(); ++i)
  75. {
  76. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  77. isAnalog = true;
  78. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  79. isDigit = true;
  80. }
  81. if (isDigit || isAnalog)
  82. {
  83. ReturnValue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  84. ReturnValueNoError = ReturnValue;
  85. }
  86. return true;
  87. }
  88. void ClassFlowPostProcessing::SavePreValue(float value, string zwtime)
  89. {
  90. FILE* pFile;
  91. pFile = fopen(FilePreValue.c_str(), "w");
  92. if (strlen(zwtime.c_str()) == 0)
  93. {
  94. time_t rawtime;
  95. struct tm* timeinfo;
  96. char buffer[80];
  97. time(&rawtime);
  98. timeinfo = localtime(&rawtime);
  99. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  100. zwtime = std::string(buffer);
  101. }
  102. PreValue = value;
  103. fputs(zwtime.c_str(), pFile);
  104. fputs("\n", pFile);
  105. fputs(to_string(value).c_str(), pFile);
  106. fputs("\n", pFile);
  107. fclose(pFile);
  108. }
  109. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc)
  110. {
  111. PreValueUse = false;
  112. PreValueAgeStartup = 30;
  113. AllowNegativeRates = false;
  114. MaxRateValue = 0.1;
  115. ErrorMessage = false;
  116. ListFlowControll = NULL;
  117. PreValueOkay = false;
  118. useMaxRateValue = false;
  119. checkDigitIncreaseConsistency = false;
  120. DecimalShift = 0;
  121. ErrorMessageText = "";
  122. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  123. ListFlowControll = lfc;
  124. }
  125. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  126. {
  127. std::vector<string> zerlegt;
  128. aktparamgraph = trim(aktparamgraph);
  129. if (aktparamgraph.size() == 0)
  130. if (!this->GetNextParagraph(pfile, aktparamgraph))
  131. return false;
  132. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  133. return false;
  134. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  135. {
  136. zerlegt = this->ZerlegeZeile(aktparamgraph);
  137. if ((toUpper(zerlegt[0]) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  138. {
  139. DecimalShift = stoi(zerlegt[1]);
  140. }
  141. if ((toUpper(zerlegt[0]) == "PREVALUEUSE") && (zerlegt.size() > 1))
  142. {
  143. if (toUpper(zerlegt[1]) == "TRUE")
  144. {
  145. PreValueUse = true;
  146. }
  147. }
  148. if ((toUpper(zerlegt[0]) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  149. {
  150. if (toUpper(zerlegt[1]) == "TRUE")
  151. checkDigitIncreaseConsistency = true;
  152. }
  153. if ((toUpper(zerlegt[0]) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  154. {
  155. if (toUpper(zerlegt[1]) == "TRUE")
  156. AllowNegativeRates = true;
  157. }
  158. if ((toUpper(zerlegt[0]) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  159. {
  160. if (toUpper(zerlegt[1]) == "TRUE")
  161. ErrorMessage = true;
  162. }
  163. if ((toUpper(zerlegt[0]) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  164. {
  165. PreValueAgeStartup = std::stoi(zerlegt[1]);
  166. }
  167. if ((toUpper(zerlegt[0]) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  168. {
  169. useMaxRateValue = true;
  170. MaxRateValue = std::stof(zerlegt[1]);
  171. }
  172. }
  173. if (PreValueUse) {
  174. PreValueOkay = LoadPreValue();
  175. }
  176. return true;
  177. }
  178. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  179. if (_decShift == 0){
  180. return in;
  181. }
  182. int _pos_dec_org, _pos_dec_neu;
  183. _pos_dec_org = findDelimiterPos(in, ".");
  184. if (_pos_dec_org == std::string::npos) {
  185. _pos_dec_org = in.length();
  186. }
  187. else
  188. {
  189. in = in.erase(_pos_dec_org, 1);
  190. }
  191. _pos_dec_neu = _pos_dec_org + _decShift;
  192. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  193. for (int i = 0; i > _pos_dec_neu; --i){
  194. in = in.insert(0, "0");
  195. }
  196. in = "0." + in;
  197. return in;
  198. }
  199. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  200. for (int i = in.length(); i < _pos_dec_neu; ++i){
  201. in = in.insert(in.length(), "0");
  202. }
  203. return in;
  204. }
  205. string zw;
  206. zw = in.substr(0, _pos_dec_neu);
  207. zw = zw + ".";
  208. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  209. return zw;
  210. }
  211. bool ClassFlowPostProcessing::doFlow(string zwtime)
  212. {
  213. string result = "";
  214. string digit = "";
  215. string analog = "";
  216. string zwvalue;
  217. bool isdigit = false;
  218. bool isanalog = false;
  219. int AnzahlAnalog = 0;
  220. string zw;
  221. time_t imagetime = 0;
  222. string rohwert;
  223. ErrorMessageText = "";
  224. for (int i = 0; i < ListFlowControll->size(); ++i)
  225. {
  226. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  227. {
  228. imagetime = ((ClassFlowMakeImage*)(*ListFlowControll)[i])->getTimeImageTaken();
  229. }
  230. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  231. {
  232. isdigit = true;
  233. digit = (*ListFlowControll)[i]->getReadout();
  234. }
  235. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  236. {
  237. isanalog = true;
  238. analog = (*ListFlowControll)[i]->getReadout();
  239. AnzahlAnalog = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  240. }
  241. }
  242. if (imagetime == 0)
  243. time(&imagetime);
  244. struct tm* timeinfo;
  245. timeinfo = localtime(&imagetime);
  246. char strftime_buf[64];
  247. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H-%M-%S", timeinfo);
  248. zwtime = std::string(strftime_buf);
  249. // // TESTING ONLY////////////////////
  250. // isdigit = true; digit = "12N";
  251. // isanalog = true; analog = "456";
  252. ReturnRawValue = "";
  253. if (isdigit)
  254. ReturnRawValue = digit;
  255. if (isdigit && isanalog)
  256. ReturnRawValue = ReturnRawValue + ".";
  257. if (isanalog)
  258. ReturnRawValue = ReturnRawValue + analog;
  259. if (!isdigit)
  260. {
  261. AnzahlAnalog = 0;
  262. }
  263. ReturnRawValue = ShiftDecimal(ReturnRawValue, DecimalShift);
  264. rohwert = ReturnRawValue;
  265. if (!PreValueUse || !PreValueOkay)
  266. {
  267. ReturnValue = ReturnRawValue;
  268. ReturnValueNoError = ReturnRawValue;
  269. if ((findDelimiterPos(ReturnValue, "N") == std::string::npos) && (ReturnValue.length() > 0))
  270. {
  271. while ((ReturnValue.length() > 1) && (ReturnValue[0] == '0'))
  272. {
  273. ReturnValue.erase(0, 1);
  274. }
  275. Value = std::stof(ReturnValue);
  276. ReturnValueNoError = ReturnValue;
  277. PreValueOkay = true;
  278. PreValue = Value;
  279. SavePreValue(Value, zwtime);
  280. }
  281. return true;
  282. }
  283. zw = ErsetzteN(ReturnRawValue);
  284. Value = std::stof(zw);
  285. if (checkDigitIncreaseConsistency)
  286. {
  287. Value = checkDigitConsistency(Value, DecimalShift, isanalog);
  288. }
  289. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  290. if ((!AllowNegativeRates) && (Value < PreValue))
  291. {
  292. ErrorMessageText = ErrorMessageText + "Negative Rate - Returned old value - read value: " + zwvalue + " - raw value: " + ReturnRawValue + " - checked value: " + std::to_string(Value) + " ";
  293. Value = PreValue;
  294. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  295. }
  296. if (useMaxRateValue && (abs(Value - PreValue) > MaxRateValue))
  297. {
  298. ErrorMessageText = ErrorMessageText + "Rate too high - Returned old value - read value: " + zwvalue + " - checked value: " + std::to_string(Value) + " ";
  299. Value = PreValue;
  300. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  301. }
  302. ReturnValueNoError = zwvalue;
  303. ReturnValue = zwvalue;
  304. if (ErrorMessage && (ErrorMessageText.length() > 0))
  305. ReturnValue = ReturnValue + "\t" + ErrorMessageText;
  306. if (ErrorMessageText.length() == 0)
  307. {
  308. PreValue = Value;
  309. SavePreValue(Value, zwtime);
  310. }
  311. return true;
  312. }
  313. string ClassFlowPostProcessing::getReadout()
  314. {
  315. return ReturnValue;
  316. }
  317. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror)
  318. {
  319. if (_rawValue)
  320. return ReturnRawValue;
  321. if (_noerror)
  322. return ReturnValueNoError;
  323. return ReturnValue;
  324. }
  325. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  326. std::stringstream stream;
  327. int _zw = _in;
  328. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  329. if (_anzNachkomma < 0) {
  330. _anzNachkomma = 0;
  331. }
  332. if (_anzNachkomma > 0)
  333. {
  334. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  335. return stream.str();
  336. }
  337. else
  338. {
  339. stream << _zw;
  340. }
  341. return stream.str();
  342. }
  343. string ClassFlowPostProcessing::ErsetzteN(string input)
  344. {
  345. int posN, posPunkt;
  346. int pot, ziffer;
  347. float zw;
  348. posN = findDelimiterPos(input, "N");
  349. posPunkt = findDelimiterPos(input, ".");
  350. if (posPunkt == std::string::npos){
  351. posPunkt = input.length();
  352. }
  353. while (posN != std::string::npos)
  354. {
  355. if (posN < posPunkt) {
  356. pot = posPunkt - posN - 1;
  357. }
  358. else {
  359. pot = posPunkt - posN;
  360. }
  361. zw = PreValue / pow(10, pot);
  362. ziffer = ((int) zw) % 10;
  363. input[posN] = ziffer + 48;
  364. posN = findDelimiterPos(input, "N");
  365. }
  366. return input;
  367. }
  368. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog){
  369. int aktdigit, olddigit;
  370. int aktdigit_before, olddigit_before;
  371. int pot, pot_max;
  372. float zw;
  373. bool no_nulldurchgang = false;
  374. pot = _decilamshift;
  375. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  376. {
  377. pot++;
  378. }
  379. pot_max = ((int) log10(input)) + 1;
  380. while (pot <= pot_max)
  381. {
  382. zw = input / pow(10, pot-1);
  383. aktdigit_before = ((int) zw) % 10;
  384. zw = PreValue / pow(10, pot-1);
  385. olddigit_before = ((int) zw) % 10;
  386. zw = input / pow(10, pot);
  387. aktdigit = ((int) zw) % 10;
  388. zw = PreValue / pow(10, pot);
  389. olddigit = ((int) zw) % 10;
  390. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  391. if (no_nulldurchgang)
  392. {
  393. if (aktdigit != olddigit)
  394. {
  395. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  396. }
  397. }
  398. else
  399. {
  400. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  401. {
  402. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  403. }
  404. }
  405. pot++;
  406. }
  407. return input;
  408. }
  409. string ClassFlowPostProcessing::getReadoutError()
  410. {
  411. return ErrorMessageText;
  412. }