ClassFlowPostProcessing.cpp 29 KB

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