ClassFlowPostProcessing.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. std::stringstream stream;
  19. stream << std::fixed << std::setprecision(AnzahlNachkomma) << PreValue;
  20. result = stream.str();
  21. }
  22. }
  23. return result;
  24. }
  25. bool ClassFlowPostProcessing::LoadPreValue(void)
  26. {
  27. FILE* pFile;
  28. char zw[1024];
  29. string zwtime, zwvalue;
  30. pFile = fopen(FilePreValue.c_str(), "r");
  31. if (pFile == NULL)
  32. return false;
  33. fgets(zw, 1024, pFile);
  34. printf("%s", zw);
  35. zwtime = trim(std::string(zw));
  36. fgets(zw, 1024, 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 AnzahlNachkomma = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  68. std::stringstream stream;
  69. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  70. ReturnValue = stream.str();
  71. ReturnValueNoError = ReturnValue;
  72. }
  73. }
  74. return true;
  75. }
  76. void ClassFlowPostProcessing::SavePreValue(float value, string zwtime)
  77. {
  78. FILE* pFile;
  79. PreValue = value;
  80. pFile = fopen(FilePreValue.c_str(), "w");
  81. if (strlen(zwtime.c_str()) == 0)
  82. {
  83. time_t rawtime;
  84. struct tm* timeinfo;
  85. char buffer[80];
  86. time(&rawtime);
  87. timeinfo = localtime(&rawtime);
  88. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  89. zwtime = std::string(buffer);
  90. }
  91. fputs(zwtime.c_str(), pFile);
  92. fputs("\n", pFile);
  93. fputs(to_string(value).c_str(), pFile);
  94. fputs("\n", pFile);
  95. fclose(pFile);
  96. }
  97. ClassFlowPostProcessing::ClassFlowPostProcessing()
  98. {
  99. PreValueUse = false;
  100. PreValueAgeStartup = 30;
  101. AllowNegativeRates = false;
  102. MaxRateValue = 0.1;
  103. ErrorMessage = false;
  104. ListFlowControll = NULL;
  105. PreValueOkay = false;
  106. useMaxRateValue = false;
  107. checkDigitIncreaseConsistency = false;
  108. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  109. }
  110. ClassFlowPostProcessing::ClassFlowPostProcessing(std::vector<ClassFlow*>* lfc)
  111. {
  112. PreValueUse = false;
  113. PreValueAgeStartup = 30;
  114. AllowNegativeRates = false;
  115. MaxRateValue = 0.1;
  116. ErrorMessage = false;
  117. ListFlowControll = NULL;
  118. PreValueOkay = false;
  119. useMaxRateValue = false;
  120. checkDigitIncreaseConsistency = false;
  121. FilePreValue = FormatFileName("/sdcard/config/prevalue.ini");
  122. ListFlowControll = lfc;
  123. }
  124. bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
  125. {
  126. std::vector<string> zerlegt;
  127. aktparamgraph = trim(aktparamgraph);
  128. if (aktparamgraph.size() == 0)
  129. if (!this->GetNextParagraph(pfile, aktparamgraph))
  130. return false;
  131. if (aktparamgraph.compare("[PostProcessing]") != 0) // Paragraph passt nich zu MakeImage
  132. return false;
  133. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  134. {
  135. zerlegt = this->ZerlegeZeile(aktparamgraph);
  136. if ((zerlegt[0] == "PreValueUse") && (zerlegt.size() > 1))
  137. {
  138. if ((zerlegt[1] == "True") || (zerlegt[1] == "true"))
  139. {
  140. PreValueUse = true;
  141. PreValueOkay = LoadPreValue();
  142. }
  143. }
  144. if ((zerlegt[0] == "CheckDigitIncreaseConsistency") && (zerlegt.size() > 1))
  145. {
  146. if (toUpper(zerlegt[1]) == "TRUE")
  147. checkDigitIncreaseConsistency = true;
  148. }
  149. if ((zerlegt[0] == "AllowNegativeRates") && (zerlegt.size() > 1))
  150. {
  151. if (toUpper(zerlegt[1]) == "TRUE")
  152. AllowNegativeRates = true;
  153. }
  154. if ((zerlegt[0] == "ErrorMessage") && (zerlegt.size() > 1))
  155. {
  156. if (toUpper(zerlegt[1]) == "TRUE")
  157. ErrorMessage = true;
  158. }
  159. if ((zerlegt[0] == "PreValueAgeStartup") && (zerlegt.size() > 1))
  160. {
  161. PreValueAgeStartup = std::stoi(zerlegt[1]);
  162. }
  163. if ((zerlegt[0] == "MaxRateValue") && (zerlegt.size() > 1))
  164. {
  165. useMaxRateValue = true;
  166. MaxRateValue = std::stof(zerlegt[1]);
  167. }
  168. }
  169. return true;
  170. }
  171. bool ClassFlowPostProcessing::doFlow(string zwtime)
  172. {
  173. string result = "";
  174. string digit = "";
  175. string analog = "";
  176. string zwvalue;
  177. bool isdigit = false;
  178. bool isanalog = false;
  179. int AnzahlNachkomma = 0;
  180. string zw;
  181. string error = "";
  182. time_t imagetime = 0;
  183. for (int i = 0; i < ListFlowControll->size(); ++i)
  184. {
  185. if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
  186. {
  187. imagetime = ((ClassFlowMakeImage*)(*ListFlowControll)[i])->getTimeImageTaken();
  188. }
  189. if (((*ListFlowControll)[i])->name().compare("ClassFlowDigit") == 0)
  190. {
  191. isdigit = true;
  192. digit = (*ListFlowControll)[i]->getReadout();
  193. }
  194. if (((*ListFlowControll)[i])->name().compare("ClassFlowAnalog") == 0)
  195. {
  196. isanalog = true;
  197. analog = (*ListFlowControll)[i]->getReadout();
  198. AnzahlNachkomma = ((ClassFlowAnalog*)(*ListFlowControll)[i])->AnzahlROIs();
  199. }
  200. }
  201. if (imagetime == 0)
  202. time(&imagetime);
  203. struct tm* timeinfo;
  204. timeinfo = localtime(&imagetime);
  205. char strftime_buf[64];
  206. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H-%M-%S", timeinfo);
  207. zwtime = std::string(strftime_buf);
  208. // // TESTING ONLY////////////////////
  209. // isdigit = true; digit = "12N";
  210. // isanalog = true; analog = "456";
  211. if (isdigit)
  212. ReturnRawValue = digit;
  213. if (isdigit && isanalog)
  214. ReturnRawValue = ReturnRawValue + ".";
  215. if (isanalog)
  216. ReturnRawValue = ReturnRawValue + analog;
  217. if (!PreValueUse || !PreValueOkay)
  218. {
  219. ReturnValue = ReturnRawValue;
  220. ReturnValueNoError = ReturnRawValue;
  221. if ((findDelimiterPos(ReturnValue, "N") == std::string::npos) && (ReturnValue.length() > 0))
  222. {
  223. while ((ReturnValue.length() > 1) && (ReturnValue[0] == '0'))
  224. {
  225. ReturnValue.erase(0, 1);
  226. }
  227. Value = std::stof(ReturnValue);
  228. ReturnValueNoError = ReturnValue;
  229. SavePreValue(Value, zwtime);
  230. }
  231. return true;
  232. }
  233. if (isdigit)
  234. {
  235. int lastanalog = -1;
  236. if (isanalog)
  237. lastanalog = analog[0] - 48;
  238. digit = ErsetzteN(digit, lastanalog);
  239. zw = digit;
  240. }
  241. if (isdigit && isanalog)
  242. zw = zw + ".";
  243. if (isanalog)
  244. zw = zw + analog;
  245. Value = std::stof(zw);
  246. std::stringstream stream;
  247. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  248. zwvalue = stream.str();
  249. if ((!AllowNegativeRates) && (Value < PreValue))
  250. {
  251. error = "Negative Rate - Returned old value - read value: " + zwvalue;
  252. Value = PreValue;
  253. stream.str("");
  254. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  255. zwvalue = stream.str();
  256. }
  257. else
  258. {
  259. if (useMaxRateValue && (abs(Value - PreValue) > MaxRateValue))
  260. {
  261. error = "Rate too high - Returned old value - read value: " + zwvalue;
  262. Value = PreValue;
  263. stream.str("");
  264. stream << std::fixed << std::setprecision(AnzahlNachkomma) << Value;
  265. zwvalue = stream.str();
  266. }
  267. }
  268. ReturnValueNoError = zwvalue;
  269. ReturnValue = zwvalue;
  270. if (ErrorMessage && (error.length() > 0))
  271. ReturnValue = ReturnValue + "\t" + error;
  272. if (error.length() == 0)
  273. SavePreValue(Value, zwtime);
  274. return true;
  275. }
  276. string ClassFlowPostProcessing::getReadout()
  277. {
  278. return ReturnValue;
  279. }
  280. string ClassFlowPostProcessing::getReadoutParam(bool _rawValue, bool _noerror)
  281. {
  282. if (_rawValue)
  283. return ReturnRawValue;
  284. if (_noerror)
  285. return ReturnValueNoError;
  286. return ReturnValue;
  287. }
  288. string ClassFlowPostProcessing::ErsetzteN(string input, int lastvalueanalog = -1)
  289. {
  290. int posN, posPunkt;
  291. int pot, ziffer;
  292. float zw;
  293. posN = findDelimiterPos(input, "N");
  294. posPunkt = input.length();
  295. while (posN != std::string::npos)
  296. {
  297. pot = posPunkt - posN - 1;
  298. zw = PreValue / pow(10, pot);
  299. ziffer = ((int) zw) % 10;
  300. input[posN] = ziffer + 48;
  301. posN = findDelimiterPos(input, "N");
  302. }
  303. ///////////////////////////// TestCode
  304. /*
  305. input = "10";
  306. posPunkt = input.length();
  307. PreValue = 9.5;
  308. lastvalueanalog = 7;
  309. */
  310. if (checkDigitIncreaseConsistency && lastvalueanalog > -1)
  311. {
  312. int zifferIST;
  313. int substrakt = 0;
  314. bool lastcorrected = false;
  315. for (int i = input.length() - 1; i >= 0; --i)
  316. {
  317. zifferIST = input[i] - 48; //std::stoi(std::string(input[i]));
  318. if (lastcorrected)
  319. {
  320. zifferIST--;
  321. input[i] = zifferIST + 48;
  322. lastcorrected = false;
  323. }
  324. pot = posPunkt - i - 1;
  325. zw = PreValue / pow(10, pot);
  326. ziffer = ((int) zw) % 10;
  327. if (zifferIST < ziffer)
  328. {
  329. if (lastvalueanalog >= 7)
  330. {
  331. input[i] = ziffer + 48;
  332. lastvalueanalog = ziffer;
  333. lastcorrected = true;
  334. }
  335. }
  336. }
  337. }
  338. return input;
  339. }