ClassFlowPostProcessing.cpp 27 KB

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