ClassFlowPostProcessing.cpp 33 KB

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