ClassFlowPostProcessing.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. std::string ret="";
  14. for (int i = 0; i < NUMBERS.size(); ++i) {
  15. ret += NUMBERS[i]->name;
  16. if (i < NUMBERS.size()-1) {
  17. ret = ret + "\t";
  18. }
  19. }
  20. // ESP_LOGI(TAG, "Result ClassFlowPostProcessing::getNumbersName: %s", ret.c_str());
  21. return ret;
  22. }
  23. std::string ClassFlowPostProcessing::GetJSON(std::string _lineend) {
  24. std::string json="{" + _lineend;
  25. for (int i = 0; i < NUMBERS.size(); ++i) {
  26. json += "\"" + NUMBERS[i]->name + "\":" + _lineend;
  27. json += getJsonFromNumber(i, _lineend) + _lineend;
  28. if ((i+1) < NUMBERS.size()) {
  29. json += "," + _lineend;
  30. }
  31. }
  32. json += "}";
  33. return json;
  34. }
  35. string ClassFlowPostProcessing::getJsonFromNumber(int i, std::string _lineend) {
  36. std::string json = "";
  37. json += " {" + _lineend;
  38. if (NUMBERS[i]->ReturnValue.length() > 0) {
  39. json += " \"value\": \"" + NUMBERS[i]->ReturnValue + "\"," + _lineend;
  40. }
  41. else {
  42. json += " \"value\": \"\"," + _lineend;
  43. }
  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. }
  50. else {
  51. json += " \"rate\": \"\"," + _lineend;
  52. }
  53. json += " \"timestamp\": \"" + NUMBERS[i]->timeStamp + "\"" + _lineend;
  54. json += " }" + _lineend;
  55. return json;
  56. }
  57. string ClassFlowPostProcessing::GetPreValue(std::string _number) {
  58. std::string result;
  59. int index = -1;
  60. if (_number == "") {
  61. _number = "default";
  62. }
  63. for (int i = 0; i < NUMBERS.size(); ++i) {
  64. if (NUMBERS[i]->name == _number) {
  65. index = i;
  66. }
  67. }
  68. if (index == -1) {
  69. return std::string("");
  70. }
  71. result = RundeOutput(NUMBERS[index]->PreValue, NUMBERS[index]->Nachkomma);
  72. return result;
  73. }
  74. bool ClassFlowPostProcessing::SetPreValue(double _newvalue, string _numbers, bool _extern) {
  75. //ESP_LOGD(TAG, "SetPrevalue: %f, %s", zw, _numbers.c_str());
  76. for (int j = 0; j < NUMBERS.size(); ++j) {
  77. //ESP_LOGD(TAG, "Number %d, %s", j, NUMBERS[j]->name.c_str());
  78. if (NUMBERS[j]->name == _numbers) {
  79. if (_newvalue >= 0) {
  80. // if new value posivive, use provided value to preset PreValue
  81. NUMBERS[j]->PreValue = _newvalue;
  82. }
  83. else {
  84. // if new value negative, use last raw value to preset PreValue
  85. char* p;
  86. double ReturnRawValueAsDouble = strtod(NUMBERS[j]->ReturnRawValue.c_str(), &p);
  87. if (ReturnRawValueAsDouble == 0) {
  88. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "SetPreValue: RawValue not a valid value for further processing: " + NUMBERS[j]->ReturnRawValue);
  89. return false;
  90. }
  91. NUMBERS[j]->PreValue = ReturnRawValueAsDouble;
  92. }
  93. NUMBERS[j]->ReturnPreValue = std::to_string(NUMBERS[j]->PreValue);
  94. NUMBERS[j]->PreValueOkay = true;
  95. if (_extern) {
  96. time(&(NUMBERS[j]->timeStampLastPreValue));
  97. localtime(&(NUMBERS[j]->timeStampLastPreValue));
  98. }
  99. //ESP_LOGD(TAG, "Found %d! - set to %.8f", j, NUMBERS[j]->PreValue);
  100. UpdatePreValueINI = true; // Only update prevalue file if a new value is set
  101. SavePreValue();
  102. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SetPreValue: PreValue for " + NUMBERS[j]->name + " set to " + std::to_string(NUMBERS[j]->PreValue));
  103. return true;
  104. }
  105. }
  106. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "SetPreValue: Numbersname not found or not valid");
  107. return false; // No new value was set (e.g. wrong numbersname, no numbers at all)
  108. }
  109. bool ClassFlowPostProcessing::LoadPreValue(void) {
  110. std::vector<string> splitted;
  111. FILE* pFile;
  112. char zw[1024];
  113. string zwtime, zwvalue, name;
  114. bool _done = false;
  115. UpdatePreValueINI = false; // Conversion to the new format
  116. pFile = fopen(FilePreValue.c_str(), "r");
  117. if (pFile == NULL) {
  118. return false;
  119. }
  120. // Makes sure that an empty file is treated as such.
  121. zw[0] = '\0';
  122. fgets(zw, 1024, pFile);
  123. ESP_LOGD(TAG, "Read line Prevalue.ini: %s", zw);
  124. zwtime = trim(std::string(zw));
  125. if (zwtime.length() == 0) {
  126. return false;
  127. }
  128. splitted = HelperZerlegeZeile(zwtime, "\t");
  129. // Conversion to the new format
  130. if (splitted.size() > 1) {
  131. while ((splitted.size() > 1) && !_done) {
  132. name = trim(splitted[0]);
  133. zwtime = trim(splitted[1]);
  134. zwvalue = trim(splitted[2]);
  135. for (int j = 0; j < NUMBERS.size(); ++j) {
  136. if (NUMBERS[j]->name == name) {
  137. NUMBERS[j]->PreValue = stod(zwvalue.c_str());
  138. 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).
  139. time_t tStart;
  140. int yy, month, dd, hh, mm, ss;
  141. struct tm whenStart;
  142. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  143. whenStart.tm_year = yy - 1900;
  144. whenStart.tm_mon = month - 1;
  145. whenStart.tm_mday = dd;
  146. whenStart.tm_hour = hh;
  147. whenStart.tm_min = mm;
  148. whenStart.tm_sec = ss;
  149. whenStart.tm_isdst = -1;
  150. NUMBERS[j]->timeStampLastPreValue = mktime(&whenStart);
  151. time(&tStart);
  152. localtime(&tStart);
  153. double difference = difftime(tStart, NUMBERS[j]->timeStampLastPreValue);
  154. difference /= 60;
  155. if (difference > PreValueAgeStartup) {
  156. NUMBERS[j]->PreValueOkay = false;
  157. }
  158. else {
  159. NUMBERS[j]->PreValueOkay = true;
  160. }
  161. }
  162. }
  163. if (!fgets(zw, 1024, pFile)) {
  164. _done = true;
  165. }
  166. else {
  167. ESP_LOGD(TAG, "Read line Prevalue.ini: %s", zw);
  168. splitted = HelperZerlegeZeile(trim(std::string(zw)), "\t");
  169. if (splitted.size() > 1) {
  170. name = trim(splitted[0]);
  171. zwtime = trim(splitted[1]);
  172. zwvalue = trim(splitted[2]);
  173. }
  174. }
  175. }
  176. fclose(pFile);
  177. }
  178. else {
  179. // Old Format
  180. fgets(zw, 1024, pFile);
  181. fclose(pFile);
  182. ESP_LOGD(TAG, "%s", zw);
  183. zwvalue = trim(std::string(zw));
  184. NUMBERS[0]->PreValue = stod(zwvalue.c_str());
  185. time_t tStart;
  186. int yy, month, dd, hh, mm, ss;
  187. struct tm whenStart;
  188. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  189. whenStart.tm_year = yy - 1900;
  190. whenStart.tm_mon = month - 1;
  191. whenStart.tm_mday = dd;
  192. whenStart.tm_hour = hh;
  193. whenStart.tm_min = mm;
  194. whenStart.tm_sec = ss;
  195. whenStart.tm_isdst = -1;
  196. 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);
  197. NUMBERS[0]->timeStampLastPreValue = mktime(&whenStart);
  198. time(&tStart);
  199. localtime(&tStart);
  200. double difference = difftime(tStart, NUMBERS[0]->timeStampLastPreValue);
  201. difference /= 60;
  202. if (difference > PreValueAgeStartup) {
  203. return false;
  204. }
  205. NUMBERS[0]->Value = NUMBERS[0]->PreValue;
  206. NUMBERS[0]->ReturnValue = to_string(NUMBERS[0]->Value);
  207. if (NUMBERS[0]->digit_roi || NUMBERS[0]->analog_roi) {
  208. NUMBERS[0]->ReturnValue = RundeOutput(NUMBERS[0]->Value, NUMBERS[0]->Nachkomma);
  209. }
  210. UpdatePreValueINI = true; // Conversion to the new format
  211. SavePreValue();
  212. }
  213. return true;
  214. }
  215. void ClassFlowPostProcessing::SavePreValue() {
  216. FILE* pFile;
  217. string _zw;
  218. // PreValues unchanged --> File does not have to be rewritten
  219. if (!UpdatePreValueINI) {
  220. return;
  221. }
  222. pFile = fopen(FilePreValue.c_str(), "w");
  223. for (int j = 0; j < NUMBERS.size(); ++j) {
  224. char buffer[80];
  225. struct tm* timeinfo = localtime(&NUMBERS[j]->timeStampLastPreValue);
  226. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  227. NUMBERS[j]->timeStamp = std::string(buffer);
  228. NUMBERS[j]->timeStampTimeUTC = NUMBERS[j]->timeStampLastPreValue;
  229. // ESP_LOGD(TAG, "SaverPreValue %d, Value: %f, Nachkomma %d", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  230. _zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n";
  231. ESP_LOGD(TAG, "Write PreValue line: %s", _zw.c_str());
  232. if (pFile) {
  233. fputs(_zw.c_str(), pFile);
  234. }
  235. }
  236. UpdatePreValueINI = false;
  237. fclose(pFile);
  238. }
  239. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc, ClassFlowCNNGeneral *_analog, ClassFlowCNNGeneral *_digit) {
  240. PreValueUse = false;
  241. PreValueAgeStartup = 30;
  242. ErrorMessage = false;
  243. ListFlowControll = NULL;
  244. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  245. ListFlowControll = lfc;
  246. flowTakeImage = NULL;
  247. UpdatePreValueINI = false;
  248. IgnoreLeadingNaN = false;
  249. flowAnalog = _analog;
  250. flowDigit = _digit;
  251. for (int i = 0; i < ListFlowControll->size(); ++i) {
  252. if (((*ListFlowControll)[i])->name().compare("ClassFlowTakeImage") == 0) {
  253. flowTakeImage = (ClassFlowTakeImage*) (*ListFlowControll)[i];
  254. }
  255. }
  256. }
  257. void ClassFlowPostProcessing::handleDecimalExtendedResolution(string _decsep, string _value) {
  258. string _digit, _decpos;
  259. int _pospunkt = _decsep.find_first_of(".");
  260. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  261. if (_pospunkt > -1) {
  262. _digit = _decsep.substr(0, _pospunkt);
  263. }
  264. else {
  265. _digit = "default";
  266. }
  267. for (int j = 0; j < NUMBERS.size(); ++j) {
  268. bool _zwdc = alphanumericToBoolean(_value);
  269. // Set to default first (if nothing else is set)
  270. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  271. NUMBERS[j]->isExtendedResolution = _zwdc;
  272. }
  273. }
  274. }
  275. void ClassFlowPostProcessing::handleDecimalSeparator(string _decsep, string _value) {
  276. string _digit, _decpos;
  277. int _pospunkt = _decsep.find_first_of(".");
  278. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  279. if (_pospunkt > -1) {
  280. _digit = _decsep.substr(0, _pospunkt);
  281. }
  282. else {
  283. _digit = "default";
  284. }
  285. for (int j = 0; j < NUMBERS.size(); ++j) {
  286. int _zwdc = 0;
  287. if (isStringNumeric(_value)) {
  288. _zwdc = std::stoi(_value);
  289. }
  290. // Set to default first (if nothing else is set)
  291. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  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::handleAnalogToDigitTransitionStart(string _decsep, string _value) {
  299. string _digit, _decpos;
  300. int _pospunkt = _decsep.find_first_of(".");
  301. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  302. if (_pospunkt > -1) {
  303. _digit = _decsep.substr(0, _pospunkt);
  304. }
  305. else {
  306. _digit = "default";
  307. }
  308. for (int j = 0; j < NUMBERS.size(); ++j) {
  309. float _zwdc = 9.2;
  310. if (isStringNumeric(_value)) {
  311. _zwdc = std::stof(_value);
  312. }
  313. // Set to default first (if nothing else is set)
  314. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  315. NUMBERS[j]->AnalogToDigitTransitionStart = _zwdc;
  316. }
  317. }
  318. }
  319. void ClassFlowPostProcessing::handleAllowNegativeRate(string _decsep, string _value) {
  320. string _digit, _decpos;
  321. int _pospunkt = _decsep.find_first_of(".");
  322. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  323. if (_pospunkt > -1) {
  324. _digit = _decsep.substr(0, _pospunkt);
  325. }
  326. else {
  327. _digit = "default";
  328. }
  329. for (int j = 0; j < NUMBERS.size(); ++j) {
  330. bool _zwdc = alphanumericToBoolean(_value);
  331. // Set to default first (if nothing else is set)
  332. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  333. NUMBERS[j]->AllowNegativeRates = _zwdc;
  334. }
  335. }
  336. }
  337. void ClassFlowPostProcessing::handleMaxRateType(string _decsep, string _value) {
  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. }
  344. else {
  345. _digit = "default";
  346. }
  347. for (int j = 0; j < NUMBERS.size(); ++j) {
  348. t_RateType _zwdc = AbsoluteChange;
  349. if (toUpper(_value) == "RATECHANGE") {
  350. _zwdc = RateChange;
  351. }
  352. // Set to default first (if nothing else is set)
  353. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  354. NUMBERS[j]->MaxRateType = _zwdc;
  355. }
  356. }
  357. }
  358. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value) {
  359. string _digit, _decpos;
  360. int _pospunkt = _decsep.find_first_of(".");
  361. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  362. if (_pospunkt > -1) {
  363. _digit = _decsep.substr(0, _pospunkt);
  364. }
  365. else {
  366. _digit = "default";
  367. }
  368. for (int j = 0; j < NUMBERS.size(); ++j) {
  369. float _zwdc = 1;
  370. if (isStringNumeric(_value)) {
  371. _zwdc = std::stof(_value);
  372. }
  373. // Set to default first (if nothing else is set)
  374. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  375. NUMBERS[j]->useMaxRateValue = true;
  376. NUMBERS[j]->MaxRateValue = _zwdc;
  377. }
  378. }
  379. }
  380. void ClassFlowPostProcessing::handleChangeRateThreshold(string _decsep, string _value) {
  381. string _digit, _decpos;
  382. int _pospunkt = _decsep.find_first_of(".");
  383. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  384. if (_pospunkt > -1) {
  385. _digit = _decsep.substr(0, _pospunkt);
  386. }
  387. else {
  388. _digit = "default";
  389. }
  390. for (int j = 0; j < NUMBERS.size(); ++j) {
  391. int _zwdc = 2;
  392. if (isStringNumeric(_value)) {
  393. _zwdc = std::stof(_value);
  394. }
  395. // Set to default first (if nothing else is set)
  396. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  397. NUMBERS[j]->ChangeRateThreshold = _zwdc;
  398. }
  399. }
  400. }
  401. /*
  402. void ClassFlowPostProcessing::handlecheckDigitIncreaseConsistency(std::string _decsep, std::string _value)
  403. {
  404. std::string _digit;
  405. int _pospunkt = _decsep.find_first_of(".");
  406. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  407. if (_pospunkt > -1) {
  408. _digit = _decsep.substr(0, _pospunkt);
  409. }
  410. else {
  411. _digit = "default";
  412. }
  413. for (int j = 0; j < NUMBERS.size(); ++j) {
  414. bool _rt = alphanumericToBoolean(_value);
  415. // Set to default first (if nothing else is set)
  416. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  417. NUMBERS[j]->checkDigitIncreaseConsistency = _rt;
  418. }
  419. }
  420. }
  421. */
  422. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph) {
  423. std::vector<string> splitted;
  424. int _n;
  425. aktparamgraph = trim(aktparamgraph);
  426. if (aktparamgraph.size() == 0) {
  427. if (!this->GetNextParagraph(pfile, aktparamgraph)) {
  428. return false;
  429. }
  430. }
  431. // Paragraph does not fit PostProcessing
  432. if (aktparamgraph.compare("[PostProcessing]") != 0) {
  433. return false;
  434. }
  435. InitNUMBERS();
  436. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) {
  437. splitted = ZerlegeZeile(aktparamgraph);
  438. std::string _param = GetParameterName(splitted[0]);
  439. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1)) {
  440. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  441. }
  442. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1)) {
  443. handleDecimalSeparator(splitted[0], splitted[1]);
  444. }
  445. if ((toUpper(_param) == "AnalogToDigitTransitionStart") && (splitted.size() > 1)) {
  446. handleAnalogToDigitTransitionStart(splitted[0], splitted[1]);
  447. }
  448. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1)) {
  449. handleMaxRateValue(splitted[0], splitted[1]);
  450. }
  451. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1)) {
  452. handleMaxRateType(splitted[0], splitted[1]);
  453. }
  454. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1)) {
  455. PreValueUse = alphanumericToBoolean(splitted[1]);
  456. }
  457. if ((toUpper(_param) == "CHANGERATETHRESHOLD") && (splitted.size() > 1)) {
  458. handleChangeRateThreshold(splitted[0], splitted[1]);
  459. }
  460. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1)) {
  461. // handlecheckDigitIncreaseConsistency(splitted[0], splitted[1]);
  462. if (alphanumericToBoolean(splitted[1])) {
  463. for (_n = 0; _n < NUMBERS.size(); ++_n) {
  464. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  465. }
  466. }
  467. }
  468. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1)) {
  469. handleAllowNegativeRate(splitted[0], splitted[1]);
  470. }
  471. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1)) {
  472. ErrorMessage = alphanumericToBoolean(splitted[1]);
  473. }
  474. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1)) {
  475. IgnoreLeadingNaN = alphanumericToBoolean(splitted[1]);
  476. }
  477. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1)) {
  478. if (isStringNumeric(splitted[1])) {
  479. PreValueAgeStartup = std::stoi(splitted[1]);
  480. }
  481. }
  482. }
  483. if (PreValueUse) {
  484. return LoadPreValue();
  485. }
  486. return true;
  487. }
  488. void ClassFlowPostProcessing::InitNUMBERS() {
  489. int anzDIGIT = 0;
  490. int anzANALOG = 0;
  491. std::vector<std::string> name_numbers;
  492. if (flowDigit) {
  493. anzDIGIT = flowDigit->getNumberGENERAL();
  494. flowDigit->UpdateNameNumbers(&name_numbers);
  495. }
  496. if (flowAnalog) {
  497. anzANALOG = flowAnalog->getNumberGENERAL();
  498. flowAnalog->UpdateNameNumbers(&name_numbers);
  499. }
  500. ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
  501. for (int _num = 0; _num < name_numbers.size(); ++_num) {
  502. NumberPost *_number = new NumberPost;
  503. _number->name = name_numbers[_num];
  504. _number->digit_roi = NULL;
  505. if (flowDigit) {
  506. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  507. }
  508. if (_number->digit_roi) {
  509. _number->AnzahlDigit = _number->digit_roi->ROI.size();
  510. }
  511. else {
  512. _number->AnzahlDigit = 0;
  513. }
  514. _number->analog_roi = NULL;
  515. if (flowAnalog) {
  516. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  517. }
  518. if (_number->analog_roi) {
  519. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  520. }
  521. else {
  522. _number->AnzahlAnalog = 0;
  523. }
  524. _number->FlowRateAct = 0; // m3 / min
  525. _number->PreValueOkay = false;
  526. _number->AllowNegativeRates = false;
  527. _number->MaxRateValue = 0.1;
  528. _number->MaxRateType = AbsoluteChange;
  529. _number->useMaxRateValue = false;
  530. _number->checkDigitIncreaseConsistency = false;
  531. _number->DecimalShift = 0;
  532. _number->DecimalShiftInitial = 0;
  533. _number->isExtendedResolution = false;
  534. _number->AnalogToDigitTransitionStart=9.2;
  535. _number->ChangeRateThreshold = 2;
  536. _number->Value = 0; // last value read out, incl. corrections
  537. _number->ReturnValue = ""; // corrected return value, possibly with error message
  538. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  539. _number->PreValue = 0; // last value read out well
  540. _number->ReturnPreValue = "";
  541. _number->ErrorMessageText = ""; // Error message for consistency check
  542. _number->Nachkomma = _number->AnzahlAnalog;
  543. NUMBERS.push_back(_number);
  544. }
  545. for (int i = 0; i < NUMBERS.size(); ++i) {
  546. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigit, NUMBERS[i]->AnzahlAnalog);
  547. }
  548. }
  549. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift) {
  550. if (_decShift == 0) {
  551. return in;
  552. }
  553. int _pos_dec_org, _pos_dec_neu;
  554. _pos_dec_org = findDelimiterPos(in, ".");
  555. if (_pos_dec_org == std::string::npos) {
  556. _pos_dec_org = in.length();
  557. }
  558. else {
  559. in = in.erase(_pos_dec_org, 1);
  560. }
  561. _pos_dec_neu = _pos_dec_org + _decShift;
  562. // comma is before the first digit
  563. if (_pos_dec_neu <= 0) {
  564. for (int i = 0; i > _pos_dec_neu; --i) {
  565. in = in.insert(0, "0");
  566. }
  567. in = "0." + in;
  568. return in;
  569. }
  570. // Comma should be after string (123 --> 1230)
  571. if (_pos_dec_neu > in.length()) {
  572. for (int i = in.length(); i < _pos_dec_neu; ++i) {
  573. in = in.insert(in.length(), "0");
  574. }
  575. return in;
  576. }
  577. string zw;
  578. zw = in.substr(0, _pos_dec_neu);
  579. zw = zw + ".";
  580. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  581. return zw;
  582. }
  583. bool ClassFlowPostProcessing::doFlow(string zwtime) {
  584. string result = "";
  585. string digit = "";
  586. string analog = "";
  587. string zwvalue;
  588. string zw;
  589. time_t imagetime = 0;
  590. string rohwert;
  591. // Update decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  592. imagetime = flowTakeImage->getTimeImageTaken();
  593. if (imagetime == 0) {
  594. time(&imagetime);
  595. }
  596. struct tm* timeinfo;
  597. timeinfo = localtime(&imagetime);
  598. char strftime_buf[64];
  599. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  600. zwtime = std::string(strftime_buf);
  601. ESP_LOGD(TAG, "Quantity NUMBERS: %d", NUMBERS.size());
  602. for (int j = 0; j < NUMBERS.size(); ++j) {
  603. NUMBERS[j]->ReturnRawValue = "";
  604. NUMBERS[j]->ReturnRateValue = "";
  605. NUMBERS[j]->ReturnValue = "";
  606. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(0.0, NUMBERS[j]->Nachkomma); // always reset change absolute
  607. NUMBERS[j]->ErrorMessageText = "";
  608. NUMBERS[j]->Value = -1;
  609. // calculate time difference
  610. // double LastValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastValue); // in seconds
  611. double LastPreValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastPreValue); // in seconds
  612. UpdateNachkommaDecimalShift();
  613. int previous_value = -1;
  614. if (NUMBERS[j]->analog_roi) {
  615. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  616. if (NUMBERS[j]->ReturnRawValue.length() > 0) {
  617. char zw = NUMBERS[j]->ReturnRawValue[0];
  618. if (zw >= 48 && zw <=57) {
  619. previous_value = zw - 48;
  620. }
  621. }
  622. }
  623. #ifdef SERIAL_DEBUG
  624. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  625. #endif
  626. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  627. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  628. }
  629. if (NUMBERS[j]->digit_roi) {
  630. if (NUMBERS[j]->analog_roi) {
  631. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogToDigitTransitionStart) + NUMBERS[j]->ReturnRawValue;
  632. }
  633. else {
  634. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  635. }
  636. }
  637. #ifdef SERIAL_DEBUG
  638. ESP_LOGD(TAG, "After digit->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  639. #endif
  640. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  641. #ifdef SERIAL_DEBUG
  642. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  643. #endif
  644. if (IgnoreLeadingNaN) {
  645. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N')) {
  646. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  647. }
  648. }
  649. #ifdef SERIAL_DEBUG
  650. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  651. #endif
  652. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  653. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos) {
  654. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  655. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  656. }
  657. else {
  658. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  659. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  660. NUMBERS[j]->ReturnValue = "";
  661. NUMBERS[j]->timeStampLastValue = imagetime;
  662. WriteDataLog(j);
  663. continue; // there is no number because there is still an N.
  664. }
  665. }
  666. #ifdef SERIAL_DEBUG
  667. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  668. #endif
  669. // Delete leading zeros (unless there is only one 0 left)
  670. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0')) {
  671. NUMBERS[j]->ReturnValue.erase(0, 1);
  672. }
  673. #ifdef SERIAL_DEBUG
  674. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  675. #endif
  676. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  677. #ifdef SERIAL_DEBUG
  678. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  679. #endif
  680. if (NUMBERS[j]->checkDigitIncreaseConsistency) {
  681. if (flowDigit) {
  682. if (flowDigit->getCNNType() != Digit) {
  683. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digit Classification)");
  684. }
  685. else {
  686. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  687. }
  688. }
  689. else {
  690. #ifdef SERIAL_DEBUG
  691. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digit numbers defined!");
  692. #endif
  693. }
  694. }
  695. #ifdef SERIAL_DEBUG
  696. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  697. #endif
  698. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  699. if (NUMBERS[j]->Nachkomma > 0) {
  700. double _difference1 = (NUMBERS[j]->PreValue - (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  701. double _difference2 = (NUMBERS[j]->PreValue + (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  702. if ((NUMBERS[j]->Value >= _difference1) && (NUMBERS[j]->Value <= _difference2)) {
  703. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  704. NUMBERS[j]->ReturnValue = std::to_string(NUMBERS[j]->PreValue);
  705. }
  706. }
  707. if ((!NUMBERS[j]->AllowNegativeRates) && (NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  708. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handleAllowNegativeRate for device: " + NUMBERS[j]->name);
  709. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  710. // more debug if extended resolution is on, see #2447
  711. if (NUMBERS[j]->isExtendedResolution) {
  712. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Neg: value=" + std::to_string(NUMBERS[j]->Value)
  713. + ", preValue=" + std::to_string(NUMBERS[j]->PreValue)
  714. + ", preToll=" + std::to_string(NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))));
  715. }
  716. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  717. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  718. NUMBERS[j]->ReturnValue = "";
  719. NUMBERS[j]->timeStampLastValue = imagetime;
  720. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  721. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  722. WriteDataLog(j);
  723. continue;
  724. }
  725. }
  726. #ifdef SERIAL_DEBUG
  727. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  728. #endif
  729. // LastValueTimeDifference = LastValueTimeDifference / 60; // in minutes
  730. LastPreValueTimeDifference = LastPreValueTimeDifference / 60; // in minutes
  731. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / LastPreValueTimeDifference;
  732. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  733. if ((NUMBERS[j]->useMaxRateValue) && (NUMBERS[j]->Value != NUMBERS[j]->PreValue)) {
  734. double _ratedifference;
  735. if (NUMBERS[j]->MaxRateType == RateChange) {
  736. _ratedifference = NUMBERS[j]->FlowRateAct;
  737. }
  738. else {
  739. // TODO:
  740. // Since I don't know if this is desired, I'll comment it out first.
  741. // int roundDifference = (int)(round(LastPreValueTimeDifference / LastValueTimeDifference)); // calculate how many rounds have passed since NUMBERS[j]->timeLastPreValue was set
  742. // _ratedifference = ((NUMBERS[j]->Value - NUMBERS[j]->PreValue) / ((int)(round(LastPreValueTimeDifference / LastValueTimeDifference)))); // Difference per round, as a safeguard in case a reading error(Neg. Rate - Read: or Rate too high - Read:) occurs in the meantime
  743. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  744. }
  745. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue)) {
  746. 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);
  747. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  748. NUMBERS[j]->ReturnValue = "";
  749. NUMBERS[j]->ReturnRateValue = "";
  750. NUMBERS[j]->timeStampLastValue = imagetime;
  751. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  752. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  753. WriteDataLog(j);
  754. continue;
  755. }
  756. }
  757. #ifdef SERIAL_DEBUG
  758. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  759. #endif
  760. }
  761. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  762. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  763. NUMBERS[j]->PreValueOkay = true;
  764. NUMBERS[j]->timeStampLastValue = imagetime;
  765. NUMBERS[j]->timeStampLastPreValue = imagetime;
  766. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  767. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  768. NUMBERS[j]->ErrorMessageText = "no error";
  769. UpdatePreValueINI = true;
  770. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  771. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  772. WriteDataLog(j);
  773. }
  774. SavePreValue();
  775. return true;
  776. }
  777. void ClassFlowPostProcessing::WriteDataLog(int _index) {
  778. if (!LogFile.GetDataLogToSD()) {
  779. return;
  780. }
  781. string analog = "";
  782. string digit = "";
  783. string timezw = "";
  784. char buffer[80];
  785. struct tm* timeinfo = localtime(&NUMBERS[_index]->timeStampLastValue);
  786. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  787. timezw = std::string(buffer);
  788. if (flowAnalog) {
  789. analog = flowAnalog->getReadoutRawString(_index);
  790. }
  791. if (flowDigit) {
  792. digit = flowDigit->getReadoutRawString(_index);
  793. }
  794. LogFile.WriteToData(timezw, NUMBERS[_index]->name, NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  795. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute, NUMBERS[_index]->ErrorMessageText, digit, analog);
  796. ESP_LOGD(TAG, "WriteDataLog: %s, %s, %s, %s, %s", NUMBERS[_index]->ReturnRawValue.c_str(), NUMBERS[_index]->ReturnValue.c_str(), NUMBERS[_index]->ErrorMessageText.c_str(), digit.c_str(), analog.c_str());
  797. }
  798. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift() {
  799. for (int j = 0; j < NUMBERS.size(); ++j) {
  800. // There are only digits
  801. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) {
  802. // ESP_LOGD(TAG, "Nurdigit");
  803. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  804. // Extended resolution is on and should also be used for this digit.
  805. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) {
  806. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  807. }
  808. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  809. }
  810. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  811. // ESP_LOGD(TAG, "Nur analog");
  812. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  813. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  814. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  815. }
  816. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  817. }
  818. // digit + analog
  819. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  820. // ESP_LOGD(TAG, "Nur digit + analog");
  821. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  822. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  823. // Extended resolution is on and should also be used for this digit.
  824. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  825. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  826. }
  827. }
  828. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  829. }
  830. }
  831. string ClassFlowPostProcessing::getReadout(int _number) {
  832. return NUMBERS[_number]->ReturnValue;
  833. }
  834. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number) {
  835. if (_rawValue) {
  836. return NUMBERS[_number]->ReturnRawValue;
  837. }
  838. if (_noerror) {
  839. return NUMBERS[_number]->ReturnValue;
  840. }
  841. return NUMBERS[_number]->ReturnValue;
  842. }
  843. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue) {
  844. int posN, posPunkt;
  845. int pot, ziffer;
  846. float zw;
  847. posN = findDelimiterPos(input, "N");
  848. posPunkt = findDelimiterPos(input, ".");
  849. if (posPunkt == std::string::npos) {
  850. posPunkt = input.length();
  851. }
  852. while (posN != std::string::npos) {
  853. if (posN < posPunkt) {
  854. pot = posPunkt - posN - 1;
  855. }
  856. else {
  857. pot = posPunkt - posN;
  858. }
  859. zw =_prevalue / pow(10, pot);
  860. ziffer = ((int) zw) % 10;
  861. input[posN] = ziffer + 48;
  862. posN = findDelimiterPos(input, "N");
  863. }
  864. return input;
  865. }
  866. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
  867. int aktdigit, olddigit;
  868. int aktdigit_before, olddigit_before;
  869. int pot, pot_max;
  870. float zw;
  871. bool no_nulldurchgang = false;
  872. pot = _decilamshift;
  873. // if there are no analogue values, the last one cannot be evaluated
  874. if (!_isanalog) {
  875. pot++;
  876. }
  877. #ifdef SERIAL_DEBUG
  878. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  879. #endif
  880. pot_max = ((int) log10(input)) + 1;
  881. while (pot <= pot_max) {
  882. zw = input / pow(10, pot-1);
  883. aktdigit_before = ((int) zw) % 10;
  884. zw = _preValue / pow(10, pot-1);
  885. olddigit_before = ((int) zw) % 10;
  886. zw = input / pow(10, pot);
  887. aktdigit = ((int) zw) % 10;
  888. zw = _preValue / pow(10, pot);
  889. olddigit = ((int) zw) % 10;
  890. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  891. if (no_nulldurchgang) {
  892. if (aktdigit != olddigit) {
  893. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  894. }
  895. }
  896. else {
  897. // despite zero crossing, digit was not incremented --> add 1
  898. if (aktdigit == olddigit) {
  899. input = input + ((float) (1)) * pow(10, pot); // add 1 at the point
  900. }
  901. }
  902. #ifdef SERIAL_DEBUG
  903. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  904. #endif
  905. pot++;
  906. }
  907. return input;
  908. }
  909. string ClassFlowPostProcessing::getReadoutRate(int _number) {
  910. return std::to_string(NUMBERS[_number]->FlowRateAct);
  911. }
  912. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number) {
  913. return NUMBERS[_number]->timeStamp;
  914. }
  915. string ClassFlowPostProcessing::getReadoutError(int _number) {
  916. return NUMBERS[_number]->ErrorMessageText;
  917. }