ClassFlowPostProcessing.cpp 35 KB

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