ClassFlowPostProcessing.cpp 31 KB

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