ClassFlowPostProcessing.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. #include "ClassFlowPostProcessing.h"
  2. #include "Helper.h"
  3. #include "ClassFlowMakeImage.h"
  4. #include "ClassLogFile.h"
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <time.h>
  8. #include "time_sntp.h"
  9. #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S"
  10. #define PREVALUE_TIME_FORMAT_INPUT "%d-%d-%dT%d:%d:%d"
  11. string ClassFlowPostProcessing::GetPreValue(std::string _number)
  12. {
  13. std::string result;
  14. int index = -1;
  15. if (_number == "")
  16. _number = "default";
  17. for (int i = 0; i < NUMBERS.size(); ++i)
  18. if (NUMBERS[i]->name == _number)
  19. index = i;
  20. result = RundeOutput(NUMBERS[index]->PreValue, NUMBERS[index]->Nachkomma);
  21. return result;
  22. }
  23. void ClassFlowPostProcessing::SetPreValue(float zw, string _numbers, bool _extern)
  24. {
  25. printf("SetPrevalue: %f, %s\n", zw, _numbers.c_str());
  26. for (int j = 0; j < NUMBERS.size(); ++j)
  27. {
  28. // printf("Number %d, %s\n", j, NUMBERS[j]->name.c_str());
  29. if (NUMBERS[j]->name == _numbers)
  30. {
  31. NUMBERS[j]->PreValue = zw;
  32. if (_extern)
  33. {
  34. time(&(NUMBERS[j]->lastvalue));
  35. localtime(&(NUMBERS[j]->lastvalue));
  36. }
  37. // printf("Found %d! - set to %f\n", j, NUMBERS[j]->PreValue);
  38. }
  39. }
  40. UpdatePreValueINI = true;
  41. SavePreValue();
  42. }
  43. bool ClassFlowPostProcessing::LoadPreValue(void)
  44. {
  45. std::vector<string> zerlegt;
  46. FILE* pFile;
  47. char zw[1024];
  48. string zwtime, zwvalue, name;
  49. bool _done = false;
  50. UpdatePreValueINI = false; // Konvertierung ins neue Format
  51. pFile = fopen(FilePreValue.c_str(), "r");
  52. if (pFile == NULL)
  53. return false;
  54. fgets(zw, 1024, pFile);
  55. printf("Read Zeile Prevalue.ini: %s", zw);
  56. zwtime = trim(std::string(zw));
  57. if (zwtime.length() == 0)
  58. return false;
  59. zerlegt = HelperZerlegeZeile(zwtime, "\t");
  60. if (zerlegt.size() > 1) // neues Format
  61. {
  62. while ((zerlegt.size() > 1) && !_done)
  63. {
  64. name = trim(zerlegt[0]);
  65. zwtime = trim(zerlegt[1]);
  66. zwvalue = trim(zerlegt[2]);
  67. for (int j = 0; j < NUMBERS.size(); ++j)
  68. {
  69. if (NUMBERS[j]->name == name)
  70. {
  71. NUMBERS[j]->PreValue = stof(zwvalue.c_str());
  72. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma + 1); // SIcherheitshalber 1 Stelle mehr, da ggf. Exgtended Resolution an ist (wird erst beim ersten Durchlauf gesetzt)
  73. time_t tStart;
  74. int yy, month, dd, hh, mm, ss;
  75. struct tm whenStart;
  76. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  77. whenStart.tm_year = yy - 1900;
  78. whenStart.tm_mon = month - 1;
  79. whenStart.tm_mday = dd;
  80. whenStart.tm_hour = hh;
  81. whenStart.tm_min = mm;
  82. whenStart.tm_sec = ss;
  83. whenStart.tm_isdst = -1;
  84. NUMBERS[j]->lastvalue = mktime(&whenStart);
  85. time(&tStart);
  86. localtime(&tStart);
  87. double difference = difftime(tStart, NUMBERS[j]->lastvalue);
  88. difference /= 60;
  89. if (difference > PreValueAgeStartup)
  90. NUMBERS[j]->PreValueOkay = false;
  91. else
  92. NUMBERS[j]->PreValueOkay = true;
  93. }
  94. }
  95. if (!fgets(zw, 1024, pFile))
  96. _done = true;
  97. else
  98. {
  99. printf("Read Zeile Prevalue.ini: %s", zw);
  100. zerlegt = HelperZerlegeZeile(trim(std::string(zw)), "\t");
  101. if (zerlegt.size() > 1)
  102. {
  103. name = trim(zerlegt[0]);
  104. zwtime = trim(zerlegt[1]);
  105. zwvalue = trim(zerlegt[2]);
  106. }
  107. }
  108. }
  109. fclose(pFile);
  110. }
  111. else // altes Format
  112. {
  113. fgets(zw, 1024, pFile);
  114. fclose(pFile);
  115. printf("%s", zw);
  116. zwvalue = trim(std::string(zw));
  117. NUMBERS[0]->PreValue = stof(zwvalue.c_str());
  118. time_t tStart;
  119. int yy, month, dd, hh, mm, ss;
  120. struct tm whenStart;
  121. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  122. whenStart.tm_year = yy - 1900;
  123. whenStart.tm_mon = month - 1;
  124. whenStart.tm_mday = dd;
  125. whenStart.tm_hour = hh;
  126. whenStart.tm_min = mm;
  127. whenStart.tm_sec = ss;
  128. whenStart.tm_isdst = -1;
  129. printf("TIME: %d, %d, %d, %d, %d, %d\n", whenStart.tm_year, whenStart.tm_mon, whenStart.tm_wday, whenStart.tm_hour, whenStart.tm_min, whenStart.tm_sec);
  130. NUMBERS[0]->lastvalue = mktime(&whenStart);
  131. time(&tStart);
  132. localtime(&tStart);
  133. double difference = difftime(tStart, NUMBERS[0]->lastvalue);
  134. difference /= 60;
  135. if (difference > PreValueAgeStartup)
  136. return false;
  137. NUMBERS[0]->Value = NUMBERS[0]->PreValue;
  138. NUMBERS[0]->ReturnValue = to_string(NUMBERS[0]->Value);
  139. NUMBERS[0]->ReturnValueNoError = NUMBERS[0]->ReturnValue;
  140. if (NUMBERS[0]->digit_roi || NUMBERS[0]->analog_roi)
  141. {
  142. NUMBERS[0]->ReturnValue = RundeOutput(NUMBERS[0]->Value, NUMBERS[0]->Nachkomma);
  143. NUMBERS[0]->ReturnValueNoError = NUMBERS[0]->ReturnValue;
  144. }
  145. UpdatePreValueINI = true; // Konvertierung ins neue Format
  146. SavePreValue();
  147. }
  148. return true;
  149. }
  150. void ClassFlowPostProcessing::SavePreValue()
  151. {
  152. FILE* pFile;
  153. string _zw;
  154. if (!UpdatePreValueINI) // PreValues unverändert --> File muss nicht neu geschrieben werden
  155. return;
  156. pFile = fopen(FilePreValue.c_str(), "w");
  157. for (int j = 0; j < NUMBERS.size(); ++j)
  158. {
  159. char buffer[80];
  160. struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue);
  161. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  162. NUMBERS[j]->timeStamp = std::string(buffer);
  163. // printf("SaverPreValue %d, Value: %f, Nachkomma %d\n", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  164. _zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n";
  165. printf("Write PreValue Zeile: %s\n", _zw.c_str());
  166. fputs(_zw.c_str(), pFile);
  167. }
  168. UpdatePreValueINI = false;
  169. fclose(pFile);
  170. }
  171. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc, ClassFlowCNNGeneral *_analog, ClassFlowCNNGeneral *_digit)
  172. {
  173. PreValueUse = false;
  174. PreValueAgeStartup = 30;
  175. ErrorMessage = false;
  176. ListFlowControll = NULL;
  177. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  178. ListFlowControll = lfc;
  179. flowMakeImage = NULL;
  180. UpdatePreValueINI = false;
  181. IgnoreLeadingNaN = false;
  182. flowAnalog = _analog;
  183. flowDigit = _digit;
  184. for (int i = 0; i < ListFlowControll->size(); ++i)
  185. {
  186. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  187. {
  188. flowMakeImage = (ClassFlowMakeImage*) (*ListFlowControll)[i];
  189. }
  190. }
  191. }
  192. void ClassFlowPostProcessing::handleDecimalExtendedResolution(string _decsep, string _value)
  193. {
  194. string _digit, _decpos;
  195. int _pospunkt = _decsep.find_first_of(".");
  196. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  197. if (_pospunkt > -1)
  198. _digit = _decsep.substr(0, _pospunkt);
  199. else
  200. _digit = "default";
  201. for (int j = 0; j < NUMBERS.size(); ++j)
  202. {
  203. bool _zwdc = false;
  204. if (toUpper(_value) == "TRUE")
  205. _zwdc = true;
  206. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  207. {
  208. NUMBERS[j]->isExtendedResolution = _zwdc;
  209. }
  210. if (NUMBERS[j]->name == _digit)
  211. {
  212. NUMBERS[j]->isExtendedResolution = _zwdc;
  213. }
  214. }
  215. }
  216. void ClassFlowPostProcessing::handleDecimalSeparator(string _decsep, string _value)
  217. {
  218. string _digit, _decpos;
  219. int _pospunkt = _decsep.find_first_of(".");
  220. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  221. if (_pospunkt > -1)
  222. _digit = _decsep.substr(0, _pospunkt);
  223. else
  224. _digit = "default";
  225. for (int j = 0; j < NUMBERS.size(); ++j)
  226. {
  227. int _zwdc = 0;
  228. // try
  229. {
  230. _zwdc = stoi(_value);
  231. }
  232. /* catch(const std::exception& e)
  233. {
  234. printf("ERROR - Decimalshift is not a number: %s\n", _value.c_str());
  235. }
  236. */
  237. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  238. {
  239. NUMBERS[j]->DecimalShift = _zwdc;
  240. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  241. }
  242. if (NUMBERS[j]->name == _digit)
  243. {
  244. NUMBERS[j]->DecimalShift = _zwdc;
  245. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  246. }
  247. NUMBERS[j]->Nachkomma = NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift;
  248. }
  249. }
  250. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  251. {
  252. string _digit, _decpos;
  253. int _pospunkt = _decsep.find_first_of(".");
  254. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  255. if (_pospunkt > -1)
  256. _digit = _decsep.substr(0, _pospunkt);
  257. else
  258. _digit = "default";
  259. for (int j = 0; j < NUMBERS.size(); ++j)
  260. {
  261. float _zwdc = 1;
  262. // try
  263. {
  264. _zwdc = stof(_value);
  265. }
  266. /* catch(const std::exception& e)
  267. {
  268. printf("ERROR - MaxRateValue is not a number: %s\n", _value.c_str());
  269. }
  270. */
  271. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  272. {
  273. NUMBERS[j]->useMaxRateValue = true;
  274. NUMBERS[j]->MaxRateValue = _zwdc;
  275. }
  276. if (NUMBERS[j]->name == _digit)
  277. {
  278. NUMBERS[j]->useMaxRateValue = true;
  279. NUMBERS[j]->MaxRateValue = _zwdc;
  280. }
  281. }
  282. }
  283. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  284. {
  285. std::vector<string> zerlegt;
  286. int _n;
  287. aktparamgraph = trim(aktparamgraph);
  288. if (aktparamgraph.size() == 0)
  289. if (!this->GetNextParagraph(pfile, aktparamgraph))
  290. return false;
  291. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  292. return false;
  293. InitNUMBERS();
  294. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  295. {
  296. zerlegt = this->ZerlegeZeile(aktparamgraph);
  297. std::string _param = GetParameterName(zerlegt[0]);
  298. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (zerlegt.size() > 1))
  299. {
  300. handleDecimalExtendedResolution(zerlegt[0], zerlegt[1]);
  301. }
  302. if ((toUpper(_param) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  303. {
  304. handleDecimalSeparator(zerlegt[0], zerlegt[1]);
  305. }
  306. if ((toUpper(_param) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  307. {
  308. handleMaxRateValue(zerlegt[0], zerlegt[1]);
  309. }
  310. if ((toUpper(_param) == "PREVALUEUSE") && (zerlegt.size() > 1))
  311. {
  312. if (toUpper(zerlegt[1]) == "TRUE")
  313. {
  314. PreValueUse = true;
  315. }
  316. }
  317. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  318. {
  319. if (toUpper(zerlegt[1]) == "TRUE")
  320. for (_n = 0; _n < NUMBERS.size(); ++_n)
  321. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  322. }
  323. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  324. {
  325. if (toUpper(zerlegt[1]) == "TRUE")
  326. for (_n = 0; _n < NUMBERS.size(); ++_n)
  327. NUMBERS[_n]->AllowNegativeRates = true;
  328. }
  329. if ((toUpper(_param) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  330. {
  331. if (toUpper(zerlegt[1]) == "TRUE")
  332. ErrorMessage = true;
  333. }
  334. if ((toUpper(_param) == "IGNORELEADINGNAN") && (zerlegt.size() > 1))
  335. {
  336. if (toUpper(zerlegt[1]) == "TRUE")
  337. IgnoreLeadingNaN = true;
  338. }
  339. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  340. {
  341. PreValueAgeStartup = std::stoi(zerlegt[1]);
  342. }
  343. }
  344. if (PreValueUse) {
  345. LoadPreValue();
  346. }
  347. return true;
  348. }
  349. void ClassFlowPostProcessing::InitNUMBERS()
  350. {
  351. int anzDIGIT = 0;
  352. int anzANALOG = 0;
  353. std::vector<std::string> name_numbers;
  354. if (flowDigit)
  355. {
  356. anzDIGIT = flowDigit->getAnzahlGENERAL();
  357. flowDigit->UpdateNameNumbers(&name_numbers);
  358. }
  359. if (flowAnalog)
  360. {
  361. anzANALOG = flowAnalog->getAnzahlGENERAL();
  362. flowAnalog->UpdateNameNumbers(&name_numbers);
  363. }
  364. printf("Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d\n", name_numbers.size(), anzDIGIT, anzANALOG);
  365. for (int _num = 0; _num < name_numbers.size(); ++_num)
  366. {
  367. NumberPost *_number = new NumberPost;
  368. _number->name = name_numbers[_num];
  369. _number->digit_roi = NULL;
  370. if (flowDigit)
  371. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  372. if (_number->digit_roi)
  373. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  374. else
  375. _number->AnzahlDigital = 0;
  376. _number->analog_roi = NULL;
  377. if (flowAnalog)
  378. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  379. if (_number->analog_roi)
  380. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  381. else
  382. _number->AnzahlAnalog = 0;
  383. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  384. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  385. _number->ReturnValueNoError = ""; // korrigierter Rückgabewert ohne Fehlermeldung
  386. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  387. _number->ReturnPreValue = "";
  388. _number->PreValueOkay = false;
  389. _number->AllowNegativeRates = false;
  390. _number->MaxRateValue = 0.1;
  391. _number->useMaxRateValue = false;
  392. _number->checkDigitIncreaseConsistency = false;
  393. _number->DecimalShift = 0;
  394. _number->DecimalShiftInitial = 0;
  395. _number->isExtendedResolution = false;
  396. _number->FlowRateAct = 0; // m3 / min
  397. _number->PreValue = 0; // letzter Wert, der gut ausgelesen wurde
  398. _number->Value = 0; // letzer ausgelesener Wert, inkl. Korrekturen
  399. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  400. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  401. _number->ReturnValueNoError = ""; // korrigierter Rückgabewert ohne Fehlermeldung
  402. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  403. _number->Nachkomma = _number->AnzahlAnalog;
  404. NUMBERS.push_back(_number);
  405. }
  406. for (int i = 0; i < NUMBERS.size(); ++i)
  407. printf("Number %s, Anz DIG: %d, Anz ANA %d\n", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  408. }
  409. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  410. if (_decShift == 0){
  411. return in;
  412. }
  413. int _pos_dec_org, _pos_dec_neu;
  414. _pos_dec_org = findDelimiterPos(in, ".");
  415. if (_pos_dec_org == std::string::npos) {
  416. _pos_dec_org = in.length();
  417. }
  418. else
  419. {
  420. in = in.erase(_pos_dec_org, 1);
  421. }
  422. _pos_dec_neu = _pos_dec_org + _decShift;
  423. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  424. for (int i = 0; i > _pos_dec_neu; --i){
  425. in = in.insert(0, "0");
  426. }
  427. in = "0." + in;
  428. return in;
  429. }
  430. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  431. for (int i = in.length(); i < _pos_dec_neu; ++i){
  432. in = in.insert(in.length(), "0");
  433. }
  434. return in;
  435. }
  436. string zw;
  437. zw = in.substr(0, _pos_dec_neu);
  438. zw = zw + ".";
  439. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  440. return zw;
  441. }
  442. bool ClassFlowPostProcessing::doFlow(string zwtime)
  443. {
  444. string result = "";
  445. string digit = "";
  446. string analog = "";
  447. string zwvalue;
  448. string zw;
  449. time_t imagetime = 0;
  450. string rohwert;
  451. // Update Nachkomma, da sich beim Wechsel von CNNType Auto --> xyz auch die Nachkommastellen ändern können:
  452. imagetime = flowMakeImage->getTimeImageTaken();
  453. if (imagetime == 0)
  454. time(&imagetime);
  455. struct tm* timeinfo;
  456. timeinfo = localtime(&imagetime);
  457. char strftime_buf[64];
  458. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  459. zwtime = std::string(strftime_buf);
  460. printf("Anzahl NUMBERS: %d\n", NUMBERS.size());
  461. for (int j = 0; j < NUMBERS.size(); ++j)
  462. {
  463. NUMBERS[j]->ReturnRawValue = "";
  464. NUMBERS[j]->ErrorMessageText = "";
  465. UpdateNachkommaDecimalShift();
  466. if (NUMBERS[j]->digit_roi)
  467. {
  468. if (NUMBERS[j]->analog_roi)
  469. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false);
  470. else
  471. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution); // Extended Resolution nur falls es keine analogen Ziffern gibt
  472. }
  473. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  474. NUMBERS[j]->ReturnRawValue = NUMBERS[j]->ReturnRawValue + ".";
  475. if (NUMBERS[j]->analog_roi)
  476. NUMBERS[j]->ReturnRawValue = NUMBERS[j]->ReturnRawValue + flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  477. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  478. if (IgnoreLeadingNaN)
  479. {
  480. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  481. {
  482. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  483. }
  484. }
  485. rohwert = NUMBERS[j]->ReturnRawValue;
  486. if (!PreValueUse || !NUMBERS[j]->PreValueOkay)
  487. {
  488. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  489. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnRawValue;
  490. if ((findDelimiterPos(NUMBERS[j]->ReturnValue, "N") == std::string::npos) && (NUMBERS[j]->ReturnValue.length() > 0))
  491. {
  492. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  493. {
  494. NUMBERS[j]->ReturnValue.erase(0, 1);
  495. }
  496. NUMBERS[j]->Value = std::stof(NUMBERS[j]->ReturnValue);
  497. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnValue;
  498. NUMBERS[j]->PreValueOkay = true;
  499. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  500. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  501. NUMBERS[j]->lastvalue = flowMakeImage->getTimeImageTaken();
  502. zwtime = ConvertTimeToString(NUMBERS[j]->lastvalue, PREVALUE_TIME_FORMAT_OUTPUT);
  503. UpdatePreValueINI = true;
  504. SavePreValue();
  505. }
  506. else
  507. {
  508. NUMBERS[j]->ReturnValue = "";
  509. NUMBERS[j]->ReturnValueNoError = "";
  510. }
  511. }
  512. else
  513. {
  514. zw = ErsetzteN(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->PreValue);
  515. NUMBERS[j]->Value = std::stof(zw);
  516. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  517. {
  518. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  519. }
  520. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  521. if ((!NUMBERS[j]->AllowNegativeRates) && (NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  522. {
  523. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  524. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  525. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  526. }
  527. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  528. difference /= 60; // in Minuten
  529. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  530. NUMBERS[j]->ReturnRateValue = std::to_string(NUMBERS[j]->FlowRateAct);
  531. if (NUMBERS[j]->useMaxRateValue && (abs(NUMBERS[j]->FlowRateAct) > NUMBERS[j]->MaxRateValue))
  532. {
  533. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Rate too high - Read: " + RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma) + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  534. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  535. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  536. }
  537. NUMBERS[j]->ReturnValueNoError = zwvalue;
  538. NUMBERS[j]->ReturnValue = zwvalue;
  539. if (NUMBERS[j]->ErrorMessage && (NUMBERS[j]->ErrorMessageText.length() > 0))
  540. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnValue + "\t" + NUMBERS[j]->ErrorMessageText;
  541. if (NUMBERS[j]->ErrorMessageText.length() == 0)
  542. {
  543. NUMBERS[j]->lastvalue = imagetime;
  544. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  545. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnValue;
  546. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  547. NUMBERS[j]->ErrorMessageText = "no error";
  548. UpdatePreValueINI = true;
  549. }
  550. else
  551. {
  552. NUMBERS[j]->ReturnRateValue = "";
  553. NUMBERS[j]->ReturnValue = "";
  554. NUMBERS[j]->ReturnValueNoError = "";
  555. NUMBERS[j]->timeStamp = "";
  556. }
  557. }
  558. string _zw = "PostProcessing - Raw: " + NUMBERS[j]->ReturnRawValue + " Value: " + NUMBERS[j]->ReturnValue + " Error: " + NUMBERS[j]->ErrorMessageText;
  559. LogFile.WriteToFile(_zw);
  560. }
  561. SavePreValue();
  562. return true;
  563. }
  564. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  565. {
  566. for (int j = 0; j < NUMBERS.size(); ++j)
  567. {
  568. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  569. {
  570. // printf("Nurdigital\n");
  571. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  572. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  573. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  574. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  575. }
  576. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  577. {
  578. // printf("Nur analog\n");
  579. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  580. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  581. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  582. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  583. }
  584. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  585. {
  586. // printf("Nur digital + analog\n");
  587. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  588. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  589. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  590. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  591. }
  592. printf("UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i\n", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  593. }
  594. }
  595. string ClassFlowPostProcessing::getReadout(int _number)
  596. {
  597. return NUMBERS[_number]->ReturnValue;
  598. }
  599. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  600. {
  601. if (_rawValue)
  602. return NUMBERS[_number]->ReturnRawValue;
  603. if (_noerror)
  604. return NUMBERS[_number]->ReturnValueNoError;
  605. return NUMBERS[_number]->ReturnValue;
  606. }
  607. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  608. std::stringstream stream;
  609. int _zw = _in;
  610. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  611. if (_anzNachkomma < 0) {
  612. _anzNachkomma = 0;
  613. }
  614. if (_anzNachkomma > 0)
  615. {
  616. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  617. return stream.str();
  618. }
  619. else
  620. {
  621. stream << _zw;
  622. }
  623. return stream.str();
  624. }
  625. string ClassFlowPostProcessing::ErsetzteN(string input, float _prevalue)
  626. {
  627. int posN, posPunkt;
  628. int pot, ziffer;
  629. float zw;
  630. posN = findDelimiterPos(input, "N");
  631. posPunkt = findDelimiterPos(input, ".");
  632. if (posPunkt == std::string::npos){
  633. posPunkt = input.length();
  634. }
  635. while (posN != std::string::npos)
  636. {
  637. if (posN < posPunkt) {
  638. pot = posPunkt - posN - 1;
  639. }
  640. else {
  641. pot = posPunkt - posN;
  642. }
  643. zw =_prevalue / pow(10, pot);
  644. ziffer = ((int) zw) % 10;
  645. input[posN] = ziffer + 48;
  646. posN = findDelimiterPos(input, "N");
  647. }
  648. return input;
  649. }
  650. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog, float _preValue){
  651. int aktdigit, olddigit;
  652. int aktdigit_before, olddigit_before;
  653. int pot, pot_max;
  654. float zw;
  655. bool no_nulldurchgang = false;
  656. pot = _decilamshift;
  657. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  658. {
  659. pot++;
  660. }
  661. pot_max = ((int) log10(input)) + 1;
  662. while (pot <= pot_max)
  663. {
  664. zw = input / pow(10, pot-1);
  665. aktdigit_before = ((int) zw + 10) % 10;
  666. zw = _preValue / pow(10, pot-1);
  667. olddigit_before = ((int) zw + 10) % 10;
  668. zw = input / pow(10, pot);
  669. aktdigit = ((int) zw + 10) % 10;
  670. zw = _preValue / pow(10, pot);
  671. olddigit = ((int) zw + 10) % 10;
  672. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  673. if (no_nulldurchgang)
  674. {
  675. if (aktdigit != olddigit)
  676. {
  677. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  678. }
  679. }
  680. else
  681. {
  682. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  683. {
  684. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  685. }
  686. }
  687. pot++;
  688. }
  689. return input;
  690. }
  691. string ClassFlowPostProcessing::getReadoutRate(int _number)
  692. {
  693. return std::to_string(NUMBERS[_number]->FlowRateAct);
  694. }
  695. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  696. {
  697. return NUMBERS[_number]->timeStamp;
  698. }
  699. string ClassFlowPostProcessing::getReadoutError(int _number)
  700. {
  701. return NUMBERS[_number]->ErrorMessageText;
  702. }