ClassFlowPostProcessing.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. {
  598. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  599. }
  600. else
  601. {
  602. WriteDataLog(j);
  603. continue; // es gibt keinen Zahl, da noch ein N vorhanden ist.
  604. }
  605. }
  606. #ifdef SERIAL_DEBUG
  607. printf("After findDelimiterPos: ReturnValue %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  608. #endif
  609. // Lösche führende Nullen (außer es ist nur noch einen 0)
  610. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  611. NUMBERS[j]->ReturnValue.erase(0, 1);
  612. #ifdef SERIAL_DEBUG
  613. printf("After removeLeadingZeros: ReturnValue %s\n", NUMBERS[j]->ReturnRawValue.c_str());
  614. #endif
  615. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  616. #ifdef SERIAL_DEBUG
  617. printf("After setting the Value: Value %f and as double is %f\n", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  618. #endif
  619. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  620. {
  621. if (flowDigit)
  622. {
  623. if (flowDigit->getCNNType() != Digital)
  624. printf("checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)\n");
  625. else
  626. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  627. }
  628. else
  629. {
  630. #ifdef SERIAL_DEBUG
  631. printf("checkDigitIncreaseConsistency = true - no digital numbers defined!\n");
  632. #endif
  633. }
  634. }
  635. #ifdef SERIAL_DEBUG
  636. printf("After checkDigitIncreaseConsistency: Value %f\n", NUMBERS[j]->Value);
  637. #endif
  638. if (!NUMBERS[j]->AllowNegativeRates)
  639. {
  640. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  641. {
  642. #ifdef SERIAL_DEBUG
  643. printf("Neg: value=%f, preValue=%f, preToll%f\n", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  644. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  645. ) ;
  646. #endif
  647. // Bei isExtendedResolution Ungenauigkeit von 0.2 mit einrechnen.
  648. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  649. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  650. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  651. } else {
  652. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  653. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  654. NUMBERS[j]->ReturnValue = "";
  655. WriteDataLog(j);
  656. continue;
  657. }
  658. }
  659. }
  660. #ifdef SERIAL_DEBUG
  661. printf("After AllowNegativeRates: Value %f\n", NUMBERS[j]->Value);
  662. #endif
  663. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  664. difference /= 60;
  665. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  666. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  667. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  668. {
  669. double _ratedifference;
  670. if (NUMBERS[j]->RateType == RateChange)
  671. _ratedifference = NUMBERS[j]->FlowRateAct;
  672. else
  673. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  674. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  675. {
  676. 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);
  677. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  678. NUMBERS[j]->ReturnValue = "";
  679. NUMBERS[j]->ReturnRateValue = "";
  680. WriteDataLog(j);
  681. continue;
  682. }
  683. }
  684. #ifdef SERIAL_DEBUG
  685. printf("After MaxRateCheck: Value %f\n", NUMBERS[j]->Value);
  686. #endif
  687. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  688. NUMBERS[j]->lastvalue = imagetime;
  689. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  690. NUMBERS[j]->PreValueOkay = true;
  691. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  692. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  693. NUMBERS[j]->ErrorMessageText = "no error";
  694. UpdatePreValueINI = true;
  695. string _zw = "PostProcessing - Raw: " + NUMBERS[j]->ReturnRawValue + " Value: " + NUMBERS[j]->ReturnValue + " Error: " + NUMBERS[j]->ErrorMessageText;
  696. printf("%s\n", zw.c_str());
  697. LogFile.WriteToFile(_zw);
  698. WriteDataLog(j);
  699. }
  700. SavePreValue();
  701. return true;
  702. }
  703. void ClassFlowPostProcessing::WriteDataLog(int _analog)
  704. {
  705. string analog = "";
  706. string digital = "";
  707. if (flowAnalog)
  708. analog = flowAnalog->getReadout(_analog);
  709. if (flowDigit)
  710. digital = flowDigit->getReadout(_analog);
  711. // LogFile.WriteToFile(analog);
  712. LogFile.WriteToData(NUMBERS[_analog]->ReturnRawValue, NUMBERS[_analog]->ReturnValue, NUMBERS[_analog]->ErrorMessageText, digital, analog);
  713. printf("WriteDataLog: %s, %s, %s, %s, %s", NUMBERS[_analog]->ReturnRawValue.c_str(), NUMBERS[_analog]->ReturnValue.c_str(), NUMBERS[_analog]->ErrorMessageText.c_str(), digital.c_str(), analog.c_str());
  714. }
  715. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  716. {
  717. for (int j = 0; j < NUMBERS.size(); ++j)
  718. {
  719. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  720. {
  721. // printf("Nurdigital\n");
  722. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  723. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  724. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  725. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  726. }
  727. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  728. {
  729. // printf("Nur analog\n");
  730. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  731. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  732. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  733. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  734. }
  735. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  736. {
  737. // printf("Nur digital + analog\n");
  738. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  739. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  740. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  741. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  742. }
  743. printf("UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i\n", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  744. }
  745. }
  746. string ClassFlowPostProcessing::getReadout(int _number)
  747. {
  748. return NUMBERS[_number]->ReturnValue;
  749. }
  750. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  751. {
  752. if (_rawValue)
  753. return NUMBERS[_number]->ReturnRawValue;
  754. if (_noerror)
  755. return NUMBERS[_number]->ReturnValue;
  756. return NUMBERS[_number]->ReturnValue;
  757. }
  758. string ClassFlowPostProcessing::RundeOutput(double _in, int _anzNachkomma){
  759. std::stringstream stream;
  760. int _zw = _in;
  761. // printf("AnzNachkomma: %d\n", _anzNachkomma);
  762. if (_anzNachkomma < 0) {
  763. _anzNachkomma = 0;
  764. }
  765. if (_anzNachkomma > 0)
  766. {
  767. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  768. return stream.str();
  769. }
  770. else
  771. {
  772. stream << _zw;
  773. }
  774. return stream.str();
  775. }
  776. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  777. {
  778. int posN, posPunkt;
  779. int pot, ziffer;
  780. float zw;
  781. posN = findDelimiterPos(input, "N");
  782. posPunkt = findDelimiterPos(input, ".");
  783. if (posPunkt == std::string::npos){
  784. posPunkt = input.length();
  785. }
  786. while (posN != std::string::npos)
  787. {
  788. if (posN < posPunkt) {
  789. pot = posPunkt - posN - 1;
  790. }
  791. else {
  792. pot = posPunkt - posN;
  793. }
  794. zw =_prevalue / pow(10, pot);
  795. ziffer = ((int) zw) % 10;
  796. input[posN] = ziffer + 48;
  797. posN = findDelimiterPos(input, "N");
  798. }
  799. return input;
  800. }
  801. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  802. int aktdigit, olddigit;
  803. int aktdigit_before, olddigit_before;
  804. int pot, pot_max;
  805. float zw;
  806. bool no_nulldurchgang = false;
  807. pot = _decilamshift;
  808. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  809. {
  810. pot++;
  811. }
  812. #ifdef SERIAL_DEBUG
  813. printf("checkDigitConsistency: pot=%d, decimalshift=%d\n", pot, _decilamshift);
  814. #endif
  815. pot_max = ((int) log10(input)) + 1;
  816. while (pot <= pot_max)
  817. {
  818. zw = input / pow(10, pot-1);
  819. aktdigit_before = ((int) zw) % 10;
  820. zw = _preValue / pow(10, pot-1);
  821. olddigit_before = ((int) zw) % 10;
  822. zw = input / pow(10, pot);
  823. aktdigit = ((int) zw) % 10;
  824. zw = _preValue / pow(10, pot);
  825. olddigit = ((int) zw) % 10;
  826. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  827. if (no_nulldurchgang)
  828. {
  829. if (aktdigit != olddigit)
  830. {
  831. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  832. }
  833. }
  834. else
  835. {
  836. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  837. {
  838. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  839. }
  840. }
  841. #ifdef SERIAL_DEBUG
  842. printf("checkDigitConsistency: input=%f", input);
  843. #endif
  844. pot++;
  845. }
  846. return input;
  847. }
  848. string ClassFlowPostProcessing::getReadoutRate(int _number)
  849. {
  850. return std::to_string(NUMBERS[_number]->FlowRateAct);
  851. }
  852. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  853. {
  854. return NUMBERS[_number]->timeStamp;
  855. }
  856. string ClassFlowPostProcessing::getReadoutError(int _number)
  857. {
  858. return NUMBERS[_number]->ErrorMessageText;
  859. }