ClassFlowPostProcessing.cpp 45 KB

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