ClassFlowPostProcessing.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. #include "../../include/defines.h"
  11. static const char* TAG = "POSTPROC";
  12. std::string ClassFlowPostProcessing::getNumbersName()
  13. {
  14. std::string ret="";
  15. for (int i = 0; i < NUMBERS.size(); ++i)
  16. {
  17. ret += NUMBERS[i]->name;
  18. if (i < NUMBERS.size()-1)
  19. ret = ret + "\t";
  20. }
  21. // ESP_LOGI(TAG, "Result ClassFlowPostProcessing::getNumbersName: %s", ret.c_str());
  22. return ret;
  23. }
  24. std::string ClassFlowPostProcessing::GetJSON(std::string _lineend)
  25. {
  26. std::string json="{" + _lineend;
  27. for (int i = 0; i < NUMBERS.size(); ++i)
  28. {
  29. json += "\"" + NUMBERS[i]->name + "\":" + _lineend;
  30. json += getJsonFromNumber(i, _lineend) + _lineend;
  31. if ((i+1) < NUMBERS.size())
  32. json += "," + _lineend;
  33. }
  34. json += "}";
  35. return json;
  36. }
  37. string ClassFlowPostProcessing::getJsonFromNumber(int i, std::string _lineend) {
  38. std::string json = "";
  39. json += " {" + _lineend;
  40. if (NUMBERS[i]->ReturnValue.length() > 0)
  41. json += " \"value\": \"" + NUMBERS[i]->ReturnValue + "\"," + _lineend;
  42. else
  43. json += " \"value\": \"\"," + _lineend;
  44. json += " \"raw\": \"" + NUMBERS[i]->ReturnRawValue + "\"," + _lineend;
  45. json += " \"pre\": \"" + NUMBERS[i]->ReturnPreValue + "\"," + _lineend;
  46. json += " \"error\": \"" + NUMBERS[i]->ErrorMessageText + "\"," + _lineend;
  47. if (NUMBERS[i]->ReturnRateValue.length() > 0)
  48. json += " \"rate\": \"" + NUMBERS[i]->ReturnRateValue + "\"," + _lineend;
  49. else
  50. json += " \"rate\": \"\"," + _lineend;
  51. json += " \"timestamp\": \"" + NUMBERS[i]->timeStamp + "\"" + _lineend;
  52. json += " }" + _lineend;
  53. return json;
  54. }
  55. string ClassFlowPostProcessing::GetPreValue(std::string _number)
  56. {
  57. std::string result;
  58. int index = -1;
  59. if (_number == "")
  60. _number = "default";
  61. for (int i = 0; i < NUMBERS.size(); ++i)
  62. if (NUMBERS[i]->name == _number)
  63. index = i;
  64. if (index == -1)
  65. return std::string("");
  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 = std::to_string(zw);
  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> splitted;
  94. FILE* pFile;
  95. char zw[1024];
  96. string zwtime, zwvalue, name;
  97. bool _done = false;
  98. UpdatePreValueINI = false; // Conversion to the new 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. splitted = HelperZerlegeZeile(zwtime, "\t");
  108. if (splitted.size() > 1) // Conversion to the new format
  109. {
  110. while ((splitted.size() > 1) && !_done)
  111. {
  112. name = trim(splitted[0]);
  113. zwtime = trim(splitted[1]);
  114. zwvalue = trim(splitted[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); // To be on the safe side, 1 digit more, as Exgtended Resolution may be on (will only be set during the first run).
  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. splitted = HelperZerlegeZeile(trim(std::string(zw)), "\t");
  149. if (splitted.size() > 1)
  150. {
  151. name = trim(splitted[0]);
  152. zwtime = trim(splitted[1]);
  153. zwvalue = trim(splitted[2]);
  154. }
  155. }
  156. }
  157. fclose(pFile);
  158. }
  159. else // Old 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; // Conversion to the new format
  192. SavePreValue();
  193. }
  194. return true;
  195. }
  196. void ClassFlowPostProcessing::SavePreValue()
  197. {
  198. FILE* pFile;
  199. string _zw;
  200. if (!UpdatePreValueINI) // PreValues unchanged --> File does not have to be rewritten
  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 line: %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") // Set to default first (if nothing else is set)
  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") // Set to default first (if nothing else is set)
  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) // Set to default first (if nothing else is set)
  314. {
  315. NUMBERS[j]->AnalogDigitalTransitionStart = _zwdc;
  316. }
  317. }
  318. }
  319. void ClassFlowPostProcessing::handleAllowNegativeRate(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. bool _rt = false;
  331. if (toUpper(_value) == "TRUE")
  332. _rt = true;
  333. if (_digit == "default") // Set to default first (if nothing else is set)
  334. {
  335. NUMBERS[j]->AllowNegativeRates = _rt;
  336. }
  337. if (NUMBERS[j]->name == _digit)
  338. {
  339. NUMBERS[j]->AllowNegativeRates = _rt;
  340. }
  341. }
  342. }
  343. void ClassFlowPostProcessing::handleMaxRateType(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. t_RateType _rt = AbsoluteChange;
  355. if (toUpper(_value) == "RATECHANGE")
  356. _rt = RateChange;
  357. if (_digit == "default") // Set to default first (if nothing else is set)
  358. {
  359. NUMBERS[j]->RateType = _rt;
  360. }
  361. if (NUMBERS[j]->name == _digit)
  362. {
  363. NUMBERS[j]->RateType = _rt;
  364. }
  365. }
  366. }
  367. void ClassFlowPostProcessing::handleMaxRateValue(string _decsep, string _value)
  368. {
  369. string _digit, _decpos;
  370. int _pospunkt = _decsep.find_first_of(".");
  371. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  372. if (_pospunkt > -1)
  373. _digit = _decsep.substr(0, _pospunkt);
  374. else
  375. _digit = "default";
  376. for (int j = 0; j < NUMBERS.size(); ++j)
  377. {
  378. float _zwdc = 1;
  379. // try
  380. {
  381. _zwdc = stof(_value);
  382. }
  383. /* catch(const std::exception& e)
  384. {
  385. ESP_LOGD(TAG, "ERROR - MaxRateValue is not a number: %s", _value.c_str());
  386. }
  387. */
  388. if (_digit == "default") // Set to default first (if nothing else is set)
  389. {
  390. NUMBERS[j]->useMaxRateValue = true;
  391. NUMBERS[j]->MaxRateValue = _zwdc;
  392. }
  393. if (NUMBERS[j]->name == _digit)
  394. {
  395. NUMBERS[j]->useMaxRateValue = true;
  396. NUMBERS[j]->MaxRateValue = _zwdc;
  397. }
  398. }
  399. }
  400. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  401. {
  402. std::vector<string> splitted;
  403. int _n;
  404. aktparamgraph = trim(aktparamgraph);
  405. if (aktparamgraph.size() == 0)
  406. if (!this->GetNextParagraph(pfile, aktparamgraph))
  407. return false;
  408. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph does not fit MakeImage
  409. return false;
  410. InitNUMBERS();
  411. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  412. {
  413. splitted = ZerlegeZeile(aktparamgraph);
  414. std::string _param = GetParameterName(splitted[0]);
  415. if ((toUpper(_param) == "EXTENDEDRESOLUTION") && (splitted.size() > 1))
  416. {
  417. handleDecimalExtendedResolution(splitted[0], splitted[1]);
  418. }
  419. if ((toUpper(_param) == "DECIMALSHIFT") && (splitted.size() > 1))
  420. {
  421. handleDecimalSeparator(splitted[0], splitted[1]);
  422. }
  423. if ((toUpper(_param) == "ANALOGDIGITALTRANSITIONSTART") && (splitted.size() > 1))
  424. {
  425. handleAnalogDigitalTransitionStart(splitted[0], splitted[1]);
  426. }
  427. if ((toUpper(_param) == "MAXRATEVALUE") && (splitted.size() > 1))
  428. {
  429. handleMaxRateValue(splitted[0], splitted[1]);
  430. }
  431. if ((toUpper(_param) == "MAXRATETYPE") && (splitted.size() > 1))
  432. {
  433. handleMaxRateType(splitted[0], splitted[1]);
  434. }
  435. if ((toUpper(_param) == "PREVALUEUSE") && (splitted.size() > 1))
  436. {
  437. if (toUpper(splitted[1]) == "TRUE")
  438. {
  439. PreValueUse = true;
  440. }
  441. }
  442. if ((toUpper(_param) == "CHECKDIGITINCREASECONSISTENCY") && (splitted.size() > 1))
  443. {
  444. if (toUpper(splitted[1]) == "TRUE")
  445. for (_n = 0; _n < NUMBERS.size(); ++_n)
  446. NUMBERS[_n]->checkDigitIncreaseConsistency = true;
  447. }
  448. if ((toUpper(_param) == "ALLOWNEGATIVERATES") && (splitted.size() > 1))
  449. {
  450. handleAllowNegativeRate(splitted[0], splitted[1]);
  451. /* Updated to allow individual Settings
  452. if (toUpper(splitted[1]) == "TRUE")
  453. for (_n = 0; _n < NUMBERS.size(); ++_n)
  454. NUMBERS[_n]->AllowNegativeRates = true;
  455. */
  456. }
  457. if ((toUpper(_param) == "ERRORMESSAGE") && (splitted.size() > 1))
  458. {
  459. if (toUpper(splitted[1]) == "TRUE")
  460. ErrorMessage = true;
  461. }
  462. if ((toUpper(_param) == "IGNORELEADINGNAN") && (splitted.size() > 1))
  463. {
  464. if (toUpper(splitted[1]) == "TRUE")
  465. IgnoreLeadingNaN = true;
  466. }
  467. if ((toUpper(_param) == "PREVALUEAGESTARTUP") && (splitted.size() > 1))
  468. {
  469. PreValueAgeStartup = std::stoi(splitted[1]);
  470. }
  471. }
  472. if (PreValueUse) {
  473. LoadPreValue();
  474. }
  475. return true;
  476. }
  477. void ClassFlowPostProcessing::InitNUMBERS()
  478. {
  479. int anzDIGIT = 0;
  480. int anzANALOG = 0;
  481. std::vector<std::string> name_numbers;
  482. if (flowDigit)
  483. {
  484. anzDIGIT = flowDigit->getNumberGENERAL();
  485. flowDigit->UpdateNameNumbers(&name_numbers);
  486. }
  487. if (flowAnalog)
  488. {
  489. anzANALOG = flowAnalog->getNumberGENERAL();
  490. flowAnalog->UpdateNameNumbers(&name_numbers);
  491. }
  492. ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
  493. for (int _num = 0; _num < name_numbers.size(); ++_num)
  494. {
  495. NumberPost *_number = new NumberPost;
  496. _number->name = name_numbers[_num];
  497. _number->digit_roi = NULL;
  498. if (flowDigit)
  499. _number->digit_roi = flowDigit->FindGENERAL(name_numbers[_num]);
  500. if (_number->digit_roi)
  501. _number->AnzahlDigital = _number->digit_roi->ROI.size();
  502. else
  503. _number->AnzahlDigital = 0;
  504. _number->analog_roi = NULL;
  505. if (flowAnalog)
  506. _number->analog_roi = flowAnalog->FindGENERAL(name_numbers[_num]);
  507. if (_number->analog_roi)
  508. _number->AnzahlAnalog = _number->analog_roi->ROI.size();
  509. else
  510. _number->AnzahlAnalog = 0;
  511. _number->ReturnRawValue = ""; // Raw value (with N & leading 0).
  512. _number->ReturnValue = ""; // corrected return value, possibly with error message
  513. _number->ErrorMessageText = ""; // Error message for consistency check
  514. _number->ReturnPreValue = "";
  515. _number->PreValueOkay = false;
  516. _number->AllowNegativeRates = false;
  517. _number->MaxRateValue = 0.1;
  518. _number->RateType = AbsoluteChange;
  519. _number->useMaxRateValue = false;
  520. _number->checkDigitIncreaseConsistency = false;
  521. _number->DecimalShift = 0;
  522. _number->DecimalShiftInitial = 0;
  523. _number->isExtendedResolution = false;
  524. _number->AnalogDigitalTransitionStart=9.2;
  525. _number->FlowRateAct = 0; // m3 / min
  526. _number->PreValue = 0; // last value read out well
  527. _number->Value = 0; // last value read out, incl. corrections
  528. _number->ReturnRawValue = ""; // raw value (with N & leading 0)
  529. _number->ReturnValue = ""; // corrected return value, possibly with error message
  530. _number->ErrorMessageText = ""; // Error message for consistency check
  531. _number->Nachkomma = _number->AnzahlAnalog;
  532. NUMBERS.push_back(_number);
  533. }
  534. for (int i = 0; i < NUMBERS.size(); ++i) {
  535. ESP_LOGD(TAG, "Number %s, Anz DIG: %d, Anz ANA %d", NUMBERS[i]->name.c_str(), NUMBERS[i]->AnzahlDigital, NUMBERS[i]->AnzahlAnalog);
  536. }
  537. }
  538. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  539. if (_decShift == 0){
  540. return in;
  541. }
  542. int _pos_dec_org, _pos_dec_neu;
  543. _pos_dec_org = findDelimiterPos(in, ".");
  544. if (_pos_dec_org == std::string::npos) {
  545. _pos_dec_org = in.length();
  546. }
  547. else
  548. {
  549. in = in.erase(_pos_dec_org, 1);
  550. }
  551. _pos_dec_neu = _pos_dec_org + _decShift;
  552. if (_pos_dec_neu <= 0) { // comma is before the first digit
  553. for (int i = 0; i > _pos_dec_neu; --i){
  554. in = in.insert(0, "0");
  555. }
  556. in = "0." + in;
  557. return in;
  558. }
  559. if (_pos_dec_neu > in.length()){ // Comma should be after string (123 --> 1230)
  560. for (int i = in.length(); i < _pos_dec_neu; ++i){
  561. in = in.insert(in.length(), "0");
  562. }
  563. return in;
  564. }
  565. string zw;
  566. zw = in.substr(0, _pos_dec_neu);
  567. zw = zw + ".";
  568. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  569. return zw;
  570. }
  571. bool ClassFlowPostProcessing::doFlow(string zwtime)
  572. {
  573. string result = "";
  574. string digit = "";
  575. string analog = "";
  576. string zwvalue;
  577. string zw;
  578. time_t imagetime = 0;
  579. string rohwert;
  580. // Update decimal point, as the decimal places can also change when changing from CNNType Auto --> xyz:
  581. imagetime = flowMakeImage->getTimeImageTaken();
  582. if (imagetime == 0)
  583. time(&imagetime);
  584. struct tm* timeinfo;
  585. timeinfo = localtime(&imagetime);
  586. char strftime_buf[64];
  587. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S", timeinfo);
  588. zwtime = std::string(strftime_buf);
  589. ESP_LOGD(TAG, "Quantity NUMBERS: %d", NUMBERS.size());
  590. for (int j = 0; j < NUMBERS.size(); ++j)
  591. {
  592. NUMBERS[j]->ReturnRawValue = "";
  593. NUMBERS[j]->ReturnRateValue = "";
  594. NUMBERS[j]->ReturnValue = "";
  595. NUMBERS[j]->ErrorMessageText = "";
  596. NUMBERS[j]->Value = -1;
  597. /* calculate time difference BEFORE we overwrite the 'lastvalue' */
  598. double difference = difftime(imagetime, NUMBERS[j]->lastvalue); // in seconds
  599. /* TODO:
  600. * We could call `NUMBERS[j]->lastvalue = imagetime;` here and remove all other such calls further down.
  601. * But we should check nothing breaks! */
  602. UpdateNachkommaDecimalShift();
  603. int previous_value = -1;
  604. if (NUMBERS[j]->analog_roi)
  605. {
  606. NUMBERS[j]->ReturnRawValue = flowAnalog->getReadout(j, NUMBERS[j]->isExtendedResolution);
  607. if (NUMBERS[j]->ReturnRawValue.length() > 0)
  608. {
  609. char zw = NUMBERS[j]->ReturnRawValue[0];
  610. if (zw >= 48 && zw <=57)
  611. previous_value = zw - 48;
  612. }
  613. }
  614. #ifdef SERIAL_DEBUG
  615. ESP_LOGD(TAG, "After analog->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  616. #endif
  617. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  618. NUMBERS[j]->ReturnRawValue = "." + NUMBERS[j]->ReturnRawValue;
  619. if (NUMBERS[j]->digit_roi)
  620. {
  621. if (NUMBERS[j]->analog_roi)
  622. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, false, previous_value, NUMBERS[j]->analog_roi->ROI[0]->result_float, NUMBERS[j]->AnalogDigitalTransitionStart) + NUMBERS[j]->ReturnRawValue;
  623. else
  624. NUMBERS[j]->ReturnRawValue = flowDigit->getReadout(j, NUMBERS[j]->isExtendedResolution, previous_value); // Extended Resolution only if there are no analogue digits
  625. }
  626. #ifdef SERIAL_DEBUG
  627. ESP_LOGD(TAG, "After digital->getReadout: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  628. #endif
  629. NUMBERS[j]->ReturnRawValue = ShiftDecimal(NUMBERS[j]->ReturnRawValue, NUMBERS[j]->DecimalShift);
  630. #ifdef SERIAL_DEBUG
  631. ESP_LOGD(TAG, "After ShiftDecimal: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  632. #endif
  633. if (IgnoreLeadingNaN)
  634. while ((NUMBERS[j]->ReturnRawValue.length() > 1) && (NUMBERS[j]->ReturnRawValue[0] == 'N'))
  635. NUMBERS[j]->ReturnRawValue.erase(0, 1);
  636. #ifdef SERIAL_DEBUG
  637. ESP_LOGD(TAG, "After IgnoreLeadingNaN: ReturnRaw %s", NUMBERS[j]->ReturnRawValue.c_str());
  638. #endif
  639. NUMBERS[j]->ReturnValue = NUMBERS[j]->ReturnRawValue;
  640. if (findDelimiterPos(NUMBERS[j]->ReturnValue, "N") != std::string::npos)
  641. {
  642. if (PreValueUse && NUMBERS[j]->PreValueOkay)
  643. {
  644. NUMBERS[j]->ReturnValue = ErsetzteN(NUMBERS[j]->ReturnValue, NUMBERS[j]->PreValue);
  645. }
  646. else
  647. {
  648. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  649. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  650. /* TODO to be discussed, see https://github.com/jomjol/AI-on-the-edge-device/issues/1617 */
  651. NUMBERS[j]->lastvalue = imagetime;
  652. WriteDataLog(j);
  653. continue; // there is no number because there is still an N.
  654. }
  655. }
  656. #ifdef SERIAL_DEBUG
  657. ESP_LOGD(TAG, "After findDelimiterPos: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  658. #endif
  659. // Delete leading zeros (unless there is only one 0 left)
  660. while ((NUMBERS[j]->ReturnValue.length() > 1) && (NUMBERS[j]->ReturnValue[0] == '0'))
  661. NUMBERS[j]->ReturnValue.erase(0, 1);
  662. #ifdef SERIAL_DEBUG
  663. ESP_LOGD(TAG, "After removeLeadingZeros: ReturnValue %s", NUMBERS[j]->ReturnRawValue.c_str());
  664. #endif
  665. NUMBERS[j]->Value = std::stod(NUMBERS[j]->ReturnValue);
  666. #ifdef SERIAL_DEBUG
  667. ESP_LOGD(TAG, "After setting the Value: Value %f and as double is %f", NUMBERS[j]->Value, std::stod(NUMBERS[j]->ReturnValue));
  668. #endif
  669. if (NUMBERS[j]->checkDigitIncreaseConsistency)
  670. {
  671. if (flowDigit)
  672. {
  673. if (flowDigit->getCNNType() != Digital)
  674. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - ignored due to wrong CNN-Type (not Digital Classification)");
  675. else
  676. NUMBERS[j]->Value = checkDigitConsistency(NUMBERS[j]->Value, NUMBERS[j]->DecimalShift, NUMBERS[j]->analog_roi != NULL, NUMBERS[j]->PreValue);
  677. }
  678. else
  679. {
  680. #ifdef SERIAL_DEBUG
  681. ESP_LOGD(TAG, "checkDigitIncreaseConsistency = true - no digital numbers defined!");
  682. #endif
  683. }
  684. }
  685. #ifdef SERIAL_DEBUG
  686. ESP_LOGD(TAG, "After checkDigitIncreaseConsistency: Value %f", NUMBERS[j]->Value);
  687. #endif
  688. if (!NUMBERS[j]->AllowNegativeRates)
  689. {
  690. if ((NUMBERS[j]->Value < NUMBERS[j]->PreValue))
  691. {
  692. #ifdef SERIAL_DEBUG
  693. ESP_LOGD(TAG, "Neg: value=%f, preValue=%f, preToll%f", NUMBERS[j]->Value, NUMBERS[j]->PreValue,
  694. NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))
  695. ) ;
  696. #endif
  697. // Include inaccuracy of 0.2 for isExtendedResolution.
  698. if (NUMBERS[j]->Value >= (NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))) && NUMBERS[j]->isExtendedResolution) {
  699. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  700. NUMBERS[j]->ReturnValue = to_string(NUMBERS[j]->PreValue);
  701. } else {
  702. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " ";
  703. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  704. NUMBERS[j]->ReturnValue = "";
  705. NUMBERS[j]->lastvalue = imagetime;
  706. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  707. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  708. WriteDataLog(j);
  709. continue;
  710. }
  711. }
  712. }
  713. #ifdef SERIAL_DEBUG
  714. ESP_LOGD(TAG, "After AllowNegativeRates: Value %f", NUMBERS[j]->Value);
  715. #endif
  716. difference /= 60;
  717. NUMBERS[j]->FlowRateAct = (NUMBERS[j]->Value - NUMBERS[j]->PreValue) / difference;
  718. NUMBERS[j]->ReturnRateValue = to_string(NUMBERS[j]->FlowRateAct);
  719. if (NUMBERS[j]->useMaxRateValue && PreValueUse && NUMBERS[j]->PreValueOkay)
  720. {
  721. double _ratedifference;
  722. if (NUMBERS[j]->RateType == RateChange)
  723. _ratedifference = NUMBERS[j]->FlowRateAct;
  724. else
  725. _ratedifference = (NUMBERS[j]->Value - NUMBERS[j]->PreValue);
  726. if (abs(_ratedifference) > abs(NUMBERS[j]->MaxRateValue))
  727. {
  728. NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Rate too high - Read: " + RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma) + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " - Rate: " + RundeOutput(_ratedifference, NUMBERS[j]->Nachkomma);
  729. NUMBERS[j]->Value = NUMBERS[j]->PreValue;
  730. NUMBERS[j]->ReturnValue = "";
  731. NUMBERS[j]->ReturnRateValue = "";
  732. NUMBERS[j]->lastvalue = imagetime;
  733. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  734. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zw);
  735. WriteDataLog(j);
  736. continue;
  737. }
  738. }
  739. #ifdef SERIAL_DEBUG
  740. ESP_LOGD(TAG, "After MaxRateCheck: Value %f", NUMBERS[j]->Value);
  741. #endif
  742. NUMBERS[j]->ReturnChangeAbsolute = RundeOutput(NUMBERS[j]->Value - NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  743. NUMBERS[j]->PreValue = NUMBERS[j]->Value;
  744. NUMBERS[j]->PreValueOkay = true;
  745. NUMBERS[j]->lastvalue = imagetime;
  746. NUMBERS[j]->ReturnValue = RundeOutput(NUMBERS[j]->Value, NUMBERS[j]->Nachkomma);
  747. NUMBERS[j]->ReturnPreValue = RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma);
  748. NUMBERS[j]->ErrorMessageText = "no error";
  749. UpdatePreValueINI = true;
  750. string _zw = NUMBERS[j]->name + ": Raw: " + NUMBERS[j]->ReturnRawValue + ", Value: " + NUMBERS[j]->ReturnValue + ", Status: " + NUMBERS[j]->ErrorMessageText;
  751. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  752. WriteDataLog(j);
  753. }
  754. SavePreValue();
  755. return true;
  756. }
  757. void ClassFlowPostProcessing::WriteDataLog(int _index)
  758. {
  759. if (!LogFile.GetDataLogToSD()){
  760. return;
  761. }
  762. string analog = "";
  763. string digital = "";
  764. string timezw = "";
  765. char buffer[80];
  766. struct tm* timeinfo = localtime(&NUMBERS[_index]->lastvalue);
  767. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  768. timezw = std::string(buffer);
  769. if (flowAnalog)
  770. analog = flowAnalog->getReadoutRawString(_index);
  771. if (flowDigit)
  772. digital = flowDigit->getReadoutRawString(_index);
  773. LogFile.WriteToData(timezw, NUMBERS[_index]->name,
  774. NUMBERS[_index]->ReturnRawValue, NUMBERS[_index]->ReturnValue, NUMBERS[_index]->ReturnPreValue,
  775. NUMBERS[_index]->ReturnRateValue, NUMBERS[_index]->ReturnChangeAbsolute,
  776. NUMBERS[_index]->ErrorMessageText,
  777. digital, analog);
  778. 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());
  779. }
  780. void ClassFlowPostProcessing::UpdateNachkommaDecimalShift()
  781. {
  782. for (int j = 0; j < NUMBERS.size(); ++j)
  783. {
  784. if (NUMBERS[j]->digit_roi && !NUMBERS[j]->analog_roi) // There are only digital digits
  785. {
  786. // ESP_LOGD(TAG, "Nurdigital");
  787. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  788. if (NUMBERS[j]->isExtendedResolution && flowDigit->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  789. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  790. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  791. }
  792. if (!NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi)
  793. {
  794. // ESP_LOGD(TAG, "Nur analog");
  795. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  796. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution())
  797. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShift-1;
  798. NUMBERS[j]->Nachkomma = -NUMBERS[j]->DecimalShift;
  799. }
  800. if (NUMBERS[j]->digit_roi && NUMBERS[j]->analog_roi) // digital + analog
  801. {
  802. // ESP_LOGD(TAG, "Nur digital + analog");
  803. NUMBERS[j]->DecimalShift = NUMBERS[j]->DecimalShiftInitial;
  804. NUMBERS[j]->Nachkomma = NUMBERS[j]->analog_roi->ROI.size() - NUMBERS[j]->DecimalShift;
  805. if (NUMBERS[j]->isExtendedResolution && flowAnalog->isExtendedResolution()) // Extended resolution is on and should also be used for this digit.
  806. NUMBERS[j]->Nachkomma = NUMBERS[j]->Nachkomma+1;
  807. }
  808. ESP_LOGD(TAG, "UpdateNachkommaDecShift NUMBER%i: Nachkomma %i, DecShift %i", j, NUMBERS[j]->Nachkomma,NUMBERS[j]->DecimalShift);
  809. }
  810. }
  811. string ClassFlowPostProcessing::getReadout(int _number)
  812. {
  813. return NUMBERS[_number]->ReturnValue;
  814. }
  815. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror, int _number)
  816. {
  817. if (_rawValue)
  818. return NUMBERS[_number]->ReturnRawValue;
  819. if (_noerror)
  820. return NUMBERS[_number]->ReturnValue;
  821. return NUMBERS[_number]->ReturnValue;
  822. }
  823. string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue)
  824. {
  825. int posN, posPunkt;
  826. int pot, ziffer;
  827. float zw;
  828. posN = findDelimiterPos(input, "N");
  829. posPunkt = findDelimiterPos(input, ".");
  830. if (posPunkt == std::string::npos){
  831. posPunkt = input.length();
  832. }
  833. while (posN != std::string::npos)
  834. {
  835. if (posN < posPunkt) {
  836. pot = posPunkt - posN - 1;
  837. }
  838. else {
  839. pot = posPunkt - posN;
  840. }
  841. zw =_prevalue / pow(10, pot);
  842. ziffer = ((int) zw) % 10;
  843. input[posN] = ziffer + 48;
  844. posN = findDelimiterPos(input, "N");
  845. }
  846. return input;
  847. }
  848. float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue){
  849. int aktdigit, olddigit;
  850. int aktdigit_before, olddigit_before;
  851. int pot, pot_max;
  852. float zw;
  853. bool no_nulldurchgang = false;
  854. pot = _decilamshift;
  855. if (!_isanalog) // if there are no analogue values, the last one cannot be evaluated
  856. {
  857. pot++;
  858. }
  859. #ifdef SERIAL_DEBUG
  860. ESP_LOGD(TAG, "checkDigitConsistency: pot=%d, decimalshift=%d", pot, _decilamshift);
  861. #endif
  862. pot_max = ((int) log10(input)) + 1;
  863. while (pot <= pot_max)
  864. {
  865. zw = input / pow(10, pot-1);
  866. aktdigit_before = ((int) zw) % 10;
  867. zw = _preValue / pow(10, pot-1);
  868. olddigit_before = ((int) zw) % 10;
  869. zw = input / pow(10, pot);
  870. aktdigit = ((int) zw) % 10;
  871. zw = _preValue / pow(10, pot);
  872. olddigit = ((int) zw) % 10;
  873. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  874. if (no_nulldurchgang)
  875. {
  876. if (aktdigit != olddigit)
  877. {
  878. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // New Digit is replaced by old Digit;
  879. }
  880. }
  881. else
  882. {
  883. if (aktdigit == olddigit) // despite zero crossing, digit was not incremented --> add 1
  884. {
  885. input = input + ((float) (1)) * pow(10, pot); // add 1 at the point
  886. }
  887. }
  888. #ifdef SERIAL_DEBUG
  889. ESP_LOGD(TAG, "checkDigitConsistency: input=%f", input);
  890. #endif
  891. pot++;
  892. }
  893. return input;
  894. }
  895. string ClassFlowPostProcessing::getReadoutRate(int _number)
  896. {
  897. return std::to_string(NUMBERS[_number]->FlowRateAct);
  898. }
  899. string ClassFlowPostProcessing::getReadoutTimeStamp(int _number)
  900. {
  901. return NUMBERS[_number]->timeStamp;
  902. }
  903. string ClassFlowPostProcessing::getReadoutError(int _number)
  904. {
  905. return NUMBERS[_number]->ErrorMessageText;
  906. }