ClassFlowPostProcessing.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. NUMBERS[j]->lastvalue = imagetime;
  573. UpdateNachkommaDecimalShift();
  574. int previous_value = -1;
  575. if (NUMBERS[j]->analog_roi)
  576. {
  577. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  578. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  579. {
  580. char zw = NUMBERS[j]->ReturnRawValue[0];
  581. if (zw >= 48 && zw <=57)
  582. previous_value = zw - 48;
  583. }
  584. }
  585. #ifdef SERIAL_DEBUG
  586. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  587. #endif
  588. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  589. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  590. if (NUMBERS[j]->digit_roi)
  591. {
  592. if (NUMBERS[j]->analog_roi)
  593. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  594. else
  595. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution nur falls es keine analogen Ziffern gibt
  596. }
  597. #ifdef SERIAL_DEBUG
  598. ESP_LOGD(TAG, "After digital->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  599. #endif
  600. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  601. #ifdef SERIAL_DEBUG
  602. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  603. #endif
  604. if (IgnoreLeadingNaN)
  605. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  606. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  607. #ifdef SERIAL_DEBUG
  608. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  609. #endif
  610. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  611. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  612. {
  613. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  614. {
  615. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  616. }
  617. else
  618. {
  619. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  620. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  621. WriteDataLog(j);
  622. continue; // es gibt keinen Zahl, da noch ein N vorhanden ist.
  623. }
  624. }
  625. #ifdef SERIAL_DEBUG
  626. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  627. #endif
  628. // Lösche führende Nullen (außer es ist nur noch einen 0)
  629. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  630. NUMBERS[j]->ReturnValue.erase(0, 1);
  631. #ifdef SERIAL_DEBUG
  632. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  633. #endif
  634. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  635. #ifdef SERIAL_DEBUG
  636. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  637. #endif
  638. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  639. {
  640. if (flowDigit)
  641. {
  642. if (flowDigit->getCNNType() != Digital)
  643. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)");
  644. else
  645. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  646. }
  647. else
  648. {
  649. #ifdef SERIAL_DEBUG
  650. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digital numbers defined!");
  651. #endif
  652. }
  653. }
  654. #ifdef SERIAL_DEBUG
  655. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  656. #endif
  657. if (!NUMBERS[j]->AllowNegativeRates)
  658. {
  659. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  660. {
  661. #ifdef SERIAL_DEBUG
  662. ESP_LOGD(TAG, "Neg: value=%f, preValue=%f, preToll%f", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  663. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  664. ) ;
  665. #endif
  666. // Bei isExtendedResolution Ungenauigkeit von 0.2 mit einrechnen.
  667. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  668. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  669. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  670. } else {
  671. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  672. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  673. NUMBERS[j]->ReturnValue = "";
  674. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  675. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  676. WriteDataLog(j);
  677. continue;
  678. }
  679. }
  680. }
  681. #ifdef SERIAL_DEBUG
  682. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  683. #endif
  684. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in Sekunden
  685. difference /= 60;
  686. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  687. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  688. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  689. {
  690. double _ratedifference;
  691. if (NUMBERS[j]->RateType == RateChange)
  692. _ratedifference = NUMBERS[j]->FlowRateAct;
  693. else
  694. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  695. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  696. {
  697. 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);
  698. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  699. NUMBERS[j]->ReturnValue = "";
  700. NUMBERS[j]->ReturnRateValue = "";
  701. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  702. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  703. WriteDataLog(j);
  704. continue;
  705. }
  706. }
  707. #ifdef SERIAL_DEBUG
  708. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  709. #endif
  710. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  711. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  712. NUMBERS[j]->PreValueOkay = true;
  713. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  714. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  715. NUMBERS[j]->ErrorMessageText = "no error";
  716. UpdatePreValueINI = true;
  717. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  718. ESP_LOGD(TAG, "%s", zw.c_str());
  719. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  720. WriteDataLog(j);
  721. }
  722. SavePreValue();
  723. return true;
  724. }
  725. void ClassFlowPostProcessing::WriteDataLog(int _index)
  726. {
  727. if (!LogFile.GetDataLogToSD()){
  728. return;
  729. }
  730. string analog = "";
  731. string digital = "";
  732. string timezw = "";
  733. char buffer[80];
  734. struct tm* timeinfo = localtime(&NUMBERS[_index]->lastvalue);
  735. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  736. timezw = std::string(buffer);
  737. if (flowAnalog)
  738. analog = flowAnalog->getReadoutRawString(_index);
  739. if (flowDigit)
  740. digital = flowDigit->getReadoutRawString(_index);
  741. LogFile.WriteToData(timezw, NUMBERS[_index]->name,
  742. NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  743. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute,
  744. NUMBERS[_index]->ErrorMessageText,
  745. digital, analog);
  746. 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());
  747. }
  748. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  749. {
  750. for (int j = 0; j < NUMBERS.size(); ++j)
  751. {
  752. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // es gibt nur digitale ziffern
  753. {
  754. // ESP_LOGD(TAG, "Nurdigital");
  755. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  756. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  757. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  758. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  759. }
  760. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // es gibt nur analoge ziffern
  761. {
  762. // ESP_LOGD(TAG, "Nur analog");
  763. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  764. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  765. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  766. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  767. }
  768. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  769. {
  770. // ESP_LOGD(TAG, "Nur digital + analog");
  771. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  772. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  773. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // extended resolution ist an und soll auch bei dieser Ziffer verwendet werden
  774. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  775. }
  776. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  777. }
  778. }
  779. string ClassFlowPostProcessing::getReadout(int _number)
  780. {
  781. return NUMBERS[_number]->ReturnValue;
  782. }
  783. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  784. {
  785. if (_rawValue)
  786. return NUMBERS[_number]->ReturnRawValue;
  787. if (_noerror)
  788. return NUMBERS[_number]->ReturnValue;
  789. return NUMBERS[_number]->ReturnValue;
  790. }
  791. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  792. {
  793. int posN, posPunkt;
  794. int pot, ziffer;
  795. float zw;
  796. posN = findDelimiterPos(input, "N");
  797. posPunkt = findDelimiterPos(input, ".");
  798. if (posPunkt == std::string::npos){
  799. posPunkt = input.length();
  800. }
  801. while (posN != std::string::npos)
  802. {
  803. if (posN < posPunkt) {
  804. pot = posPunkt - posN - 1;
  805. }
  806. else {
  807. pot = posPunkt - posN;
  808. }
  809. zw =_prevalue / pow(10, pot);
  810. ziffer = ((int) zw) % 10;
  811. input[posN] = ziffer + 48;
  812. posN = findDelimiterPos(input, "N");
  813. }
  814. return input;
  815. }
  816. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  817. int aktdigit, olddigit;
  818. int aktdigit_before, olddigit_before;
  819. int pot, pot_max;
  820. float zw;
  821. bool no_nulldurchgang = false;
  822. pot = _decilamshift;
  823. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  824. {
  825. pot++;
  826. }
  827. #ifdef SERIAL_DEBUG
  828. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  829. #endif
  830. pot_max = ((int) log10(input)) + 1;
  831. while (pot <= pot_max)
  832. {
  833. zw = input / pow(10, pot-1);
  834. aktdigit_before = ((int) zw) % 10;
  835. zw = _preValue / pow(10, pot-1);
  836. olddigit_before = ((int) zw) % 10;
  837. zw = input / pow(10, pot);
  838. aktdigit = ((int) zw) % 10;
  839. zw = _preValue / pow(10, pot);
  840. olddigit = ((int) zw) % 10;
  841. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  842. if (no_nulldurchgang)
  843. {
  844. if (aktdigit != olddigit)
  845. {
  846. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  847. }
  848. }
  849. else
  850. {
  851. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  852. {
  853. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  854. }
  855. }
  856. #ifdef SERIAL_DEBUG
  857. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  858. #endif
  859. pot++;
  860. }
  861. return input;
  862. }
  863. string ClassFlowPostProcessing::getReadoutRate(int _number)
  864. {
  865. return std::to_string(NUMBERS[_number]->FlowRateAct);
  866. }
  867. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  868. {
  869. return NUMBERS[_number]->timeStamp;
  870. }
  871. string ClassFlowPostProcessing::getReadoutError(int _number)
  872. {
  873. return NUMBERS[_number]->ErrorMessageText;
  874. }