ClassFlowPostProcessing.cpp 28 KB

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