ClassFlowPostProcessing.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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) == "ANALOGDIGITALTRANSITIONSTART") && (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->AnalogDigitalTransitionStart=9.2;
  484. _number->FlowRateAct = 0; // m3 / min
  485. _number->PreValue = 0; // letzter Wert, der gut ausgelesen wurde
  486. _number->Value = 0; // letzer ausgelesener Wert, inkl. Korrekturen
  487. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  488. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  489. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  490. _number->Nachkomma = _number->AnzahlAnalog;
  491. NUMBERS.push_back(_number);
  492. }
  493. for (int i = 0; i < NUMBERS.size(); ++i) {
  494. printf("Number %s, Anz DIG: %d, Anz ANA %d\n", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  495. }
  496. }
  497. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  498. if (_decShift == 0){
  499. return in;
  500. }
  501. int _pos_dec_org, _pos_dec_neu;
  502. _pos_dec_org = findDelimiterPos(in, ".");
  503. if (_pos_dec_org == std::string::npos) {
  504. _pos_dec_org = in.length();
  505. }
  506. else
  507. {
  508. in = in.erase(_pos_dec_org, 1);
  509. }
  510. _pos_dec_neu = _pos_dec_org + _decShift;
  511. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  512. for (int i = 0; i > _pos_dec_neu; --i){
  513. in = in.insert(0, "0");
  514. }
  515. in = "0." + in;
  516. return in;
  517. }
  518. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  519. for (int i = in.length(); i < _pos_dec_neu; ++i){
  520. in = in.insert(in.length(), "0");
  521. }
  522. return in;
  523. }
  524. string zw;
  525. zw = in.substr(0, _pos_dec_neu);
  526. zw = zw + ".";
  527. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  528. return zw;
  529. }
  530. bool ClassFlowPostProcessing::doFlow(string zwtime)
  531. {
  532. string result = "";
  533. string digit = "";
  534. string analog = "";
  535. string zwvalue;
  536. string zw;
  537. time_t imagetime = 0;
  538. string rohwert;
  539. // Update Nachkomma, da sich beim Wechsel von CNNType Auto --> xyz auch die Nachkommastellen ändern können:
  540. imagetime = flowMakeImage->getTimeImageTaken();
  541. if (imagetime == 0)
  542. time(&imagetime);
  543. struct tm* timeinfo;
  544. timeinfo = localtime(&imagetime);
  545. char strftime_buf[64];
  546. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  547. zwtime = std::string(strftime_buf);
  548. printf("Anzahl NUMBERS: %d\n", NUMBERS.size());
  549. for (int j = 0; j < NUMBERS.size(); ++j)
  550. {
  551. NUMBERS[j]->ReturnRawValue = "";
  552. NUMBERS[j]->ReturnRateValue = "";
  553. NUMBERS[j]->ReturnValue = "";
  554. NUMBERS[j]->ErrorMessageText = "";
  555. NUMBERS[j]->Value = -1;
  556. UpdateNachkommaDecimalShift();
  557. int previous_value = -1;
  558. if (NUMBERS[j]->analog_roi)
  559. {
  560. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  561. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  562. {
  563. char zw = NUMBERS[j]->ReturnRawValue[0];
  564. if (zw >= 48 && zw <=57)
  565. previous_value = zw - 48;
  566. }
  567. }
  568. #ifdef SERIAL_DEBUG
  569. printf("After analog->getReadout: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  570. #endif
  571. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  572. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  573. if (NUMBERS[j]->digit_roi)
  574. {
  575. if (NUMBERS[j]->analog_roi)
  576. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  577. else
  578. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution nur falls es keine analogen Ziffern gibt
  579. }
  580. #ifdef SERIAL_DEBUG
  581. printf("After digital->getReadout: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  582. #endif
  583. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  584. #ifdef SERIAL_DEBUG
  585. printf("After ShiftDecimal: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  586. #endif
  587. if (IgnoreLeadingNaN)
  588. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  589. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  590. #ifdef SERIAL_DEBUG
  591. printf("After IgnoreLeadingNaN: ReturnRaw %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  592. #endif
  593. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  594. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  595. {
  596. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  597. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  598. else
  599. continue; // es gibt keinen Zahl, da noch ein N vorhanden ist.
  600. }
  601. #ifdef SERIAL_DEBUG
  602. printf("After findDelimiterPos: ReturnValue %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  603. #endif
  604. // Lösche führende Nullen (außer es ist nur noch einen 0)
  605. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  606. NUMBERS[j]->ReturnValue.erase(0, 1);
  607. #ifdef SERIAL_DEBUG
  608. printf("After removeLeadingZeros: ReturnValue %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  609. #endif
  610. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  611. #ifdef SERIAL_DEBUG
  612. printf("After setting the Value: Value %f and as double is %f\n", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  613. #endif
  614. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  615. {
  616. if (flowDigit)
  617. {
  618. if (flowDigit->getCNNType() != Digital)
  619. printf("checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)\n");
  620. else
  621. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  622. }
  623. else
  624. {
  625. #ifdef SERIAL_DEBUG
  626. printf("checkDigitIncreaseConsistency = true - no digital numbers defined!\n");
  627. #endif
  628. }
  629. }
  630. #ifdef SERIAL_DEBUG
  631. printf("After checkDigitIncreaseConsistency: Value %f\n", NUMBERS[j]->Value);
  632. #endif
  633. if (!NUMBERS[j]->AllowNegativeRates)
  634. {
  635. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  636. {
  637. #ifdef SERIAL_DEBUG
  638. printf("Neg: value=%f, preValue=%f, preToll%f\n", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  639. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  640. ) ;
  641. #endif
  642. // Bei isExtendedResolution Ungenauigkeit von 0.2 mit einrechnen.
  643. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  644. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  645. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  646. } else {
  647. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  648. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  649. NUMBERS[j]->ReturnValue = "";
  650. continue;
  651. }
  652. }
  653. }
  654. #ifdef SERIAL_DEBUG
  655. printf("After AllowNegativeRates: Value %f\n", NUMBERS[j]->Value);
  656. #endif
  657. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  658. difference /= 60;
  659. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  660. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  661. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  662. {
  663. double _ratedifference;
  664. if (NUMBERS[j]->RateType == RateChange)
  665. _ratedifference = NUMBERS[j]->FlowRateAct;
  666. else
  667. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  668. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  669. {
  670. 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);
  671. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  672. NUMBERS[j]->ReturnValue = "";
  673. NUMBERS[j]->ReturnRateValue = "";
  674. continue;
  675. }
  676. }
  677. #ifdef SERIAL_DEBUG
  678. printf("After MaxRateCheck: Value %f\n", NUMBERS[j]->Value);
  679. #endif
  680. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  681. NUMBERS[j]->lastvalue = imagetime;
  682. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  683. NUMBERS[j]->PreValueOkay = true;
  684. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  685. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  686. NUMBERS[j]->ErrorMessageText = "no error";
  687. UpdatePreValueINI = true;
  688. string _zw = "PostProcessing - Raw: " + NUMBERS[j]->ReturnRawValue + " Value: " + NUMBERS[j]->ReturnValue + " Error: " + NUMBERS[j]->ErrorMessageText;
  689. LogFile.WriteToFile(_zw);
  690. WriteDataLog(j);
  691. }
  692. SavePreValue();
  693. return true;
  694. }
  695. void ClassFlowPostProcessing::WriteDataLog(int _analog)
  696. {
  697. string analog = "";
  698. string digital = "";
  699. if (flowAnalog)
  700. analog = flowAnalog->getReadout(_analog);
  701. if (flowDigit)
  702. digital = flowDigit->getReadout(_analog);
  703. // LogFile.WriteToFile(analog);
  704. LogFile.WriteToData(NUMBERS[_analog]->ReturnRawValue, NUMBERS[_analog]->ReturnValue, NUMBERS[_analog]->ErrorMessageText, digital, analog);
  705. }
  706. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  707. {
  708. for (int j = 0; j < NUMBERS.size(); ++j)
  709. {
  710. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  711. {
  712. // printf("Nurdigital\n");
  713. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  714. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  715. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  716. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  717. }
  718. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  719. {
  720. // printf("Nur analog\n");
  721. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  722. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  723. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  724. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  725. }
  726. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  727. {
  728. // printf("Nur digital + analog\n");
  729. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  730. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  731. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  732. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  733. }
  734. printf("UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i\n", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  735. }
  736. }
  737. string ClassFlowPostProcessing::getReadout(int _number)
  738. {
  739. return NUMBERS[_number]->ReturnValue;
  740. }
  741. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  742. {
  743. if (_rawValue)
  744. return NUMBERS[_number]->ReturnRawValue;
  745. if (_noerror)
  746. return NUMBERS[_number]->ReturnValue;
  747. return NUMBERS[_number]->ReturnValue;
  748. }
  749. string ClassFlowPostProcessing::RundeOutput(double _in, int _anzNachkomma){
  750. std::stringstream stream;
  751. int _zw = _in;
  752. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  753. if (_anzNachkomma < 0) {
  754. _anzNachkomma = 0;
  755. }
  756. if (_anzNachkomma > 0)
  757. {
  758. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  759. return stream.str();
  760. }
  761. else
  762. {
  763. stream << _zw;
  764. }
  765. return stream.str();
  766. }
  767. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  768. {
  769. int posN, posPunkt;
  770. int pot, ziffer;
  771. float zw;
  772. posN = findDelimiterPos(input, "N");
  773. posPunkt = findDelimiterPos(input, ".");
  774. if (posPunkt == std::string::npos){
  775. posPunkt = input.length();
  776. }
  777. while (posN != std::string::npos)
  778. {
  779. if (posN < posPunkt) {
  780. pot = posPunkt - posN - 1;
  781. }
  782. else {
  783. pot = posPunkt - posN;
  784. }
  785. zw =_prevalue / pow(10, pot);
  786. ziffer = ((int) zw) % 10;
  787. input[posN] = ziffer + 48;
  788. posN = findDelimiterPos(input, "N");
  789. }
  790. return input;
  791. }
  792. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  793. int aktdigit, olddigit;
  794. int aktdigit_before, olddigit_before;
  795. int pot, pot_max;
  796. float zw;
  797. bool no_nulldurchgang = false;
  798. pot = _decilamshift;
  799. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  800. {
  801. pot++;
  802. }
  803. #ifdef SERIAL_DEBUG
  804. printf("checkDigitConsistency: pot=%d, decimalshift=%d\n", pot, _decilamshift);
  805. #endif
  806. pot_max = ((int) log10(input)) + 1;
  807. while (pot <= pot_max)
  808. {
  809. zw = input / pow(10, pot-1);
  810. aktdigit_before = ((int) zw) % 10;
  811. zw = _preValue / pow(10, pot-1);
  812. olddigit_before = ((int) zw) % 10;
  813. zw = input / pow(10, pot);
  814. aktdigit = ((int) zw) % 10;
  815. zw = _preValue / pow(10, pot);
  816. olddigit = ((int) zw) % 10;
  817. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  818. if (no_nulldurchgang)
  819. {
  820. if (aktdigit != olddigit)
  821. {
  822. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  823. }
  824. }
  825. else
  826. {
  827. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  828. {
  829. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  830. }
  831. }
  832. #ifdef SERIAL_DEBUG
  833. printf("checkDigitConsistency: input=%f", input);
  834. #endif
  835. pot++;
  836. }
  837. return input;
  838. }
  839. string ClassFlowPostProcessing::getReadoutRate(int _number)
  840. {
  841. return std::to_string(NUMBERS[_number]->FlowRateAct);
  842. }
  843. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  844. {
  845. return NUMBERS[_number]->timeStamp;
  846. }
  847. string ClassFlowPostProcessing::getReadoutError(int _number)
  848. {
  849. return NUMBERS[_number]->ErrorMessageText;
  850. }