ClassFlowPostProcessing.cpp 12 KB

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