ClassFlowPostProcessing.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. #include "ClassFlowPostProcessing.h"
  2. #include "Helper.h"
  3. #include "ClassFlowTakeImage.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. #include "../../include/defines.h"
  11. static const char* TAG = "POSTPROC";
  12. std::string ClassFlowPostProcessing::getNumbersName()
  13. {
  14. std::string ret="";
  15. for (int i = 0; i < NUMBERS.size(); ++i)
  16. {
  17. ret += NUMBERS[i]->name;
  18. if (i < NUMBERS.size()-1)
  19. ret = ret + "\t";
  20. }
  21. // ESP_LOGI(TAG, "Result ClassFlowPostProcessing::getNumbersName: %s", ret.c_str());
  22. return ret;
  23. }
  24. std::string ClassFlowPostProcessing::GetJSON(std::string _lineend)
  25. {
  26. std::string json="{" + _lineend;
  27. for (int i = 0; i < NUMBERS.size(); ++i)
  28. {
  29. json += "\"" + NUMBERS[i]->name + "\":" + _lineend;
  30. json += getJsonFromNumber(i, _lineend) + _lineend;
  31. if ((i+1) < NUMBERS.size())
  32. json += "," + _lineend;
  33. }
  34. json += "}";
  35. return json;
  36. }
  37. string ClassFlowPostProcessing::getJsonFromNumber(int i, std::string _lineend) {
  38. std::string json = "";
  39. json += " {" + _lineend;
  40. if (NUMBERS[i]->ReturnValue.length() > 0)
  41. json += " \"value\": \"" + NUMBERS[i]->ReturnValue + "\"," + _lineend;
  42. else
  43. json += " \"value\": \"\"," + _lineend;
  44. json += " \"raw\": \"" + NUMBERS[i]->ReturnRawValue + "\"," + _lineend;
  45. json += " \"pre\": \"" + NUMBERS[i]->ReturnPreValue + "\"," + _lineend;
  46. json += " \"error\": \"" + NUMBERS[i]->ErrorMessageText + "\"," + _lineend;
  47. if (NUMBERS[i]->ReturnRateValue.length() > 0)
  48. json += " \"rate\": \"" + NUMBERS[i]->ReturnRateValue + "\"," + _lineend;
  49. else
  50. json += " \"rate\": \"\"," + _lineend;
  51. json += " \"timestamp\": \"" + NUMBERS[i]->timeStamp + "\"" + _lineend;
  52. json += " }" + _lineend;
  53. return json;
  54. }
  55. string ClassFlowPostProcessing::GetPreValue(std::string _number)
  56. {
  57. std::string result;
  58. int index = -1;
  59. if (_number == "")
  60. _number = "default";
  61. for (int i = 0; i < NUMBERS.size(); ++i)
  62. if (NUMBERS[i]->name == _number)
  63. index = i;
  64. if (index == -1)
  65. return std::string("");
  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 = std::to_string(zw);
  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> splitted;
  94. FILE* pFile;
  95. char zw[1024];
  96. string zwtime, zwvalue, name;
  97. bool _done = false;
  98. UpdatePreValueINI = false; // Conversion to the new 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. splitted = HelperZerlegeZeile(zwtime, "\t");
  108. if (splitted.size() > 1) // Conversion to the new format
  109. {
  110. while ((splitted.size() > 1) && !_done)
  111. {
  112. name = trim(splitted[0]);
  113. zwtime = trim(splitted[1]);
  114. zwvalue = trim(splitted[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); // To be on the safe side, 1 digit more, as Exgtended Resolution may be on (will only be set during the first run).
  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. splitted = HelperZerlegeZeile(trim(std::string(zw)), "\t");
  149. if (splitted.size() > 1)
  150. {
  151. name = trim(splitted[0]);
  152. zwtime = trim(splitted[1]);
  153. zwvalue = trim(splitted[2]);
  154. }
  155. }
  156. }
  157. fclose(pFile);
  158. }
  159. else // Old 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; // Conversion to the new format
  192. SavePreValue();
  193. }
  194. return true;
  195. }
  196. void ClassFlowPostProcessing::SavePreValue()
  197. {
  198. FILE* pFile;
  199. string _zw;
  200. if (!UpdatePreValueINI) // PreValues unchanged --> File does not have to be rewritten
  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 line: %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. flowTakeImage = 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("ClassFlowTakeImage") == 0)
  235. {
  236. flowTakeImage = (ClassFlowTakeImage*) (*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") // Set to default first (if nothing else is set)
  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") // Set to default first (if nothing else is set)
  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) // Set to default first (if nothing else is set)
  314. {
  315. NUMBERS[j]->AnalogDigitalTransitionStart = _zwdc;
  316. }
  317. }
  318. }
  319. void ClassFlowPostProcessing::handleAllowNegativeRate(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. bool _rt = false;
  331. if (toUpper(_value) == "TRUE")
  332. _rt = true;
  333. if (_digit == "default") // Set to default first (if nothing else is set)
  334. {
  335. NUMBERS[j]->AllowNegativeRates = _rt;
  336. }
  337. if (NUMBERS[j]->name == _digit)
  338. {
  339. NUMBERS[j]->AllowNegativeRates = _rt;
  340. }
  341. }
  342. }
  343. void ClassFlowPostProcessing::handleMaxRateType(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. t_RateType _rt = AbsoluteChange;
  355. if (toUpper(_value) == "RATECHANGE")
  356. _rt = RateChange;
  357. if (_digit == "default") // Set to default first (if nothing else is set)
  358. {
  359. NUMBERS[j]->RateType = _rt;
  360. }
  361. if (NUMBERS[j]->name == _digit)
  362. {
  363. NUMBERS[j]->RateType = _rt;
  364. }
  365. }
  366. }
  367. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  368. {
  369. string _digit, _decpos;
  370. int _pospunkt = _decsep.find_first_of(".");
  371. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  372. if (_pospunkt > -1)
  373. _digit = _decsep.substr(0, _pospunkt);
  374. else
  375. _digit = "default";
  376. for (int j = 0; j < NUMBERS.size(); ++j)
  377. {
  378. float _zwdc = 1;
  379. // try
  380. {
  381. _zwdc = stof(_value);
  382. }
  383. /* catch(const std::exception& e)
  384. {
  385. ESP_LOGD(TAG, "ERROR - MaxRateValue is not a number: %s", _value.c_str());
  386. }
  387. */
  388. if (_digit == "default") // Set to default first (if nothing else is set)
  389. {
  390. NUMBERS[j]->useMaxRateValue = true;
  391. NUMBERS[j]->MaxRateValue = _zwdc;
  392. }
  393. if (NUMBERS[j]->name == _digit)
  394. {
  395. NUMBERS[j]->useMaxRateValue = true;
  396. NUMBERS[j]->MaxRateValue = _zwdc;
  397. }
  398. }
  399. }
  400. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  401. {
  402. std::vector<string> splitted;
  403. int _n;
  404. aktparamgraph = trim(aktparamgraph);
  405. if (aktparamgraph.size() == 0)
  406. if (!this->GetNextParagraph(pfile, aktparamgraph))
  407. return false;
  408. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph does not fit PostProcessing
  409. return false;
  410. InitNUMBERS();
  411. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  412. {
  413. splitted = ZerlegeZeile(aktparamgraph);
  414. std::string _param = GetParameterName(splitted[0]);
  415. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1))
  416. {
  417. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  418. }
  419. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1))
  420. {
  421. handleDecimalSeparator(splitted[0], splitted[1]);
  422. }
  423. if ((toUpper(_param) == "ANALOGDIGITALTRANSITIONSTART") && (splitted.size() > 1))
  424. {
  425. handleAnalogDigitalTransitionStart(splitted[0], splitted[1]);
  426. }
  427. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1))
  428. {
  429. handleMaxRateValue(splitted[0], splitted[1]);
  430. }
  431. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1))
  432. {
  433. handleMaxRateType(splitted[0], splitted[1]);
  434. }
  435. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1))
  436. {
  437. if (toUpper(splitted[1]) == "TRUE")
  438. {
  439. PreValueUse = true;
  440. }
  441. }
  442. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1))
  443. {
  444. if (toUpper(splitted[1]) == "TRUE")
  445. for (_n = 0; _n < NUMBERS.size(); ++_n)
  446. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  447. }
  448. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1))
  449. {
  450. handleAllowNegativeRate(splitted[0], splitted[1]);
  451. /* Updated to allow individual Settings
  452. if (toUpper(splitted[1]) == "TRUE")
  453. for (_n = 0; _n < NUMBERS.size(); ++_n)
  454. NUMBERS[_n]->AllowNegativeRates = true;
  455. */
  456. }
  457. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1))
  458. {
  459. if (toUpper(splitted[1]) == "TRUE")
  460. ErrorMessage = true;
  461. }
  462. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1))
  463. {
  464. if (toUpper(splitted[1]) == "TRUE")
  465. IgnoreLeadingNaN = true;
  466. }
  467. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1))
  468. {
  469. PreValueAgeStartup = std::stoi(splitted[1]);
  470. }
  471. }
  472. if (PreValueUse) {
  473. LoadPreValue();
  474. }
  475. return true;
  476. }
  477. void ClassFlowPostProcessing::InitNUMBERS()
  478. {
  479. int anzDIGIT = 0;
  480. int anzANALOG = 0;
  481. std::vector<std::string> name_numbers;
  482. if (flowDigit)
  483. {
  484. anzDIGIT = flowDigit->getNumberGENERAL();
  485. flowDigit->UpdateNameNumbers(&name_numbers);
  486. }
  487. if (flowAnalog)
  488. {
  489. anzANALOG = flowAnalog->getNumberGENERAL();
  490. flowAnalog->UpdateNameNumbers(&name_numbers);
  491. }
  492. ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
  493. for (int _num = 0; _num < name_numbers.size(); ++_num)
  494. {
  495. NumberPost *_number = new NumberPost;
  496. _number->name = name_numbers[_num];
  497. _number->digit_roi = NULL;
  498. if (flowDigit)
  499. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  500. if (_number->digit_roi)
  501. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  502. else
  503. _number->AnzahlDigital = 0;
  504. _number->analog_roi = NULL;
  505. if (flowAnalog)
  506. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  507. if (_number->analog_roi)
  508. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  509. else
  510. _number->AnzahlAnalog = 0;
  511. _number->ReturnRawValue = ""; // Raw value (with N & leading 0).
  512. _number->ReturnValue = ""; // corrected return value, possibly with error message
  513. _number->ErrorMessageText = ""; // Error message for consistency check
  514. _number->ReturnPreValue = "";
  515. _number->PreValueOkay = false;
  516. _number->AllowNegativeRates = false;
  517. _number->MaxRateValue = 0.1;
  518. _number->RateType = AbsoluteChange;
  519. _number->useMaxRateValue = false;
  520. _number->checkDigitIncreaseConsistency = false;
  521. _number->DecimalShift = 0;
  522. _number->DecimalShiftInitial = 0;
  523. _number->isExtendedResolution = false;
  524. _number->AnalogDigitalTransitionStart=9.2;
  525. _number->FlowRateAct = 0; // m3 / min
  526. _number->PreValue = 0; // last value read out well
  527. _number->Value = 0; // last value read out, incl. corrections
  528. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  529. _number->ReturnValue = ""; // corrected return value, possibly with error message
  530. _number->ErrorMessageText = ""; // Error message for consistency check
  531. _number->Nachkomma = _number->AnzahlAnalog;
  532. NUMBERS.push_back(_number);
  533. }
  534. for (int i = 0; i < NUMBERS.size(); ++i) {
  535. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  536. }
  537. }
  538. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  539. if (_decShift == 0){
  540. return in;
  541. }
  542. int _pos_dec_org, _pos_dec_neu;
  543. _pos_dec_org = findDelimiterPos(in, ".");
  544. if (_pos_dec_org == std::string::npos) {
  545. _pos_dec_org = in.length();
  546. }
  547. else
  548. {
  549. in = in.erase(_pos_dec_org, 1);
  550. }
  551. _pos_dec_neu = _pos_dec_org + _decShift;
  552. if (_pos_dec_neu <= 0) { // comma is before the first digit
  553. for (int i = 0; i > _pos_dec_neu; --i){
  554. in = in.insert(0, "0");
  555. }
  556. in = "0." + in;
  557. return in;
  558. }
  559. if (_pos_dec_neu > in.length()){ // Comma should be after string (123 --> 1230)
  560. for (int i = in.length(); i < _pos_dec_neu; ++i){
  561. in = in.insert(in.length(), "0");
  562. }
  563. return in;
  564. }
  565. string zw;
  566. zw = in.substr(0, _pos_dec_neu);
  567. zw = zw + ".";
  568. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  569. return zw;
  570. }
  571. bool ClassFlowPostProcessing::doFlow(string zwtime)
  572. {
  573. string result = "";
  574. string digit = "";
  575. string analog = "";
  576. string zwvalue;
  577. string zw;
  578. time_t imagetime = 0;
  579. string rohwert;
  580. // Update decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  581. imagetime = flowTakeImage->getTimeImageTaken();
  582. if (imagetime == 0)
  583. time(&imagetime);
  584. struct tm* timeinfo;
  585. timeinfo = localtime(&imagetime);
  586. char strftime_buf[64];
  587. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  588. zwtime = std::string(strftime_buf);
  589. ESP_LOGD(TAG, "Quantity NUMBERS: %d", NUMBERS.size());
  590. for (int j = 0; j < NUMBERS.size(); ++j)
  591. {
  592. NUMBERS[j]->ReturnRawValue = "";
  593. NUMBERS[j]->ReturnRateValue = "";
  594. NUMBERS[j]->ReturnValue = "";
  595. NUMBERS[j]->ErrorMessageText = "";
  596. NUMBERS[j]->Value = -1;
  597. /* calculate time difference BEFORE we overwrite the 'lastvalue' */
  598. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in seconds
  599. /* TODO:
  600. * We could call `NUMBERS[j]->lastvalue = imagetime;` here and remove all other such calls further down.
  601. * But we should check nothing breaks! */
  602. UpdateNachkommaDecimalShift();
  603. int previous_value = -1;
  604. if (NUMBERS[j]->analog_roi)
  605. {
  606. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  607. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  608. {
  609. char zw = NUMBERS[j]->ReturnRawValue[0];
  610. if (zw >= 48 && zw <=57)
  611. previous_value = zw - 48;
  612. }
  613. }
  614. #ifdef SERIAL_DEBUG
  615. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  616. #endif
  617. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  618. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  619. if (NUMBERS[j]->digit_roi)
  620. {
  621. if (NUMBERS[j]->analog_roi)
  622. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  623. else
  624. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  625. }
  626. #ifdef SERIAL_DEBUG
  627. ESP_LOGD(TAG, "After digital->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  628. #endif
  629. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  630. #ifdef SERIAL_DEBUG
  631. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  632. #endif
  633. if (IgnoreLeadingNaN)
  634. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  635. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  636. #ifdef SERIAL_DEBUG
  637. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  638. #endif
  639. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  640. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  641. {
  642. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  643. {
  644. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  645. }
  646. else
  647. {
  648. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  649. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  650. /* TODO to be discussed, see https://github.com/jomjol/AI-on-the-edge-device/issues/1617 */
  651. NUMBERS[j]->lastvalue = imagetime;
  652. WriteDataLog(j);
  653. continue; // there is no number because there is still an N.
  654. }
  655. }
  656. #ifdef SERIAL_DEBUG
  657. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  658. #endif
  659. // Delete leading zeros (unless there is only one 0 left)
  660. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  661. NUMBERS[j]->ReturnValue.erase(0, 1);
  662. #ifdef SERIAL_DEBUG
  663. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  664. #endif
  665. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  666. #ifdef SERIAL_DEBUG
  667. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  668. #endif
  669. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  670. {
  671. if (flowDigit)
  672. {
  673. if (flowDigit->getCNNType() != Digital)
  674. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)");
  675. else
  676. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  677. }
  678. else
  679. {
  680. #ifdef SERIAL_DEBUG
  681. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digital numbers defined!");
  682. #endif
  683. }
  684. }
  685. #ifdef SERIAL_DEBUG
  686. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  687. #endif
  688. if (!NUMBERS[j]->AllowNegativeRates)
  689. {
  690. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handleAllowNegativeRate for device: " + NUMBERS[j]->name);
  691. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  692. {
  693. #ifdef SERIAL_DEBUG
  694. ESP_LOGD(TAG, "Neg: value=%f, preValue=%f, preToll%f", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  695. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  696. ) ;
  697. #endif
  698. // Include inaccuracy of 0.2 for isExtendedResolution.
  699. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  700. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  701. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  702. } else {
  703. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  704. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  705. NUMBERS[j]->ReturnValue = "";
  706. NUMBERS[j]->lastvalue = imagetime;
  707. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  708. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  709. WriteDataLog(j);
  710. continue;
  711. }
  712. }
  713. }
  714. #ifdef SERIAL_DEBUG
  715. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  716. #endif
  717. difference /= 60;
  718. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  719. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  720. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  721. {
  722. double _ratedifference;
  723. if (NUMBERS[j]->RateType == RateChange)
  724. _ratedifference = NUMBERS[j]->FlowRateAct;
  725. else
  726. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  727. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  728. {
  729. 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);
  730. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  731. NUMBERS[j]->ReturnValue = "";
  732. NUMBERS[j]->ReturnRateValue = "";
  733. NUMBERS[j]->lastvalue = imagetime;
  734. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  735. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  736. WriteDataLog(j);
  737. continue;
  738. }
  739. }
  740. #ifdef SERIAL_DEBUG
  741. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  742. #endif
  743. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  744. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  745. NUMBERS[j]->PreValueOkay = true;
  746. NUMBERS[j]->lastvalue = imagetime;
  747. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  748. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  749. NUMBERS[j]->ErrorMessageText = "no error";
  750. UpdatePreValueINI = true;
  751. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  752. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  753. WriteDataLog(j);
  754. }
  755. SavePreValue();
  756. return true;
  757. }
  758. void ClassFlowPostProcessing::WriteDataLog(int _index)
  759. {
  760. if (!LogFile.GetDataLogToSD()){
  761. return;
  762. }
  763. string analog = "";
  764. string digital = "";
  765. string timezw = "";
  766. char buffer[80];
  767. struct tm* timeinfo = localtime(&NUMBERS[_index]->lastvalue);
  768. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  769. timezw = std::string(buffer);
  770. if (flowAnalog)
  771. analog = flowAnalog->getReadoutRawString(_index);
  772. if (flowDigit)
  773. digital = flowDigit->getReadoutRawString(_index);
  774. LogFile.WriteToData(timezw, NUMBERS[_index]->name,
  775. NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  776. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute,
  777. NUMBERS[_index]->ErrorMessageText,
  778. digital, analog);
  779. 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());
  780. }
  781. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  782. {
  783. for (int j = 0; j < NUMBERS.size(); ++j)
  784. {
  785. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // There are only digital digits
  786. {
  787. // ESP_LOGD(TAG, "Nurdigital");
  788. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  789. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  790. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  791. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  792. }
  793. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  794. {
  795. // ESP_LOGD(TAG, "Nur analog");
  796. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  797. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution())
  798. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  799. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  800. }
  801. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  802. {
  803. // ESP_LOGD(TAG, "Nur digital + analog");
  804. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  805. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  806. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  807. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  808. }
  809. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  810. }
  811. }
  812. string ClassFlowPostProcessing::getReadout(int _number)
  813. {
  814. return NUMBERS[_number]->ReturnValue;
  815. }
  816. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  817. {
  818. if (_rawValue)
  819. return NUMBERS[_number]->ReturnRawValue;
  820. if (_noerror)
  821. return NUMBERS[_number]->ReturnValue;
  822. return NUMBERS[_number]->ReturnValue;
  823. }
  824. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  825. {
  826. int posN, posPunkt;
  827. int pot, ziffer;
  828. float zw;
  829. posN = findDelimiterPos(input, "N");
  830. posPunkt = findDelimiterPos(input, ".");
  831. if (posPunkt == std::string::npos){
  832. posPunkt = input.length();
  833. }
  834. while (posN != std::string::npos)
  835. {
  836. if (posN < posPunkt) {
  837. pot = posPunkt - posN - 1;
  838. }
  839. else {
  840. pot = posPunkt - posN;
  841. }
  842. zw =_prevalue / pow(10, pot);
  843. ziffer = ((int) zw) % 10;
  844. input[posN] = ziffer + 48;
  845. posN = findDelimiterPos(input, "N");
  846. }
  847. return input;
  848. }
  849. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  850. int aktdigit, olddigit;
  851. int aktdigit_before, olddigit_before;
  852. int pot, pot_max;
  853. float zw;
  854. bool no_nulldurchgang = false;
  855. pot = _decilamshift;
  856. if (!_isanalog) // if there are no analogue values, the last one cannot be evaluated
  857. {
  858. pot++;
  859. }
  860. #ifdef SERIAL_DEBUG
  861. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  862. #endif
  863. pot_max = ((int) log10(input)) + 1;
  864. while (pot <= pot_max)
  865. {
  866. zw = input / pow(10, pot-1);
  867. aktdigit_before = ((int) zw) % 10;
  868. zw = _preValue / pow(10, pot-1);
  869. olddigit_before = ((int) zw) % 10;
  870. zw = input / pow(10, pot);
  871. aktdigit = ((int) zw) % 10;
  872. zw = _preValue / pow(10, pot);
  873. olddigit = ((int) zw) % 10;
  874. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  875. if (no_nulldurchgang)
  876. {
  877. if (aktdigit != olddigit)
  878. {
  879. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  880. }
  881. }
  882. else
  883. {
  884. if (aktdigit == olddigit) // despite zero crossing, digit was not incremented --> add 1
  885. {
  886. input = input + ((float) (1)) * pow(10, pot); // add 1 at the point
  887. }
  888. }
  889. #ifdef SERIAL_DEBUG
  890. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  891. #endif
  892. pot++;
  893. }
  894. return input;
  895. }
  896. string ClassFlowPostProcessing::getReadoutRate(int _number)
  897. {
  898. return std::to_string(NUMBERS[_number]->FlowRateAct);
  899. }
  900. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  901. {
  902. return NUMBERS[_number]->timeStamp;
  903. }
  904. string ClassFlowPostProcessing::getReadoutError(int _number)
  905. {
  906. return NUMBERS[_number]->ErrorMessageText;
  907. }