ClassFlowPostProcessing.cpp 40 KB

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