ClassFlowPostProcessing.cpp 42 KB

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