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