ClassFlowPostProcessing.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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::handleAnalogDigitalTransitionStart(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. float _zwdc = 9.2;
  296. {
  297. _zwdc = stof(_value);
  298. }
  299. if (_digit == "default" || NUMBERS[j]->name == _digit) // erstmal auf default setzen (falls sonst nichts gesetzt)
  300. {
  301. NUMBERS[j]->AnalogDigitalTransitionStart = _zwdc;
  302. }
  303. }
  304. }
  305. void ClassFlowPostProcessing::handleMaxRateType(string _decsep, string _value)
  306. {
  307. string _digit, _decpos;
  308. int _pospunkt = _decsep.find_first_of(".");
  309. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  310. if (_pospunkt > -1)
  311. _digit = _decsep.substr(0, _pospunkt);
  312. else
  313. _digit = "default";
  314. for (int j = 0; j < NUMBERS.size(); ++j)
  315. {
  316. t_RateType _rt = AbsoluteChange;
  317. if (toUpper(_value) == "RATECHANGE")
  318. _rt = RateChange;
  319. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  320. {
  321. NUMBERS[j]->RateType = _rt;
  322. }
  323. if (NUMBERS[j]->name == _digit)
  324. {
  325. NUMBERS[j]->RateType = _rt;
  326. }
  327. }
  328. }
  329. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  330. {
  331. string _digit, _decpos;
  332. int _pospunkt = _decsep.find_first_of(".");
  333. // printf("Name: %s, Pospunkt: %d\n", _decsep.c_str(), _pospunkt);
  334. if (_pospunkt > -1)
  335. _digit = _decsep.substr(0, _pospunkt);
  336. else
  337. _digit = "default";
  338. for (int j = 0; j < NUMBERS.size(); ++j)
  339. {
  340. float _zwdc = 1;
  341. // try
  342. {
  343. _zwdc = stof(_value);
  344. }
  345. /* catch(const std::exception& e)
  346. {
  347. printf("ERROR - MaxRateValue is not a number: %s\n", _value.c_str());
  348. }
  349. */
  350. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  351. {
  352. NUMBERS[j]->useMaxRateValue = true;
  353. NUMBERS[j]->MaxRateValue = _zwdc;
  354. }
  355. if (NUMBERS[j]->name == _digit)
  356. {
  357. NUMBERS[j]->useMaxRateValue = true;
  358. NUMBERS[j]->MaxRateValue = _zwdc;
  359. }
  360. }
  361. }
  362. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  363. {
  364. std::vector<string> zerlegt;
  365. int _n;
  366. aktparamgraph = trim(aktparamgraph);
  367. if (aktparamgraph.size() == 0)
  368. if (!this->GetNextParagraph(pfile, aktparamgraph))
  369. return false;
  370. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  371. return false;
  372. InitNUMBERS();
  373. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  374. {
  375. zerlegt = this->ZerlegeZeile(aktparamgraph);
  376. std::string _param = GetParameterName(zerlegt[0]);
  377. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (zerlegt.size() > 1))
  378. {
  379. handleDecimalExtendedResolution(zerlegt[0], zerlegt[1]);
  380. }
  381. if ((toUpper(_param) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  382. {
  383. handleDecimalSeparator(zerlegt[0], zerlegt[1]);
  384. }
  385. if ((toUpper(_param) == "ANALOG_DIGITAL_TRANSITION_START") && (zerlegt.size() > 1))
  386. {
  387. handleAnalogDigitalTransitionStart(zerlegt[0], zerlegt[1]);
  388. }
  389. if ((toUpper(_param) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  390. {
  391. handleMaxRateValue(zerlegt[0], zerlegt[1]);
  392. }
  393. if ((toUpper(_param) == "MAXRATETYPE") && (zerlegt.size() > 1))
  394. {
  395. handleMaxRateType(zerlegt[0], zerlegt[1]);
  396. }
  397. if ((toUpper(_param) == "PREVALUEUSE") && (zerlegt.size() > 1))
  398. {
  399. if (toUpper(zerlegt[1]) == "TRUE")
  400. {
  401. PreValueUse = true;
  402. }
  403. }
  404. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  405. {
  406. if (toUpper(zerlegt[1]) == "TRUE")
  407. for (_n = 0; _n < NUMBERS.size(); ++_n)
  408. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  409. }
  410. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  411. {
  412. if (toUpper(zerlegt[1]) == "TRUE")
  413. for (_n = 0; _n < NUMBERS.size(); ++_n)
  414. NUMBERS[_n]->AllowNegativeRates = true;
  415. }
  416. if ((toUpper(_param) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  417. {
  418. if (toUpper(zerlegt[1]) == "TRUE")
  419. ErrorMessage = true;
  420. }
  421. if ((toUpper(_param) == "IGNORELEADINGNAN") && (zerlegt.size() > 1))
  422. {
  423. if (toUpper(zerlegt[1]) == "TRUE")
  424. IgnoreLeadingNaN = true;
  425. }
  426. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  427. {
  428. PreValueAgeStartup = std::stoi(zerlegt[1]);
  429. }
  430. }
  431. if (PreValueUse) {
  432. LoadPreValue();
  433. }
  434. return true;
  435. }
  436. void ClassFlowPostProcessing::InitNUMBERS()
  437. {
  438. int anzDIGIT = 0;
  439. int anzANALOG = 0;
  440. std::vector<std::string> name_numbers;
  441. if (flowDigit)
  442. {
  443. anzDIGIT = flowDigit->getAnzahlGENERAL();
  444. flowDigit->UpdateNameNumbers(&name_numbers);
  445. }
  446. if (flowAnalog)
  447. {
  448. anzANALOG = flowAnalog->getAnzahlGENERAL();
  449. flowAnalog->UpdateNameNumbers(&name_numbers);
  450. }
  451. printf("Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d\n", name_numbers.size(), anzDIGIT, anzANALOG);
  452. for (int _num = 0; _num < name_numbers.size(); ++_num)
  453. {
  454. NumberPost *_number = new NumberPost;
  455. _number->name = name_numbers[_num];
  456. _number->digit_roi = NULL;
  457. if (flowDigit)
  458. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  459. if (_number->digit_roi)
  460. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  461. else
  462. _number->AnzahlDigital = 0;
  463. _number->analog_roi = NULL;
  464. if (flowAnalog)
  465. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  466. if (_number->analog_roi)
  467. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  468. else
  469. _number->AnzahlAnalog = 0;
  470. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  471. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  472. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  473. _number->ReturnPreValue = "";
  474. _number->PreValueOkay = false;
  475. _number->AllowNegativeRates = false;
  476. _number->MaxRateValue = 0.1;
  477. _number->RateType = AbsoluteChange;
  478. _number->useMaxRateValue = false;
  479. _number->checkDigitIncreaseConsistency = false;
  480. _number->DecimalShift = 0;
  481. _number->DecimalShiftInitial = 0;
  482. _number->isExtendedResolution = false;
  483. _number->FlowRateAct = 0; // m3 / min
  484. _number->PreValue = 0; // letzter Wert, der gut ausgelesen wurde
  485. _number->Value = 0; // letzer ausgelesener Wert, inkl. Korrekturen
  486. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  487. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  488. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  489. _number->Nachkomma = _number->AnzahlAnalog;
  490. NUMBERS.push_back(_number);
  491. }
  492. for (int i = 0; i < NUMBERS.size(); ++i) {
  493. printf("Number %s, Anz DIG: %d, Anz ANA %d\n", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  494. }
  495. }
  496. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  497. if (_decShift == 0){
  498. return in;
  499. }
  500. int _pos_dec_org, _pos_dec_neu;
  501. _pos_dec_org = findDelimiterPos(in, ".");
  502. if (_pos_dec_org == std::string::npos) {
  503. _pos_dec_org = in.length();
  504. }
  505. else
  506. {
  507. in = in.erase(_pos_dec_org, 1);
  508. }
  509. _pos_dec_neu = _pos_dec_org + _decShift;
  510. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  511. for (int i = 0; i > _pos_dec_neu; --i){
  512. in = in.insert(0, "0");
  513. }
  514. in = "0." + in;
  515. return in;
  516. }
  517. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  518. for (int i = in.length(); i < _pos_dec_neu; ++i){
  519. in = in.insert(in.length(), "0");
  520. }
  521. return in;
  522. }
  523. string zw;
  524. zw = in.substr(0, _pos_dec_neu);
  525. zw = zw + ".";
  526. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  527. return zw;
  528. }
  529. bool ClassFlowPostProcessing::doFlow(string zwtime)
  530. {
  531. string result = "";
  532. string digit = "";
  533. string analog = "";
  534. string zwvalue;
  535. string zw;
  536. time_t imagetime = 0;
  537. string rohwert;
  538. // Update Nachkomma, da sich beim Wechsel von CNNType Auto --> xyz auch die Nachkommastellen ändern können:
  539. imagetime = flowMakeImage->getTimeImageTaken();
  540. if (imagetime == 0)
  541. time(&imagetime);
  542. struct tm* timeinfo;
  543. timeinfo = localtime(&imagetime);
  544. char strftime_buf[64];
  545. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  546. zwtime = std::string(strftime_buf);
  547. printf("Anzahl NUMBERS: %d\n", NUMBERS.size());
  548. for (int j = 0; j < NUMBERS.size(); ++j)
  549. {
  550. NUMBERS[j]->ReturnRawValue = "";
  551. NUMBERS[j]->ReturnRateValue = "";
  552. NUMBERS[j]->ReturnValue = "";
  553. NUMBERS[j]->ErrorMessageText = "";
  554. NUMBERS[j]->Value = -1;
  555. UpdateNachkommaDecimalShift();
  556. int previous_value = -1;
  557. if (NUMBERS[j]->analog_roi)
  558. {
  559. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  560. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  561. {
  562. char zw = NUMBERS[j]->ReturnRawValue[0];
  563. if (zw >= 48 && zw <=57)
  564. previous_value = zw - 48;
  565. }
  566. }
  567. #ifdef SERIAL_DEBUG
  568. printf("After analog->getReadout: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  569. #endif
  570. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  571. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  572. if (NUMBERS[j]->digit_roi)
  573. {
  574. if (NUMBERS[j]->analog_roi)
  575. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  576. else
  577. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution nur falls es keine analogen Ziffern gibt
  578. }
  579. #ifdef SERIAL_DEBUG
  580. printf("After digital->getReadout: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  581. #endif
  582. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  583. #ifdef SERIAL_DEBUG
  584. printf("After ShiftDecimal: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  585. #endif
  586. if (IgnoreLeadingNaN)
  587. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  588. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  589. #ifdef SERIAL_DEBUG
  590. printf("After IgnoreLeadingNaN: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  591. #endif
  592. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  593. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  594. {
  595. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  596. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  597. else
  598. continue; // es gibt keinen Zahl, da noch ein N vorhanden ist.
  599. }
  600. #ifdef SERIAL_DEBUG
  601. printf("After findDelimiterPos: ReturnValue %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  602. #endif
  603. // Lösche führende Nullen (außer es ist nur noch einen 0)
  604. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  605. NUMBERS[j]->ReturnValue.erase(0, 1);
  606. #ifdef SERIAL_DEBUG
  607. printf("After removeLeadingZeros: ReturnValue %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  608. #endif
  609. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  610. #ifdef SERIAL_DEBUG
  611. printf("After setting the Value: Value %f and as double is %f\n", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  612. #endif
  613. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  614. {
  615. if (flowDigit)
  616. {
  617. if (flowDigit->getCNNType() != Digital)
  618. printf("checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)\n");
  619. else
  620. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  621. }
  622. else
  623. {
  624. #ifdef SERIAL_DEBUG
  625. printf("checkDigitIncreaseConsistency = true - no digital numbers defined!\n");
  626. #endif
  627. }
  628. }
  629. #ifdef SERIAL_DEBUG
  630. printf("After checkDigitIncreaseConsistency: Value %f\n", NUMBERS[j]->Value);
  631. #endif
  632. if (!NUMBERS[j]->AllowNegativeRates)
  633. {
  634. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  635. {
  636. #ifdef SERIAL_DEBUG
  637. printf("Neg: value=%f, preValue=%f, preToll%f\n", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  638. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  639. ) ;
  640. #endif
  641. // Bei isExtendedResolution Ungenauigkeit von 0.2 mit einrechnen.
  642. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  643. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  644. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  645. } else {
  646. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  647. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  648. NUMBERS[j]->ReturnValue = "";
  649. continue;
  650. }
  651. }
  652. }
  653. #ifdef SERIAL_DEBUG
  654. printf("After AllowNegativeRates: Value %f\n", NUMBERS[j]->Value);
  655. #endif
  656. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  657. difference /= 60;
  658. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  659. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  660. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  661. {
  662. double _ratedifference;
  663. if (NUMBERS[j]->RateType == RateChange)
  664. _ratedifference = NUMBERS[j]->FlowRateAct;
  665. else
  666. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  667. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  668. {
  669. 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);
  670. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  671. NUMBERS[j]->ReturnValue = "";
  672. NUMBERS[j]->ReturnRateValue = "";
  673. continue;
  674. }
  675. }
  676. #ifdef SERIAL_DEBUG
  677. printf("After MaxRateCheck: Value %f\n", NUMBERS[j]->Value);
  678. #endif
  679. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  680. NUMBERS[j]->lastvalue = imagetime;
  681. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  682. NUMBERS[j]->PreValueOkay = true;
  683. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  684. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  685. NUMBERS[j]->ErrorMessageText = "no error";
  686. UpdatePreValueINI = true;
  687. string _zw = "PostProcessing - Raw: " + NUMBERS[j]->ReturnRawValue + " Value: " + NUMBERS[j]->ReturnValue + " Error: " + NUMBERS[j]->ErrorMessageText;
  688. LogFile.WriteToFile(_zw);
  689. }
  690. SavePreValue();
  691. return true;
  692. }
  693. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  694. {
  695. for (int j = 0; j < NUMBERS.size(); ++j)
  696. {
  697. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  698. {
  699. // printf("Nurdigital\n");
  700. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  701. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  702. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  703. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  704. }
  705. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  706. {
  707. // printf("Nur analog\n");
  708. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  709. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  710. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  711. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  712. }
  713. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  714. {
  715. // printf("Nur digital + analog\n");
  716. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  717. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  718. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  719. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  720. }
  721. printf("UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i\n", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  722. }
  723. }
  724. string ClassFlowPostProcessing::getReadout(int _number)
  725. {
  726. return NUMBERS[_number]->ReturnValue;
  727. }
  728. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  729. {
  730. if (_rawValue)
  731. return NUMBERS[_number]->ReturnRawValue;
  732. if (_noerror)
  733. return NUMBERS[_number]->ReturnValue;
  734. return NUMBERS[_number]->ReturnValue;
  735. }
  736. string ClassFlowPostProcessing::RundeOutput(double _in, int _anzNachkomma){
  737. std::stringstream stream;
  738. int _zw = _in;
  739. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  740. if (_anzNachkomma < 0) {
  741. _anzNachkomma = 0;
  742. }
  743. if (_anzNachkomma > 0)
  744. {
  745. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  746. return stream.str();
  747. }
  748. else
  749. {
  750. stream << _zw;
  751. }
  752. return stream.str();
  753. }
  754. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  755. {
  756. int posN, posPunkt;
  757. int pot, ziffer;
  758. float zw;
  759. posN = findDelimiterPos(input, "N");
  760. posPunkt = findDelimiterPos(input, ".");
  761. if (posPunkt == std::string::npos){
  762. posPunkt = input.length();
  763. }
  764. while (posN != std::string::npos)
  765. {
  766. if (posN < posPunkt) {
  767. pot = posPunkt - posN - 1;
  768. }
  769. else {
  770. pot = posPunkt - posN;
  771. }
  772. zw =_prevalue / pow(10, pot);
  773. ziffer = ((int) zw) % 10;
  774. input[posN] = ziffer + 48;
  775. posN = findDelimiterPos(input, "N");
  776. }
  777. return input;
  778. }
  779. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  780. int aktdigit, olddigit;
  781. int aktdigit_before, olddigit_before;
  782. int pot, pot_max;
  783. float zw;
  784. bool no_nulldurchgang = false;
  785. pot = _decilamshift;
  786. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  787. {
  788. pot++;
  789. }
  790. #ifdef SERIAL_DEBUG
  791. printf("checkDigitConsistency: pot=%d, decimalshift=%d\n", pot, _decilamshift);
  792. #endif
  793. pot_max = ((int) log10(input)) + 1;
  794. while (pot <= pot_max)
  795. {
  796. zw = input / pow(10, pot-1);
  797. aktdigit_before = ((int) zw) % 10;
  798. zw = _preValue / pow(10, pot-1);
  799. olddigit_before = ((int) zw) % 10;
  800. zw = input / pow(10, pot);
  801. aktdigit = ((int) zw) % 10;
  802. zw = _preValue / pow(10, pot);
  803. olddigit = ((int) zw) % 10;
  804. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  805. if (no_nulldurchgang)
  806. {
  807. if (aktdigit != olddigit)
  808. {
  809. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  810. }
  811. }
  812. else
  813. {
  814. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  815. {
  816. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  817. }
  818. }
  819. #ifdef SERIAL_DEBUG
  820. printf("checkDigitConsistency: input=%f", input);
  821. #endif
  822. pot++;
  823. }
  824. return input;
  825. }
  826. string ClassFlowPostProcessing::getReadoutRate(int _number)
  827. {
  828. return std::to_string(NUMBERS[_number]->FlowRateAct);
  829. }
  830. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  831. {
  832. return NUMBERS[_number]->timeStamp;
  833. }
  834. string ClassFlowPostProcessing::getReadoutError(int _number)
  835. {
  836. return NUMBERS[_number]->ErrorMessageText;
  837. }