ClassFlowPostProcessing.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 AnzahlNachkomma = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  18. result = RundeOutput(PreValue, AnzahlNachkomma + 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 AnzahlNachkomma = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  66. ReturnValue = RundeOutput(Value, AnzahlNachkomma + 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. PreValueOkay = LoadPreValue();
  144. }
  145. }
  146. if ((toUpper(zerlegt[0]) == "CHECKDIGITINCREASECONSISTENCY") && (zerlegt.size() > 1))
  147. {
  148. if (toUpper(zerlegt[1]) == "TRUE")
  149. checkDigitIncreaseConsistency = true;
  150. }
  151. if ((toUpper(zerlegt[0]) == "ALLOWNEGATIVERATES") && (zerlegt.size() > 1))
  152. {
  153. if (toUpper(zerlegt[1]) == "TRUE")
  154. AllowNegativeRates = true;
  155. }
  156. if ((toUpper(zerlegt[0]) == "ERRORMESSAGE") && (zerlegt.size() > 1))
  157. {
  158. if (toUpper(zerlegt[1]) == "TRUE")
  159. ErrorMessage = true;
  160. }
  161. if ((toUpper(zerlegt[0]) == "PREVALUEAGESTARTUP") && (zerlegt.size() > 1))
  162. {
  163. PreValueAgeStartup = std::stoi(zerlegt[1]);
  164. }
  165. if ((toUpper(zerlegt[0]) == "MAXRATEVALUE") && (zerlegt.size() > 1))
  166. {
  167. useMaxRateValue = true;
  168. MaxRateValue = std::stof(zerlegt[1]);
  169. }
  170. }
  171. return true;
  172. }
  173. string ClassFlowPostProcessing::ShiftDecimal(string in, int _decShift){
  174. if (_decShift == 0){
  175. return in;
  176. }
  177. int _pos_dec_org, _pos_dec_neu;
  178. _pos_dec_org = findDelimiterPos(in, ".");
  179. if (_pos_dec_org == std::string::npos) {
  180. _pos_dec_org = in.length();
  181. }
  182. else
  183. {
  184. in = in.erase(_pos_dec_org, 1);
  185. }
  186. _pos_dec_neu = _pos_dec_org + _decShift;
  187. if (_pos_dec_neu <= 0) { // Komma ist vor der ersten Ziffer
  188. for (int i = 0; i > _pos_dec_neu; --i){
  189. in = in.insert(0, "0");
  190. }
  191. in = "0." + in;
  192. return in;
  193. }
  194. if (_pos_dec_neu > in.length()){ // Komma soll hinter String (123 --> 1230)
  195. for (int i = in.length(); i < _pos_dec_neu; ++i){
  196. in = in.insert(in.length(), "0");
  197. }
  198. return in;
  199. }
  200. string zw;
  201. zw = in.substr(0, _pos_dec_neu);
  202. zw = zw + ".";
  203. zw = zw + in.substr(_pos_dec_neu, in.length() - _pos_dec_neu);
  204. return zw;
  205. }
  206. bool ClassFlowPostProcessing::doFlow(string zwtime)
  207. {
  208. string result = "";
  209. string digit = "";
  210. string analog = "";
  211. string zwvalue;
  212. bool isdigit = false;
  213. bool isanalog = false;
  214. int AnzahlNachkomma = 0;
  215. string zw;
  216. string error = "";
  217. time_t imagetime = 0;
  218. for (int i = 0; i < ListFlowControll->size(); ++i)
  219. {
  220. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  221. {
  222. imagetime = ((ClassFlowMakeImage*)(*ListFlowControll)[i])->getTimeImageTaken();
  223. }
  224. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  225. {
  226. isdigit = true;
  227. digit = (*ListFlowControll)[i]->getReadout();
  228. }
  229. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  230. {
  231. isanalog = true;
  232. analog = (*ListFlowControll)[i]->getReadout();
  233. AnzahlNachkomma = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  234. }
  235. }
  236. if (imagetime == 0)
  237. time(&imagetime);
  238. struct tm* timeinfo;
  239. timeinfo = localtime(&imagetime);
  240. char strftime_buf[64];
  241. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H-%M-%S", timeinfo);
  242. zwtime = std::string(strftime_buf);
  243. // // TESTING ONLY////////////////////
  244. // isdigit = true; digit = "12N";
  245. // isanalog = true; analog = "456";
  246. if (isdigit)
  247. ReturnRawValue = digit;
  248. if (isdigit && isanalog)
  249. ReturnRawValue = ReturnRawValue + ".";
  250. if (isanalog)
  251. ReturnRawValue = ReturnRawValue + analog;
  252. ReturnRawValue = ShiftDecimal(ReturnRawValue, DecimalShift);
  253. if (!PreValueUse || !PreValueOkay)
  254. {
  255. ReturnValue = ReturnRawValue;
  256. ReturnValueNoError = ReturnRawValue;
  257. if ((findDelimiterPos(ReturnValue, "N") == std::string::npos) && (ReturnValue.length() > 0))
  258. {
  259. while ((ReturnValue.length() > 1) && (ReturnValue[0] == '0'))
  260. {
  261. ReturnValue.erase(0, 1);
  262. }
  263. Value = std::stof(ReturnValue);
  264. ReturnValueNoError = ReturnValue;
  265. SavePreValue(Value, zwtime);
  266. }
  267. return true;
  268. }
  269. if (isdigit)
  270. {
  271. int lastanalog = -1;
  272. if (isanalog)
  273. lastanalog = analog[0] - 48;
  274. digit = ErsetzteN(digit, lastanalog);
  275. zw = digit;
  276. }
  277. if (isdigit && isanalog)
  278. zw = zw + ".";
  279. if (isanalog)
  280. zw = zw + analog;
  281. Value = std::stof(zw);
  282. zwvalue = RundeOutput(Value, AnzahlNachkomma + DecimalShift);
  283. if ((!AllowNegativeRates) && (Value < PreValue))
  284. {
  285. error = "Negative Rate - Returned old value - read value: " + zwvalue;
  286. Value = PreValue;
  287. zwvalue = RundeOutput(Value, AnzahlNachkomma + DecimalShift);
  288. }
  289. else
  290. {
  291. if (useMaxRateValue && (abs(Value - PreValue) > MaxRateValue))
  292. {
  293. error = "Rate too high - Returned old value - read value: " + zwvalue;
  294. Value = PreValue;
  295. zwvalue = RundeOutput(Value, AnzahlNachkomma + DecimalShift);
  296. }
  297. }
  298. ReturnValueNoError = zwvalue;
  299. ReturnValue = zwvalue;
  300. if (ErrorMessage && (error.length() > 0))
  301. ReturnValue = ReturnValue + "\t" + error;
  302. if (error.length() == 0)
  303. SavePreValue(Value, zwtime);
  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. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  321. return stream.str();
  322. }
  323. string ClassFlowPostProcessing::ErsetzteN(string input, int lastvalueanalog = -1)
  324. {
  325. int posN, posPunkt;
  326. int pot, ziffer;
  327. float zw;
  328. posN = findDelimiterPos(input, "N");
  329. posPunkt = input.length();
  330. while (posN != std::string::npos)
  331. {
  332. pot = posPunkt - posN - 1;
  333. zw = PreValue / pow(10, pot);
  334. ziffer = ((int) zw) % 10;
  335. input[posN] = ziffer + 48;
  336. posN = findDelimiterPos(input, "N");
  337. }
  338. ///////////////////////////// TestCode
  339. /*
  340. input = "10";
  341. posPunkt = input.length();
  342. PreValue = 9.5;
  343. lastvalueanalog = 7;
  344. */
  345. if (checkDigitIncreaseConsistency && lastvalueanalog > -1)
  346. {
  347. int zifferIST;
  348. // int substrakt = 0;
  349. bool lastcorrected = false;
  350. for (int i = input.length() - 1; i >= 0; --i)
  351. {
  352. zifferIST = input[i] - 48; //std::stoi(std::string(input[i]));
  353. if (lastcorrected)
  354. {
  355. zifferIST--;
  356. input[i] = zifferIST + 48;
  357. lastcorrected = false;
  358. }
  359. pot = posPunkt - i - 1;
  360. zw = PreValue / pow(10, pot);
  361. ziffer = ((int) zw) % 10;
  362. if (zifferIST < ziffer)
  363. {
  364. if (lastvalueanalog >= 7)
  365. {
  366. input[i] = ziffer + 48;
  367. lastvalueanalog = ziffer;
  368. lastcorrected = true;
  369. }
  370. }
  371. }
  372. }
  373. return input;
  374. }