ClassFlowPostProcessing.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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::handleMaxRateType(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. t_RateType _rt = AbsoluteChange;
  262. if (toUpper(_value) == "RATECHANGE")
  263. _rt = RateChange;
  264. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  265. {
  266. NUMBERS[j]->RateType = _rt;
  267. }
  268. if (NUMBERS[j]->name == _digit)
  269. {
  270. NUMBERS[j]->RateType = _rt;
  271. }
  272. }
  273. }
  274. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  275. {
  276. string _digit, _decpos;
  277. int _pospunkt = _decsep.find_first_of(".");
  278. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  279. if (_pospunkt > -1)
  280. _digit = _decsep.substr(0, _pospunkt);
  281. else
  282. _digit = "default";
  283. for (int j = 0; j < NUMBERS.size(); ++j)
  284. {
  285. float _zwdc = 1;
  286. // try
  287. {
  288. _zwdc = stof(_value);
  289. }
  290. /* catch(const std::exception& e)
  291. {
  292. printf("ERROR - MaxRateValue is not a number: %s\n", _value.c_str());
  293. }
  294. */
  295. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  296. {
  297. NUMBERS[j]->useMaxRateValue = true;
  298. NUMBERS[j]->MaxRateValue = _zwdc;
  299. }
  300. if (NUMBERS[j]->name == _digit)
  301. {
  302. NUMBERS[j]->useMaxRateValue = true;
  303. NUMBERS[j]->MaxRateValue = _zwdc;
  304. }
  305. }
  306. }
  307. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  308. {
  309. std::vector<string> zerlegt;
  310. int _n;
  311. aktparamgraph = trim(aktparamgraph);
  312. if (aktparamgraph.size() == 0)
  313. if (!this->GetNextParagraph(pfile, aktparamgraph))
  314. return false;
  315. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  316. return false;
  317. InitNUMBERS();
  318. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  319. {
  320. zerlegt = this->ZerlegeZeile(aktparamgraph);
  321. std::string _param = GetParameterName(zerlegt[0]);
  322. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (zerlegt.size() > 1))
  323. {
  324. handleDecimalExtendedResolution(zerlegt[0], zerlegt[1]);
  325. }
  326. if ((toUpper(_param) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  327. {
  328. handleDecimalSeparator(zerlegt[0], zerlegt[1]);
  329. }
  330. if ((toUpper(_param) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  331. {
  332. handleMaxRateValue(zerlegt[0], zerlegt[1]);
  333. }
  334. if ((toUpper(_param) == "MAXRATETYPE") && (zerlegt.size() > 1))
  335. {
  336. handleMaxRateType(zerlegt[0], zerlegt[1]);
  337. }
  338. if ((toUpper(_param) == "PREVALUEUSE") && (zerlegt.size() > 1))
  339. {
  340. if (toUpper(zerlegt[1]) == "TRUE")
  341. {
  342. PreValueUse = true;
  343. }
  344. }
  345. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  346. {
  347. if (toUpper(zerlegt[1]) == "TRUE")
  348. for (_n = 0; _n < NUMBERS.size(); ++_n)
  349. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  350. }
  351. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  352. {
  353. if (toUpper(zerlegt[1]) == "TRUE")
  354. for (_n = 0; _n < NUMBERS.size(); ++_n)
  355. NUMBERS[_n]->AllowNegativeRates = true;
  356. }
  357. if ((toUpper(_param) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  358. {
  359. if (toUpper(zerlegt[1]) == "TRUE")
  360. ErrorMessage = true;
  361. }
  362. if ((toUpper(_param) == "IGNORELEADINGNAN") && (zerlegt.size() > 1))
  363. {
  364. if (toUpper(zerlegt[1]) == "TRUE")
  365. IgnoreLeadingNaN = true;
  366. }
  367. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  368. {
  369. PreValueAgeStartup = std::stoi(zerlegt[1]);
  370. }
  371. }
  372. if (PreValueUse) {
  373. LoadPreValue();
  374. }
  375. return true;
  376. }
  377. void ClassFlowPostProcessing::InitNUMBERS()
  378. {
  379. int anzDIGIT = 0;
  380. int anzANALOG = 0;
  381. std::vector<std::string> name_numbers;
  382. if (flowDigit)
  383. {
  384. anzDIGIT = flowDigit->getAnzahlGENERAL();
  385. flowDigit->UpdateNameNumbers(&name_numbers);
  386. }
  387. if (flowAnalog)
  388. {
  389. anzANALOG = flowAnalog->getAnzahlGENERAL();
  390. flowAnalog->UpdateNameNumbers(&name_numbers);
  391. }
  392. printf("Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d\n", name_numbers.size(), anzDIGIT, anzANALOG);
  393. for (int _num = 0; _num < name_numbers.size(); ++_num)
  394. {
  395. NumberPost *_number = new NumberPost;
  396. _number->name = name_numbers[_num];
  397. _number->digit_roi = NULL;
  398. if (flowDigit)
  399. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  400. if (_number->digit_roi)
  401. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  402. else
  403. _number->AnzahlDigital = 0;
  404. _number->analog_roi = NULL;
  405. if (flowAnalog)
  406. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  407. if (_number->analog_roi)
  408. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  409. else
  410. _number->AnzahlAnalog = 0;
  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->ReturnPreValue = "";
  416. _number->PreValueOkay = false;
  417. _number->AllowNegativeRates = false;
  418. _number->MaxRateValue = 0.1;
  419. _number->RateType = AbsoluteChange;
  420. _number->useMaxRateValue = false;
  421. _number->checkDigitIncreaseConsistency = false;
  422. _number->DecimalShift = 0;
  423. _number->DecimalShiftInitial = 0;
  424. _number->isExtendedResolution = false;
  425. _number->FlowRateAct = 0; // m3 / min
  426. _number->PreValue = 0; // letzter Wert, der gut ausgelesen wurde
  427. _number->Value = 0; // letzer ausgelesener Wert, inkl. Korrekturen
  428. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  429. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  430. _number->ReturnValueNoError = ""; // korrigierter Rückgabewert ohne Fehlermeldung
  431. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  432. _number->Nachkomma = _number->AnzahlAnalog;
  433. NUMBERS.push_back(_number);
  434. }
  435. for (int i = 0; i < NUMBERS.size(); ++i)
  436. printf("Number %s, Anz DIG: %d, Anz ANA %d\n", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  437. }
  438. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  439. if (_decShift == 0){
  440. return in;
  441. }
  442. int _pos_dec_org, _pos_dec_neu;
  443. _pos_dec_org = findDelimiterPos(in, ".");
  444. if (_pos_dec_org == std::string::npos) {
  445. _pos_dec_org = in.length();
  446. }
  447. else
  448. {
  449. in = in.erase(_pos_dec_org, 1);
  450. }
  451. _pos_dec_neu = _pos_dec_org + _decShift;
  452. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  453. for (int i = 0; i > _pos_dec_neu; --i){
  454. in = in.insert(0, "0");
  455. }
  456. in = "0." + in;
  457. return in;
  458. }
  459. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  460. for (int i = in.length(); i < _pos_dec_neu; ++i){
  461. in = in.insert(in.length(), "0");
  462. }
  463. return in;
  464. }
  465. string zw;
  466. zw = in.substr(0, _pos_dec_neu);
  467. zw = zw + ".";
  468. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  469. return zw;
  470. }
  471. bool ClassFlowPostProcessing::doFlow(string zwtime)
  472. {
  473. string result = "";
  474. string digit = "";
  475. string analog = "";
  476. string zwvalue;
  477. string zw;
  478. time_t imagetime = 0;
  479. string rohwert;
  480. // Update Nachkomma, da sich beim Wechsel von CNNType Auto --> xyz auch die Nachkommastellen ändern können:
  481. imagetime = flowMakeImage->getTimeImageTaken();
  482. if (imagetime == 0)
  483. time(&imagetime);
  484. struct tm* timeinfo;
  485. timeinfo = localtime(&imagetime);
  486. char strftime_buf[64];
  487. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  488. zwtime = std::string(strftime_buf);
  489. printf("Anzahl NUMBERS: %d\n", NUMBERS.size());
  490. for (int j = 0; j < NUMBERS.size(); ++j)
  491. {
  492. NUMBERS[j]->ReturnRawValue = "";
  493. NUMBERS[j]->ErrorMessageText = "";
  494. UpdateNachkommaDecimalShift();
  495. if (NUMBERS[j]->digit_roi)
  496. {
  497. if (NUMBERS[j]->analog_roi)
  498. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false);
  499. else
  500. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution); // Extended Resolution nur falls es keine analogen Ziffern gibt
  501. }
  502. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  503. NUMBERS[j]->ReturnRawValue = NUMBERS[j]->ReturnRawValue + ".";
  504. if (NUMBERS[j]->analog_roi)
  505. NUMBERS[j]->ReturnRawValue = NUMBERS[j]->ReturnRawValue + flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  506. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  507. if (IgnoreLeadingNaN)
  508. {
  509. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  510. {
  511. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  512. }
  513. }
  514. rohwert = NUMBERS[j]->ReturnRawValue;
  515. if (!PreValueUse || !NUMBERS[j]->PreValueOkay)
  516. {
  517. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  518. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnRawValue;
  519. if ((findDelimiterPos(NUMBERS[j]->ReturnValue, "N") == std::string::npos) && (NUMBERS[j]->ReturnValue.length() > 0))
  520. {
  521. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  522. {
  523. NUMBERS[j]->ReturnValue.erase(0, 1);
  524. }
  525. NUMBERS[j]->Value = std::stof(NUMBERS[j]->ReturnValue);
  526. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnValue;
  527. NUMBERS[j]->PreValueOkay = true;
  528. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  529. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  530. NUMBERS[j]->lastvalue = flowMakeImage->getTimeImageTaken();
  531. zwtime = ConvertTimeToString(NUMBERS[j]->lastvalue, PREVALUE_TIME_FORMAT_OUTPUT);
  532. UpdatePreValueINI = true;
  533. SavePreValue();
  534. }
  535. else
  536. {
  537. NUMBERS[j]->ReturnValue = "";
  538. NUMBERS[j]->ReturnValueNoError = "";
  539. }
  540. }
  541. else
  542. {
  543. zw = ErsetzteN(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->PreValue);
  544. NUMBERS[j]->Value = std::stof(zw);
  545. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  546. {
  547. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  548. }
  549. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  550. if ((!NUMBERS[j]->AllowNegativeRates) && (NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  551. {
  552. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  553. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  554. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  555. }
  556. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  557. difference /= 60;
  558. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  559. NUMBERS[j]->ReturnRateValue = std::to_string(NUMBERS[j]->FlowRateAct);
  560. float _ratedifference;
  561. if (NUMBERS[j]->RateType == RateChange)
  562. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  563. else
  564. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  565. if (NUMBERS[j]->useMaxRateValue && (abs(_ratedifference) > NUMBERS[j]->MaxRateValue))
  566. {
  567. 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);
  568. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  569. zwvalue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  570. }
  571. NUMBERS[j]->ReturnValueNoError = zwvalue;
  572. NUMBERS[j]->ReturnValue = zwvalue;
  573. if (NUMBERS[j]->ErrorMessage && (NUMBERS[j]->ErrorMessageText.length() > 0))
  574. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnValue + "\t" + NUMBERS[j]->ErrorMessageText;
  575. if (NUMBERS[j]->ErrorMessageText.length() == 0)
  576. {
  577. NUMBERS[j]->lastvalue = imagetime;
  578. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  579. NUMBERS[j]->ReturnValueNoError = NUMBERS[j]->ReturnValue;
  580. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  581. NUMBERS[j]->ErrorMessageText = "no error";
  582. UpdatePreValueINI = true;
  583. }
  584. else
  585. {
  586. NUMBERS[j]->ReturnRateValue = "";
  587. NUMBERS[j]->ReturnValue = "";
  588. NUMBERS[j]->ReturnValueNoError = "";
  589. NUMBERS[j]->timeStamp = "";
  590. }
  591. }
  592. string _zw = "PostProcessing - Raw: " + NUMBERS[j]->ReturnRawValue + " Value: " + NUMBERS[j]->ReturnValue + " Error: " + NUMBERS[j]->ErrorMessageText;
  593. LogFile.WriteToFile(_zw);
  594. }
  595. SavePreValue();
  596. return true;
  597. }
  598. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  599. {
  600. for (int j = 0; j < NUMBERS.size(); ++j)
  601. {
  602. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  603. {
  604. // printf("Nurdigital\n");
  605. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  606. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  607. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  608. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  609. }
  610. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  611. {
  612. // printf("Nur analog\n");
  613. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  614. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  615. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  616. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  617. }
  618. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  619. {
  620. // printf("Nur digital + analog\n");
  621. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  622. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  623. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  624. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  625. }
  626. printf("UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i\n", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  627. }
  628. }
  629. string ClassFlowPostProcessing::getReadout(int _number)
  630. {
  631. return NUMBERS[_number]->ReturnValue;
  632. }
  633. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  634. {
  635. if (_rawValue)
  636. return NUMBERS[_number]->ReturnRawValue;
  637. if (_noerror)
  638. return NUMBERS[_number]->ReturnValueNoError;
  639. return NUMBERS[_number]->ReturnValue;
  640. }
  641. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  642. std::stringstream stream;
  643. int _zw = _in;
  644. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  645. if (_anzNachkomma < 0) {
  646. _anzNachkomma = 0;
  647. }
  648. if (_anzNachkomma > 0)
  649. {
  650. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  651. return stream.str();
  652. }
  653. else
  654. {
  655. stream << _zw;
  656. }
  657. return stream.str();
  658. }
  659. string ClassFlowPostProcessing::ErsetzteN(string input, float _prevalue)
  660. {
  661. int posN, posPunkt;
  662. int pot, ziffer;
  663. float zw;
  664. posN = findDelimiterPos(input, "N");
  665. posPunkt = findDelimiterPos(input, ".");
  666. if (posPunkt == std::string::npos){
  667. posPunkt = input.length();
  668. }
  669. while (posN != std::string::npos)
  670. {
  671. if (posN < posPunkt) {
  672. pot = posPunkt - posN - 1;
  673. }
  674. else {
  675. pot = posPunkt - posN;
  676. }
  677. zw =_prevalue / pow(10, pot);
  678. ziffer = ((int) zw) % 10;
  679. input[posN] = ziffer + 48;
  680. posN = findDelimiterPos(input, "N");
  681. }
  682. return input;
  683. }
  684. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog, float _preValue){
  685. int aktdigit, olddigit;
  686. int aktdigit_before, olddigit_before;
  687. int pot, pot_max;
  688. float zw;
  689. bool no_nulldurchgang = false;
  690. pot = _decilamshift;
  691. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  692. {
  693. pot++;
  694. }
  695. pot_max = ((int) log10(input)) + 1;
  696. while (pot <= pot_max)
  697. {
  698. zw = input / pow(10, pot-1);
  699. aktdigit_before = ((int) round(zw) + 10) % 10;
  700. zw = _preValue / pow(10, pot-1);
  701. olddigit_before = ((int) round(zw) + 10) % 10;
  702. zw = input / pow(10, pot);
  703. aktdigit = ((int) round(zw) + 10) % 10;
  704. zw = _preValue / pow(10, pot);
  705. olddigit = ((int) round(zw) + 10) % 10;
  706. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  707. if (no_nulldurchgang)
  708. {
  709. if (aktdigit != olddigit)
  710. {
  711. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  712. }
  713. }
  714. else
  715. {
  716. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  717. {
  718. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  719. }
  720. }
  721. pot++;
  722. }
  723. return input;
  724. }
  725. string ClassFlowPostProcessing::getReadoutRate(int _number)
  726. {
  727. return std::to_string(NUMBERS[_number]->FlowRateAct);
  728. }
  729. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  730. {
  731. return NUMBERS[_number]->timeStamp;
  732. }
  733. string ClassFlowPostProcessing::getReadoutError(int _number)
  734. {
  735. return NUMBERS[_number]->ErrorMessageText;
  736. }