ClassFlowPostProcessing.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. #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. 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") // 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::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") // Set to default first (if nothing else is set)
  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") // Set to default first (if nothing else is set)
  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> splitted;
  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 does not fit MakeImage
  385. return false;
  386. InitNUMBERS();
  387. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  388. {
  389. splitted = ZerlegeZeile(aktparamgraph);
  390. std::string _param = GetParameterName(splitted[0]);
  391. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1))
  392. {
  393. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  394. }
  395. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1))
  396. {
  397. handleDecimalSeparator(splitted[0], splitted[1]);
  398. }
  399. if ((toUpper(_param) == "ANALOGDIGITALTRANSITIONSTART") && (splitted.size() > 1))
  400. {
  401. handleAnalogDigitalTransitionStart(splitted[0], splitted[1]);
  402. }
  403. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1))
  404. {
  405. handleMaxRateValue(splitted[0], splitted[1]);
  406. }
  407. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1))
  408. {
  409. handleMaxRateType(splitted[0], splitted[1]);
  410. }
  411. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1))
  412. {
  413. if (toUpper(splitted[1]) == "TRUE")
  414. {
  415. PreValueUse = true;
  416. }
  417. }
  418. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1))
  419. {
  420. if (toUpper(splitted[1]) == "TRUE")
  421. for (_n = 0; _n < NUMBERS.size(); ++_n)
  422. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  423. }
  424. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1))
  425. {
  426. if (toUpper(splitted[1]) == "TRUE")
  427. for (_n = 0; _n < NUMBERS.size(); ++_n)
  428. NUMBERS[_n]->AllowNegativeRates = true;
  429. }
  430. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1))
  431. {
  432. if (toUpper(splitted[1]) == "TRUE")
  433. ErrorMessage = true;
  434. }
  435. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1))
  436. {
  437. if (toUpper(splitted[1]) == "TRUE")
  438. IgnoreLeadingNaN = true;
  439. }
  440. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1))
  441. {
  442. PreValueAgeStartup = std::stoi(splitted[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->getNumberGENERAL();
  458. flowDigit->UpdateNameNumbers(&name_numbers);
  459. }
  460. if (flowAnalog)
  461. {
  462. anzANALOG = flowAnalog->getNumberGENERAL();
  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 = ""; // Raw value (with N & leading 0).
  485. _number->ReturnValue = ""; // corrected return value, possibly with error message
  486. _number->ErrorMessageText = ""; // Error message for 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; // last value read out well
  500. _number->Value = 0; // last value read out, incl. corrections
  501. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  502. _number->ReturnValue = ""; // corrected return value, possibly with error message
  503. _number->ErrorMessageText = ""; // Error message for 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) { // comma is before the first digit
  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()){ // Comma should be after 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 decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  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, "Quantity 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. /* TODO to be discussed, see https://github.com/jomjol/AI-on-the-edge-device/issues/1617 */
  571. // NUMBERS[j]->lastvalue = imagetime; // must only be set in case of good value !!! --> move to the end
  572. UpdateNachkommaDecimalShift();
  573. int previous_value = -1;
  574. if (NUMBERS[j]->analog_roi)
  575. {
  576. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  577. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  578. {
  579. char zw = NUMBERS[j]->ReturnRawValue[0];
  580. if (zw >= 48 && zw <=57)
  581. previous_value = zw - 48;
  582. }
  583. }
  584. #ifdef SERIAL_DEBUG
  585. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  586. #endif
  587. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  588. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  589. if (NUMBERS[j]->digit_roi)
  590. {
  591. if (NUMBERS[j]->analog_roi)
  592. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  593. else
  594. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  595. }
  596. #ifdef SERIAL_DEBUG
  597. ESP_LOGD(TAG, "After digital->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  598. #endif
  599. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  600. #ifdef SERIAL_DEBUG
  601. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  602. #endif
  603. if (IgnoreLeadingNaN)
  604. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  605. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  606. #ifdef SERIAL_DEBUG
  607. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  608. #endif
  609. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  610. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  611. {
  612. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  613. {
  614. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  615. }
  616. else
  617. {
  618. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  619. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  620. /* TODO to be discussed, see https://github.com/jomjol/AI-on-the-edge-device/issues/1617 */
  621. NUMBERS[j]->lastvalue = imagetime;
  622. WriteDataLog(j);
  623. continue; // there is no number because there is still an N.
  624. }
  625. }
  626. #ifdef SERIAL_DEBUG
  627. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  628. #endif
  629. // Delete leading zeros (unless there is only one 0 left)
  630. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  631. NUMBERS[j]->ReturnValue.erase(0, 1);
  632. #ifdef SERIAL_DEBUG
  633. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  634. #endif
  635. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  636. #ifdef SERIAL_DEBUG
  637. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  638. #endif
  639. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  640. {
  641. if (flowDigit)
  642. {
  643. if (flowDigit->getCNNType() != Digital)
  644. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)");
  645. else
  646. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  647. }
  648. else
  649. {
  650. #ifdef SERIAL_DEBUG
  651. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digital numbers defined!");
  652. #endif
  653. }
  654. }
  655. #ifdef SERIAL_DEBUG
  656. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  657. #endif
  658. if (!NUMBERS[j]->AllowNegativeRates)
  659. {
  660. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  661. {
  662. #ifdef SERIAL_DEBUG
  663. ESP_LOGD(TAG, "Neg: value=%f, preValue=%f, preToll%f", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  664. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  665. ) ;
  666. #endif
  667. // Include inaccuracy of 0.2 for isExtendedResolution.
  668. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  669. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  670. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  671. } else {
  672. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  673. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  674. NUMBERS[j]->ReturnValue = "";
  675. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  676. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  677. WriteDataLog(j);
  678. continue;
  679. }
  680. }
  681. }
  682. #ifdef SERIAL_DEBUG
  683. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  684. #endif
  685. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in seconds
  686. difference /= 60;
  687. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  688. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  689. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  690. {
  691. double _ratedifference;
  692. if (NUMBERS[j]->RateType == RateChange)
  693. _ratedifference = NUMBERS[j]->FlowRateAct;
  694. else
  695. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  696. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  697. {
  698. WriteDataLog(j);
  699. 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);
  700. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  701. NUMBERS[j]->ReturnValue = "";
  702. NUMBERS[j]->ReturnRateValue = "";
  703. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  704. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  705. WriteDataLog(j);
  706. continue;
  707. }
  708. }
  709. #ifdef SERIAL_DEBUG
  710. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  711. #endif
  712. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  713. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  714. NUMBERS[j]->PreValueOkay = true;
  715. NUMBERS[j]->lastvalue = imagetime;
  716. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  717. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  718. NUMBERS[j]->ErrorMessageText = "no error";
  719. UpdatePreValueINI = true;
  720. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  721. ESP_LOGD(TAG, "%s", zw.c_str());
  722. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  723. WriteDataLog(j);
  724. }
  725. SavePreValue();
  726. return true;
  727. }
  728. void ClassFlowPostProcessing::WriteDataLog(int _index)
  729. {
  730. if (!LogFile.GetDataLogToSD()){
  731. return;
  732. }
  733. string analog = "";
  734. string digital = "";
  735. string timezw = "";
  736. char buffer[80];
  737. struct tm* timeinfo = localtime(&NUMBERS[_index]->lastvalue);
  738. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  739. timezw = std::string(buffer);
  740. if (flowAnalog)
  741. analog = flowAnalog->getReadoutRawString(_index);
  742. if (flowDigit)
  743. digital = flowDigit->getReadoutRawString(_index);
  744. LogFile.WriteToData(timezw, NUMBERS[_index]->name,
  745. NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  746. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute,
  747. NUMBERS[_index]->ErrorMessageText,
  748. digital, analog);
  749. 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());
  750. }
  751. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  752. {
  753. for (int j = 0; j < NUMBERS.size(); ++j)
  754. {
  755. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // There are only digital digits
  756. {
  757. // ESP_LOGD(TAG, "Nurdigital");
  758. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  759. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  760. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  761. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  762. }
  763. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  764. {
  765. // ESP_LOGD(TAG, "Nur analog");
  766. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  767. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution())
  768. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  769. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  770. }
  771. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  772. {
  773. // ESP_LOGD(TAG, "Nur digital + analog");
  774. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  775. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  776. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  777. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  778. }
  779. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  780. }
  781. }
  782. string ClassFlowPostProcessing::getReadout(int _number)
  783. {
  784. return NUMBERS[_number]->ReturnValue;
  785. }
  786. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  787. {
  788. if (_rawValue)
  789. return NUMBERS[_number]->ReturnRawValue;
  790. if (_noerror)
  791. return NUMBERS[_number]->ReturnValue;
  792. return NUMBERS[_number]->ReturnValue;
  793. }
  794. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  795. {
  796. int posN, posPunkt;
  797. int pot, ziffer;
  798. float zw;
  799. posN = findDelimiterPos(input, "N");
  800. posPunkt = findDelimiterPos(input, ".");
  801. if (posPunkt == std::string::npos){
  802. posPunkt = input.length();
  803. }
  804. while (posN != std::string::npos)
  805. {
  806. if (posN < posPunkt) {
  807. pot = posPunkt - posN - 1;
  808. }
  809. else {
  810. pot = posPunkt - posN;
  811. }
  812. zw =_prevalue / pow(10, pot);
  813. ziffer = ((int) zw) % 10;
  814. input[posN] = ziffer + 48;
  815. posN = findDelimiterPos(input, "N");
  816. }
  817. return input;
  818. }
  819. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  820. int aktdigit, olddigit;
  821. int aktdigit_before, olddigit_before;
  822. int pot, pot_max;
  823. float zw;
  824. bool no_nulldurchgang = false;
  825. pot = _decilamshift;
  826. if (!_isanalog) // if there are no analogue values, the last one cannot be evaluated
  827. {
  828. pot++;
  829. }
  830. #ifdef SERIAL_DEBUG
  831. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  832. #endif
  833. pot_max = ((int) log10(input)) + 1;
  834. while (pot <= pot_max)
  835. {
  836. zw = input / pow(10, pot-1);
  837. aktdigit_before = ((int) zw) % 10;
  838. zw = _preValue / pow(10, pot-1);
  839. olddigit_before = ((int) zw) % 10;
  840. zw = input / pow(10, pot);
  841. aktdigit = ((int) zw) % 10;
  842. zw = _preValue / pow(10, pot);
  843. olddigit = ((int) zw) % 10;
  844. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  845. if (no_nulldurchgang)
  846. {
  847. if (aktdigit != olddigit)
  848. {
  849. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  850. }
  851. }
  852. else
  853. {
  854. if (aktdigit == olddigit) // despite zero crossing, digit was not incremented --> add 1
  855. {
  856. input = input + ((float) (1)) * pow(10, pot); // add 1 at the point
  857. }
  858. }
  859. #ifdef SERIAL_DEBUG
  860. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  861. #endif
  862. pot++;
  863. }
  864. return input;
  865. }
  866. string ClassFlowPostProcessing::getReadoutRate(int _number)
  867. {
  868. return std::to_string(NUMBERS[_number]->FlowRateAct);
  869. }
  870. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  871. {
  872. return NUMBERS[_number]->timeStamp;
  873. }
  874. string ClassFlowPostProcessing::getReadoutError(int _number)
  875. {
  876. return NUMBERS[_number]->ErrorMessageText;
  877. }