ClassFlowPostProcessing.cpp 13 KB

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