ClassFlowPostProcessing.cpp 44 KB

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