ClassFlowPostProcessing.cpp 42 KB

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