ClassFlowPostProcessing.cpp 35 KB

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