ClassFlowPostProcessing.cpp 32 KB

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