ClassFlowPostProcessing.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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-%dT%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-%dT%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-%dT%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. if ((!AllowNegativeRates) && (Value < PreValue))
  298. {
  299. ErrorMessageText = ErrorMessageText + "Negative Rate - Returned old value - read value: " + zwvalue + " - raw value: " + ReturnRawValue + " - checked value: " + std::to_string(Value) + " ";
  300. Value = PreValue;
  301. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  302. }
  303. if (useMaxRateValue && (abs(Value - PreValue) > MaxRateValue))
  304. {
  305. ErrorMessageText = ErrorMessageText + "Rate too high - Returned old value - read value: " + zwvalue + " - checked value: " + RundeOutput(Value, AnzahlAnalog - DecimalShift) + " ";
  306. Value = PreValue;
  307. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  308. }
  309. ReturnValueNoError = zwvalue;
  310. ReturnValue = zwvalue;
  311. if (ErrorMessage && (ErrorMessageText.length() > 0))
  312. ReturnValue = ReturnValue + "\t" + ErrorMessageText;
  313. if (ErrorMessageText.length() == 0)
  314. {
  315. time_t currenttime;
  316. time(&currenttime);
  317. localtime(&currenttime);
  318. // currenttime =
  319. double difference = difftime(currenttime, lastvalue); // in Sekunden
  320. difference /= 60; // in Minuten
  321. FlowRateAct = (Value - PreValue) / difference;
  322. PreValue = Value;
  323. SavePreValue(Value, zwtime);
  324. }
  325. return true;
  326. }
  327. string ClassFlowPostProcessing::getReadout()
  328. {
  329. return ReturnValue;
  330. }
  331. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror)
  332. {
  333. if (_rawValue)
  334. return ReturnRawValue;
  335. if (_noerror)
  336. return ReturnValueNoError;
  337. return ReturnValue;
  338. }
  339. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  340. std::stringstream stream;
  341. int _zw = _in;
  342. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  343. if (_anzNachkomma < 0) {
  344. _anzNachkomma = 0;
  345. }
  346. if (_anzNachkomma > 0)
  347. {
  348. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  349. return stream.str();
  350. }
  351. else
  352. {
  353. stream << _zw;
  354. }
  355. return stream.str();
  356. }
  357. string ClassFlowPostProcessing::ErsetzteN(string input)
  358. {
  359. int posN, posPunkt;
  360. int pot, ziffer;
  361. float zw;
  362. posN = findDelimiterPos(input, "N");
  363. posPunkt = findDelimiterPos(input, ".");
  364. if (posPunkt == std::string::npos){
  365. posPunkt = input.length();
  366. }
  367. while (posN != std::string::npos)
  368. {
  369. if (posN < posPunkt) {
  370. pot = posPunkt - posN - 1;
  371. }
  372. else {
  373. pot = posPunkt - posN;
  374. }
  375. zw = PreValue / pow(10, pot);
  376. ziffer = ((int) zw) % 10;
  377. input[posN] = ziffer + 48;
  378. posN = findDelimiterPos(input, "N");
  379. }
  380. return input;
  381. }
  382. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog){
  383. int aktdigit, olddigit;
  384. int aktdigit_before, olddigit_before;
  385. int pot, pot_max;
  386. float zw;
  387. bool no_nulldurchgang = false;
  388. pot = _decilamshift;
  389. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  390. {
  391. pot++;
  392. }
  393. pot_max = ((int) log10(input)) + 1;
  394. while (pot <= pot_max)
  395. {
  396. zw = input / pow(10, pot-1);
  397. aktdigit_before = ((int) zw) % 10;
  398. zw = PreValue / pow(10, pot-1);
  399. olddigit_before = ((int) zw) % 10;
  400. zw = input / pow(10, pot);
  401. aktdigit = ((int) zw) % 10;
  402. zw = PreValue / pow(10, pot);
  403. olddigit = ((int) zw) % 10;
  404. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  405. if (no_nulldurchgang)
  406. {
  407. if (aktdigit != olddigit)
  408. {
  409. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  410. }
  411. }
  412. else
  413. {
  414. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  415. {
  416. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  417. }
  418. }
  419. pot++;
  420. }
  421. return input;
  422. }
  423. string ClassFlowPostProcessing::getReadoutRate()
  424. {
  425. return std::to_string(FlowRateAct);
  426. }
  427. string ClassFlowPostProcessing::getReadoutTimeStamp()
  428. {
  429. return timeStamp;
  430. }
  431. string ClassFlowPostProcessing::getReadoutError()
  432. {
  433. return ErrorMessageText;
  434. }