ClassFlowPostProcessing.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. #include "ClassFlowPostProcessing.h"
  2. #include "Helper.h"
  3. #include "ClassFlowTakeImage.h"
  4. #include "ClassLogFile.h"
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <cassert>
  8. #include <time.h>
  9. #include "time_sntp.h"
  10. #include "esp_log.h"
  11. #include "../../include/defines.h"
  12. static const char* TAG = "POSTPROC";
  13. std::string ClassFlowPostProcessing::getNumbersName()
  14. {
  15. std::string ret="";
  16. for (int i = 0; i < NUMBERS.size(); ++i)
  17. {
  18. ret += NUMBERS[i]->name;
  19. if (i < NUMBERS.size()-1)
  20. ret = ret + "\t";
  21. }
  22. // ESP_LOGI(TAG, "Result ClassFlowPostProcessing::getNumbersName: %s", ret.c_str());
  23. return ret;
  24. }
  25. std::string ClassFlowPostProcessing::GetJSON(std::string _lineend)
  26. {
  27. std::string json="{" + _lineend;
  28. for (int i = 0; i < NUMBERS.size(); ++i)
  29. {
  30. json += "\"" + NUMBERS[i]->name + "\":" + _lineend;
  31. json += getJsonFromNumber(i, _lineend) + _lineend;
  32. if ((i+1) < NUMBERS.size())
  33. json += "," + _lineend;
  34. }
  35. json += "}";
  36. return json;
  37. }
  38. string ClassFlowPostProcessing::getJsonFromNumber(int i, std::string _lineend) {
  39. std::string json = "";
  40. json += " {" + _lineend;
  41. if (NUMBERS[i]->ReturnValue.length() > 0)
  42. json += " \"value\": \"" + NUMBERS[i]->ReturnValue + "\"," + _lineend;
  43. else
  44. json += " \"value\": \"\"," + _lineend;
  45. json += " \"raw\": \"" + NUMBERS[i]->ReturnRawValue + "\"," + _lineend;
  46. json += " \"pre\": \"" + NUMBERS[i]->ReturnPreValue + "\"," + _lineend;
  47. json += " \"error\": \"" + NUMBERS[i]->ErrorMessageText + "\"," + _lineend;
  48. if (NUMBERS[i]->ReturnRateValue.length() > 0)
  49. json += " \"rate\": \"" + NUMBERS[i]->ReturnRateValue + "\"," + _lineend;
  50. else
  51. json += " \"rate\": \"\"," + _lineend;
  52. json += " \"timestamp\": \"" + NUMBERS[i]->timeStamp + "\"" + _lineend;
  53. json += " }" + _lineend;
  54. return json;
  55. }
  56. string ClassFlowPostProcessing::GetPreValue(std::string _number)
  57. {
  58. std::string result;
  59. int index = -1;
  60. if (_number == "")
  61. _number = "default";
  62. for (int i = 0; i < NUMBERS.size(); ++i)
  63. if (NUMBERS[i]->name == _number)
  64. index = i;
  65. if (index == -1)
  66. return std::string("");
  67. result = RundeOutput(NUMBERS[index]->PreValue, NUMBERS[index]->Nachkomma);
  68. return result;
  69. }
  70. bool ClassFlowPostProcessing::SetPreValue(double _newvalue, string _numbers, bool _extern)
  71. {
  72. //ESP_LOGD(TAG, "SetPrevalue: %f, %s", zw, _numbers.c_str());
  73. for (int j = 0; j < NUMBERS.size(); ++j) {
  74. //ESP_LOGD(TAG, "Number %d, %s", j, NUMBERS[j]->name.c_str());
  75. if (NUMBERS[j]->name == _numbers) {
  76. if (_newvalue >= 0) { // if new value posivive, use provided value to preset PreValue
  77. NUMBERS[j]->PreValue = _newvalue;
  78. }
  79. else { // if new value negative, use last raw value to preset PreValue
  80. char* p;
  81. double ReturnRawValueAsDouble = strtod(NUMBERS[j]->ReturnRawValue.c_str(), &p);
  82. if (ReturnRawValueAsDouble == 0) {
  83. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "SetPreValue: RawValue not a valid value for further processing: "
  84. + NUMBERS[j]->ReturnRawValue);
  85. return false;
  86. }
  87. NUMBERS[j]->PreValue = ReturnRawValueAsDouble;
  88. }
  89. NUMBERS[j]->ReturnPreValue = std::to_string(NUMBERS[j]->PreValue);
  90. NUMBERS[j]->PreValueOkay = true;
  91. if (_extern)
  92. {
  93. time(&(NUMBERS[j]->lastvalue));
  94. localtime(&(NUMBERS[j]->lastvalue));
  95. }
  96. //ESP_LOGD(TAG, "Found %d! - set to %.8f", j, NUMBERS[j]->PreValue);
  97. UpdatePreValueINI = true; // Only update prevalue file if a new value is set
  98. SavePreValue();
  99. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SetPreValue: PreValue for " + NUMBERS[j]->name + " set to " +
  100. std::to_string(NUMBERS[j]->PreValue));
  101. return true;
  102. }
  103. }
  104. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "SetPreValue: Numbersname not found or not valid");
  105. return false; // No new value was set (e.g. wrong numbersname, no numbers at all)
  106. }
  107. bool ClassFlowPostProcessing::LoadPreValue(void)
  108. {
  109. std::vector<string> splitted;
  110. FILE* pFile;
  111. char zw[1024];
  112. string zwtime, zwvalue, name;
  113. bool _done = false;
  114. UpdatePreValueINI = false; // Conversion to the new format
  115. pFile = fopen(FilePreValue.c_str(), "r");
  116. if (pFile == NULL)
  117. return false;
  118. fgets(zw, 1024, pFile);
  119. ESP_LOGD(TAG, "Read line Prevalue.ini: %s", zw);
  120. zwtime = trim(std::string(zw));
  121. if (zwtime.length() == 0)
  122. return false;
  123. splitted = HelperZerlegeZeile(zwtime, "\t");
  124. if (splitted.size() > 1) // Conversion to the new format
  125. {
  126. while ((splitted.size() > 1) && !_done)
  127. {
  128. name = trim(splitted[0]);
  129. zwtime = trim(splitted[1]);
  130. zwvalue = trim(splitted[2]);
  131. for (int j = 0; j < NUMBERS.size(); ++j)
  132. {
  133. if (NUMBERS[j]->name == name)
  134. {
  135. NUMBERS[j]->PreValue = stod(zwvalue.c_str());
  136. 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).
  137. time_t tStart;
  138. int yy, month, dd, hh, mm, ss;
  139. struct tm whenStart;
  140. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  141. whenStart.tm_year = yy - 1900;
  142. whenStart.tm_mon = month - 1;
  143. whenStart.tm_mday = dd;
  144. whenStart.tm_hour = hh;
  145. whenStart.tm_min = mm;
  146. whenStart.tm_sec = ss;
  147. whenStart.tm_isdst = -1;
  148. NUMBERS[j]->lastvalue = mktime(&whenStart);
  149. time(&tStart);
  150. localtime(&tStart);
  151. double difference = difftime(tStart, NUMBERS[j]->lastvalue);
  152. difference /= 60;
  153. if (difference > PreValueAgeStartup)
  154. NUMBERS[j]->PreValueOkay = false;
  155. else
  156. NUMBERS[j]->PreValueOkay = true;
  157. }
  158. }
  159. if (!fgets(zw, 1024, pFile))
  160. _done = true;
  161. else
  162. {
  163. ESP_LOGD(TAG, "Read line Prevalue.ini: %s", zw);
  164. splitted = HelperZerlegeZeile(trim(std::string(zw)), "\t");
  165. if (splitted.size() > 1)
  166. {
  167. name = trim(splitted[0]);
  168. zwtime = trim(splitted[1]);
  169. zwvalue = trim(splitted[2]);
  170. }
  171. }
  172. }
  173. fclose(pFile);
  174. }
  175. else // Old Format
  176. {
  177. fgets(zw, 1024, pFile);
  178. fclose(pFile);
  179. ESP_LOGD(TAG, "%s", zw);
  180. zwvalue = trim(std::string(zw));
  181. NUMBERS[0]->PreValue = stod(zwvalue.c_str());
  182. time_t tStart;
  183. int yy, month, dd, hh, mm, ss;
  184. struct tm whenStart;
  185. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  186. whenStart.tm_year = yy - 1900;
  187. whenStart.tm_mon = month - 1;
  188. whenStart.tm_mday = dd;
  189. whenStart.tm_hour = hh;
  190. whenStart.tm_min = mm;
  191. whenStart.tm_sec = ss;
  192. whenStart.tm_isdst = -1;
  193. 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);
  194. NUMBERS[0]->lastvalue = mktime(&whenStart);
  195. time(&tStart);
  196. localtime(&tStart);
  197. double difference = difftime(tStart, NUMBERS[0]->lastvalue);
  198. difference /= 60;
  199. if (difference > PreValueAgeStartup)
  200. return false;
  201. NUMBERS[0]->Value = NUMBERS[0]->PreValue;
  202. NUMBERS[0]->ReturnValue = to_string(NUMBERS[0]->Value);
  203. if (NUMBERS[0]->digit_roi || NUMBERS[0]->analog_roi)
  204. {
  205. NUMBERS[0]->ReturnValue = RundeOutput(NUMBERS[0]->Value, NUMBERS[0]->Nachkomma);
  206. }
  207. UpdatePreValueINI = true; // Conversion to the new format
  208. SavePreValue();
  209. }
  210. return true;
  211. }
  212. void ClassFlowPostProcessing::SavePreValue()
  213. {
  214. FILE* pFile;
  215. string _zw;
  216. if (!UpdatePreValueINI) // PreValues unchanged --> File does not have to be rewritten
  217. return;
  218. pFile = fopen(FilePreValue.c_str(), "w");
  219. for (int j = 0; j < NUMBERS.size(); ++j)
  220. {
  221. char buffer[80];
  222. struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue);
  223. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  224. NUMBERS[j]->timeStamp = std::string(buffer);
  225. NUMBERS[j]->timeStampTimeUTC = NUMBERS[j]->lastvalue;
  226. // ESP_LOGD(TAG, "SaverPreValue %d, Value: %f, Nachkomma %d", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  227. _zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n";
  228. ESP_LOGD(TAG, "Write PreValue line: %s", _zw.c_str());
  229. if (pFile) {
  230. fputs(_zw.c_str(), pFile);
  231. }
  232. }
  233. UpdatePreValueINI = false;
  234. fclose(pFile);
  235. }
  236. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc, ClassFlowCNNGeneral *_analog, ClassFlowCNNGeneral *_digit)
  237. {
  238. PreValueUse = false;
  239. PreValueAgeStartup = 30;
  240. ErrorMessage = false;
  241. ListFlowControll = NULL;
  242. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  243. ListFlowControll = lfc;
  244. flowTakeImage = NULL;
  245. UpdatePreValueINI = false;
  246. IgnoreLeadingNaN = false;
  247. flowAnalog = _analog;
  248. flowDigit = _digit;
  249. for (int i = 0; i < ListFlowControll->size(); ++i)
  250. {
  251. if (((*ListFlowControll)[i])->name().compare("ClassFlowTakeImage") == 0)
  252. {
  253. flowTakeImage = (ClassFlowTakeImage*) (*ListFlowControll)[i];
  254. }
  255. }
  256. }
  257. void ClassFlowPostProcessing::handleDecimalExtendedResolution(string _decsep, string _value)
  258. {
  259. string _digit, _decpos;
  260. int _pospunkt = _decsep.find_first_of(".");
  261. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  262. if (_pospunkt > -1)
  263. _digit = _decsep.substr(0, _pospunkt);
  264. else
  265. _digit = "default";
  266. for (int j = 0; j < NUMBERS.size(); ++j)
  267. {
  268. bool _zwdc = false;
  269. if (toUpper(_value) == "TRUE")
  270. _zwdc = true;
  271. if (_digit == "default") // Set to default first (if nothing else is set)
  272. {
  273. NUMBERS[j]->isExtendedResolution = _zwdc;
  274. }
  275. if (NUMBERS[j]->name == _digit)
  276. {
  277. NUMBERS[j]->isExtendedResolution = _zwdc;
  278. }
  279. }
  280. }
  281. void ClassFlowPostProcessing::handleDecimalSeparator(string _decsep, string _value)
  282. {
  283. string _digit, _decpos;
  284. int _pospunkt = _decsep.find_first_of(".");
  285. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  286. if (_pospunkt > -1)
  287. _digit = _decsep.substr(0, _pospunkt);
  288. else
  289. _digit = "default";
  290. for (int j = 0; j < NUMBERS.size(); ++j)
  291. {
  292. int _zwdc = 0;
  293. // try
  294. {
  295. _zwdc = stoi(_value);
  296. }
  297. /* catch(const std::exception& e)
  298. {
  299. ESP_LOGD(TAG, "ERROR - Decimalshift is not a number: %s", _value.c_str());
  300. }
  301. */
  302. if (_digit == "default") // Set to default first (if nothing else is set)
  303. {
  304. NUMBERS[j]->DecimalShift = _zwdc;
  305. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  306. }
  307. if (NUMBERS[j]->name == _digit)
  308. {
  309. NUMBERS[j]->DecimalShift = _zwdc;
  310. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  311. }
  312. NUMBERS[j]->Nachkomma = NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift;
  313. }
  314. }
  315. void ClassFlowPostProcessing::handleAnalogDigitalTransitionStart(string _decsep, string _value)
  316. {
  317. string _digit, _decpos;
  318. int _pospunkt = _decsep.find_first_of(".");
  319. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  320. if (_pospunkt > -1)
  321. _digit = _decsep.substr(0, _pospunkt);
  322. else
  323. _digit = "default";
  324. for (int j = 0; j < NUMBERS.size(); ++j)
  325. {
  326. float _zwdc = 9.2;
  327. {
  328. _zwdc = stof(_value);
  329. }
  330. if (_digit == "default" || NUMBERS[j]->name == _digit) // Set to default first (if nothing else is set)
  331. {
  332. NUMBERS[j]->AnalogDigitalTransitionStart = _zwdc;
  333. }
  334. }
  335. }
  336. void ClassFlowPostProcessing::handleAllowNegativeRate(string _decsep, string _value)
  337. {
  338. string _digit, _decpos;
  339. int _pospunkt = _decsep.find_first_of(".");
  340. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  341. if (_pospunkt > -1)
  342. _digit = _decsep.substr(0, _pospunkt);
  343. else
  344. _digit = "default";
  345. for (int j = 0; j < NUMBERS.size(); ++j)
  346. {
  347. bool _rt = false;
  348. if (toUpper(_value) == "TRUE")
  349. _rt = true;
  350. if (_digit == "default") // Set to default first (if nothing else is set)
  351. {
  352. NUMBERS[j]->AllowNegativeRates = _rt;
  353. }
  354. if (NUMBERS[j]->name == _digit)
  355. {
  356. NUMBERS[j]->AllowNegativeRates = _rt;
  357. }
  358. }
  359. }
  360. void ClassFlowPostProcessing::handleMaxRateType(string _decsep, string _value)
  361. {
  362. string _digit, _decpos;
  363. int _pospunkt = _decsep.find_first_of(".");
  364. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  365. if (_pospunkt > -1)
  366. _digit = _decsep.substr(0, _pospunkt);
  367. else
  368. _digit = "default";
  369. for (int j = 0; j < NUMBERS.size(); ++j)
  370. {
  371. t_RateType _rt = AbsoluteChange;
  372. if (toUpper(_value) == "RATECHANGE")
  373. _rt = RateChange;
  374. if (_digit == "default") // Set to default first (if nothing else is set)
  375. {
  376. NUMBERS[j]->RateType = _rt;
  377. }
  378. if (NUMBERS[j]->name == _digit)
  379. {
  380. NUMBERS[j]->RateType = _rt;
  381. }
  382. }
  383. }
  384. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  385. {
  386. string _digit, _decpos;
  387. int _pospunkt = _decsep.find_first_of(".");
  388. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  389. if (_pospunkt > -1)
  390. _digit = _decsep.substr(0, _pospunkt);
  391. else
  392. _digit = "default";
  393. for (int j = 0; j < NUMBERS.size(); ++j)
  394. {
  395. float _zwdc = 1;
  396. // try
  397. {
  398. _zwdc = stof(_value);
  399. }
  400. /* catch(const std::exception& e)
  401. {
  402. ESP_LOGD(TAG, "ERROR - MaxRateValue is not a number: %s", _value.c_str());
  403. }
  404. */
  405. if (_digit == "default") // Set to default first (if nothing else is set)
  406. {
  407. NUMBERS[j]->useMaxRateValue = true;
  408. NUMBERS[j]->MaxRateValue = _zwdc;
  409. }
  410. if (NUMBERS[j]->name == _digit)
  411. {
  412. NUMBERS[j]->useMaxRateValue = true;
  413. NUMBERS[j]->MaxRateValue = _zwdc;
  414. }
  415. }
  416. }
  417. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  418. {
  419. std::vector<string> splitted;
  420. int _n;
  421. aktparamgraph = trim(aktparamgraph);
  422. if (aktparamgraph.size() == 0)
  423. if (!this->GetNextParagraph(pfile, aktparamgraph))
  424. return false;
  425. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph does not fit PostProcessing
  426. return false;
  427. InitNUMBERS();
  428. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  429. {
  430. splitted = ZerlegeZeile(aktparamgraph);
  431. std::string _param = GetParameterName(splitted[0]);
  432. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1))
  433. {
  434. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  435. }
  436. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1))
  437. {
  438. handleDecimalSeparator(splitted[0], splitted[1]);
  439. }
  440. if ((toUpper(_param) == "ANALOGDIGITALTRANSITIONSTART") && (splitted.size() > 1))
  441. {
  442. handleAnalogDigitalTransitionStart(splitted[0], splitted[1]);
  443. }
  444. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1))
  445. {
  446. handleMaxRateValue(splitted[0], splitted[1]);
  447. }
  448. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1))
  449. {
  450. handleMaxRateType(splitted[0], splitted[1]);
  451. }
  452. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1))
  453. {
  454. if (toUpper(splitted[1]) == "TRUE")
  455. {
  456. PreValueUse = true;
  457. }
  458. }
  459. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1))
  460. {
  461. if (toUpper(splitted[1]) == "TRUE")
  462. for (_n = 0; _n < NUMBERS.size(); ++_n)
  463. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  464. }
  465. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1))
  466. {
  467. handleAllowNegativeRate(splitted[0], splitted[1]);
  468. /* Updated to allow individual Settings
  469. if (toUpper(splitted[1]) == "TRUE")
  470. for (_n = 0; _n < NUMBERS.size(); ++_n)
  471. NUMBERS[_n]->AllowNegativeRates = true;
  472. */
  473. }
  474. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1))
  475. {
  476. if (toUpper(splitted[1]) == "TRUE")
  477. ErrorMessage = true;
  478. }
  479. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1))
  480. {
  481. if (toUpper(splitted[1]) == "TRUE")
  482. IgnoreLeadingNaN = true;
  483. }
  484. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1))
  485. {
  486. PreValueAgeStartup = std::stoi(splitted[1]);
  487. }
  488. }
  489. if (PreValueUse) {
  490. LoadPreValue();
  491. }
  492. return true;
  493. }
  494. void ClassFlowPostProcessing::InitNUMBERS()
  495. {
  496. int anzDIGIT = 0;
  497. int anzANALOG = 0;
  498. std::vector<std::string> name_numbers;
  499. if (flowDigit)
  500. {
  501. anzDIGIT = flowDigit->getNumberGENERAL();
  502. flowDigit->UpdateNameNumbers(&name_numbers);
  503. }
  504. if (flowAnalog)
  505. {
  506. anzANALOG = flowAnalog->getNumberGENERAL();
  507. flowAnalog->UpdateNameNumbers(&name_numbers);
  508. }
  509. ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
  510. for (int _num = 0; _num < name_numbers.size(); ++_num)
  511. {
  512. NumberPost *_number = new NumberPost;
  513. _number->name = name_numbers[_num];
  514. _number->digit_roi = NULL;
  515. if (flowDigit)
  516. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  517. if (_number->digit_roi)
  518. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  519. else
  520. _number->AnzahlDigital = 0;
  521. _number->analog_roi = NULL;
  522. if (flowAnalog)
  523. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  524. if (_number->analog_roi)
  525. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  526. else
  527. _number->AnzahlAnalog = 0;
  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->ReturnPreValue = "";
  532. _number->PreValueOkay = false;
  533. _number->AllowNegativeRates = false;
  534. _number->MaxRateValue = 0.1;
  535. _number->RateType = AbsoluteChange;
  536. _number->useMaxRateValue = false;
  537. _number->checkDigitIncreaseConsistency = false;
  538. _number->DecimalShift = 0;
  539. _number->DecimalShiftInitial = 0;
  540. _number->isExtendedResolution = false;
  541. _number->AnalogDigitalTransitionStart=9.2;
  542. _number->FlowRateAct = 0; // m3 / min
  543. _number->PreValue = 0; // last value read out well
  544. _number->Value = 0; // last value read out, incl. corrections
  545. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  546. _number->ReturnValue = ""; // corrected return value, possibly with error message
  547. _number->ErrorMessageText = ""; // Error message for consistency check
  548. _number->Nachkomma = _number->AnzahlAnalog;
  549. NUMBERS.push_back(_number);
  550. }
  551. for (int i = 0; i < NUMBERS.size(); ++i) {
  552. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  553. }
  554. }
  555. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  556. if (_decShift == 0){
  557. return in;
  558. }
  559. int _pos_dec_org, _pos_dec_neu;
  560. _pos_dec_org = findDelimiterPos(in, ".");
  561. if (_pos_dec_org == std::string::npos) {
  562. _pos_dec_org = in.length();
  563. }
  564. else
  565. {
  566. in = in.erase(_pos_dec_org, 1);
  567. }
  568. _pos_dec_neu = _pos_dec_org + _decShift;
  569. if (_pos_dec_neu <= 0) { // comma is before the first digit
  570. for (int i = 0; i > _pos_dec_neu; --i){
  571. in = in.insert(0, "0");
  572. }
  573. in = "0." + in;
  574. return in;
  575. }
  576. if (_pos_dec_neu > in.length()){ // Comma should be after string (123 --> 1230)
  577. for (int i = in.length(); i < _pos_dec_neu; ++i){
  578. in = in.insert(in.length(), "0");
  579. }
  580. return in;
  581. }
  582. string zw;
  583. zw = in.substr(0, _pos_dec_neu);
  584. zw = zw + ".";
  585. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  586. return zw;
  587. }
  588. float wrapAround(float val)
  589. {
  590. return fmod(val, 10.);
  591. }
  592. /**
  593. * @brief Checks whether val is in the range [min, max]
  594. *
  595. * Note, this function also handles the wrap around case,
  596. * in which min could be larger than max in case of
  597. * a circular range
  598. *
  599. * @param val The value to be checked
  600. * @param min Minimal bound of the range
  601. * @param max Maximum bound of the range
  602. * @return True, if val is in the range
  603. */
  604. bool inRange(float val, float min, float max)
  605. {
  606. assert(min >= 0. && min < 10.0);
  607. assert(max >= 0. && max <= 10.0);
  608. assert(val >= 0. && val < 10.0);
  609. if (min <= max)
  610. {
  611. return min <= val && val <= max;
  612. }
  613. else
  614. {
  615. // e.g. between 8 and 2 (of the next round)
  616. return (min <= val && val < 10.) || (0. <= val && val <= max);
  617. }
  618. }
  619. /**
  620. * @brief Synchronizes a potential misalignment between analog and digital part
  621. *
  622. * @param The current value assembled from digits (pre comma) and analogs (post comma)
  623. * @param digitalPrecision The post-comma value of the last digit ([0...9])
  624. * @param analogDigitalShift The value of the 0.1 analog, when the digital precision == 5 in [0, 9.9]
  625. *
  626. * We define 3 phases:
  627. * - Pre transition: analog post comma < analogDigitalShift && not in transition
  628. * - Transition: Digital Precision in range 1...9
  629. * - Post transition: analog post comma > analogDigitalShift && not in transition
  630. *
  631. * @return The synchronized values as a string
  632. */
  633. std::string syncDigitalAnalog(const std::string& value, const std::string& digitalPrecision,
  634. double analogDigitalShift)
  635. {
  636. if (digitalPrecision.empty())
  637. {
  638. return value;
  639. }
  640. const auto pos = value.find('.');
  641. if (pos == std::string::npos)
  642. {
  643. return value;
  644. }
  645. // disassemble value into pre and post comma part
  646. const auto preComma = value.substr(0, pos);
  647. // memorize, to be able to assemble right numbers of leading zeros
  648. const size_t nPreComma = preComma.size();
  649. const auto postComma = value.substr(pos+1);
  650. const float digitalPostComma = std::atof(digitalPrecision.c_str());
  651. int digitalPreComma = std::atoi(preComma.c_str());
  652. const float analogPostComma = std::atof(("0." + postComma).c_str());
  653. // Determine phase
  654. const bool inTransition = digitalPostComma > 0. && digitalPostComma < 10.;
  655. const bool postTransition = !inTransition && inRange(analogPostComma*10., analogDigitalShift, wrapAround(analogDigitalShift + 5.));
  656. const bool preTransition = !inTransition && inRange(analogPostComma*10., 0, analogDigitalShift);
  657. if (inRange(analogDigitalShift, 0.5, 5.))
  658. {
  659. // late transition, last digit starts transition, when analog is between [0.5, 5)
  660. if (inTransition || preTransition)
  661. {
  662. ESP_LOGD("syncDigitalAnalog", "Late digital transition. Increase digital value by 1.");
  663. digitalPreComma += 1;
  664. }
  665. }
  666. else if (inRange(analogDigitalShift, 5., 9.5))
  667. {
  668. // early transition
  669. if (postTransition && analogPostComma*10 > analogDigitalShift)
  670. {
  671. ESP_LOGD("syncDigitalAnalog", "Early digital transition. Decrease digital value by 1.");
  672. digitalPreComma -= 1;
  673. }
  674. // transition has not finished, but we are already at the new cycle
  675. // this also should handle hanging digits
  676. if (inTransition && analogPostComma < 0.5) {
  677. digitalPreComma += 1;
  678. }
  679. }
  680. else
  681. {
  682. return value;
  683. }
  684. // assemble result into string again, pad with zeros
  685. auto preCommaNew = std::to_string(digitalPreComma);
  686. for (size_t i = preCommaNew.size(); i < nPreComma; ++i)
  687. preCommaNew = "0" + preCommaNew;
  688. const std::string result = preCommaNew + "." + postComma;
  689. #if debugSync
  690. ESP_LOGD("syncDigitalAnalog", "result: %s", result.c_str());
  691. #endif
  692. return result;
  693. }
  694. bool ClassFlowPostProcessing::doFlow(string zwtime)
  695. {
  696. string result = "";
  697. string digit = "";
  698. string analog = "";
  699. string zwvalue;
  700. string zw;
  701. time_t imagetime = 0;
  702. string rohwert;
  703. // Update decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  704. imagetime = flowTakeImage->getTimeImageTaken();
  705. if (imagetime == 0)
  706. time(&imagetime);
  707. struct tm* timeinfo;
  708. timeinfo = localtime(&imagetime);
  709. char strftime_buf[64];
  710. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  711. zwtime = std::string(strftime_buf);
  712. ESP_LOGD(TAG, "Quantity NUMBERS: %d", NUMBERS.size());
  713. for (int j = 0; j < NUMBERS.size(); ++j)
  714. {
  715. NUMBERS[j]->ReturnRawValue = "";
  716. NUMBERS[j]->ReturnRateValue = "";
  717. NUMBERS[j]->ReturnValue = "";
  718. NUMBERS[j]->ErrorMessageText = "";
  719. NUMBERS[j]->Value = -1;
  720. /* calculate time difference BEFORE we overwrite the 'lastvalue' */
  721. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in seconds
  722. /* TODO:
  723. * We could call `NUMBERS[j]->lastvalue = imagetime;` here and remove all other such calls further down.
  724. * But we should check nothing breaks! */
  725. UpdateNachkommaDecimalShift();
  726. int previous_value = -1;
  727. // ------------------- start processing analog values --------------------------//
  728. if (NUMBERS[j]->analog_roi)
  729. {
  730. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  731. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  732. {
  733. char zw = NUMBERS[j]->ReturnRawValue[0];
  734. if (zw >= 48 && zw <=57)
  735. previous_value = zw - 48;
  736. }
  737. }
  738. #ifdef SERIAL_DEBUG
  739. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  740. #endif
  741. // ----------------- start processing digital values --------------------------//
  742. // we need the precision of the digital values to determine transition phase
  743. std::string digitalPrecision = {"0"};
  744. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  745. // we have nachkommad and vorkomman part!
  746. std::string analogValues = NUMBERS[j]->ReturnRawValue;
  747. std::string digitValues = flowDigit->getReadout(j, true, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, 0.);
  748. if (flowDigit->getCNNType() != Digital)
  749. {
  750. // The digital type does not provide an extended resolution, so we cannot extract it
  751. digitalPrecision = digitValues.substr(digitValues.size() - 1);
  752. digitValues = digitValues.substr(0, digitValues.size() - 1);
  753. }
  754. NUMBERS[j]->ReturnRawValue = digitValues + "." + analogValues;
  755. }
  756. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi)
  757. {
  758. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  759. }
  760. #ifdef SERIAL_DEBUG
  761. ESP_LOGD(TAG, "After digital->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  762. #endif
  763. // ------------------ start corrections --------------------------//
  764. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  765. #ifdef SERIAL_DEBUG
  766. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  767. #endif
  768. if (IgnoreLeadingNaN)
  769. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  770. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  771. #ifdef SERIAL_DEBUG
  772. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  773. #endif
  774. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  775. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  776. {
  777. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  778. {
  779. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  780. }
  781. else
  782. {
  783. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  784. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  785. /* TODO to be discussed, see https://github.com/jomjol/AI-on-the-edge-device/issues/1617 */
  786. NUMBERS[j]->lastvalue = imagetime;
  787. WriteDataLog(j);
  788. continue; // there is no number because there is still an N.
  789. }
  790. }
  791. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  792. {
  793. // Synchronize potential misalignment between analog and digital part
  794. NUMBERS[j]->ReturnValue = syncDigitalAnalog(NUMBERS[j]->ReturnValue, digitalPrecision, NUMBERS[j]->AnalogDigitalTransitionStart);
  795. }
  796. #ifdef SERIAL_DEBUG
  797. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  798. #endif
  799. // Delete leading zeros (unless there is only one 0 left)
  800. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  801. NUMBERS[j]->ReturnValue.erase(0, 1);
  802. #ifdef SERIAL_DEBUG
  803. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  804. #endif
  805. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  806. #ifdef SERIAL_DEBUG
  807. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  808. #endif
  809. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  810. {
  811. if (flowDigit)
  812. {
  813. if (flowDigit->getCNNType() != Digital)
  814. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)");
  815. else
  816. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  817. }
  818. else
  819. {
  820. #ifdef SERIAL_DEBUG
  821. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digital numbers defined!");
  822. #endif
  823. }
  824. }
  825. #ifdef SERIAL_DEBUG
  826. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  827. #endif
  828. if (!NUMBERS[j]->AllowNegativeRates)
  829. {
  830. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handleAllowNegativeRate for device: " + NUMBERS[j]->name);
  831. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  832. {
  833. // more debug if extended resolution is on, see #2447
  834. if (NUMBERS[j]->isExtendedResolution) {
  835. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Neg: value=" + std::to_string(NUMBERS[j]->Value)
  836. + ", preValue=" + std::to_string(NUMBERS[j]->PreValue)
  837. + ", preToll=" + std::to_string(NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))));
  838. }
  839. // Include inaccuracy of 0.2 for isExtendedResolution.
  840. if ((NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution)
  841. // not extended resolution allows -1 on the lowest digit
  842. || (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(1/pow(10, NUMBERS[j]->Nachkomma))) && !NUMBERS[j]->isExtendedResolution)) {
  843. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  844. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  845. }
  846. else {
  847. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  848. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  849. NUMBERS[j]->ReturnValue = "";
  850. NUMBERS[j]->lastvalue = imagetime;
  851. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  852. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  853. WriteDataLog(j);
  854. continue;
  855. }
  856. }
  857. }
  858. #ifdef SERIAL_DEBUG
  859. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  860. #endif
  861. difference /= 60;
  862. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  863. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  864. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  865. {
  866. double _ratedifference;
  867. if (NUMBERS[j]->RateType == RateChange)
  868. _ratedifference = NUMBERS[j]->FlowRateAct;
  869. else
  870. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  871. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  872. {
  873. 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);
  874. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  875. NUMBERS[j]->ReturnValue = "";
  876. NUMBERS[j]->ReturnRateValue = "";
  877. NUMBERS[j]->lastvalue = imagetime;
  878. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  879. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  880. WriteDataLog(j);
  881. continue;
  882. }
  883. }
  884. #ifdef SERIAL_DEBUG
  885. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  886. #endif
  887. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  888. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  889. NUMBERS[j]->PreValueOkay = true;
  890. NUMBERS[j]->lastvalue = imagetime;
  891. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  892. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  893. NUMBERS[j]->ErrorMessageText = "no error";
  894. UpdatePreValueINI = true;
  895. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  896. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  897. WriteDataLog(j);
  898. }
  899. SavePreValue();
  900. return true;
  901. }
  902. void ClassFlowPostProcessing::WriteDataLog(int _index)
  903. {
  904. if (!LogFile.GetDataLogToSD()){
  905. return;
  906. }
  907. string analog = "";
  908. string digital = "";
  909. string timezw = "";
  910. char buffer[80];
  911. struct tm* timeinfo = localtime(&NUMBERS[_index]->lastvalue);
  912. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  913. timezw = std::string(buffer);
  914. if (flowAnalog)
  915. analog = flowAnalog->getReadoutRawString(_index);
  916. if (flowDigit)
  917. digital = flowDigit->getReadoutRawString(_index);
  918. LogFile.WriteToData(timezw, NUMBERS[_index]->name,
  919. NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  920. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute,
  921. NUMBERS[_index]->ErrorMessageText,
  922. digital, analog);
  923. 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());
  924. }
  925. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  926. {
  927. for (int j = 0; j < NUMBERS.size(); ++j)
  928. {
  929. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // There are only digital digits
  930. {
  931. // ESP_LOGD(TAG, "Nurdigital");
  932. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  933. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  934. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  935. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  936. }
  937. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  938. {
  939. // ESP_LOGD(TAG, "Nur analog");
  940. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  941. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution())
  942. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  943. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  944. }
  945. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  946. {
  947. // ESP_LOGD(TAG, "Nur digital + analog");
  948. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  949. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  950. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  951. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  952. }
  953. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  954. }
  955. }
  956. string ClassFlowPostProcessing::getReadout(int _number)
  957. {
  958. return NUMBERS[_number]->ReturnValue;
  959. }
  960. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  961. {
  962. if (_rawValue)
  963. return NUMBERS[_number]->ReturnRawValue;
  964. if (_noerror)
  965. return NUMBERS[_number]->ReturnValue;
  966. return NUMBERS[_number]->ReturnValue;
  967. }
  968. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  969. {
  970. int posN, posPunkt;
  971. int pot, ziffer;
  972. float zw;
  973. posN = findDelimiterPos(input, "N");
  974. posPunkt = findDelimiterPos(input, ".");
  975. if (posPunkt == std::string::npos){
  976. posPunkt = input.length();
  977. }
  978. while (posN != std::string::npos)
  979. {
  980. if (posN < posPunkt) {
  981. pot = posPunkt - posN - 1;
  982. }
  983. else {
  984. pot = posPunkt - posN;
  985. }
  986. zw =_prevalue / pow(10, pot);
  987. ziffer = ((int) zw) % 10;
  988. input[posN] = ziffer + 48;
  989. posN = findDelimiterPos(input, "N");
  990. }
  991. return input;
  992. }
  993. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  994. int aktdigit, olddigit;
  995. int aktdigit_before, olddigit_before;
  996. int pot, pot_max;
  997. float zw;
  998. bool no_nulldurchgang = false;
  999. pot = _decilamshift;
  1000. if (!_isanalog) // if there are no analogue values, the last one cannot be evaluated
  1001. {
  1002. pot++;
  1003. }
  1004. #ifdef SERIAL_DEBUG
  1005. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  1006. #endif
  1007. pot_max = ((int) log10(input)) + 1;
  1008. while (pot <= pot_max)
  1009. {
  1010. zw = input / pow(10, pot-1);
  1011. aktdigit_before = ((int) zw) % 10;
  1012. zw = _preValue / pow(10, pot-1);
  1013. olddigit_before = ((int) zw) % 10;
  1014. zw = input / pow(10, pot);
  1015. aktdigit = ((int) zw) % 10;
  1016. zw = _preValue / pow(10, pot);
  1017. olddigit = ((int) zw) % 10;
  1018. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  1019. if (no_nulldurchgang)
  1020. {
  1021. if (aktdigit != olddigit)
  1022. {
  1023. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  1024. }
  1025. }
  1026. else
  1027. {
  1028. if (aktdigit == olddigit) // despite zero crossing, digit was not incremented --> add 1
  1029. {
  1030. input = input + ((float) (1)) * pow(10, pot); // add 1 at the point
  1031. }
  1032. }
  1033. #ifdef SERIAL_DEBUG
  1034. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  1035. #endif
  1036. pot++;
  1037. }
  1038. return input;
  1039. }
  1040. string ClassFlowPostProcessing::getReadoutRate(int _number)
  1041. {
  1042. return std::to_string(NUMBERS[_number]->FlowRateAct);
  1043. }
  1044. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  1045. {
  1046. return NUMBERS[_number]->timeStamp;
  1047. }
  1048. string ClassFlowPostProcessing::getReadoutError(int _number)
  1049. {
  1050. return NUMBERS[_number]->ErrorMessageText;
  1051. }