ClassFlowPostProcessing.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. if (!InitNUMBERS()) {
  469. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Post-processing initialization failed; skipping PreValue loading");
  470. return false;
  471. }
  472. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) {
  473. splitted = ZerlegeZeile(aktparamgraph);
  474. std::string _param = GetParameterName(splitted[0]);
  475. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1)) {
  476. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  477. }
  478. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1)) {
  479. handleDecimalSeparator(splitted[0], splitted[1]);
  480. }
  481. if ((toUpper(_param) == "ANALOGTODIGITTRANSITIONSTART") && (splitted.size() > 1)) {
  482. handleAnalogToDigitTransitionStart(splitted[0], splitted[1]);
  483. }
  484. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1)) {
  485. handleMaxRateValue(splitted[0], splitted[1]);
  486. }
  487. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1)) {
  488. handleMaxRateType(splitted[0], splitted[1]);
  489. }
  490. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1)) {
  491. PreValueUse = alphanumericToBoolean(splitted[1]);
  492. }
  493. if ((toUpper(_param) == "CHANGERATETHRESHOLD") && (splitted.size() > 1)) {
  494. handleChangeRateThreshold(splitted[0], splitted[1]);
  495. }
  496. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1)) {
  497. handlecheckDigitIncreaseConsistency(splitted[0], splitted[1]);
  498. }
  499. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1)) {
  500. handleAllowNegativeRate(splitted[0], splitted[1]);
  501. }
  502. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1)) {
  503. ErrorMessage = alphanumericToBoolean(splitted[1]);
  504. }
  505. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1)) {
  506. handleIgnoreLeadingNaN(splitted[0], splitted[1]);
  507. }
  508. if ((toUpper(_param) == "CONTINUOUSLASTDIGIT") && (splitted.size() > 1)) {
  509. handleContinuousLastDigit(splitted[0], splitted[1]);
  510. }
  511. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1)) {
  512. if (isStringNumeric(splitted[1])) {
  513. PreValueAgeStartup = std::stoi(splitted[1]);
  514. }
  515. }
  516. }
  517. if (PreValueUse) {
  518. return LoadPreValue();
  519. }
  520. return true;
  521. }
  522. ClassFlowPostProcessing::~ClassFlowPostProcessing() {
  523. for (NumberPost* number : NUMBERS) {
  524. delete number;
  525. }
  526. NUMBERS.clear();
  527. }
  528. bool ClassFlowPostProcessing::InitNUMBERS() {
  529. // The configuration can be reloaded; release any previous per-number state first.
  530. for (NumberPost* number : NUMBERS) {
  531. delete number;
  532. }
  533. NUMBERS.clear();
  534. int anzDIGIT = 0;
  535. int anzANALOG = 0;
  536. std::vector<std::string> name_numbers;
  537. if (flowDigit) {
  538. anzDIGIT = flowDigit->getNumberGENERAL();
  539. flowDigit->UpdateNameNumbers(&name_numbers);
  540. }
  541. if (flowAnalog) {
  542. anzANALOG = flowAnalog->getNumberGENERAL();
  543. flowAnalog->UpdateNameNumbers(&name_numbers);
  544. }
  545. ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
  546. for (int _num = 0; _num < name_numbers.size(); ++_num) {
  547. NumberPost *_number = new (std::nothrow) NumberPost;
  548. if (_number == NULL) {
  549. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate post-processing state for number " + name_numbers[_num]);
  550. for (NumberPost* number : NUMBERS) {
  551. delete number;
  552. }
  553. NUMBERS.clear();
  554. return false;
  555. }
  556. _number->name = name_numbers[_num];
  557. _number->digit_roi = NULL;
  558. if (flowDigit) {
  559. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  560. }
  561. if (_number->digit_roi) {
  562. _number->AnzahlDigit = _number->digit_roi->ROI.size();
  563. }
  564. else {
  565. _number->AnzahlDigit = 0;
  566. }
  567. _number->analog_roi = NULL;
  568. if (flowAnalog) {
  569. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  570. }
  571. if (_number->analog_roi) {
  572. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  573. }
  574. else {
  575. _number->AnzahlAnalog = 0;
  576. }
  577. _number->FlowRateAct = 0; // m3 / min
  578. _number->PreValueOkay = false;
  579. _number->AllowNegativeRates = false;
  580. _number->IgnoreLeadingNaN = false;
  581. _number->MaxRateValue = 0.1;
  582. _number->MaxRateType = AbsoluteChange;
  583. _number->useMaxRateValue = false;
  584. _number->checkDigitIncreaseConsistency = false;
  585. _number->DecimalShift = 0;
  586. _number->DecimalShiftInitial = 0;
  587. _number->isExtendedResolution = false;
  588. _number->AnalogToDigitTransitionStart=9.2;
  589. _number->ContinuousLastDigit = false;
  590. _number->ChangeRateThreshold = 2;
  591. _number->Value = 0; // last value read out, incl. corrections
  592. _number->ReturnValue = ""; // corrected return value, possibly with error message
  593. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  594. _number->PreValue = 0; // last value read out well
  595. _number->ReturnPreValue = "";
  596. _number->ErrorMessageText = ""; // Error message for consistency check
  597. _number->Nachkomma = _number->AnzahlAnalog;
  598. NUMBERS.push_back(_number);
  599. }
  600. for (int i = 0; i < NUMBERS.size(); ++i) {
  601. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigit, NUMBERS[i]->AnzahlAnalog);
  602. }
  603. return true;
  604. }
  605. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift) {
  606. if (_decShift == 0) {
  607. return in;
  608. }
  609. int _pos_dec_org, _pos_dec_neu;
  610. _pos_dec_org = findDelimiterPos(in, ".");
  611. if (_pos_dec_org == std::string::npos) {
  612. _pos_dec_org = in.length();
  613. }
  614. else {
  615. in = in.erase(_pos_dec_org, 1);
  616. }
  617. _pos_dec_neu = _pos_dec_org + _decShift;
  618. // comma is before the first digit
  619. if (_pos_dec_neu <= 0) {
  620. for (int i = 0; i > _pos_dec_neu; --i) {
  621. in = in.insert(0, "0");
  622. }
  623. in = "0." + in;
  624. return in;
  625. }
  626. // Comma should be after string (123 --> 1230)
  627. if (_pos_dec_neu > in.length()) {
  628. for (int i = in.length(); i < _pos_dec_neu; ++i) {
  629. in = in.insert(in.length(), "0");
  630. }
  631. return in;
  632. }
  633. string zw;
  634. zw = in.substr(0, _pos_dec_neu);
  635. zw = zw + ".";
  636. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  637. return zw;
  638. }
  639. bool ClassFlowPostProcessing::doFlow(string zwtime) {
  640. std::lock_guard<std::mutex> lock(preValueMutex);
  641. string zwvalue;
  642. time_t imagetime = flowTakeImage->getTimeImageTaken();
  643. if (imagetime == 0) {
  644. time(&imagetime);
  645. }
  646. struct tm* timeinfo;
  647. timeinfo = localtime(&imagetime);
  648. char strftime_buf[64];
  649. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  650. zwtime = std::string(strftime_buf);
  651. ESP_LOGD(TAG, "Quantity NUMBERS: %d", NUMBERS.size());
  652. for (int j = 0; j < NUMBERS.size(); ++j) {
  653. NUMBERS[j]->ReturnRawValue = "";
  654. NUMBERS[j]->ReturnRateValue = "";
  655. NUMBERS[j]->ReturnValue = "";
  656. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(0.0, NUMBERS[j]->Nachkomma); // always reset change absolute
  657. NUMBERS[j]->ErrorMessageText = "";
  658. NUMBERS[j]->Value = -1;
  659. // calculate time difference
  660. // double LastValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastValue); // in seconds
  661. double LastPreValueTimeDifference = difftime(imagetime, NUMBERS[j]->timeStampLastPreValue); // in seconds
  662. // Update decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  663. UpdateNachkommaDecimalShift();
  664. int previous_value = -1;
  665. if (NUMBERS[j]->analog_roi) {
  666. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  667. if (NUMBERS[j]->ReturnRawValue.length() > 0) {
  668. char zw = NUMBERS[j]->ReturnRawValue[0];
  669. if (zw >= 48 && zw <=57) {
  670. previous_value = zw - 48;
  671. }
  672. }
  673. }
  674. #ifdef SERIAL_DEBUG
  675. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  676. #endif
  677. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  678. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  679. }
  680. if (NUMBERS[j]->digit_roi) {
  681. if (NUMBERS[j]->analog_roi) {
  682. 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;
  683. }
  684. else {
  685. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  686. }
  687. }
  688. #ifdef SERIAL_DEBUG
  689. ESP_LOGD(TAG, "After digit->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  690. #endif
  691. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  692. #ifdef SERIAL_DEBUG
  693. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  694. #endif
  695. if (NUMBERS[j]->IgnoreLeadingNaN) {
  696. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N')) {
  697. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  698. }
  699. }
  700. #ifdef SERIAL_DEBUG
  701. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  702. #endif
  703. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  704. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos) {
  705. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  706. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  707. }
  708. else {
  709. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  710. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  711. NUMBERS[j]->ReturnValue = "";
  712. NUMBERS[j]->timeStampLastValue = imagetime;
  713. WriteDataLog(j);
  714. continue; // there is no number because there is still an N.
  715. }
  716. }
  717. #ifdef SERIAL_DEBUG
  718. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  719. #endif
  720. // Delete leading zeros (unless there is only one 0 left)
  721. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0')) {
  722. NUMBERS[j]->ReturnValue.erase(0, 1);
  723. }
  724. #ifdef SERIAL_DEBUG
  725. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  726. #endif
  727. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  728. #ifdef SERIAL_DEBUG
  729. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  730. #endif
  731. if (NUMBERS[j]->checkDigitIncreaseConsistency) {
  732. if (flowDigit) {
  733. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Before checkDigitConsistency: value=" + std::to_string(NUMBERS[j]->Value));
  734. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  735. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "After checkDigitConsistency: value=" + std::to_string(NUMBERS[j]->Value));
  736. }
  737. else {
  738. #ifdef SERIAL_DEBUG
  739. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digit numbers defined!");
  740. #endif
  741. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "checkDigitIncreaseConsistency = true - no digit numbers defined!");
  742. }
  743. }
  744. #ifdef SERIAL_DEBUG
  745. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  746. #endif
  747. if (PreValueUse && NUMBERS[j]->PreValueOkay) {
  748. if ((NUMBERS[j]->Nachkomma > 0) && (NUMBERS[j]->ChangeRateThreshold > 0)) {
  749. double _difference1 = (NUMBERS[j]->PreValue - (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  750. double _difference2 = (NUMBERS[j]->PreValue + (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
  751. if ((NUMBERS[j]->Value >= _difference1) && (NUMBERS[j]->Value <= _difference2)) {
  752. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  753. NUMBERS[j]->ReturnValue = std::to_string(NUMBERS[j]->PreValue);
  754. }
  755. }
  756. if ((!NUMBERS[j]->AllowNegativeRates) && (NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  757. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handleAllowNegativeRate for device: " + NUMBERS[j]->name);
  758. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue)) {
  759. // more debug if extended resolution is on, see #2447
  760. if (NUMBERS[j]->isExtendedResolution) {
  761. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Neg: value=" + std::to_string(NUMBERS[j]->Value)
  762. + ", preValue=" + std::to_string(NUMBERS[j]->PreValue)
  763. + ", preToll=" + std::to_string(NUMBERS[j]->PreValue - (2 / pow(10, NUMBERS[j]->Nachkomma))));
  764. }
  765. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  766. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  767. NUMBERS[j]->ReturnValue = "";
  768. NUMBERS[j]->timeStampLastValue = imagetime;
  769. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  770. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  771. WriteDataLog(j);
  772. continue;
  773. }
  774. }
  775. #ifdef SERIAL_DEBUG
  776. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  777. #endif
  778. // LastValueTimeDifference = LastValueTimeDifference / 60; // in minutes
  779. LastPreValueTimeDifference = LastPreValueTimeDifference / 60; // in minutes
  780. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / LastPreValueTimeDifference;
  781. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  782. if ((NUMBERS[j]->useMaxRateValue) && (NUMBERS[j]->Value != NUMBERS[j]->PreValue)) {
  783. double _ratedifference;
  784. if (NUMBERS[j]->MaxRateType == RateChange) {
  785. _ratedifference = NUMBERS[j]->FlowRateAct;
  786. }
  787. else {
  788. // TODO:
  789. // Since I don't know if this is desired, I'll comment it out first.
  790. // int roundDifference = (int)(round(LastPreValueTimeDifference / LastValueTimeDifference)); // calculate how many rounds have passed since NUMBERS[j]->timeLastPreValue was set
  791. // _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
  792. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  793. }
  794. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue)) {
  795. 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);
  796. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  797. NUMBERS[j]->ReturnValue = "";
  798. NUMBERS[j]->ReturnRateValue = "";
  799. NUMBERS[j]->timeStampLastValue = imagetime;
  800. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  801. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  802. WriteDataLog(j);
  803. continue;
  804. }
  805. }
  806. #ifdef SERIAL_DEBUG
  807. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  808. #endif
  809. }
  810. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  811. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  812. NUMBERS[j]->PreValueOkay = true;
  813. NUMBERS[j]->timeStampLastValue = imagetime;
  814. NUMBERS[j]->timeStampLastPreValue = imagetime;
  815. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  816. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  817. NUMBERS[j]->ErrorMessageText = "no error";
  818. UpdatePreValueINI = true;
  819. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  820. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  821. WriteDataLog(j);
  822. }
  823. SavePreValue();
  824. return true;
  825. }
  826. void ClassFlowPostProcessing::WriteDataLog(int _index) {
  827. if (!LogFile.GetDataLogToSD()) {
  828. return;
  829. }
  830. string analog = "";
  831. string digit = "";
  832. string timezw = "";
  833. char buffer[80];
  834. struct tm* timeinfo = localtime(&NUMBERS[_index]->timeStampLastValue);
  835. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  836. timezw = std::string(buffer);
  837. if (flowAnalog) {
  838. analog = flowAnalog->getReadoutRawString(_index);
  839. }
  840. if (flowDigit) {
  841. digit = flowDigit->getReadoutRawString(_index);
  842. }
  843. LogFile.WriteToData(timezw, NUMBERS[_index]->name, NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  844. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute, NUMBERS[_index]->ErrorMessageText, digit, analog);
  845. 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());
  846. }
  847. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift() {
  848. for (int j = 0; j < NUMBERS.size(); ++j) {
  849. // There are only digits
  850. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) {
  851. // ESP_LOGD(TAG, "Nurdigit");
  852. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  853. // Extended resolution is on and should also be used for this digit.
  854. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) {
  855. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  856. }
  857. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  858. }
  859. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  860. // ESP_LOGD(TAG, "Nur analog");
  861. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  862. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  863. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  864. }
  865. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  866. }
  867. // digit + analog
  868. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) {
  869. // ESP_LOGD(TAG, "Nur digit + analog");
  870. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  871. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  872. // Extended resolution is on and should also be used for this digit.
  873. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) {
  874. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  875. }
  876. }
  877. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  878. }
  879. }
  880. string ClassFlowPostProcessing::getReadout(int _number) {
  881. return NUMBERS[_number]->ReturnValue;
  882. }
  883. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number) {
  884. if (_rawValue) {
  885. return NUMBERS[_number]->ReturnRawValue;
  886. }
  887. if (_noerror) {
  888. return NUMBERS[_number]->ReturnValue;
  889. }
  890. return NUMBERS[_number]->ReturnValue;
  891. }
  892. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue) {
  893. int posN, posPunkt;
  894. int pot, ziffer;
  895. float zw;
  896. posN = findDelimiterPos(input, "N");
  897. posPunkt = findDelimiterPos(input, ".");
  898. if (posPunkt == std::string::npos) {
  899. posPunkt = input.length();
  900. }
  901. while (posN != std::string::npos) {
  902. if (posN < posPunkt) {
  903. pot = posPunkt - posN - 1;
  904. }
  905. else {
  906. pot = posPunkt - posN;
  907. }
  908. zw =_prevalue / pow(10, pot);
  909. ziffer = ((int) zw) % 10;
  910. input[posN] = ziffer + 48;
  911. posN = findDelimiterPos(input, "N");
  912. }
  913. return input;
  914. }
  915. double ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
  916. int aktdigit, olddigit;
  917. int aktdigit_before, olddigit_before;
  918. int pot, pot_max;
  919. double zw;
  920. bool no_nulldurchgang = false;
  921. pot = _decilamshift;
  922. // if there are no analogue values, the last one cannot be evaluated
  923. if (!_isanalog) {
  924. pot++;
  925. }
  926. #ifdef SERIAL_DEBUG
  927. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  928. #endif
  929. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "checkDigitConsistency: pot=" + std::to_string(pot) + ", decimalshift=" + std::to_string(_decilamshift));
  930. pot_max = ((int) log10(input)) + 1;
  931. while (pot <= pot_max) {
  932. zw = input / pow(10, pot-1);
  933. aktdigit_before = ((int) zw) % 10;
  934. zw = _preValue / pow(10, pot-1);
  935. olddigit_before = ((int) zw) % 10;
  936. zw = input / pow(10, pot);
  937. aktdigit = ((int) zw) % 10;
  938. zw = _preValue / pow(10, pot);
  939. olddigit = ((int) zw) % 10;
  940. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  941. if (no_nulldurchgang) {
  942. if (aktdigit != olddigit) {
  943. input = input + ((double)(olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  944. }
  945. }
  946. else {
  947. // despite zero crossing, digit was not incremented --> add 1
  948. if (aktdigit == olddigit) {
  949. input = input + ((double)(1)) * pow(10, pot); // add 1 at the point
  950. }
  951. }
  952. #ifdef SERIAL_DEBUG
  953. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  954. #endif
  955. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "checkDigitConsistency: input=" + std::to_string(input));
  956. pot++;
  957. }
  958. return input;
  959. }
  960. string ClassFlowPostProcessing::getReadoutRate(int _number) {
  961. return std::to_string(NUMBERS[_number]->FlowRateAct);
  962. }
  963. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number) {
  964. return NUMBERS[_number]->timeStamp;
  965. }
  966. string ClassFlowPostProcessing::getReadoutError(int _number) {
  967. return NUMBERS[_number]->ErrorMessageText;
  968. }