ClassFlowPostProcessing.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #include "ClassFlowPostProcessing.h"
  2. #include "Helper.h"
  3. #include "ClassFlowAnalog.h"
  4. #include "ClassFlowDigit.h"
  5. #include "ClassFlowMakeImage.h"
  6. #include "ClassLogFile.h"
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <time.h>
  10. string ClassFlowPostProcessing::GetPreValue()
  11. {
  12. std::string result;
  13. result = RundeOutput(PreValue, -DecimalShift);
  14. for (int i = 0; i < ListFlowControll->size(); ++i)
  15. {
  16. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  17. {
  18. int AnzahlAnalog = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  19. result = RundeOutput(PreValue, AnzahlAnalog - DecimalShift);
  20. }
  21. }
  22. return result;
  23. }
  24. bool ClassFlowPostProcessing::LoadPreValue(void)
  25. {
  26. FILE* pFile;
  27. char zw[1024];
  28. string zwtime, zwvalue;
  29. pFile = fopen(FilePreValue.c_str(), "r");
  30. if (pFile == NULL)
  31. return false;
  32. fgets(zw, 1024, pFile);
  33. printf("%s", zw);
  34. zwtime = trim(std::string(zw));
  35. fgets(zw, 1024, pFile);
  36. fclose(pFile);
  37. printf("%s", zw);
  38. zwvalue = trim(std::string(zw));
  39. PreValue = stof(zwvalue.c_str());
  40. time_t tStart;
  41. int yy, month, dd, hh, mm, ss;
  42. struct tm whenStart;
  43. sscanf(zwtime.c_str(), "%d-%d-%d_%d-%d-%d", &yy, &month, &dd, &hh, &mm, &ss);
  44. whenStart.tm_year = yy - 1900;
  45. whenStart.tm_mon = month - 1;
  46. whenStart.tm_mday = dd;
  47. whenStart.tm_hour = hh;
  48. whenStart.tm_min = mm;
  49. whenStart.tm_sec = ss;
  50. whenStart.tm_isdst = -1;
  51. tStart = mktime(&whenStart);
  52. time_t now;
  53. time(&now);
  54. localtime(&now);
  55. double difference = difftime(now, tStart);
  56. difference /= 60;
  57. if (difference > PreValueAgeStartup)
  58. return false;
  59. Value = PreValue;
  60. ReturnValue = to_string(Value);
  61. ReturnValueNoError = ReturnValue;
  62. // falls es Analog gibt, dann die Anzahl der Nachkommastellen feststellen und entsprechend runden:
  63. for (int i = 0; i < ListFlowControll->size(); ++i)
  64. {
  65. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  66. {
  67. int AnzahlAnalog = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  68. ReturnValue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  69. ReturnValueNoError = ReturnValue;
  70. }
  71. }
  72. return true;
  73. }
  74. void ClassFlowPostProcessing::SavePreValue(float value, string zwtime)
  75. {
  76. FILE* pFile;
  77. pFile = fopen(FilePreValue.c_str(), "w");
  78. if (strlen(zwtime.c_str()) == 0)
  79. {
  80. time_t rawtime;
  81. struct tm* timeinfo;
  82. char buffer[80];
  83. time(&rawtime);
  84. timeinfo = localtime(&rawtime);
  85. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  86. zwtime = std::string(buffer);
  87. }
  88. fputs(zwtime.c_str(), pFile);
  89. fputs("\n", pFile);
  90. fputs(to_string(value).c_str(), pFile);
  91. fputs("\n", pFile);
  92. fclose(pFile);
  93. }
  94. ClassFlowPostProcessing::ClassFlowPostProcessing()
  95. {
  96. PreValueUse = false;
  97. PreValueAgeStartup = 30;
  98. AllowNegativeRates = false;
  99. MaxRateValue = 0.1;
  100. ErrorMessage = false;
  101. ListFlowControll = NULL;
  102. PreValueOkay = false;
  103. useMaxRateValue = false;
  104. checkDigitIncreaseConsistency = false;
  105. DecimalShift = 0;
  106. ErrorMessageText = "";
  107. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  108. }
  109. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc)
  110. {
  111. PreValueUse = false;
  112. PreValueAgeStartup = 30;
  113. AllowNegativeRates = false;
  114. MaxRateValue = 0.1;
  115. ErrorMessage = false;
  116. ListFlowControll = NULL;
  117. PreValueOkay = false;
  118. useMaxRateValue = false;
  119. checkDigitIncreaseConsistency = false;
  120. DecimalShift = 0;
  121. ErrorMessageText = "";
  122. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  123. ListFlowControll = lfc;
  124. }
  125. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  126. {
  127. std::vector<string> zerlegt;
  128. aktparamgraph = trim(aktparamgraph);
  129. if (aktparamgraph.size() == 0)
  130. if (!this->GetNextParagraph(pfile, aktparamgraph))
  131. return false;
  132. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  133. return false;
  134. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  135. {
  136. zerlegt = this->ZerlegeZeile(aktparamgraph);
  137. if ((toUpper(zerlegt[0]) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  138. {
  139. DecimalShift = stoi(zerlegt[1]);
  140. }
  141. if ((toUpper(zerlegt[0]) == "PREVALUEUSE") && (zerlegt.size() > 1))
  142. {
  143. if (toUpper(zerlegt[1]) == "TRUE")
  144. {
  145. PreValueUse = true;
  146. }
  147. }
  148. if ((toUpper(zerlegt[0]) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  149. {
  150. if (toUpper(zerlegt[1]) == "TRUE")
  151. checkDigitIncreaseConsistency = true;
  152. }
  153. if ((toUpper(zerlegt[0]) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  154. {
  155. if (toUpper(zerlegt[1]) == "TRUE")
  156. AllowNegativeRates = true;
  157. }
  158. if ((toUpper(zerlegt[0]) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  159. {
  160. if (toUpper(zerlegt[1]) == "TRUE")
  161. ErrorMessage = true;
  162. }
  163. if ((toUpper(zerlegt[0]) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  164. {
  165. PreValueAgeStartup = std::stoi(zerlegt[1]);
  166. }
  167. if ((toUpper(zerlegt[0]) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  168. {
  169. useMaxRateValue = true;
  170. MaxRateValue = std::stof(zerlegt[1]);
  171. }
  172. }
  173. if (PreValueUse) {
  174. PreValueOkay = LoadPreValue();
  175. }
  176. return true;
  177. }
  178. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  179. if (_decShift == 0){
  180. return in;
  181. }
  182. int _pos_dec_org, _pos_dec_neu;
  183. _pos_dec_org = findDelimiterPos(in, ".");
  184. if (_pos_dec_org == std::string::npos) {
  185. _pos_dec_org = in.length();
  186. }
  187. else
  188. {
  189. in = in.erase(_pos_dec_org, 1);
  190. }
  191. _pos_dec_neu = _pos_dec_org + _decShift;
  192. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  193. for (int i = 0; i > _pos_dec_neu; --i){
  194. in = in.insert(0, "0");
  195. }
  196. in = "0." + in;
  197. return in;
  198. }
  199. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  200. for (int i = in.length(); i < _pos_dec_neu; ++i){
  201. in = in.insert(in.length(), "0");
  202. }
  203. return in;
  204. }
  205. string zw;
  206. zw = in.substr(0, _pos_dec_neu);
  207. zw = zw + ".";
  208. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  209. return zw;
  210. }
  211. bool ClassFlowPostProcessing::doFlow(string zwtime)
  212. {
  213. string result = "";
  214. string digit = "";
  215. string analog = "";
  216. string zwvalue;
  217. bool isdigit = false;
  218. bool isanalog = false;
  219. int AnzahlAnalog = 0;
  220. string zw;
  221. time_t imagetime = 0;
  222. ErrorMessageText = "";
  223. for (int i = 0; i < ListFlowControll->size(); ++i)
  224. {
  225. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  226. {
  227. imagetime = ((ClassFlowMakeImage*)(*ListFlowControll)[i])->getTimeImageTaken();
  228. }
  229. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  230. {
  231. isdigit = true;
  232. digit = (*ListFlowControll)[i]->getReadout();
  233. }
  234. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  235. {
  236. isanalog = true;
  237. analog = (*ListFlowControll)[i]->getReadout();
  238. AnzahlAnalog = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  239. }
  240. }
  241. if (imagetime == 0)
  242. time(&imagetime);
  243. struct tm* timeinfo;
  244. timeinfo = localtime(&imagetime);
  245. char strftime_buf[64];
  246. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H-%M-%S", timeinfo);
  247. zwtime = std::string(strftime_buf);
  248. // // TESTING ONLY////////////////////
  249. // isdigit = true; digit = "12N";
  250. // isanalog = true; analog = "456";
  251. if (isdigit)
  252. ReturnRawValue = digit;
  253. if (isdigit && isanalog)
  254. ReturnRawValue = ReturnRawValue + ".";
  255. if (isanalog)
  256. ReturnRawValue = ReturnRawValue + analog;
  257. ReturnRawValue = ShiftDecimal(ReturnRawValue, DecimalShift);
  258. if (!PreValueUse || !PreValueOkay)
  259. {
  260. ReturnValue = ReturnRawValue;
  261. ReturnValueNoError = ReturnRawValue;
  262. if ((findDelimiterPos(ReturnValue, "N") == std::string::npos) && (ReturnValue.length() > 0))
  263. {
  264. while ((ReturnValue.length() > 1) && (ReturnValue[0] == '0'))
  265. {
  266. ReturnValue.erase(0, 1);
  267. }
  268. Value = std::stof(ReturnValue);
  269. ReturnValueNoError = ReturnValue;
  270. PreValueOkay = true;
  271. PreValue = Value;
  272. SavePreValue(Value, zwtime);
  273. }
  274. return true;
  275. }
  276. zw = ErsetzteN(ReturnRawValue);
  277. Value = std::stof(zw);
  278. if (checkDigitIncreaseConsistency)
  279. {
  280. Value = checkDigitConsistency(Value, DecimalShift, isanalog);
  281. }
  282. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  283. if ((!AllowNegativeRates) && (Value < PreValue))
  284. {
  285. ErrorMessageText = ErrorMessageText + "Negative Rate - Returned old value - read value: " + zwvalue + " ";
  286. Value = PreValue;
  287. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  288. }
  289. if (useMaxRateValue && (abs(Value - PreValue) > MaxRateValue))
  290. {
  291. ErrorMessageText = ErrorMessageText + "Rate too high - Returned old value - read value: " + zwvalue + " ";
  292. Value = PreValue;
  293. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  294. }
  295. ReturnValueNoError = zwvalue;
  296. ReturnValue = zwvalue;
  297. if (ErrorMessage && (ErrorMessageText.length() > 0))
  298. ReturnValue = ReturnValue + "\t" + ErrorMessageText;
  299. if (ErrorMessageText.length() == 0)
  300. {
  301. PreValue = Value;
  302. SavePreValue(Value, zwtime);
  303. }
  304. return true;
  305. }
  306. string ClassFlowPostProcessing::getReadout()
  307. {
  308. return ReturnValue;
  309. }
  310. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror)
  311. {
  312. if (_rawValue)
  313. return ReturnRawValue;
  314. if (_noerror)
  315. return ReturnValueNoError;
  316. return ReturnValue;
  317. }
  318. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  319. std::stringstream stream;
  320. if (_anzNachkomma < 0) {
  321. _anzNachkomma = 0;
  322. }
  323. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  324. return stream.str();
  325. }
  326. string ClassFlowPostProcessing::ErsetzteN(string input)
  327. {
  328. int posN, posPunkt;
  329. int pot, ziffer;
  330. float zw;
  331. posN = findDelimiterPos(input, "N");
  332. posPunkt = findDelimiterPos(input, ".");
  333. if (posPunkt == std::string::npos){
  334. posPunkt = input.length();
  335. }
  336. while (posN != std::string::npos)
  337. {
  338. if (posN < posPunkt) {
  339. pot = posPunkt - posN - 1;
  340. }
  341. else {
  342. pot = posPunkt - posN;
  343. }
  344. zw = PreValue / pow(10, pot);
  345. ziffer = ((int) zw) % 10;
  346. input[posN] = ziffer + 48;
  347. posN = findDelimiterPos(input, "N");
  348. }
  349. return input;
  350. }
  351. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog){
  352. int aktdigit, olddigit;
  353. int aktdigit_before, olddigit_before;
  354. int pot, pot_max;
  355. float zw;
  356. bool no_nulldurchgang = false;
  357. pot = _decilamshift;
  358. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  359. {
  360. pot++;
  361. }
  362. pot_max = ((int) log10(input)) + 1;
  363. while (pot <= pot_max)
  364. {
  365. zw = input / pow(10, pot-1);
  366. aktdigit_before = ((int) zw) % 10;
  367. zw = PreValue / pow(10, pot-1);
  368. olddigit_before = ((int) zw) % 10;
  369. zw = input / pow(10, pot);
  370. aktdigit = ((int) zw) % 10;
  371. zw = PreValue / pow(10, pot);
  372. olddigit = ((int) zw) % 10;
  373. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  374. if (no_nulldurchgang)
  375. {
  376. if (aktdigit != olddigit)
  377. {
  378. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  379. }
  380. }
  381. else
  382. {
  383. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  384. {
  385. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  386. }
  387. }
  388. pot++;
  389. }
  390. return input;
  391. }
  392. string ClassFlowPostProcessing::getReadoutError()
  393. {
  394. return ErrorMessageText;
  395. }