ClassFlowPostProcessing.cpp 34 KB

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