ClassFlowPostProcessing.cpp 28 KB

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