ClassFlowPostProcessing.cpp 14 KB

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