ClassFlowPostProcessing.cpp 34 KB

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