ClassFlowPostProcessing.cpp 29 KB

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