ClassFlowPostProcessing.cpp 12 KB

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