ClassFlowPostProcessing.cpp 35 KB

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