ClassFlowPostProcessing.cpp 42 KB

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