ClassFlowPostProcessing.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. NUMBERS[j]->ReturnPreValue = std::to_string(zw);
  63. NUMBERS[j]->PreValueOkay = true;
  64. if (_extern)
  65. {
  66. time(&(NUMBERS[j]->lastvalue));
  67. localtime(&(NUMBERS[j]->lastvalue));
  68. }
  69. // printf("Found %d! - set to %f\n", j, NUMBERS[j]->PreValue);
  70. }
  71. }
  72. UpdatePreValueINI = true;
  73. SavePreValue();
  74. }
  75. bool ClassFlowPostProcessing::LoadPreValue(void)
  76. {
  77. std::vector<string> zerlegt;
  78. FILE* pFile;
  79. char zw[1024];
  80. string zwtime, zwvalue, name;
  81. bool _done = false;
  82. UpdatePreValueINI = false; // Konvertierung ins neue Format
  83. pFile = fopen(FilePreValue.c_str(), "r");
  84. if (pFile == NULL)
  85. return false;
  86. fgets(zw, 1024, pFile);
  87. printf("Read Zeile Prevalue.ini: %s", zw);
  88. zwtime = trim(std::string(zw));
  89. if (zwtime.length() == 0)
  90. return false;
  91. zerlegt = HelperZerlegeZeile(zwtime, "\t");
  92. if (zerlegt.size() > 1) // neues Format
  93. {
  94. while ((zerlegt.size() > 1) && !_done)
  95. {
  96. name = trim(zerlegt[0]);
  97. zwtime = trim(zerlegt[1]);
  98. zwvalue = trim(zerlegt[2]);
  99. for (int j = 0; j < NUMBERS.size(); ++j)
  100. {
  101. if (NUMBERS[j]->name == name)
  102. {
  103. NUMBERS[j]->PreValue = stof(zwvalue.c_str());
  104. 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)
  105. time_t tStart;
  106. int yy, month, dd, hh, mm, ss;
  107. struct tm whenStart;
  108. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  109. whenStart.tm_year = yy - 1900;
  110. whenStart.tm_mon = month - 1;
  111. whenStart.tm_mday = dd;
  112. whenStart.tm_hour = hh;
  113. whenStart.tm_min = mm;
  114. whenStart.tm_sec = ss;
  115. whenStart.tm_isdst = -1;
  116. NUMBERS[j]->lastvalue = mktime(&whenStart);
  117. time(&tStart);
  118. localtime(&tStart);
  119. double difference = difftime(tStart, NUMBERS[j]->lastvalue);
  120. difference /= 60;
  121. if (difference > PreValueAgeStartup)
  122. NUMBERS[j]->PreValueOkay = false;
  123. else
  124. NUMBERS[j]->PreValueOkay = true;
  125. }
  126. }
  127. if (!fgets(zw, 1024, pFile))
  128. _done = true;
  129. else
  130. {
  131. printf("Read Zeile Prevalue.ini: %s", zw);
  132. zerlegt = HelperZerlegeZeile(trim(std::string(zw)), "\t");
  133. if (zerlegt.size() > 1)
  134. {
  135. name = trim(zerlegt[0]);
  136. zwtime = trim(zerlegt[1]);
  137. zwvalue = trim(zerlegt[2]);
  138. }
  139. }
  140. }
  141. fclose(pFile);
  142. }
  143. else // altes Format
  144. {
  145. fgets(zw, 1024, pFile);
  146. fclose(pFile);
  147. printf("%s", zw);
  148. zwvalue = trim(std::string(zw));
  149. NUMBERS[0]->PreValue = stof(zwvalue.c_str());
  150. time_t tStart;
  151. int yy, month, dd, hh, mm, ss;
  152. struct tm whenStart;
  153. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  154. whenStart.tm_year = yy - 1900;
  155. whenStart.tm_mon = month - 1;
  156. whenStart.tm_mday = dd;
  157. whenStart.tm_hour = hh;
  158. whenStart.tm_min = mm;
  159. whenStart.tm_sec = ss;
  160. whenStart.tm_isdst = -1;
  161. 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);
  162. NUMBERS[0]->lastvalue = mktime(&whenStart);
  163. time(&tStart);
  164. localtime(&tStart);
  165. double difference = difftime(tStart, NUMBERS[0]->lastvalue);
  166. difference /= 60;
  167. if (difference > PreValueAgeStartup)
  168. return false;
  169. NUMBERS[0]->Value = NUMBERS[0]->PreValue;
  170. NUMBERS[0]->ReturnValue = to_string(NUMBERS[0]->Value);
  171. if (NUMBERS[0]->digit_roi || NUMBERS[0]->analog_roi)
  172. {
  173. NUMBERS[0]->ReturnValue = RundeOutput(NUMBERS[0]->Value, NUMBERS[0]->Nachkomma);
  174. }
  175. UpdatePreValueINI = true; // Konvertierung ins neue Format
  176. SavePreValue();
  177. }
  178. return true;
  179. }
  180. void ClassFlowPostProcessing::SavePreValue()
  181. {
  182. FILE* pFile;
  183. string _zw;
  184. if (!UpdatePreValueINI) // PreValues unverändert --> File muss nicht neu geschrieben werden
  185. return;
  186. pFile = fopen(FilePreValue.c_str(), "w");
  187. for (int j = 0; j < NUMBERS.size(); ++j)
  188. {
  189. char buffer[80];
  190. struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue);
  191. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  192. NUMBERS[j]->timeStamp = std::string(buffer);
  193. // printf("SaverPreValue %d, Value: %f, Nachkomma %d\n", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  194. _zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n";
  195. printf("Write PreValue Zeile: %s\n", _zw.c_str());
  196. fputs(_zw.c_str(), pFile);
  197. }
  198. UpdatePreValueINI = false;
  199. fclose(pFile);
  200. }
  201. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc, ClassFlowCNNGeneral *_analog, ClassFlowCNNGeneral *_digit)
  202. {
  203. PreValueUse = false;
  204. PreValueAgeStartup = 30;
  205. ErrorMessage = false;
  206. ListFlowControll = NULL;
  207. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  208. ListFlowControll = lfc;
  209. flowMakeImage = NULL;
  210. UpdatePreValueINI = false;
  211. IgnoreLeadingNaN = false;
  212. flowAnalog = _analog;
  213. flowDigit = _digit;
  214. for (int i = 0; i < ListFlowControll->size(); ++i)
  215. {
  216. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  217. {
  218. flowMakeImage = (ClassFlowMakeImage*) (*ListFlowControll)[i];
  219. }
  220. }
  221. }
  222. void ClassFlowPostProcessing::handleDecimalExtendedResolution(string _decsep, string _value)
  223. {
  224. string _digit, _decpos;
  225. int _pospunkt = _decsep.find_first_of(".");
  226. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  227. if (_pospunkt > -1)
  228. _digit = _decsep.substr(0, _pospunkt);
  229. else
  230. _digit = "default";
  231. for (int j = 0; j < NUMBERS.size(); ++j)
  232. {
  233. bool _zwdc = false;
  234. if (toUpper(_value) == "TRUE")
  235. _zwdc = true;
  236. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  237. {
  238. NUMBERS[j]->isExtendedResolution = _zwdc;
  239. }
  240. if (NUMBERS[j]->name == _digit)
  241. {
  242. NUMBERS[j]->isExtendedResolution = _zwdc;
  243. }
  244. }
  245. }
  246. void ClassFlowPostProcessing::handleDecimalSeparator(string _decsep, string _value)
  247. {
  248. string _digit, _decpos;
  249. int _pospunkt = _decsep.find_first_of(".");
  250. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  251. if (_pospunkt > -1)
  252. _digit = _decsep.substr(0, _pospunkt);
  253. else
  254. _digit = "default";
  255. for (int j = 0; j < NUMBERS.size(); ++j)
  256. {
  257. int _zwdc = 0;
  258. // try
  259. {
  260. _zwdc = stoi(_value);
  261. }
  262. /* catch(const std::exception& e)
  263. {
  264. printf("ERROR - Decimalshift is not a number: %s\n", _value.c_str());
  265. }
  266. */
  267. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  268. {
  269. NUMBERS[j]->DecimalShift = _zwdc;
  270. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  271. }
  272. if (NUMBERS[j]->name == _digit)
  273. {
  274. NUMBERS[j]->DecimalShift = _zwdc;
  275. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  276. }
  277. NUMBERS[j]->Nachkomma = NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift;
  278. }
  279. }
  280. void ClassFlowPostProcessing::handleMaxRateType(string _decsep, string _value)
  281. {
  282. string _digit, _decpos;
  283. int _pospunkt = _decsep.find_first_of(".");
  284. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  285. if (_pospunkt > -1)
  286. _digit = _decsep.substr(0, _pospunkt);
  287. else
  288. _digit = "default";
  289. for (int j = 0; j < NUMBERS.size(); ++j)
  290. {
  291. t_RateType _rt = AbsoluteChange;
  292. if (toUpper(_value) == "RATECHANGE")
  293. _rt = RateChange;
  294. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  295. {
  296. NUMBERS[j]->RateType = _rt;
  297. }
  298. if (NUMBERS[j]->name == _digit)
  299. {
  300. NUMBERS[j]->RateType = _rt;
  301. }
  302. }
  303. }
  304. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  305. {
  306. string _digit, _decpos;
  307. int _pospunkt = _decsep.find_first_of(".");
  308. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  309. if (_pospunkt > -1)
  310. _digit = _decsep.substr(0, _pospunkt);
  311. else
  312. _digit = "default";
  313. for (int j = 0; j < NUMBERS.size(); ++j)
  314. {
  315. float _zwdc = 1;
  316. // try
  317. {
  318. _zwdc = stof(_value);
  319. }
  320. /* catch(const std::exception& e)
  321. {
  322. printf("ERROR - MaxRateValue is not a number: %s\n", _value.c_str());
  323. }
  324. */
  325. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  326. {
  327. NUMBERS[j]->useMaxRateValue = true;
  328. NUMBERS[j]->MaxRateValue = _zwdc;
  329. }
  330. if (NUMBERS[j]->name == _digit)
  331. {
  332. NUMBERS[j]->useMaxRateValue = true;
  333. NUMBERS[j]->MaxRateValue = _zwdc;
  334. }
  335. }
  336. }
  337. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  338. {
  339. std::vector<string> zerlegt;
  340. int _n;
  341. aktparamgraph = trim(aktparamgraph);
  342. if (aktparamgraph.size() == 0)
  343. if (!this->GetNextParagraph(pfile, aktparamgraph))
  344. return false;
  345. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  346. return false;
  347. InitNUMBERS();
  348. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  349. {
  350. zerlegt = this->ZerlegeZeile(aktparamgraph);
  351. std::string _param = GetParameterName(zerlegt[0]);
  352. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (zerlegt.size() > 1))
  353. {
  354. handleDecimalExtendedResolution(zerlegt[0], zerlegt[1]);
  355. }
  356. if ((toUpper(_param) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  357. {
  358. handleDecimalSeparator(zerlegt[0], zerlegt[1]);
  359. }
  360. if ((toUpper(_param) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  361. {
  362. handleMaxRateValue(zerlegt[0], zerlegt[1]);
  363. }
  364. if ((toUpper(_param) == "MAXRATETYPE") && (zerlegt.size() > 1))
  365. {
  366. handleMaxRateType(zerlegt[0], zerlegt[1]);
  367. }
  368. if ((toUpper(_param) == "PREVALUEUSE") && (zerlegt.size() > 1))
  369. {
  370. if (toUpper(zerlegt[1]) == "TRUE")
  371. {
  372. PreValueUse = true;
  373. }
  374. }
  375. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  376. {
  377. if (toUpper(zerlegt[1]) == "TRUE")
  378. for (_n = 0; _n < NUMBERS.size(); ++_n)
  379. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  380. }
  381. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  382. {
  383. if (toUpper(zerlegt[1]) == "TRUE")
  384. for (_n = 0; _n < NUMBERS.size(); ++_n)
  385. NUMBERS[_n]->AllowNegativeRates = true;
  386. }
  387. if ((toUpper(_param) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  388. {
  389. if (toUpper(zerlegt[1]) == "TRUE")
  390. ErrorMessage = true;
  391. }
  392. if ((toUpper(_param) == "IGNORELEADINGNAN") && (zerlegt.size() > 1))
  393. {
  394. if (toUpper(zerlegt[1]) == "TRUE")
  395. IgnoreLeadingNaN = true;
  396. }
  397. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  398. {
  399. PreValueAgeStartup = std::stoi(zerlegt[1]);
  400. }
  401. }
  402. if (PreValueUse) {
  403. LoadPreValue();
  404. }
  405. return true;
  406. }
  407. void ClassFlowPostProcessing::InitNUMBERS()
  408. {
  409. int anzDIGIT = 0;
  410. int anzANALOG = 0;
  411. std::vector<std::string> name_numbers;
  412. if (flowDigit)
  413. {
  414. anzDIGIT = flowDigit->getAnzahlGENERAL();
  415. flowDigit->UpdateNameNumbers(&name_numbers);
  416. }
  417. if (flowAnalog)
  418. {
  419. anzANALOG = flowAnalog->getAnzahlGENERAL();
  420. flowAnalog->UpdateNameNumbers(&name_numbers);
  421. }
  422. printf("Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d\n", name_numbers.size(), anzDIGIT, anzANALOG);
  423. for (int _num = 0; _num < name_numbers.size(); ++_num)
  424. {
  425. NumberPost *_number = new NumberPost;
  426. _number->name = name_numbers[_num];
  427. _number->digit_roi = NULL;
  428. if (flowDigit)
  429. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  430. if (_number->digit_roi)
  431. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  432. else
  433. _number->AnzahlDigital = 0;
  434. _number->analog_roi = NULL;
  435. if (flowAnalog)
  436. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  437. if (_number->analog_roi)
  438. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  439. else
  440. _number->AnzahlAnalog = 0;
  441. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  442. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  443. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  444. _number->ReturnPreValue = "";
  445. _number->PreValueOkay = false;
  446. _number->AllowNegativeRates = false;
  447. _number->MaxRateValue = 0.1;
  448. _number->RateType = AbsoluteChange;
  449. _number->useMaxRateValue = false;
  450. _number->checkDigitIncreaseConsistency = false;
  451. _number->DecimalShift = 0;
  452. _number->DecimalShiftInitial = 0;
  453. _number->isExtendedResolution = false;
  454. _number->FlowRateAct = 0; // m3 / min
  455. _number->PreValue = 0; // letzter Wert, der gut ausgelesen wurde
  456. _number->Value = 0; // letzer ausgelesener Wert, inkl. Korrekturen
  457. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  458. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit 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]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  603. NUMBERS[j]->ErrorMessageText = "no error";
  604. UpdatePreValueINI = true;
  605. string _zw = "PostProcessing - Raw: " + NUMBERS[j]->ReturnRawValue + " Value: " + NUMBERS[j]->ReturnValue + " Error: " + NUMBERS[j]->ErrorMessageText;
  606. LogFile.WriteToFile(_zw);
  607. }
  608. SavePreValue();
  609. return true;
  610. }
  611. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  612. {
  613. for (int j = 0; j < NUMBERS.size(); ++j)
  614. {
  615. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  616. {
  617. // printf("Nurdigital\n");
  618. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  619. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  620. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  621. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  622. }
  623. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  624. {
  625. // printf("Nur analog\n");
  626. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  627. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  628. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  629. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  630. }
  631. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  632. {
  633. // printf("Nur digital + analog\n");
  634. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  635. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  636. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  637. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  638. }
  639. printf("UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i\n", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  640. }
  641. }
  642. string ClassFlowPostProcessing::getReadout(int _number)
  643. {
  644. return NUMBERS[_number]->ReturnValue;
  645. }
  646. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  647. {
  648. if (_rawValue)
  649. return NUMBERS[_number]->ReturnRawValue;
  650. if (_noerror)
  651. return NUMBERS[_number]->ReturnValue;
  652. return NUMBERS[_number]->ReturnValue;
  653. }
  654. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  655. std::stringstream stream;
  656. int _zw = _in;
  657. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  658. if (_anzNachkomma < 0) {
  659. _anzNachkomma = 0;
  660. }
  661. if (_anzNachkomma > 0)
  662. {
  663. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  664. return stream.str();
  665. }
  666. else
  667. {
  668. stream << _zw;
  669. }
  670. return stream.str();
  671. }
  672. string ClassFlowPostProcessing::ErsetzteN(string input, float _prevalue)
  673. {
  674. int posN, posPunkt;
  675. int pot, ziffer;
  676. float zw;
  677. posN = findDelimiterPos(input, "N");
  678. posPunkt = findDelimiterPos(input, ".");
  679. if (posPunkt == std::string::npos){
  680. posPunkt = input.length();
  681. }
  682. while (posN != std::string::npos)
  683. {
  684. if (posN < posPunkt) {
  685. pot = posPunkt - posN - 1;
  686. }
  687. else {
  688. pot = posPunkt - posN;
  689. }
  690. zw =_prevalue / pow(10, pot);
  691. ziffer = ((int) zw) % 10;
  692. input[posN] = ziffer + 48;
  693. posN = findDelimiterPos(input, "N");
  694. }
  695. return input;
  696. }
  697. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog, float _preValue){
  698. int aktdigit, olddigit;
  699. int aktdigit_before, olddigit_before;
  700. int pot, pot_max;
  701. float zw;
  702. bool no_nulldurchgang = false;
  703. pot = _decilamshift;
  704. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  705. {
  706. pot++;
  707. }
  708. pot_max = ((int) log10(input)) + 1;
  709. while (pot <= pot_max)
  710. {
  711. zw = input / pow(10, pot-1);
  712. aktdigit_before = ((int) zw) % 10;
  713. zw = _preValue / pow(10, pot-1);
  714. olddigit_before = ((int) zw) % 10;
  715. zw = input / pow(10, pot);
  716. aktdigit = ((int) zw) % 10;
  717. zw = _preValue / pow(10, pot);
  718. olddigit = ((int) zw) % 10;
  719. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  720. if (no_nulldurchgang)
  721. {
  722. if (aktdigit != olddigit)
  723. {
  724. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  725. }
  726. }
  727. else
  728. {
  729. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  730. {
  731. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  732. }
  733. }
  734. pot++;
  735. }
  736. return input;
  737. }
  738. string ClassFlowPostProcessing::getReadoutRate(int _number)
  739. {
  740. return std::to_string(NUMBERS[_number]->FlowRateAct);
  741. }
  742. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  743. {
  744. return NUMBERS[_number]->timeStamp;
  745. }
  746. string ClassFlowPostProcessing::getReadoutError(int _number)
  747. {
  748. return NUMBERS[_number]->ErrorMessageText;
  749. }