ClassFlowPostProcessing.cpp 15 KB

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