ClassFlowPostProcessing.cpp 44 KB

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