ClassFlowPostProcessing.cpp 10 KB

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