ClassFlowPostProcessing.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. PreValue = value;
  89. fputs(zwtime.c_str(), pFile);
  90. fputs("\n", pFile);
  91. fputs(to_string(value).c_str(), pFile);
  92. fputs("\n", pFile);
  93. fclose(pFile);
  94. }
  95. ClassFlowPostProcessing::ClassFlowPostProcessing()
  96. {
  97. PreValueUse = false;
  98. PreValueAgeStartup = 30;
  99. AllowNegativeRates = false;
  100. MaxRateValue = 0.1;
  101. ErrorMessage = false;
  102. ListFlowControll = NULL;
  103. PreValueOkay = false;
  104. useMaxRateValue = false;
  105. checkDigitIncreaseConsistency = false;
  106. DecimalShift = 0;
  107. ErrorMessageText = "";
  108. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  109. }
  110. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc)
  111. {
  112. PreValueUse = false;
  113. PreValueAgeStartup = 30;
  114. AllowNegativeRates = false;
  115. MaxRateValue = 0.1;
  116. ErrorMessage = false;
  117. ListFlowControll = NULL;
  118. PreValueOkay = false;
  119. useMaxRateValue = false;
  120. checkDigitIncreaseConsistency = false;
  121. DecimalShift = 0;
  122. ErrorMessageText = "";
  123. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  124. ListFlowControll = lfc;
  125. }
  126. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  127. {
  128. std::vector<string> zerlegt;
  129. aktparamgraph = trim(aktparamgraph);
  130. if (aktparamgraph.size() == 0)
  131. if (!this->GetNextParagraph(pfile, aktparamgraph))
  132. return false;
  133. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  134. return false;
  135. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  136. {
  137. zerlegt = this->ZerlegeZeile(aktparamgraph);
  138. if ((toUpper(zerlegt[0]) == "DECIMALSHIFT") && (zerlegt.size() > 1))
  139. {
  140. DecimalShift = stoi(zerlegt[1]);
  141. }
  142. if ((toUpper(zerlegt[0]) == "PREVALUEUSE") && (zerlegt.size() > 1))
  143. {
  144. if (toUpper(zerlegt[1]) == "TRUE")
  145. {
  146. PreValueUse = true;
  147. }
  148. }
  149. if ((toUpper(zerlegt[0]) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  150. {
  151. if (toUpper(zerlegt[1]) == "TRUE")
  152. checkDigitIncreaseConsistency = true;
  153. }
  154. if ((toUpper(zerlegt[0]) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  155. {
  156. if (toUpper(zerlegt[1]) == "TRUE")
  157. AllowNegativeRates = true;
  158. }
  159. if ((toUpper(zerlegt[0]) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  160. {
  161. if (toUpper(zerlegt[1]) == "TRUE")
  162. ErrorMessage = true;
  163. }
  164. if ((toUpper(zerlegt[0]) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  165. {
  166. PreValueAgeStartup = std::stoi(zerlegt[1]);
  167. }
  168. if ((toUpper(zerlegt[0]) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  169. {
  170. useMaxRateValue = true;
  171. MaxRateValue = std::stof(zerlegt[1]);
  172. }
  173. }
  174. if (PreValueUse) {
  175. PreValueOkay = LoadPreValue();
  176. }
  177. return true;
  178. }
  179. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  180. if (_decShift == 0){
  181. return in;
  182. }
  183. int _pos_dec_org, _pos_dec_neu;
  184. _pos_dec_org = findDelimiterPos(in, ".");
  185. if (_pos_dec_org == std::string::npos) {
  186. _pos_dec_org = in.length();
  187. }
  188. else
  189. {
  190. in = in.erase(_pos_dec_org, 1);
  191. }
  192. _pos_dec_neu = _pos_dec_org + _decShift;
  193. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  194. for (int i = 0; i > _pos_dec_neu; --i){
  195. in = in.insert(0, "0");
  196. }
  197. in = "0." + in;
  198. return in;
  199. }
  200. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  201. for (int i = in.length(); i < _pos_dec_neu; ++i){
  202. in = in.insert(in.length(), "0");
  203. }
  204. return in;
  205. }
  206. string zw;
  207. zw = in.substr(0, _pos_dec_neu);
  208. zw = zw + ".";
  209. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  210. return zw;
  211. }
  212. bool ClassFlowPostProcessing::doFlow(string zwtime)
  213. {
  214. string result = "";
  215. string digit = "";
  216. string analog = "";
  217. string zwvalue;
  218. bool isdigit = false;
  219. bool isanalog = false;
  220. int AnzahlAnalog = 0;
  221. string zw;
  222. time_t imagetime = 0;
  223. ErrorMessageText = "";
  224. for (int i = 0; i < ListFlowControll->size(); ++i)
  225. {
  226. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  227. {
  228. imagetime = ((ClassFlowMakeImage*)(*ListFlowControll)[i])->getTimeImageTaken();
  229. }
  230. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  231. {
  232. isdigit = true;
  233. digit = (*ListFlowControll)[i]->getReadout();
  234. }
  235. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  236. {
  237. isanalog = true;
  238. analog = (*ListFlowControll)[i]->getReadout();
  239. AnzahlAnalog = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  240. }
  241. }
  242. if (imagetime == 0)
  243. time(&imagetime);
  244. struct tm* timeinfo;
  245. timeinfo = localtime(&imagetime);
  246. char strftime_buf[64];
  247. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H-%M-%S", timeinfo);
  248. zwtime = std::string(strftime_buf);
  249. // // TESTING ONLY////////////////////
  250. // isdigit = true; digit = "12N";
  251. // isanalog = true; analog = "456";
  252. if (isdigit)
  253. ReturnRawValue = digit;
  254. if (isdigit && isanalog)
  255. ReturnRawValue = ReturnRawValue + ".";
  256. if (isanalog)
  257. ReturnRawValue = ReturnRawValue + analog;
  258. ReturnRawValue = ShiftDecimal(ReturnRawValue, DecimalShift);
  259. if (!PreValueUse || !PreValueOkay)
  260. {
  261. ReturnValue = ReturnRawValue;
  262. ReturnValueNoError = ReturnRawValue;
  263. if ((findDelimiterPos(ReturnValue, "N") == std::string::npos) && (ReturnValue.length() > 0))
  264. {
  265. while ((ReturnValue.length() > 1) && (ReturnValue[0] == '0'))
  266. {
  267. ReturnValue.erase(0, 1);
  268. }
  269. Value = std::stof(ReturnValue);
  270. ReturnValueNoError = ReturnValue;
  271. PreValueOkay = true;
  272. PreValue = Value;
  273. SavePreValue(Value, zwtime);
  274. }
  275. return true;
  276. }
  277. zw = ErsetzteN(ReturnRawValue);
  278. Value = std::stof(zw);
  279. if (checkDigitIncreaseConsistency)
  280. {
  281. Value = checkDigitConsistency(Value, DecimalShift, isanalog);
  282. }
  283. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  284. if ((!AllowNegativeRates) && (Value < PreValue))
  285. {
  286. ErrorMessageText = ErrorMessageText + "Negative Rate - Returned old value - read value: " + zwvalue + " ";
  287. Value = PreValue;
  288. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  289. }
  290. if (useMaxRateValue && (abs(Value - PreValue) > MaxRateValue))
  291. {
  292. ErrorMessageText = ErrorMessageText + "Rate too high - Returned old value - read value: " + zwvalue + " ";
  293. Value = PreValue;
  294. zwvalue = RundeOutput(Value, AnzahlAnalog - DecimalShift);
  295. }
  296. ReturnValueNoError = zwvalue;
  297. ReturnValue = zwvalue;
  298. if (ErrorMessage && (ErrorMessageText.length() > 0))
  299. ReturnValue = ReturnValue + "\t" + ErrorMessageText;
  300. if (ErrorMessageText.length() == 0)
  301. {
  302. PreValue = Value;
  303. SavePreValue(Value, zwtime);
  304. }
  305. return true;
  306. }
  307. string ClassFlowPostProcessing::getReadout()
  308. {
  309. return ReturnValue;
  310. }
  311. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror)
  312. {
  313. if (_rawValue)
  314. return ReturnRawValue;
  315. if (_noerror)
  316. return ReturnValueNoError;
  317. return ReturnValue;
  318. }
  319. string ClassFlowPostProcessing::RundeOutput(float _in, int _anzNachkomma){
  320. std::stringstream stream;
  321. if (_anzNachkomma < 0) {
  322. _anzNachkomma = 0;
  323. }
  324. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  325. return stream.str();
  326. }
  327. string ClassFlowPostProcessing::ErsetzteN(string input)
  328. {
  329. int posN, posPunkt;
  330. int pot, ziffer;
  331. float zw;
  332. posN = findDelimiterPos(input, "N");
  333. posPunkt = findDelimiterPos(input, ".");
  334. if (posPunkt == std::string::npos){
  335. posPunkt = input.length();
  336. }
  337. while (posN != std::string::npos)
  338. {
  339. if (posN < posPunkt) {
  340. pot = posPunkt - posN - 1;
  341. }
  342. else {
  343. pot = posPunkt - posN;
  344. }
  345. zw = PreValue / pow(10, pot);
  346. ziffer = ((int) zw) % 10;
  347. input[posN] = ziffer + 48;
  348. posN = findDelimiterPos(input, "N");
  349. }
  350. return input;
  351. }
  352. float ClassFlowPostProcessing::checkDigitConsistency(float input, int _decilamshift, bool _isanalog){
  353. int aktdigit, olddigit;
  354. int aktdigit_before, olddigit_before;
  355. int pot, pot_max;
  356. float zw;
  357. bool no_nulldurchgang = false;
  358. pot = _decilamshift;
  359. if (!_isanalog) // falls es keine analogwerte gibt, kann die letzte nicht bewertet werden
  360. {
  361. pot++;
  362. }
  363. pot_max = ((int) log10(input)) + 1;
  364. while (pot <= pot_max)
  365. {
  366. zw = input / pow(10, pot-1);
  367. aktdigit_before = ((int) zw) % 10;
  368. zw = PreValue / pow(10, pot-1);
  369. olddigit_before = ((int) zw) % 10;
  370. zw = input / pow(10, pot);
  371. aktdigit = ((int) zw) % 10;
  372. zw = PreValue / pow(10, pot);
  373. olddigit = ((int) zw) % 10;
  374. no_nulldurchgang = (olddigit_before <= aktdigit_before);
  375. if (no_nulldurchgang)
  376. {
  377. if (aktdigit != olddigit)
  378. {
  379. input = input + ((float) (olddigit - aktdigit)) * pow(10, pot); // Neue Digit wird durch alte Digit ersetzt;
  380. }
  381. }
  382. else
  383. {
  384. if (aktdigit == olddigit) // trotz Nulldurchgang wurde Stelle nicht hochgezählt --> addiere 1
  385. {
  386. input = input + ((float) (1)) * pow(10, pot); // addiere 1 an der Stelle
  387. }
  388. }
  389. pot++;
  390. }
  391. return input;
  392. }
  393. string ClassFlowPostProcessing::getReadoutError()
  394. {
  395. return ErrorMessageText;
  396. }