ClassFlowPostProcessing.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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);
  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]->AnzahlAnalog - NUMBERS[j]->DecimalShift);
  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]->AnzahlAnalog - NUMBERS[0]->DecimalShift);
  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::handleDecimalSeparator(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. int _zwdc = 0;
  216. try
  217. {
  218. _zwdc = stoi(_value);
  219. }
  220. catch(const std::exception& e)
  221. {
  222. printf("ERROR - Decimalshift is not a number: %s\n", _value.c_str());
  223. }
  224. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  225. {
  226. NUMBERS[j]->DecimalShift = _zwdc;
  227. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  228. }
  229. if (NUMBERS[j]->name == _digit)
  230. {
  231. NUMBERS[j]->DecimalShift = _zwdc;
  232. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  233. }
  234. NUMBERS[j]->Nachkomma = NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift;
  235. }
  236. }
  237. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  238. {
  239. string _digit, _decpos;
  240. int _pospunkt = _decsep.find_first_of(".");
  241. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  242. if (_pospunkt > -1)
  243. _digit = _decsep.substr(0, _pospunkt);
  244. else
  245. _digit = "default";
  246. for (int j = 0; j < NUMBERS.size(); ++j)
  247. {
  248. float _zwdc = 1;
  249. try
  250. {
  251. _zwdc = stof(_value);
  252. }
  253. catch(const std::exception& e)
  254. {
  255. printf("ERROR - MaxRateValue is not a number: %s\n", _value.c_str());
  256. }
  257. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  258. {
  259. NUMBERS[j]->useMaxRateValue = true;
  260. NUMBERS[j]->MaxRateValue = _zwdc;
  261. }
  262. if (NUMBERS[j]->name == _digit)
  263. {
  264. NUMBERS[j]->useMaxRateValue = true;
  265. NUMBERS[j]->MaxRateValue = _zwdc;
  266. }
  267. }
  268. }
  269. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  270. {
  271. std::vector<string> zerlegt;
  272. int _n;
  273. aktparamgraph = trim(aktparamgraph);
  274. if (aktparamgraph.size() == 0)
  275. if (!this->GetNextParagraph(pfile, aktparamgraph))
  276. return false;
  277. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  278. return false;
  279. InitNUMBERS();
  280. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  281. {
  282. zerlegt = this->ZerlegeZeile(aktparamgraph);
  283. std::string _param = GetParameterName(zerlegt[0]);
  284. if ((toUpper(_param) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  285. {
  286. handleDecimalSeparator(zerlegt[0], zerlegt[1]);
  287. }
  288. if ((toUpper(_param) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  289. {
  290. handleMaxRateValue(zerlegt[0], zerlegt[1]);
  291. }
  292. if ((toUpper(_param) == "PREVALUEUSE") && (zerlegt.size() > 1))
  293. {
  294. if (toUpper(zerlegt[1]) == "TRUE")
  295. {
  296. PreValueUse = true;
  297. }
  298. }
  299. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  300. {
  301. if (toUpper(zerlegt[1]) == "TRUE")
  302. for (_n = 0; _n < NUMBERS.size(); ++_n)
  303. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  304. }
  305. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  306. {
  307. if (toUpper(zerlegt[1]) == "TRUE")
  308. for (_n = 0; _n < NUMBERS.size(); ++_n)
  309. NUMBERS[_n]->AllowNegativeRates = true;
  310. }
  311. if ((toUpper(_param) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  312. {
  313. if (toUpper(zerlegt[1]) == "TRUE")
  314. ErrorMessage = true;
  315. }
  316. if ((toUpper(_param) == "IGNORELEADINGNAN") && (zerlegt.size() > 1))
  317. {
  318. if (toUpper(zerlegt[1]) == "TRUE")
  319. IgnoreLeadingNaN = true;
  320. }
  321. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  322. {
  323. PreValueAgeStartup = std::stoi(zerlegt[1]);
  324. }
  325. }
  326. if (PreValueUse) {
  327. LoadPreValue();
  328. }
  329. return true;
  330. }
  331. void ClassFlowPostProcessing::InitNUMBERS()
  332. {
  333. int anzDIGIT = 0;
  334. int anzANALOG = 0;
  335. std::vector<std::string> name_numbers;
  336. if (flowDigit)
  337. {
  338. anzDIGIT = flowDigit->getAnzahlGENERAL();
  339. flowDigit->UpdateNameNumbers(&name_numbers);
  340. }
  341. if (flowAnalog)
  342. {
  343. anzANALOG = flowAnalog->getAnzahlGENERAL();
  344. flowAnalog->UpdateNameNumbers(&name_numbers);
  345. }
  346. printf("Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d\n", name_numbers.size(), anzDIGIT, anzANALOG);
  347. for (int _num = 0; _num < name_numbers.size(); ++_num)
  348. {
  349. NumberPost *_number = new NumberPost;
  350. _number->name = name_numbers[_num];
  351. _number->digit_roi = NULL;
  352. if (flowDigit)
  353. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  354. if (_number->digit_roi)
  355. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  356. else
  357. _number->AnzahlDigital = 0;
  358. _number->analog_roi = NULL;
  359. if (flowAnalog)
  360. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  361. if (_number->analog_roi)
  362. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  363. else
  364. _number->AnzahlAnalog = 0;
  365. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  366. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  367. _number->ReturnValueNoError = ""; // korrigierter Rückgabewert ohne Fehlermeldung
  368. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  369. _number->ReturnPreValue = "";
  370. _number->PreValueOkay = false;
  371. _number->AllowNegativeRates = false;
  372. _number->MaxRateValue = 0.1;
  373. _number->useMaxRateValue = false;
  374. _number->checkDigitIncreaseConsistency = false;
  375. _number->PreValueOkay = false;
  376. _number->useMaxRateValue = false;
  377. _number->DecimalShift = 0;
  378. _number->DecimalShiftInitial = 0;
  379. _number->FlowRateAct = 0; // m3 / min
  380. _number->PreValue = 0; // letzter Wert, der gut ausgelesen wurde
  381. _number->Value = 0; // letzer ausgelesener Wert, inkl. Korrekturen
  382. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  383. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  384. _number->ReturnValueNoError = ""; // korrigierter Rückgabewert ohne Fehlermeldung
  385. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  386. _number->Nachkomma = _number->AnzahlAnalog;
  387. NUMBERS.push_back(_number);
  388. }
  389. for (int i = 0; i < NUMBERS.size(); ++i)
  390. printf("Number %s, Anz DIG: %d, Anz ANA %d\n", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  391. }
  392. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  393. if (_decShift == 0){
  394. return in;
  395. }
  396. int _pos_dec_org, _pos_dec_neu;
  397. _pos_dec_org = findDelimiterPos(in, ".");
  398. if (_pos_dec_org == std::string::npos) {
  399. _pos_dec_org = in.length();
  400. }
  401. else
  402. {
  403. in = in.erase(_pos_dec_org, 1);
  404. }
  405. _pos_dec_neu = _pos_dec_org + _decShift;
  406. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  407. for (int i = 0; i > _pos_dec_neu; --i){
  408. in = in.insert(0, "0");
  409. }
  410. in = "0." + in;
  411. return in;
  412. }
  413. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  414. for (int i = in.length(); i < _pos_dec_neu; ++i){
  415. in = in.insert(in.length(), "0");
  416. }
  417. return in;
  418. }
  419. string zw;
  420. zw = in.substr(0, _pos_dec_neu);
  421. zw = zw + ".";
  422. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  423. return zw;
  424. }
  425. bool ClassFlowPostProcessing::doFlow(string zwtime)
  426. {
  427. string result = "";
  428. string digit = "";
  429. string analog = "";
  430. string zwvalue;
  431. string zw;
  432. time_t imagetime = 0;
  433. string rohwert;
  434. // ErrorMessageText = "";
  435. // Update Nachkomma, da sich beim Wechsel von CNNType Auto --> xyz auch die Nachkommastellen ändern können:
  436. imagetime = flowMakeImage->getTimeImageTaken();
  437. if (imagetime == 0)
  438. time(&imagetime);
  439. struct tm* timeinfo;
  440. timeinfo = localtime(&imagetime);
  441. char strftime_buf[64];
  442. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  443. zwtime = std::string(strftime_buf);
  444. printf("Anzahl NUMBERS: %d\n", NUMBERS.size());
  445. for (int j = 0; j < NUMBERS.size(); ++j)
  446. {
  447. NUMBERS[j]->ReturnRawValue = "";
  448. NUMBERS[j]->ErrorMessageText = "";
  449. if (flowAnalog) NUMBERS[j]->AnzahlAnalog = flowAnalog->AnzahlROIs(j);
  450. if (flowDigit) NUMBERS[j]->AnzahlDigital = flowDigit->AnzahlROIs(j);
  451. if (flowDigit->isExtendedResolution())
  452. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial - 1;
  453. NUMBERS[j]->Nachkomma = NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift;
  454. if (NUMBERS[j]->digit_roi)
  455. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j);
  456. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  457. NUMBERS[j]->ReturnRawValue = NUMBERS[j]->ReturnRawValue + ".";
  458. if (NUMBERS[j]->analog_roi)
  459. NUMBERS[j]->ReturnRawValue = NUMBERS[j]->ReturnRawValue + flowAnalog->getReadout(j);
  460. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  461. ///////////////// SPEZIALFALL für User Gustl ///////////////////////////////////////////////////////
  462. if (IgnoreLeadingNaN)
  463. {
  464. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  465. {
  466. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  467. }
  468. }
  469. ////////////////////////////////////////////////////////////////////////////////////////////////////
  470. rohwert = NUMBERS[j]->ReturnRawValue;
  471. if (!PreValueUse || !NUMBERS[j]->PreValueOkay)
  472. {
  473. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  474. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnRawValue;
  475. if ((findDelimiterPos(NUMBERS[j]->ReturnValue, "N") == std::string::npos) && (NUMBERS[j]->ReturnValue.length() > 0))
  476. {
  477. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  478. {
  479. NUMBERS[j]->ReturnValue.erase(0, 1);
  480. }
  481. NUMBERS[j]->Value = std::stof(NUMBERS[j]->ReturnValue);
  482. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnValue;
  483. NUMBERS[j]->PreValueOkay = true;
  484. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  485. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  486. NUMBERS[j]->lastvalue = flowMakeImage->getTimeImageTaken();
  487. zwtime = ConvertTimeToString(NUMBERS[j]->lastvalue, PREVALUE_TIME_FORMAT_OUTPUT);
  488. UpdatePreValueINI = true;
  489. SavePreValue();
  490. }
  491. }
  492. else
  493. {
  494. zw = ErsetzteN(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->PreValue);
  495. NUMBERS[j]->Value = std::stof(zw);
  496. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  497. {
  498. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  499. }
  500. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift);
  501. if ((!NUMBERS[j]->AllowNegativeRates) && (NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  502. {
  503. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  504. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  505. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift);
  506. }
  507. if (NUMBERS[j]->useMaxRateValue && (abs(NUMBERS[j]->Value - NUMBERS[j]->PreValue) > NUMBERS[j]->MaxRateValue))
  508. {
  509. 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);
  510. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  511. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  512. }
  513. NUMBERS[j]->ReturnValueNoError = zwvalue;
  514. NUMBERS[j]->ReturnValue = zwvalue;
  515. if (NUMBERS[j]->ErrorMessage && (NUMBERS[j]->ErrorMessageText.length() > 0))
  516. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnValue + "\t" + NUMBERS[j]->ErrorMessageText;
  517. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  518. difference /= 60; // in Minuten
  519. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  520. NUMBERS[j]->lastvalue = imagetime;
  521. if (NUMBERS[j]->ErrorMessageText.length() == 0)
  522. {
  523. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  524. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  525. NUMBERS[j]->ErrorMessageText = "no error";
  526. UpdatePreValueINI = true;
  527. }
  528. }
  529. }
  530. SavePreValue();
  531. return true;
  532. }
  533. string ClassFlowPostProcessing::getReadout(int _number)
  534. {
  535. return NUMBERS[_number]->ReturnValue;
  536. }
  537. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  538. {
  539. if (_rawValue)
  540. return NUMBERS[_number]->ReturnRawValue;
  541. if (_noerror)
  542. return NUMBERS[_number]->ReturnValueNoError;
  543. return NUMBERS[_number]->ReturnValue;
  544. }
  545. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  546. std::stringstream stream;
  547. int _zw = _in;
  548. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  549. if (_anzNachkomma < 0) {
  550. _anzNachkomma = 0;
  551. }
  552. if (_anzNachkomma > 0)
  553. {
  554. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  555. return stream.str();
  556. }
  557. else
  558. {
  559. stream << _zw;
  560. }
  561. return stream.str();
  562. }
  563. string ClassFlowPostProcessing::ErsetzteN(string input, float _prevalue)
  564. {
  565. int posN, posPunkt;
  566. int pot, ziffer;
  567. float zw;
  568. posN = findDelimiterPos(input, "N");
  569. posPunkt = findDelimiterPos(input, ".");
  570. if (posPunkt == std::string::npos){
  571. posPunkt = input.length();
  572. }
  573. while (posN != std::string::npos)
  574. {
  575. if (posN < posPunkt) {
  576. pot = posPunkt - posN - 1;
  577. }
  578. else {
  579. pot = posPunkt - posN;
  580. }
  581. zw =_prevalue / pow(10, pot);
  582. ziffer = ((int) zw) % 10;
  583. input[posN] = ziffer + 48;
  584. posN = findDelimiterPos(input, "N");
  585. }
  586. return input;
  587. }
  588. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog, float _preValue){
  589. int aktdigit, olddigit;
  590. int aktdigit_before, olddigit_before;
  591. int pot, pot_max;
  592. float zw;
  593. bool no_nulldurchgang = false;
  594. pot = _decilamshift;
  595. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  596. {
  597. pot++;
  598. }
  599. pot_max = ((int) log10(input)) + 1;
  600. while (pot <= pot_max)
  601. {
  602. zw = input / pow(10, pot-1);
  603. aktdigit_before = ((int) zw) % 10;
  604. zw = _preValue / pow(10, pot-1);
  605. olddigit_before = ((int) zw) % 10;
  606. zw = input / pow(10, pot);
  607. aktdigit = ((int) zw) % 10;
  608. zw = _preValue / pow(10, pot);
  609. olddigit = ((int) zw) % 10;
  610. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  611. if (no_nulldurchgang)
  612. {
  613. if (aktdigit != olddigit)
  614. {
  615. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  616. }
  617. }
  618. else
  619. {
  620. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  621. {
  622. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  623. }
  624. }
  625. pot++;
  626. }
  627. return input;
  628. }
  629. string ClassFlowPostProcessing::getReadoutRate(int _number)
  630. {
  631. return std::to_string(NUMBERS[_number]->FlowRateAct);
  632. }
  633. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  634. {
  635. return NUMBERS[_number]->timeStamp;
  636. }
  637. string ClassFlowPostProcessing::getReadoutError(int _number)
  638. {
  639. return NUMBERS[_number]->ErrorMessageText;
  640. }