ClassFlowPostProcessing.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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::handleAnalogDigitalTransitionStart(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]->AnalogDigitalTransitionStart = _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]->RateType = _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. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph) {
  402. std::vector<string> splitted;
  403. int _n;
  404. aktparamgraph = trim(aktparamgraph);
  405. if (aktparamgraph.size() == 0) {
  406. if (!this->GetNextParagraph(pfile, aktparamgraph)) {
  407. return false;
  408. }
  409. }
  410. // Paragraph does not fit PostProcessing
  411. if (aktparamgraph.compare("[PostProcessing]") != 0) {
  412. return false;
  413. }
  414. InitNUMBERS();
  415. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) {
  416. splitted = ZerlegeZeile(aktparamgraph);
  417. std::string _param = GetParameterName(splitted[0]);
  418. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1)) {
  419. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  420. }
  421. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1)) {
  422. handleDecimalSeparator(splitted[0], splitted[1]);
  423. }
  424. if ((toUpper(_param) == "ANALOGDIGITALTRANSITIONSTART") && (splitted.size() > 1)) {
  425. handleAnalogDigitalTransitionStart(splitted[0], splitted[1]);
  426. }
  427. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1)) {
  428. handleMaxRateValue(splitted[0], splitted[1]);
  429. }
  430. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1)) {
  431. handleMaxRateType(splitted[0], splitted[1]);
  432. }
  433. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1)) {
  434. PreValueUse = alphanumericToBoolean(splitted[1]);
  435. }
  436. if ((toUpper(_param) == "CHANGERATETHRESHOLD") && (splitted.size() > 1)) {
  437. handleChangeRateThreshold(splitted[0], splitted[1]);
  438. }
  439. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1)) {
  440. if (alphanumericToBoolean(splitted[1])) {
  441. for (_n = 0; _n < NUMBERS.size(); ++_n) {
  442. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  443. }
  444. }
  445. }
  446. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1)) {
  447. handleAllowNegativeRate(splitted[0], splitted[1]);
  448. }
  449. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1)) {
  450. ErrorMessage = alphanumericToBoolean(splitted[1]);
  451. }
  452. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1)) {
  453. IgnoreLeadingNaN = alphanumericToBoolean(splitted[1]);
  454. }
  455. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1)) {
  456. if (isStringNumeric(splitted[1])) {
  457. PreValueAgeStartup = std::stoi(splitted[1]);
  458. }
  459. }
  460. }
  461. if (PreValueUse) {
  462. return LoadPreValue();
  463. }
  464. return true;
  465. }
  466. void ClassFlowPostProcessing::InitNUMBERS() {
  467. int anzDIGIT = 0;
  468. int anzANALOG = 0;
  469. std::vector<std::string> name_numbers;
  470. if (flowDigit) {
  471. anzDIGIT = flowDigit->getNumberGENERAL();
  472. flowDigit->UpdateNameNumbers(&name_numbers);
  473. }
  474. if (flowAnalog) {
  475. anzANALOG = flowAnalog->getNumberGENERAL();
  476. flowAnalog->UpdateNameNumbers(&name_numbers);
  477. }
  478. ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
  479. for (int _num = 0; _num < name_numbers.size(); ++_num) {
  480. NumberPost *_number = new NumberPost;
  481. _number->name = name_numbers[_num];
  482. _number->digit_roi = NULL;
  483. if (flowDigit) {
  484. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  485. }
  486. if (_number->digit_roi) {
  487. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  488. }
  489. else {
  490. _number->AnzahlDigital = 0;
  491. }
  492. _number->analog_roi = NULL;
  493. if (flowAnalog) {
  494. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  495. }
  496. if (_number->analog_roi) {
  497. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  498. }
  499. else {
  500. _number->AnzahlAnalog = 0;
  501. }
  502. _number->ReturnRawValue = ""; // Raw value (with N & leading 0).
  503. _number->ReturnValue = ""; // corrected return value, possibly with error message
  504. _number->ErrorMessageText = ""; // Error message for consistency check
  505. _number->ReturnPreValue = "";
  506. _number->PreValueOkay = false;
  507. _number->AllowNegativeRates = false;
  508. _number->MaxRateValue = 0.1;
  509. _number->RateType = AbsoluteChange;
  510. _number->useMaxRateValue = false;
  511. _number->checkDigitIncreaseConsistency = false;
  512. _number->DecimalShift = 0;
  513. _number->DecimalShiftInitial = 0;
  514. _number->isExtendedResolution = false;
  515. _number->AnalogDigitalTransitionStart=9.2;
  516. _number->ChangeRateThreshold = 2;
  517. _number->FlowRateAct = 0; // m3 / min
  518. _number->PreValue = 0; // last value read out well
  519. _number->Value = 0; // last value read out, incl. corrections
  520. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  521. _number->ReturnValue = ""; // corrected return value, possibly with error message
  522. _number->ErrorMessageText = ""; // Error message for consistency check
  523. _number->Nachkomma = _number->AnzahlAnalog;
  524. NUMBERS.push_back(_number);
  525. }
  526. for (int i = 0; i < NUMBERS.size(); ++i) {
  527. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  528. }
  529. }
  530. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift) {
  531. if (_decShift == 0) {
  532. return in;
  533. }
  534. int _pos_dec_org, _pos_dec_neu;
  535. _pos_dec_org = findDelimiterPos(in, ".");
  536. if (_pos_dec_org == std::string::npos) {
  537. _pos_dec_org = in.length();
  538. }
  539. else {
  540. in = in.erase(_pos_dec_org, 1);
  541. }
  542. _pos_dec_neu = _pos_dec_org + _decShift;
  543. // comma is before the first digit
  544. if (_pos_dec_neu <= 0) {
  545. for (int i = 0; i > _pos_dec_neu; --i) {
  546. in = in.insert(0, "0");
  547. }
  548. in = "0." + in;
  549. return in;
  550. }
  551. // Comma should be after string (123 --> 1230)
  552. if (_pos_dec_neu > in.length()) {
  553. for (int i = in.length(); i < _pos_dec_neu; ++i) {
  554. in = in.insert(in.length(), "0");
  555. }
  556. return in;
  557. }
  558. string zw;
  559. zw = in.substr(0, _pos_dec_neu);
  560. zw = zw + ".";
  561. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  562. return zw;
  563. }
  564. bool ClassFlowPostProcessing::doFlow(string zwtime) {
  565. string result = "";
  566. string digit = "";
  567. string analog = "";
  568. string zwvalue;
  569. string zw;
  570. time_t imagetime = 0;
  571. string rohwert;
  572. // Update decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  573. imagetime = flowTakeImage->getTimeImageTaken();
  574. if (imagetime == 0) {
  575. time(&imagetime);
  576. }
  577. struct tm* timeinfo;
  578. timeinfo = localtime(&imagetime);
  579. char strftime_buf[64];
  580. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  581. zwtime = std::string(strftime_buf);
  582. ESP_LOGD(TAG, "Quantity NUMBERS: %d", NUMBERS.size());
  583. for (int j = 0; j < NUMBERS.size(); ++j) {
  584. NUMBERS[j]->ReturnRawValue = "";
  585. NUMBERS[j]->ReturnRateValue = "";
  586. NUMBERS[j]->ReturnValue = "";
  587. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(0.0, NUMBERS[j]->Nachkomma); // always reset change absolute
  588. NUMBERS[j]->ErrorMessageText = "";
  589. NUMBERS[j]->Value = -1;
  590. // calculate time difference
  591. // double LastValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastValue); // in seconds
  592. double LastPreValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastPreValue); // in seconds
  593. UpdateNachkommaDecimalShift();
  594. int previous_value = -1;
  595. if (NUMBERS[j]->analog_roi) {
  596. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  597. if (NUMBERS[j]->ReturnRawValue.length() > 0) {
  598. char zw = NUMBERS[j]->ReturnRawValue[0];
  599. if (zw >= 48 && zw <=57) {
  600. previous_value = zw - 48;
  601. }
  602. }
  603. }
  604. #ifdef SERIAL_DEBUG
  605. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  606. #endif
  607. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  608. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  609. }
  610. if (NUMBERS[j]->digit_roi) {
  611. if (NUMBERS[j]->analog_roi) {
  612. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  613. }
  614. else {
  615. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  616. }
  617. }
  618. #ifdef SERIAL_DEBUG
  619. ESP_LOGD(TAG, "After digital->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  620. #endif
  621. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  622. #ifdef SERIAL_DEBUG
  623. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  624. #endif
  625. if (IgnoreLeadingNaN) {
  626. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N')) {
  627. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  628. }
  629. }
  630. #ifdef SERIAL_DEBUG
  631. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  632. #endif
  633. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  634. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos) {
  635. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  636. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  637. }
  638. else {
  639. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  640. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  641. NUMBERS[j]->timeStampLastValue = imagetime;
  642. WriteDataLog(j);
  643. continue; // there is no number because there is still an N.
  644. }
  645. }
  646. #ifdef SERIAL_DEBUG
  647. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  648. #endif
  649. // Delete leading zeros (unless there is only one 0 left)
  650. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0')) {
  651. NUMBERS[j]->ReturnValue.erase(0, 1);
  652. }
  653. #ifdef SERIAL_DEBUG
  654. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  655. #endif
  656. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  657. #ifdef SERIAL_DEBUG
  658. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  659. #endif
  660. if (NUMBERS[j]->checkDigitIncreaseConsistency) {
  661. if (flowDigit) {
  662. if (flowDigit->getCNNType() != Digital) {
  663. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)");
  664. }
  665. else {
  666. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  667. }
  668. }
  669. else {
  670. #ifdef SERIAL_DEBUG
  671. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digital numbers defined!");
  672. #endif
  673. }
  674. }
  675. #ifdef SERIAL_DEBUG
  676. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  677. #endif
  678. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  679. if (NUMBERS[j]->Nachkomma > 0) {
  680. double _difference1 = (NUMBERS[j]->PreValue - (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  681. double _difference2 = (NUMBERS[j]->PreValue + (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  682. if ((NUMBERS[j]->Value >= _difference1) && (NUMBERS[j]->Value <= _difference2)) {
  683. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  684. NUMBERS[j]->ReturnValue = std::to_string(NUMBERS[j]->PreValue);
  685. }
  686. }
  687. if ((!NUMBERS[j]->AllowNegativeRates) && (NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  688. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handleAllowNegativeRate for device: " + NUMBERS[j]->name);
  689. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  690. // more debug if extended resolution is on, see #2447
  691. if (NUMBERS[j]->isExtendedResolution) {
  692. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Neg: value=" + std::to_string(NUMBERS[j]->Value)
  693. + ", preValue=" + std::to_string(NUMBERS[j]->PreValue)
  694. + ", preToll=" + std::to_string(NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))));
  695. }
  696. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  697. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  698. NUMBERS[j]->ReturnValue = "";
  699. NUMBERS[j]->timeStampLastValue = imagetime;
  700. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  701. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  702. WriteDataLog(j);
  703. continue;
  704. }
  705. }
  706. #ifdef SERIAL_DEBUG
  707. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  708. #endif
  709. // LastValueTimeDifference = LastValueTimeDifference / 60; // in minutes
  710. LastPreValueTimeDifference = LastPreValueTimeDifference / 60; // in minutes
  711. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / LastPreValueTimeDifference;
  712. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  713. if ((NUMBERS[j]->useMaxRateValue) && (NUMBERS[j]->Value != NUMBERS[j]->PreValue)) {
  714. double _ratedifference;
  715. if (NUMBERS[j]->RateType == RateChange) {
  716. _ratedifference = NUMBERS[j]->FlowRateAct;
  717. }
  718. else {
  719. // TODO:
  720. // Since I don't know if this is desired, I'll comment it out first.
  721. // int roundDifference = (int)(round(LastPreValueTimeDifference / LastValueTimeDifference)); // calculate how many rounds have passed since NUMBERS[j]->timeLastPreValue was set
  722. // _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
  723. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  724. }
  725. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue)) {
  726. 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);
  727. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  728. NUMBERS[j]->ReturnValue = "";
  729. NUMBERS[j]->ReturnRateValue = "";
  730. NUMBERS[j]->timeStampLastValue = imagetime;
  731. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  732. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  733. WriteDataLog(j);
  734. continue;
  735. }
  736. }
  737. #ifdef SERIAL_DEBUG
  738. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  739. #endif
  740. }
  741. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  742. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  743. NUMBERS[j]->PreValueOkay = true;
  744. NUMBERS[j]->timeStampLastValue = imagetime;
  745. NUMBERS[j]->timeStampLastPreValue = imagetime;
  746. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  747. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  748. NUMBERS[j]->ErrorMessageText = "no error";
  749. UpdatePreValueINI = true;
  750. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  751. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  752. WriteDataLog(j);
  753. }
  754. SavePreValue();
  755. return true;
  756. }
  757. void ClassFlowPostProcessing::WriteDataLog(int _index) {
  758. if (!LogFile.GetDataLogToSD()) {
  759. return;
  760. }
  761. string analog = "";
  762. string digital = "";
  763. string timezw = "";
  764. char buffer[80];
  765. struct tm* timeinfo = localtime(&NUMBERS[_index]->timeStampLastValue);
  766. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  767. timezw = std::string(buffer);
  768. if (flowAnalog) {
  769. analog = flowAnalog->getReadoutRawString(_index);
  770. }
  771. if (flowDigit) {
  772. digital = flowDigit->getReadoutRawString(_index);
  773. }
  774. LogFile.WriteToData(timezw, NUMBERS[_index]->name, NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  775. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute, NUMBERS[_index]->ErrorMessageText, digital, analog);
  776. 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());
  777. }
  778. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift() {
  779. for (int j = 0; j < NUMBERS.size(); ++j) {
  780. // There are only digital digits
  781. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) {
  782. // ESP_LOGD(TAG, "Nurdigital");
  783. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  784. // Extended resolution is on and should also be used for this digit.
  785. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) {
  786. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  787. }
  788. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  789. }
  790. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  791. // ESP_LOGD(TAG, "Nur analog");
  792. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  793. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  794. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  795. }
  796. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  797. }
  798. // digital + analog
  799. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  800. // ESP_LOGD(TAG, "Nur digital + analog");
  801. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  802. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  803. // Extended resolution is on and should also be used for this digit.
  804. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  805. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  806. }
  807. }
  808. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  809. }
  810. }
  811. string ClassFlowPostProcessing::getReadout(int _number) {
  812. return NUMBERS[_number]->ReturnValue;
  813. }
  814. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number) {
  815. if (_rawValue) {
  816. return NUMBERS[_number]->ReturnRawValue;
  817. }
  818. if (_noerror) {
  819. return NUMBERS[_number]->ReturnValue;
  820. }
  821. return NUMBERS[_number]->ReturnValue;
  822. }
  823. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue) {
  824. int posN, posPunkt;
  825. int pot, ziffer;
  826. float zw;
  827. posN = findDelimiterPos(input, "N");
  828. posPunkt = findDelimiterPos(input, ".");
  829. if (posPunkt == std::string::npos) {
  830. posPunkt = input.length();
  831. }
  832. while (posN != std::string::npos) {
  833. if (posN < posPunkt) {
  834. pot = posPunkt - posN - 1;
  835. }
  836. else {
  837. pot = posPunkt - posN;
  838. }
  839. zw =_prevalue / pow(10, pot);
  840. ziffer = ((int) zw) % 10;
  841. input[posN] = ziffer + 48;
  842. posN = findDelimiterPos(input, "N");
  843. }
  844. return input;
  845. }
  846. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
  847. int aktdigit, olddigit;
  848. int aktdigit_before, olddigit_before;
  849. int pot, pot_max;
  850. float zw;
  851. bool no_nulldurchgang = false;
  852. pot = _decilamshift;
  853. // if there are no analogue values, the last one cannot be evaluated
  854. if (!_isanalog) {
  855. pot++;
  856. }
  857. #ifdef SERIAL_DEBUG
  858. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  859. #endif
  860. pot_max = ((int) log10(input)) + 1;
  861. while (pot <= pot_max) {
  862. zw = input / pow(10, pot-1);
  863. aktdigit_before = ((int) zw) % 10;
  864. zw = _preValue / pow(10, pot-1);
  865. olddigit_before = ((int) zw) % 10;
  866. zw = input / pow(10, pot);
  867. aktdigit = ((int) zw) % 10;
  868. zw = _preValue / pow(10, pot);
  869. olddigit = ((int) zw) % 10;
  870. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  871. if (no_nulldurchgang) {
  872. if (aktdigit != olddigit) {
  873. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  874. }
  875. }
  876. else {
  877. // despite zero crossing, digit was not incremented --> add 1
  878. if (aktdigit == olddigit) {
  879. input = input + ((float) (1)) * pow(10, pot); // add 1 at the point
  880. }
  881. }
  882. #ifdef SERIAL_DEBUG
  883. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  884. #endif
  885. pot++;
  886. }
  887. return input;
  888. }
  889. string ClassFlowPostProcessing::getReadoutRate(int _number) {
  890. return std::to_string(NUMBERS[_number]->FlowRateAct);
  891. }
  892. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number) {
  893. return NUMBERS[_number]->timeStamp;
  894. }
  895. string ClassFlowPostProcessing::getReadoutError(int _number) {
  896. return NUMBERS[_number]->ErrorMessageText;
  897. }