ClassFlowPostProcessing.cpp 41 KB

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