ClassFlowPostProcessing.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. #include "ClassFlowPostProcessing.h"
  2. #include "Helper.h"
  3. #include "ClassFlowMakeImage.h"
  4. #include "ClassLogFile.h"
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <time.h>
  8. #include "time_sntp.h"
  9. #include "esp_log.h"
  10. static const char* TAG = "FLOW POSTPROC";
  11. //#define SERIAL_DEBUG // testing debug on serial enabled
  12. #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"
  13. #define PREVALUE_TIME_FORMAT_INPUT "%d-%d-%dT%d:%d:%d"
  14. std::string ClassFlowPostProcessing::getNumbersName()
  15. {
  16. std::string ret="";
  17. for (int i = 0; i < NUMBERS.size(); ++i)
  18. {
  19. ret += NUMBERS[i]->name;
  20. if (i < NUMBERS.size()-1)
  21. ret = ret + "\t";
  22. }
  23. // ESP_LOGI(TAG, "Result ClassFlowPostProcessing::getNumbersName: %s", ret.c_str());
  24. return ret;
  25. }
  26. std::string ClassFlowPostProcessing::GetJSON(std::string _lineend)
  27. {
  28. std::string json="{" + _lineend;
  29. for (int i = 0; i < NUMBERS.size(); ++i)
  30. {
  31. json += "\"" + NUMBERS[i]->name + "\":" + _lineend;
  32. json += getJsonFromNumber(i, _lineend) + _lineend;
  33. if ((i+1) < NUMBERS.size())
  34. json += "," + _lineend;
  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. else
  45. json += " \"value\": \"\"," + _lineend;
  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. else
  52. json += " \"rate\": \"\"," + _lineend;
  53. json += " \"timestamp\": \"" + NUMBERS[i]->timeStamp + "\"" + _lineend;
  54. json += " }" + _lineend;
  55. return json;
  56. }
  57. string ClassFlowPostProcessing::GetPreValue(std::string _number)
  58. {
  59. std::string result;
  60. int index = -1;
  61. if (_number == "")
  62. _number = "default";
  63. for (int i = 0; i < NUMBERS.size(); ++i)
  64. if (NUMBERS[i]->name == _number)
  65. index = i;
  66. if (index == -1)
  67. return std::string("");
  68. result = RundeOutput(NUMBERS[index]->PreValue, NUMBERS[index]->Nachkomma);
  69. return result;
  70. }
  71. void ClassFlowPostProcessing::SetPreValue(double zw, string _numbers, bool _extern)
  72. {
  73. ESP_LOGD(TAG, "SetPrevalue: %f, %s", zw, _numbers.c_str());
  74. for (int j = 0; j < NUMBERS.size(); ++j)
  75. {
  76. // ESP_LOGD(TAG, "Number %d, %s", j, NUMBERS[j]->name.c_str());
  77. if (NUMBERS[j]->name == _numbers)
  78. {
  79. NUMBERS[j]->PreValue = zw;
  80. NUMBERS[j]->ReturnPreValue = std::to_string(zw);
  81. NUMBERS[j]->PreValueOkay = true;
  82. if (_extern)
  83. {
  84. time(&(NUMBERS[j]->lastvalue));
  85. localtime(&(NUMBERS[j]->lastvalue));
  86. }
  87. // ESP_LOGD(TAG, "Found %d! - set to %f", j, NUMBERS[j]->PreValue);
  88. }
  89. }
  90. UpdatePreValueINI = true;
  91. SavePreValue();
  92. }
  93. bool ClassFlowPostProcessing::LoadPreValue(void)
  94. {
  95. std::vector<string> zerlegt;
  96. FILE* pFile;
  97. char zw[1024];
  98. string zwtime, zwvalue, name;
  99. bool _done = false;
  100. UpdatePreValueINI = false; // Konvertierung ins neue Format
  101. pFile = fopen(FilePreValue.c_str(), "r");
  102. if (pFile == NULL)
  103. return false;
  104. fgets(zw, 1024, pFile);
  105. ESP_LOGD(TAG, "Read line Prevalue.ini: %s", zw);
  106. zwtime = trim(std::string(zw));
  107. if (zwtime.length() == 0)
  108. return false;
  109. zerlegt = HelperZerlegeZeile(zwtime, "\t");
  110. if (zerlegt.size() > 1) // neues Format
  111. {
  112. while ((zerlegt.size() > 1) && !_done)
  113. {
  114. name = trim(zerlegt[0]);
  115. zwtime = trim(zerlegt[1]);
  116. zwvalue = trim(zerlegt[2]);
  117. for (int j = 0; j < NUMBERS.size(); ++j)
  118. {
  119. if (NUMBERS[j]->name == name)
  120. {
  121. NUMBERS[j]->PreValue = stod(zwvalue.c_str());
  122. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma + 1); // SIcherheitshalber 1 Stelle mehr, da ggf. Exgtended Resolution an ist (wird erst beim ersten Durchlauf gesetzt)
  123. time_t tStart;
  124. int yy, month, dd, hh, mm, ss;
  125. struct tm whenStart;
  126. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  127. whenStart.tm_year = yy - 1900;
  128. whenStart.tm_mon = month - 1;
  129. whenStart.tm_mday = dd;
  130. whenStart.tm_hour = hh;
  131. whenStart.tm_min = mm;
  132. whenStart.tm_sec = ss;
  133. whenStart.tm_isdst = -1;
  134. NUMBERS[j]->lastvalue = mktime(&whenStart);
  135. time(&tStart);
  136. localtime(&tStart);
  137. double difference = difftime(tStart, NUMBERS[j]->lastvalue);
  138. difference /= 60;
  139. if (difference > PreValueAgeStartup)
  140. NUMBERS[j]->PreValueOkay = false;
  141. else
  142. NUMBERS[j]->PreValueOkay = true;
  143. }
  144. }
  145. if (!fgets(zw, 1024, pFile))
  146. _done = true;
  147. else
  148. {
  149. ESP_LOGD(TAG, "Read line Prevalue.ini: %s", zw);
  150. zerlegt = HelperZerlegeZeile(trim(std::string(zw)), "\t");
  151. if (zerlegt.size() > 1)
  152. {
  153. name = trim(zerlegt[0]);
  154. zwtime = trim(zerlegt[1]);
  155. zwvalue = trim(zerlegt[2]);
  156. }
  157. }
  158. }
  159. fclose(pFile);
  160. }
  161. else // altes Format
  162. {
  163. fgets(zw, 1024, pFile);
  164. fclose(pFile);
  165. ESP_LOGD(TAG, "%s", zw);
  166. zwvalue = trim(std::string(zw));
  167. NUMBERS[0]->PreValue = stod(zwvalue.c_str());
  168. time_t tStart;
  169. int yy, month, dd, hh, mm, ss;
  170. struct tm whenStart;
  171. sscanf(zwtime.c_str(), PREVALUE_TIME_FORMAT_INPUT, &yy, &month, &dd, &hh, &mm, &ss);
  172. whenStart.tm_year = yy - 1900;
  173. whenStart.tm_mon = month - 1;
  174. whenStart.tm_mday = dd;
  175. whenStart.tm_hour = hh;
  176. whenStart.tm_min = mm;
  177. whenStart.tm_sec = ss;
  178. whenStart.tm_isdst = -1;
  179. ESP_LOGD(TAG, "TIME: %d, %d, %d, %d, %d, %d", whenStart.tm_year, whenStart.tm_mon, whenStart.tm_wday, whenStart.tm_hour, whenStart.tm_min, whenStart.tm_sec);
  180. NUMBERS[0]->lastvalue = mktime(&whenStart);
  181. time(&tStart);
  182. localtime(&tStart);
  183. double difference = difftime(tStart, NUMBERS[0]->lastvalue);
  184. difference /= 60;
  185. if (difference > PreValueAgeStartup)
  186. return false;
  187. NUMBERS[0]->Value = NUMBERS[0]->PreValue;
  188. NUMBERS[0]->ReturnValue = to_string(NUMBERS[0]->Value);
  189. if (NUMBERS[0]->digit_roi || NUMBERS[0]->analog_roi)
  190. {
  191. NUMBERS[0]->ReturnValue = RundeOutput(NUMBERS[0]->Value, NUMBERS[0]->Nachkomma);
  192. }
  193. UpdatePreValueINI = true; // Konvertierung ins neue Format
  194. SavePreValue();
  195. }
  196. return true;
  197. }
  198. void ClassFlowPostProcessing::SavePreValue()
  199. {
  200. FILE* pFile;
  201. string _zw;
  202. if (!UpdatePreValueINI) // PreValues unverändert --> File muss nicht neu geschrieben werden
  203. return;
  204. pFile = fopen(FilePreValue.c_str(), "w");
  205. for (int j = 0; j < NUMBERS.size(); ++j)
  206. {
  207. char buffer[80];
  208. struct tm* timeinfo = localtime(&NUMBERS[j]->lastvalue);
  209. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  210. NUMBERS[j]->timeStamp = std::string(buffer);
  211. // ESP_LOGD(TAG, "SaverPreValue %d, Value: %f, Nachkomma %d", j, NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  212. _zw = NUMBERS[j]->name + "\t" + NUMBERS[j]->timeStamp + "\t" + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + "\n";
  213. ESP_LOGD(TAG, "Write PreValue Zeile: %s", _zw.c_str());
  214. if (pFile) {
  215. fputs(_zw.c_str(), pFile);
  216. }
  217. }
  218. UpdatePreValueINI = false;
  219. fclose(pFile);
  220. }
  221. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc, ClassFlowCNNGeneral *_analog, ClassFlowCNNGeneral *_digit)
  222. {
  223. PreValueUse = false;
  224. PreValueAgeStartup = 30;
  225. ErrorMessage = false;
  226. ListFlowControll = NULL;
  227. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  228. ListFlowControll = lfc;
  229. flowMakeImage = NULL;
  230. UpdatePreValueINI = false;
  231. IgnoreLeadingNaN = false;
  232. flowAnalog = _analog;
  233. flowDigit = _digit;
  234. for (int i = 0; i < ListFlowControll->size(); ++i)
  235. {
  236. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  237. {
  238. flowMakeImage = (ClassFlowMakeImage*) (*ListFlowControll)[i];
  239. }
  240. }
  241. }
  242. void ClassFlowPostProcessing::handleDecimalExtendedResolution(string _decsep, string _value)
  243. {
  244. string _digit, _decpos;
  245. int _pospunkt = _decsep.find_first_of(".");
  246. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  247. if (_pospunkt > -1)
  248. _digit = _decsep.substr(0, _pospunkt);
  249. else
  250. _digit = "default";
  251. for (int j = 0; j < NUMBERS.size(); ++j)
  252. {
  253. bool _zwdc = false;
  254. if (toUpper(_value) == "TRUE")
  255. _zwdc = true;
  256. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  257. {
  258. NUMBERS[j]->isExtendedResolution = _zwdc;
  259. }
  260. if (NUMBERS[j]->name == _digit)
  261. {
  262. NUMBERS[j]->isExtendedResolution = _zwdc;
  263. }
  264. }
  265. }
  266. void ClassFlowPostProcessing::handleDecimalSeparator(string _decsep, string _value)
  267. {
  268. string _digit, _decpos;
  269. int _pospunkt = _decsep.find_first_of(".");
  270. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  271. if (_pospunkt > -1)
  272. _digit = _decsep.substr(0, _pospunkt);
  273. else
  274. _digit = "default";
  275. for (int j = 0; j < NUMBERS.size(); ++j)
  276. {
  277. int _zwdc = 0;
  278. // try
  279. {
  280. _zwdc = stoi(_value);
  281. }
  282. /* catch(const std::exception& e)
  283. {
  284. ESP_LOGD(TAG, "ERROR - Decimalshift is not a number: %s", _value.c_str());
  285. }
  286. */
  287. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  288. {
  289. NUMBERS[j]->DecimalShift = _zwdc;
  290. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  291. }
  292. if (NUMBERS[j]->name == _digit)
  293. {
  294. NUMBERS[j]->DecimalShift = _zwdc;
  295. NUMBERS[j]->DecimalShiftInitial = _zwdc;
  296. }
  297. NUMBERS[j]->Nachkomma = NUMBERS[j]->AnzahlAnalog - NUMBERS[j]->DecimalShift;
  298. }
  299. }
  300. void ClassFlowPostProcessing::handleAnalogDigitalTransitionStart(string _decsep, string _value)
  301. {
  302. string _digit, _decpos;
  303. int _pospunkt = _decsep.find_first_of(".");
  304. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  305. if (_pospunkt > -1)
  306. _digit = _decsep.substr(0, _pospunkt);
  307. else
  308. _digit = "default";
  309. for (int j = 0; j < NUMBERS.size(); ++j)
  310. {
  311. float _zwdc = 9.2;
  312. {
  313. _zwdc = stof(_value);
  314. }
  315. if (_digit == "default" || NUMBERS[j]->name == _digit) // erstmal auf default setzen (falls sonst nichts gesetzt)
  316. {
  317. NUMBERS[j]->AnalogDigitalTransitionStart = _zwdc;
  318. }
  319. }
  320. }
  321. void ClassFlowPostProcessing::handleMaxRateType(string _decsep, string _value)
  322. {
  323. string _digit, _decpos;
  324. int _pospunkt = _decsep.find_first_of(".");
  325. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  326. if (_pospunkt > -1)
  327. _digit = _decsep.substr(0, _pospunkt);
  328. else
  329. _digit = "default";
  330. for (int j = 0; j < NUMBERS.size(); ++j)
  331. {
  332. t_RateType _rt = AbsoluteChange;
  333. if (toUpper(_value) == "RATECHANGE")
  334. _rt = RateChange;
  335. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  336. {
  337. NUMBERS[j]->RateType = _rt;
  338. }
  339. if (NUMBERS[j]->name == _digit)
  340. {
  341. NUMBERS[j]->RateType = _rt;
  342. }
  343. }
  344. }
  345. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  346. {
  347. string _digit, _decpos;
  348. int _pospunkt = _decsep.find_first_of(".");
  349. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  350. if (_pospunkt > -1)
  351. _digit = _decsep.substr(0, _pospunkt);
  352. else
  353. _digit = "default";
  354. for (int j = 0; j < NUMBERS.size(); ++j)
  355. {
  356. float _zwdc = 1;
  357. // try
  358. {
  359. _zwdc = stof(_value);
  360. }
  361. /* catch(const std::exception& e)
  362. {
  363. ESP_LOGD(TAG, "ERROR - MaxRateValue is not a number: %s", _value.c_str());
  364. }
  365. */
  366. if (_digit == "default") // erstmal auf default setzen (falls sonst nichts gesetzt)
  367. {
  368. NUMBERS[j]->useMaxRateValue = true;
  369. NUMBERS[j]->MaxRateValue = _zwdc;
  370. }
  371. if (NUMBERS[j]->name == _digit)
  372. {
  373. NUMBERS[j]->useMaxRateValue = true;
  374. NUMBERS[j]->MaxRateValue = _zwdc;
  375. }
  376. }
  377. }
  378. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  379. {
  380. std::vector<string> zerlegt;
  381. int _n;
  382. aktparamgraph = trim(aktparamgraph);
  383. if (aktparamgraph.size() == 0)
  384. if (!this->GetNextParagraph(pfile, aktparamgraph))
  385. return false;
  386. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  387. return false;
  388. InitNUMBERS();
  389. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  390. {
  391. zerlegt = ZerlegeZeile(aktparamgraph);
  392. std::string _param = GetParameterName(zerlegt[0]);
  393. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (zerlegt.size() > 1))
  394. {
  395. handleDecimalExtendedResolution(zerlegt[0], zerlegt[1]);
  396. }
  397. if ((toUpper(_param) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  398. {
  399. handleDecimalSeparator(zerlegt[0], zerlegt[1]);
  400. }
  401. if ((toUpper(_param) == "ANALOGDIGITALTRANSITIONSTART") && (zerlegt.size() > 1))
  402. {
  403. handleAnalogDigitalTransitionStart(zerlegt[0], zerlegt[1]);
  404. }
  405. if ((toUpper(_param) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  406. {
  407. handleMaxRateValue(zerlegt[0], zerlegt[1]);
  408. }
  409. if ((toUpper(_param) == "MAXRATETYPE") && (zerlegt.size() > 1))
  410. {
  411. handleMaxRateType(zerlegt[0], zerlegt[1]);
  412. }
  413. if ((toUpper(_param) == "PREVALUEUSE") && (zerlegt.size() > 1))
  414. {
  415. if (toUpper(zerlegt[1]) == "TRUE")
  416. {
  417. PreValueUse = true;
  418. }
  419. }
  420. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  421. {
  422. if (toUpper(zerlegt[1]) == "TRUE")
  423. for (_n = 0; _n < NUMBERS.size(); ++_n)
  424. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  425. }
  426. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  427. {
  428. if (toUpper(zerlegt[1]) == "TRUE")
  429. for (_n = 0; _n < NUMBERS.size(); ++_n)
  430. NUMBERS[_n]->AllowNegativeRates = true;
  431. }
  432. if ((toUpper(_param) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  433. {
  434. if (toUpper(zerlegt[1]) == "TRUE")
  435. ErrorMessage = true;
  436. }
  437. if ((toUpper(_param) == "IGNORELEADINGNAN") && (zerlegt.size() > 1))
  438. {
  439. if (toUpper(zerlegt[1]) == "TRUE")
  440. IgnoreLeadingNaN = true;
  441. }
  442. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  443. {
  444. PreValueAgeStartup = std::stoi(zerlegt[1]);
  445. }
  446. }
  447. if (PreValueUse) {
  448. LoadPreValue();
  449. }
  450. return true;
  451. }
  452. void ClassFlowPostProcessing::InitNUMBERS()
  453. {
  454. int anzDIGIT = 0;
  455. int anzANALOG = 0;
  456. std::vector<std::string> name_numbers;
  457. if (flowDigit)
  458. {
  459. anzDIGIT = flowDigit->getAnzahlGENERAL();
  460. flowDigit->UpdateNameNumbers(&name_numbers);
  461. }
  462. if (flowAnalog)
  463. {
  464. anzANALOG = flowAnalog->getAnzahlGENERAL();
  465. flowAnalog->UpdateNameNumbers(&name_numbers);
  466. }
  467. ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
  468. for (int _num = 0; _num < name_numbers.size(); ++_num)
  469. {
  470. NumberPost *_number = new NumberPost;
  471. _number->name = name_numbers[_num];
  472. _number->digit_roi = NULL;
  473. if (flowDigit)
  474. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  475. if (_number->digit_roi)
  476. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  477. else
  478. _number->AnzahlDigital = 0;
  479. _number->analog_roi = NULL;
  480. if (flowAnalog)
  481. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  482. if (_number->analog_roi)
  483. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  484. else
  485. _number->AnzahlAnalog = 0;
  486. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  487. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  488. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  489. _number->ReturnPreValue = "";
  490. _number->PreValueOkay = false;
  491. _number->AllowNegativeRates = false;
  492. _number->MaxRateValue = 0.1;
  493. _number->RateType = AbsoluteChange;
  494. _number->useMaxRateValue = false;
  495. _number->checkDigitIncreaseConsistency = false;
  496. _number->DecimalShift = 0;
  497. _number->DecimalShiftInitial = 0;
  498. _number->isExtendedResolution = false;
  499. _number->AnalogDigitalTransitionStart=9.2;
  500. _number->FlowRateAct = 0; // m3 / min
  501. _number->PreValue = 0; // letzter Wert, der gut ausgelesen wurde
  502. _number->Value = 0; // letzer ausgelesener Wert, inkl. Korrekturen
  503. _number->ReturnRawValue = ""; // Rohwert (mit N & führenden 0)
  504. _number->ReturnValue = ""; // korrigierter Rückgabewert, ggf. mit Fehlermeldung
  505. _number->ErrorMessageText = ""; // Fehlermeldung bei Consistency Check
  506. _number->Nachkomma = _number->AnzahlAnalog;
  507. NUMBERS.push_back(_number);
  508. }
  509. for (int i = 0; i < NUMBERS.size(); ++i) {
  510. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  511. }
  512. }
  513. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  514. if (_decShift == 0){
  515. return in;
  516. }
  517. int _pos_dec_org, _pos_dec_neu;
  518. _pos_dec_org = findDelimiterPos(in, ".");
  519. if (_pos_dec_org == std::string::npos) {
  520. _pos_dec_org = in.length();
  521. }
  522. else
  523. {
  524. in = in.erase(_pos_dec_org, 1);
  525. }
  526. _pos_dec_neu = _pos_dec_org + _decShift;
  527. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  528. for (int i = 0; i > _pos_dec_neu; --i){
  529. in = in.insert(0, "0");
  530. }
  531. in = "0." + in;
  532. return in;
  533. }
  534. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  535. for (int i = in.length(); i < _pos_dec_neu; ++i){
  536. in = in.insert(in.length(), "0");
  537. }
  538. return in;
  539. }
  540. string zw;
  541. zw = in.substr(0, _pos_dec_neu);
  542. zw = zw + ".";
  543. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  544. return zw;
  545. }
  546. bool ClassFlowPostProcessing::doFlow(string zwtime)
  547. {
  548. string result = "";
  549. string digit = "";
  550. string analog = "";
  551. string zwvalue;
  552. string zw;
  553. time_t imagetime = 0;
  554. string rohwert;
  555. // Update Nachkomma, da sich beim Wechsel von CNNType Auto --> xyz auch die Nachkommastellen ändern können:
  556. imagetime = flowMakeImage->getTimeImageTaken();
  557. if (imagetime == 0)
  558. time(&imagetime);
  559. struct tm* timeinfo;
  560. timeinfo = localtime(&imagetime);
  561. char strftime_buf[64];
  562. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  563. zwtime = std::string(strftime_buf);
  564. ESP_LOGD(TAG, "Anzahl NUMBERS: %d", NUMBERS.size());
  565. for (int j = 0; j < NUMBERS.size(); ++j)
  566. {
  567. NUMBERS[j]->ReturnRawValue = "";
  568. NUMBERS[j]->ReturnRateValue = "";
  569. NUMBERS[j]->ReturnValue = "";
  570. NUMBERS[j]->ErrorMessageText = "";
  571. NUMBERS[j]->Value = -1;
  572. UpdateNachkommaDecimalShift();
  573. int previous_value = -1;
  574. if (NUMBERS[j]->analog_roi)
  575. {
  576. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  577. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  578. {
  579. char zw = NUMBERS[j]->ReturnRawValue[0];
  580. if (zw >= 48 && zw <=57)
  581. previous_value = zw - 48;
  582. }
  583. }
  584. #ifdef SERIAL_DEBUG
  585. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  586. #endif
  587. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  588. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  589. if (NUMBERS[j]->digit_roi)
  590. {
  591. if (NUMBERS[j]->analog_roi)
  592. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  593. else
  594. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution nur falls es keine analogen Ziffern gibt
  595. }
  596. #ifdef SERIAL_DEBUG
  597. ESP_LOGD(TAG, "After digital->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  598. #endif
  599. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  600. #ifdef SERIAL_DEBUG
  601. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  602. #endif
  603. if (IgnoreLeadingNaN)
  604. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  605. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  606. #ifdef SERIAL_DEBUG
  607. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  608. #endif
  609. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  610. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  611. {
  612. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  613. {
  614. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  615. }
  616. else
  617. {
  618. WriteDataLog(j);
  619. continue; // es gibt keinen Zahl, da noch ein N vorhanden ist.
  620. }
  621. }
  622. #ifdef SERIAL_DEBUG
  623. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  624. #endif
  625. // Lösche führende Nullen (außer es ist nur noch einen 0)
  626. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  627. NUMBERS[j]->ReturnValue.erase(0, 1);
  628. #ifdef SERIAL_DEBUG
  629. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  630. #endif
  631. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  632. #ifdef SERIAL_DEBUG
  633. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  634. #endif
  635. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  636. {
  637. if (flowDigit)
  638. {
  639. if (flowDigit->getCNNType() != Digital)
  640. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)");
  641. else
  642. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  643. }
  644. else
  645. {
  646. #ifdef SERIAL_DEBUG
  647. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digital numbers defined!");
  648. #endif
  649. }
  650. }
  651. #ifdef SERIAL_DEBUG
  652. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  653. #endif
  654. if (!NUMBERS[j]->AllowNegativeRates)
  655. {
  656. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  657. {
  658. #ifdef SERIAL_DEBUG
  659. ESP_LOGD(TAG, "Neg: value=%f, preValue=%f, preToll%f", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  660. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  661. ) ;
  662. #endif
  663. // Bei isExtendedResolution Ungenauigkeit von 0.2 mit einrechnen.
  664. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  665. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  666. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  667. } else {
  668. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  669. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  670. NUMBERS[j]->ReturnValue = "";
  671. WriteDataLog(j);
  672. continue;
  673. }
  674. }
  675. }
  676. #ifdef SERIAL_DEBUG
  677. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  678. #endif
  679. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  680. difference /= 60;
  681. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  682. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  683. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  684. {
  685. double _ratedifference;
  686. if (NUMBERS[j]->RateType == RateChange)
  687. _ratedifference = NUMBERS[j]->FlowRateAct;
  688. else
  689. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  690. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  691. {
  692. 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);
  693. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  694. NUMBERS[j]->ReturnValue = "";
  695. NUMBERS[j]->ReturnRateValue = "";
  696. WriteDataLog(j);
  697. continue;
  698. }
  699. }
  700. #ifdef SERIAL_DEBUG
  701. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  702. #endif
  703. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  704. NUMBERS[j]->lastvalue = imagetime;
  705. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  706. NUMBERS[j]->PreValueOkay = true;
  707. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  708. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  709. NUMBERS[j]->ErrorMessageText = "no error";
  710. UpdatePreValueINI = true;
  711. string _zw = "PostProcessing - Raw: " + NUMBERS[j]->ReturnRawValue + " Value: " + NUMBERS[j]->ReturnValue + " Error: " + NUMBERS[j]->ErrorMessageText;
  712. ESP_LOGD(TAG, "%s", zw.c_str());
  713. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
  714. WriteDataLog(j);
  715. }
  716. SavePreValue();
  717. return true;
  718. }
  719. void ClassFlowPostProcessing::WriteDataLog(int _index)
  720. {
  721. if (!LogFile.GetDataLogToSD()){
  722. return;
  723. }
  724. string analog = "";
  725. string digital = "";
  726. string timezw = "";
  727. char buffer[80];
  728. struct tm* timeinfo = localtime(&NUMBERS[_index]->lastvalue);
  729. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  730. timezw = std::string(buffer);
  731. if (flowAnalog)
  732. analog = flowAnalog->getReadoutRawString(_index);
  733. if (flowDigit)
  734. digital = flowDigit->getReadoutRawString(_index);
  735. LogFile.WriteToData(timezw, NUMBERS[_index]->name,
  736. NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  737. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute,
  738. NUMBERS[_index]->ErrorMessageText,
  739. digital, analog);
  740. ESP_LOGD(TAG, "WriteDataLog: %s, %s, %s, %s, %s", NUMBERS[_index]->ReturnRawValue.c_str(), NUMBERS[_index]->ReturnValue.c_str(), NUMBERS[_index]->ErrorMessageText.c_str(), digital.c_str(), analog.c_str());
  741. }
  742. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  743. {
  744. for (int j = 0; j < NUMBERS.size(); ++j)
  745. {
  746. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  747. {
  748. // ESP_LOGD(TAG, "Nurdigital");
  749. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  750. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  751. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  752. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  753. }
  754. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  755. {
  756. // ESP_LOGD(TAG, "Nur analog");
  757. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  758. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  759. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  760. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  761. }
  762. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  763. {
  764. // ESP_LOGD(TAG, "Nur digital + analog");
  765. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  766. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  767. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  768. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  769. }
  770. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  771. }
  772. }
  773. string ClassFlowPostProcessing::getReadout(int _number)
  774. {
  775. return NUMBERS[_number]->ReturnValue;
  776. }
  777. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  778. {
  779. if (_rawValue)
  780. return NUMBERS[_number]->ReturnRawValue;
  781. if (_noerror)
  782. return NUMBERS[_number]->ReturnValue;
  783. return NUMBERS[_number]->ReturnValue;
  784. }
  785. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  786. {
  787. int posN, posPunkt;
  788. int pot, ziffer;
  789. float zw;
  790. posN = findDelimiterPos(input, "N");
  791. posPunkt = findDelimiterPos(input, ".");
  792. if (posPunkt == std::string::npos){
  793. posPunkt = input.length();
  794. }
  795. while (posN != std::string::npos)
  796. {
  797. if (posN < posPunkt) {
  798. pot = posPunkt - posN - 1;
  799. }
  800. else {
  801. pot = posPunkt - posN;
  802. }
  803. zw =_prevalue / pow(10, pot);
  804. ziffer = ((int) zw) % 10;
  805. input[posN] = ziffer + 48;
  806. posN = findDelimiterPos(input, "N");
  807. }
  808. return input;
  809. }
  810. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  811. int aktdigit, olddigit;
  812. int aktdigit_before, olddigit_before;
  813. int pot, pot_max;
  814. float zw;
  815. bool no_nulldurchgang = false;
  816. pot = _decilamshift;
  817. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  818. {
  819. pot++;
  820. }
  821. #ifdef SERIAL_DEBUG
  822. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  823. #endif
  824. pot_max = ((int) log10(input)) + 1;
  825. while (pot <= pot_max)
  826. {
  827. zw = input / pow(10, pot-1);
  828. aktdigit_before = ((int) zw) % 10;
  829. zw = _preValue / pow(10, pot-1);
  830. olddigit_before = ((int) zw) % 10;
  831. zw = input / pow(10, pot);
  832. aktdigit = ((int) zw) % 10;
  833. zw = _preValue / pow(10, pot);
  834. olddigit = ((int) zw) % 10;
  835. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  836. if (no_nulldurchgang)
  837. {
  838. if (aktdigit != olddigit)
  839. {
  840. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  841. }
  842. }
  843. else
  844. {
  845. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  846. {
  847. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  848. }
  849. }
  850. #ifdef SERIAL_DEBUG
  851. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  852. #endif
  853. pot++;
  854. }
  855. return input;
  856. }
  857. string ClassFlowPostProcessing::getReadoutRate(int _number)
  858. {
  859. return std::to_string(NUMBERS[_number]->FlowRateAct);
  860. }
  861. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  862. {
  863. return NUMBERS[_number]->timeStamp;
  864. }
  865. string ClassFlowPostProcessing::getReadoutError(int _number)
  866. {
  867. return NUMBERS[_number]->ErrorMessageText;
  868. }