ClassFlowPostProcessing.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. flowAnalog = _analog;
  249. flowDigit = _digit;
  250. for (int i = 0; i < ListFlowControll->size(); ++i) {
  251. if (((*ListFlowControll)[i])->name().compare("ClassFlowTakeImage") == 0) {
  252. flowTakeImage = (ClassFlowTakeImage*) (*ListFlowControll)[i];
  253. }
  254. }
  255. }
  256. void ClassFlowPostProcessing::handleDecimalExtendedResolution(string _decsep, string _value) {
  257. string _digit, _decpos;
  258. int _pospunkt = _decsep.find_first_of(".");
  259. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  260. if (_pospunkt > -1) {
  261. _digit = _decsep.substr(0, _pospunkt);
  262. }
  263. else {
  264. _digit = "default";
  265. }
  266. for (int j = 0; j < NUMBERS.size(); ++j) {
  267. bool _zwdc = alphanumericToBoolean(_value);
  268. // Set to default first (if nothing else is set)
  269. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  270. NUMBERS[j]->isExtendedResolution = _zwdc;
  271. }
  272. }
  273. }
  274. void ClassFlowPostProcessing::handleDecimalSeparator(string _decsep, string _value) {
  275. string _digit, _decpos;
  276. int _pospunkt = _decsep.find_first_of(".");
  277. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  278. if (_pospunkt > -1) {
  279. _digit = _decsep.substr(0, _pospunkt);
  280. }
  281. else {
  282. _digit = "default";
  283. }
  284. for (int j = 0; j < NUMBERS.size(); ++j) {
  285. int _zwdc = 0;
  286. if (isStringNumeric(_value)) {
  287. _zwdc = std::stoi(_value);
  288. }
  289. // Set to default first (if nothing else is set)
  290. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  291. NUMBERS[j]->DecimalShift = _zwdc;
  292. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  293. }
  294. NUMBERS[j]->Nachkomma = NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift;
  295. }
  296. }
  297. void ClassFlowPostProcessing::handleAnalogToDigitTransitionStart(string _decsep, string _value) {
  298. string _digit, _decpos;
  299. int _pospunkt = _decsep.find_first_of(".");
  300. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  301. if (_pospunkt > -1) {
  302. _digit = _decsep.substr(0, _pospunkt);
  303. }
  304. else {
  305. _digit = "default";
  306. }
  307. for (int j = 0; j < NUMBERS.size(); ++j) {
  308. float _zwdc = 9.2;
  309. if (isStringNumeric(_value)) {
  310. _zwdc = std::stof(_value);
  311. }
  312. // Set to default first (if nothing else is set)
  313. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  314. NUMBERS[j]->AnalogToDigitTransitionStart = _zwdc;
  315. }
  316. }
  317. }
  318. void ClassFlowPostProcessing::handleAllowNegativeRate(string _decsep, string _value) {
  319. string _digit, _decpos;
  320. int _pospunkt = _decsep.find_first_of(".");
  321. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  322. if (_pospunkt > -1) {
  323. _digit = _decsep.substr(0, _pospunkt);
  324. }
  325. else {
  326. _digit = "default";
  327. }
  328. for (int j = 0; j < NUMBERS.size(); ++j) {
  329. bool _zwdc = alphanumericToBoolean(_value);
  330. // Set to default first (if nothing else is set)
  331. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  332. NUMBERS[j]->AllowNegativeRates = _zwdc;
  333. }
  334. }
  335. }
  336. void ClassFlowPostProcessing::handleIgnoreLeadingNaN(string _decsep, string _value) {
  337. string _digit, _decpos;
  338. int _pospunkt = _decsep.find_first_of(".");
  339. if (_pospunkt > -1) {
  340. _digit = _decsep.substr(0, _pospunkt);
  341. }
  342. else {
  343. _digit = "default";
  344. }
  345. for (int j = 0; j < NUMBERS.size(); ++j) {
  346. bool _zwdc = alphanumericToBoolean(_value);
  347. // Set to default first (if nothing else is set)
  348. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  349. NUMBERS[j]->IgnoreLeadingNaN = _zwdc;
  350. }
  351. }
  352. }
  353. void ClassFlowPostProcessing::handleMaxRateType(string _decsep, string _value) {
  354. string _digit, _decpos;
  355. int _pospunkt = _decsep.find_first_of(".");
  356. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  357. if (_pospunkt > -1) {
  358. _digit = _decsep.substr(0, _pospunkt);
  359. }
  360. else {
  361. _digit = "default";
  362. }
  363. for (int j = 0; j < NUMBERS.size(); ++j) {
  364. t_RateType _zwdc = AbsoluteChange;
  365. if (toUpper(_value) == "RATECHANGE") {
  366. _zwdc = RateChange;
  367. }
  368. // Set to default first (if nothing else is set)
  369. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  370. NUMBERS[j]->MaxRateType = _zwdc;
  371. }
  372. }
  373. }
  374. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value) {
  375. string _digit, _decpos;
  376. int _pospunkt = _decsep.find_first_of(".");
  377. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  378. if (_pospunkt > -1) {
  379. _digit = _decsep.substr(0, _pospunkt);
  380. }
  381. else {
  382. _digit = "default";
  383. }
  384. for (int j = 0; j < NUMBERS.size(); ++j) {
  385. float _zwdc = 1;
  386. if (isStringNumeric(_value)) {
  387. _zwdc = std::stof(_value);
  388. }
  389. // Set to default first (if nothing else is set)
  390. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  391. NUMBERS[j]->useMaxRateValue = true;
  392. NUMBERS[j]->MaxRateValue = _zwdc;
  393. }
  394. }
  395. }
  396. void ClassFlowPostProcessing::handleChangeRateThreshold(string _decsep, string _value) {
  397. string _digit, _decpos;
  398. int _pospunkt = _decsep.find_first_of(".");
  399. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  400. if (_pospunkt > -1) {
  401. _digit = _decsep.substr(0, _pospunkt);
  402. }
  403. else {
  404. _digit = "default";
  405. }
  406. for (int j = 0; j < NUMBERS.size(); ++j) {
  407. int _zwdc = 2;
  408. if (isStringNumeric(_value)) {
  409. _zwdc = std::stof(_value);
  410. }
  411. // Set to default first (if nothing else is set)
  412. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  413. NUMBERS[j]->ChangeRateThreshold = _zwdc;
  414. }
  415. }
  416. }
  417. void ClassFlowPostProcessing::handlecheckDigitIncreaseConsistency(std::string _decsep, std::string _value)
  418. {
  419. std::string _digit;
  420. int _pospunkt = _decsep.find_first_of(".");
  421. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  422. if (_pospunkt > -1) {
  423. _digit = _decsep.substr(0, _pospunkt);
  424. }
  425. else {
  426. _digit = "default";
  427. }
  428. for (int j = 0; j < NUMBERS.size(); ++j) {
  429. bool _rt = alphanumericToBoolean(_value);
  430. // Set to default first (if nothing else is set)
  431. if ((_digit == "default") || (NUMBERS[j]->name == _digit)) {
  432. NUMBERS[j]->checkDigitIncreaseConsistency = _rt;
  433. }
  434. }
  435. }
  436. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph) {
  437. std::vector<string> splitted;
  438. int _n;
  439. aktparamgraph = trim(aktparamgraph);
  440. if (aktparamgraph.size() == 0) {
  441. if (!this->GetNextParagraph(pfile, aktparamgraph)) {
  442. return false;
  443. }
  444. }
  445. // Paragraph does not fit PostProcessing
  446. if (aktparamgraph.compare("[PostProcessing]") != 0) {
  447. return false;
  448. }
  449. InitNUMBERS();
  450. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) {
  451. splitted = ZerlegeZeile(aktparamgraph);
  452. std::string _param = GetParameterName(splitted[0]);
  453. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1)) {
  454. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  455. }
  456. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1)) {
  457. handleDecimalSeparator(splitted[0], splitted[1]);
  458. }
  459. if ((toUpper(_param) == "ANALOGTODIGITTRANSITIONSTART") && (splitted.size() > 1)) {
  460. handleAnalogToDigitTransitionStart(splitted[0], splitted[1]);
  461. }
  462. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1)) {
  463. handleMaxRateValue(splitted[0], splitted[1]);
  464. }
  465. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1)) {
  466. handleMaxRateType(splitted[0], splitted[1]);
  467. }
  468. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1)) {
  469. PreValueUse = alphanumericToBoolean(splitted[1]);
  470. }
  471. if ((toUpper(_param) == "CHANGERATETHRESHOLD") && (splitted.size() > 1)) {
  472. handleChangeRateThreshold(splitted[0], splitted[1]);
  473. }
  474. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1)) {
  475. handlecheckDigitIncreaseConsistency(splitted[0], splitted[1]);
  476. }
  477. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1)) {
  478. handleAllowNegativeRate(splitted[0], splitted[1]);
  479. }
  480. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1)) {
  481. ErrorMessage = alphanumericToBoolean(splitted[1]);
  482. }
  483. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1)) {
  484. handleIgnoreLeadingNaN(splitted[0], splitted[1]);
  485. }
  486. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1)) {
  487. if (isStringNumeric(splitted[1])) {
  488. PreValueAgeStartup = std::stoi(splitted[1]);
  489. }
  490. }
  491. }
  492. if (PreValueUse) {
  493. return LoadPreValue();
  494. }
  495. return true;
  496. }
  497. void ClassFlowPostProcessing::InitNUMBERS() {
  498. int anzDIGIT = 0;
  499. int anzANALOG = 0;
  500. std::vector<std::string> name_numbers;
  501. if (flowDigit) {
  502. anzDIGIT = flowDigit->getNumberGENERAL();
  503. flowDigit->UpdateNameNumbers(&name_numbers);
  504. }
  505. if (flowAnalog) {
  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. NumberPost *_number = new NumberPost;
  512. _number->name = name_numbers[_num];
  513. _number->digit_roi = NULL;
  514. if (flowDigit) {
  515. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  516. }
  517. if (_number->digit_roi) {
  518. _number->AnzahlDigit = _number->digit_roi->ROI.size();
  519. }
  520. else {
  521. _number->AnzahlDigit = 0;
  522. }
  523. _number->analog_roi = NULL;
  524. if (flowAnalog) {
  525. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  526. }
  527. if (_number->analog_roi) {
  528. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  529. }
  530. else {
  531. _number->AnzahlAnalog = 0;
  532. }
  533. _number->FlowRateAct = 0; // m3 / min
  534. _number->PreValueOkay = false;
  535. _number->AllowNegativeRates = false;
  536. _number->IgnoreLeadingNaN = false;
  537. _number->MaxRateValue = 0.1;
  538. _number->MaxRateType = AbsoluteChange;
  539. _number->useMaxRateValue = false;
  540. _number->checkDigitIncreaseConsistency = false;
  541. _number->DecimalShift = 0;
  542. _number->DecimalShiftInitial = 0;
  543. _number->isExtendedResolution = false;
  544. _number->AnalogToDigitTransitionStart=9.2;
  545. _number->ChangeRateThreshold = 2;
  546. _number->Value = 0; // last value read out, incl. corrections
  547. _number->ReturnValue = ""; // corrected return value, possibly with error message
  548. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  549. _number->PreValue = 0; // last value read out well
  550. _number->ReturnPreValue = "";
  551. _number->ErrorMessageText = ""; // Error message for consistency check
  552. _number->Nachkomma = _number->AnzahlAnalog;
  553. NUMBERS.push_back(_number);
  554. }
  555. for (int i = 0; i < NUMBERS.size(); ++i) {
  556. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigit, NUMBERS[i]->AnzahlAnalog);
  557. }
  558. }
  559. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift) {
  560. if (_decShift == 0) {
  561. return in;
  562. }
  563. int _pos_dec_org, _pos_dec_neu;
  564. _pos_dec_org = findDelimiterPos(in, ".");
  565. if (_pos_dec_org == std::string::npos) {
  566. _pos_dec_org = in.length();
  567. }
  568. else {
  569. in = in.erase(_pos_dec_org, 1);
  570. }
  571. _pos_dec_neu = _pos_dec_org + _decShift;
  572. // comma is before the first digit
  573. if (_pos_dec_neu <= 0) {
  574. for (int i = 0; i > _pos_dec_neu; --i) {
  575. in = in.insert(0, "0");
  576. }
  577. in = "0." + in;
  578. return in;
  579. }
  580. // Comma should be after string (123 --> 1230)
  581. if (_pos_dec_neu > in.length()) {
  582. for (int i = in.length(); i < _pos_dec_neu; ++i) {
  583. in = in.insert(in.length(), "0");
  584. }
  585. return in;
  586. }
  587. string zw;
  588. zw = in.substr(0, _pos_dec_neu);
  589. zw = zw + ".";
  590. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  591. return zw;
  592. }
  593. bool ClassFlowPostProcessing::doFlow(string zwtime) {
  594. string result = "";
  595. string digit = "";
  596. string analog = "";
  597. string zwvalue;
  598. string zw;
  599. time_t imagetime = 0;
  600. string rohwert;
  601. // Update decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  602. imagetime = flowTakeImage->getTimeImageTaken();
  603. if (imagetime == 0) {
  604. time(&imagetime);
  605. }
  606. struct tm* timeinfo;
  607. timeinfo = localtime(&imagetime);
  608. char strftime_buf[64];
  609. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  610. zwtime = std::string(strftime_buf);
  611. ESP_LOGD(TAG, "Quantity NUMBERS: %d", NUMBERS.size());
  612. for (int j = 0; j < NUMBERS.size(); ++j) {
  613. NUMBERS[j]->ReturnRawValue = "";
  614. NUMBERS[j]->ReturnRateValue = "";
  615. NUMBERS[j]->ReturnValue = "";
  616. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(0.0, NUMBERS[j]->Nachkomma); // always reset change absolute
  617. NUMBERS[j]->ErrorMessageText = "";
  618. NUMBERS[j]->Value = -1;
  619. // calculate time difference
  620. // double LastValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastValue); // in seconds
  621. double LastPreValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastPreValue); // in seconds
  622. UpdateNachkommaDecimalShift();
  623. int previous_value = -1;
  624. if (NUMBERS[j]->analog_roi) {
  625. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  626. if (NUMBERS[j]->ReturnRawValue.length() > 0) {
  627. char zw = NUMBERS[j]->ReturnRawValue[0];
  628. if (zw >= 48 && zw <=57) {
  629. previous_value = zw - 48;
  630. }
  631. }
  632. }
  633. #ifdef SERIAL_DEBUG
  634. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  635. #endif
  636. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  637. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  638. }
  639. if (NUMBERS[j]->digit_roi) {
  640. if (NUMBERS[j]->analog_roi) {
  641. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogToDigitTransitionStart) + NUMBERS[j]->ReturnRawValue;
  642. }
  643. else {
  644. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  645. }
  646. }
  647. #ifdef SERIAL_DEBUG
  648. ESP_LOGD(TAG, "After digit->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  649. #endif
  650. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  651. #ifdef SERIAL_DEBUG
  652. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  653. #endif
  654. if (NUMBERS[j]->IgnoreLeadingNaN) {
  655. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N')) {
  656. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  657. }
  658. }
  659. #ifdef SERIAL_DEBUG
  660. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  661. #endif
  662. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  663. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos) {
  664. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  665. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  666. }
  667. else {
  668. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  669. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  670. NUMBERS[j]->ReturnValue = "";
  671. NUMBERS[j]->timeStampLastValue = imagetime;
  672. WriteDataLog(j);
  673. continue; // there is no number because there is still an N.
  674. }
  675. }
  676. #ifdef SERIAL_DEBUG
  677. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  678. #endif
  679. // Delete leading zeros (unless there is only one 0 left)
  680. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0')) {
  681. NUMBERS[j]->ReturnValue.erase(0, 1);
  682. }
  683. #ifdef SERIAL_DEBUG
  684. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  685. #endif
  686. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  687. #ifdef SERIAL_DEBUG
  688. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  689. #endif
  690. if (NUMBERS[j]->checkDigitIncreaseConsistency) {
  691. if (flowDigit) {
  692. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  693. }
  694. else {
  695. #ifdef SERIAL_DEBUG
  696. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digit numbers defined!");
  697. #endif
  698. }
  699. }
  700. #ifdef SERIAL_DEBUG
  701. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  702. #endif
  703. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  704. if ((NUMBERS[j]->Nachkomma > 0) && (NUMBERS[j]->ChangeRateThreshold > 0)) {
  705. double _difference1 = (NUMBERS[j]->PreValue - (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  706. double _difference2 = (NUMBERS[j]->PreValue + (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  707. if ((NUMBERS[j]->Value >= _difference1) && (NUMBERS[j]->Value <= _difference2)) {
  708. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  709. NUMBERS[j]->ReturnValue = std::to_string(NUMBERS[j]->PreValue);
  710. }
  711. }
  712. if ((!NUMBERS[j]->AllowNegativeRates) && (NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  713. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handleAllowNegativeRate for device: " + NUMBERS[j]->name);
  714. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  715. // more debug if extended resolution is on, see #2447
  716. if (NUMBERS[j]->isExtendedResolution) {
  717. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Neg: value=" + std::to_string(NUMBERS[j]->Value)
  718. + ", preValue=" + std::to_string(NUMBERS[j]->PreValue)
  719. + ", preToll=" + std::to_string(NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))));
  720. }
  721. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  722. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  723. NUMBERS[j]->ReturnValue = "";
  724. NUMBERS[j]->timeStampLastValue = imagetime;
  725. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  726. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  727. WriteDataLog(j);
  728. continue;
  729. }
  730. }
  731. #ifdef SERIAL_DEBUG
  732. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  733. #endif
  734. // LastValueTimeDifference = LastValueTimeDifference / 60; // in minutes
  735. LastPreValueTimeDifference = LastPreValueTimeDifference / 60; // in minutes
  736. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / LastPreValueTimeDifference;
  737. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  738. if ((NUMBERS[j]->useMaxRateValue) && (NUMBERS[j]->Value != NUMBERS[j]->PreValue)) {
  739. double _ratedifference;
  740. if (NUMBERS[j]->MaxRateType == RateChange) {
  741. _ratedifference = NUMBERS[j]->FlowRateAct;
  742. }
  743. else {
  744. // TODO:
  745. // Since I don't know if this is desired, I'll comment it out first.
  746. // int roundDifference = (int)(round(LastPreValueTimeDifference / LastValueTimeDifference)); // calculate how many rounds have passed since NUMBERS[j]->timeLastPreValue was set
  747. // _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
  748. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  749. }
  750. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue)) {
  751. 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);
  752. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  753. NUMBERS[j]->ReturnValue = "";
  754. NUMBERS[j]->ReturnRateValue = "";
  755. NUMBERS[j]->timeStampLastValue = imagetime;
  756. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  757. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  758. WriteDataLog(j);
  759. continue;
  760. }
  761. }
  762. #ifdef SERIAL_DEBUG
  763. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  764. #endif
  765. }
  766. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  767. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  768. NUMBERS[j]->PreValueOkay = true;
  769. NUMBERS[j]->timeStampLastValue = imagetime;
  770. NUMBERS[j]->timeStampLastPreValue = imagetime;
  771. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  772. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  773. NUMBERS[j]->ErrorMessageText = "no error";
  774. UpdatePreValueINI = true;
  775. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  776. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  777. WriteDataLog(j);
  778. }
  779. SavePreValue();
  780. return true;
  781. }
  782. void ClassFlowPostProcessing::WriteDataLog(int _index) {
  783. if (!LogFile.GetDataLogToSD()) {
  784. return;
  785. }
  786. string analog = "";
  787. string digit = "";
  788. string timezw = "";
  789. char buffer[80];
  790. struct tm* timeinfo = localtime(&NUMBERS[_index]->timeStampLastValue);
  791. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  792. timezw = std::string(buffer);
  793. if (flowAnalog) {
  794. analog = flowAnalog->getReadoutRawString(_index);
  795. }
  796. if (flowDigit) {
  797. digit = flowDigit->getReadoutRawString(_index);
  798. }
  799. LogFile.WriteToData(timezw, NUMBERS[_index]->name, NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  800. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute, NUMBERS[_index]->ErrorMessageText, digit, analog);
  801. 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());
  802. }
  803. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift() {
  804. for (int j = 0; j < NUMBERS.size(); ++j) {
  805. // There are only digits
  806. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) {
  807. // ESP_LOGD(TAG, "Nurdigit");
  808. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  809. // Extended resolution is on and should also be used for this digit.
  810. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) {
  811. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  812. }
  813. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  814. }
  815. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  816. // ESP_LOGD(TAG, "Nur analog");
  817. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  818. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  819. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  820. }
  821. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  822. }
  823. // digit + analog
  824. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  825. // ESP_LOGD(TAG, "Nur digit + analog");
  826. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  827. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  828. // Extended resolution is on and should also be used for this digit.
  829. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  830. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  831. }
  832. }
  833. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  834. }
  835. }
  836. string ClassFlowPostProcessing::getReadout(int _number) {
  837. return NUMBERS[_number]->ReturnValue;
  838. }
  839. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number) {
  840. if (_rawValue) {
  841. return NUMBERS[_number]->ReturnRawValue;
  842. }
  843. if (_noerror) {
  844. return NUMBERS[_number]->ReturnValue;
  845. }
  846. return NUMBERS[_number]->ReturnValue;
  847. }
  848. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue) {
  849. int posN, posPunkt;
  850. int pot, ziffer;
  851. float zw;
  852. posN = findDelimiterPos(input, "N");
  853. posPunkt = findDelimiterPos(input, ".");
  854. if (posPunkt == std::string::npos) {
  855. posPunkt = input.length();
  856. }
  857. while (posN != std::string::npos) {
  858. if (posN < posPunkt) {
  859. pot = posPunkt - posN - 1;
  860. }
  861. else {
  862. pot = posPunkt - posN;
  863. }
  864. zw =_prevalue / pow(10, pot);
  865. ziffer = ((int) zw) % 10;
  866. input[posN] = ziffer + 48;
  867. posN = findDelimiterPos(input, "N");
  868. }
  869. return input;
  870. }
  871. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
  872. int aktdigit, olddigit;
  873. int aktdigit_before, olddigit_before;
  874. int pot, pot_max;
  875. float zw;
  876. bool no_nulldurchgang = false;
  877. pot = _decilamshift;
  878. // if there are no analogue values, the last one cannot be evaluated
  879. if (!_isanalog) {
  880. pot++;
  881. }
  882. #ifdef SERIAL_DEBUG
  883. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  884. #endif
  885. pot_max = ((int) log10(input)) + 1;
  886. while (pot <= pot_max) {
  887. zw = input / pow(10, pot-1);
  888. aktdigit_before = ((int) zw) % 10;
  889. zw = _preValue / pow(10, pot-1);
  890. olddigit_before = ((int) zw) % 10;
  891. zw = input / pow(10, pot);
  892. aktdigit = ((int) zw) % 10;
  893. zw = _preValue / pow(10, pot);
  894. olddigit = ((int) zw) % 10;
  895. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  896. if (no_nulldurchgang) {
  897. if (aktdigit != olddigit) {
  898. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  899. }
  900. }
  901. else {
  902. // despite zero crossing, digit was not incremented --> add 1
  903. if (aktdigit == olddigit) {
  904. input = input + ((float) (1)) * pow(10, pot); // add 1 at the point
  905. }
  906. }
  907. #ifdef SERIAL_DEBUG
  908. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  909. #endif
  910. pot++;
  911. }
  912. return input;
  913. }
  914. string ClassFlowPostProcessing::getReadoutRate(int _number) {
  915. return std::to_string(NUMBERS[_number]->FlowRateAct);
  916. }
  917. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number) {
  918. return NUMBERS[_number]->timeStamp;
  919. }
  920. string ClassFlowPostProcessing::getReadoutError(int _number) {
  921. return NUMBERS[_number]->ErrorMessageText;
  922. }