TemperCheckImp.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "stdafx.h"
  2. #include "TemperCheckImp.h"
  3. TemperCheckImp::TemperCheckImp()
  4. {
  5. //ofstream ocout;
  6. //ocout.open("D:\\default constructor.txt");
  7. //ocout << "begin" << endl;
  8. //ocout << "end" << endl;
  9. //ocout.close();
  10. }
  11. TemperCheckImp::TemperCheckImp(float fSetTempHigh, float fSetTempLow, float fSetTempStdDev, float fSetGradientMean, float fSetGradientStdDev, int nSetQueueSize, char* path)
  12. {
  13. m_fTempHigh = fSetTempHigh;
  14. m_fTempLow = fSetTempLow;
  15. m_fTempStdDev = fSetTempStdDev;
  16. m_nQueueSize = nSetQueueSize;
  17. m_fGradientMean = fSetGradientMean;
  18. m_fGradientStdDev = fSetGradientStdDev;
  19. m_strLogPath = path;
  20. m_dqTemp.clear();// = new deque <float>;
  21. m_dqTempGradient.clear(); //= new deque <float>;
  22. m_mapTemp.clear();// = new map <double, float >;
  23. //ofstream ocout;
  24. //string file = "TempeCheckMethod2.log";
  25. //string Path = *LogPath + file;
  26. //ocout.open(Path);
  27. ////ocout.open(Path, ios::app);
  28. //PrintTime();
  29. //ocout << "test begin:" << endl;
  30. //PrintTime();
  31. //ocout << "the example of temperature map is:(time, temperature)" << endl;
  32. }
  33. TemperCheckImp::~TemperCheckImp(void)
  34. {
  35. m_dqTemp.clear();
  36. m_dqTempGradient.clear();
  37. m_mapTemp.clear();
  38. //ofstream ocout;
  39. //ocout.open("D:\\destructor.txt");
  40. //ocout << "begin" << endl;
  41. //if (LogPath)
  42. //{
  43. // delete LogPath;
  44. //}
  45. //if (dqTemp)
  46. //{
  47. // delete dqTemp;
  48. //}
  49. //if (dqTempGradient)
  50. //{
  51. // delete dqTempGradient;
  52. //}
  53. //if (mapTemp)
  54. //{
  55. // delete mapTemp;
  56. //}
  57. //ocout << "end" << endl;
  58. //ocout.close();
  59. }
  60. int TemperCheckImp::TempeCheckMethod1(float fTemp)
  61. {
  62. if (m_fTempHigh < m_fTempLow || m_fTempStdDev < 0 || m_nQueueSize < 1)
  63. return -1;
  64. int nFlag = m_nQueueSize - (int)m_dqTemp.size();
  65. if (nFlag > 1)
  66. {
  67. m_dqTemp.push_back(fTemp);
  68. return -2;
  69. }
  70. else if (nFlag == 1)
  71. {
  72. m_dqTemp.push_back(fTemp);
  73. }
  74. else if (nFlag == 0)
  75. {
  76. m_dqTemp.pop_front();
  77. m_dqTemp.push_back(fTemp);
  78. }
  79. else
  80. {
  81. m_dqTemp.clear();
  82. return -3;
  83. }
  84. //#pragma omp parallel for
  85. for (int i = 0; i < m_nQueueSize; i++)
  86. {
  87. if (m_dqTemp[i] < m_fTempLow || m_dqTemp[i] > m_fTempHigh)
  88. return 0;
  89. }
  90. float fSum = 0.0f;
  91. //#pragma omp parallel for
  92. for (int i = 0; i < m_nQueueSize; i++)
  93. {
  94. fSum += m_dqTemp[i];
  95. }
  96. float fMean = fSum / m_nQueueSize;
  97. float fStdDev = 0.0f;
  98. //#pragma omp parallel for
  99. for (int i = 0; i < m_nQueueSize; i++)
  100. {
  101. fStdDev += (m_dqTemp[i] - fMean)*(m_dqTemp[i] - fMean);
  102. }
  103. fStdDev = sqrt(fStdDev / m_nQueueSize);
  104. if (fStdDev < m_fTempStdDev)
  105. return 1;
  106. else
  107. return 0;
  108. }
  109. int TemperCheckImp::TempeCheckMethod2(float fTemp, double dTime)
  110. {
  111. SYSTEMTIME obSysetmTime;
  112. SYSTEMTIME obSysetmTime1;
  113. SYSTEMTIME obSysetmTime2;
  114. const time_t t = time(NULL);
  115. struct tm* current_time = localtime( &t);
  116. ofstream ocout;
  117. string file = "TemperatureCheck";
  118. string time;
  119. stringstream stream1;
  120. stringstream stream2;
  121. stringstream stream3;
  122. stream1 << (current_time->tm_year + 1900);
  123. stream2 << (current_time->tm_mon + 1);
  124. stream3 << (current_time->tm_mday);
  125. time = stream1.str() + stream2.str() + stream3.str() + +".log";
  126. string Path = m_strLogPath + file + time;
  127. //ocout.open(Path);
  128. ocout.open(Path, ios::app);
  129. if (!ocout)
  130. return -11;
  131. //const time_t t = time(NULL);
  132. //struct tm* current_time = localtime(&t);
  133. //ocout << "test begin:" << endl;
  134. //ocout << "the example of temperature map is:(time--- temperature)" << endl;
  135. if (m_fTempHigh < m_fTempLow || m_fTempStdDev < 0 || m_nQueueSize < 2 || dTime < 43022 || dTime > 50000 || m_fGradientStdDev < 0)
  136. {
  137. PrintTime();
  138. ocout << "end = -1" << endl;
  139. ocout.close();
  140. return -1;
  141. }
  142. if (fTemp<m_fTempLow || fTemp>m_fTempHigh)
  143. {
  144. PrintTime();
  145. ocout << "end = -2" << endl;
  146. ocout.close();
  147. return -2;
  148. }
  149. int nFlag = m_nQueueSize - (int)m_mapTemp.size();
  150. if (nFlag > 1)
  151. {
  152. m_mapTemp.insert(make_pair(dTime, fTemp));
  153. VariantTimeToSystemTime(dTime, &obSysetmTime);
  154. PrintTime();
  155. ocout << "added to the temprature list:";
  156. ocout << "(";
  157. ocout << obSysetmTime.wYear;
  158. ocout << "-";
  159. ocout << obSysetmTime.wMonth;
  160. ocout << "-";
  161. ocout << obSysetmTime.wDay;
  162. ocout << " ";
  163. ocout << obSysetmTime.wHour;
  164. ocout << ":";
  165. ocout << obSysetmTime.wMinute;
  166. ocout << ":";
  167. ocout << obSysetmTime.wSecond;
  168. ocout << ", ";
  169. ocout << fTemp;
  170. ocout << ")" << endl;
  171. ocout.close();
  172. return -3;
  173. }
  174. else if (nFlag == 1) //队列正好满了
  175. {
  176. m_mapTemp.insert(make_pair(dTime, fTemp));
  177. VariantTimeToSystemTime(dTime, &obSysetmTime);
  178. PrintTime();
  179. ocout << "added to the temprature list:";
  180. ocout << "(";
  181. ocout << obSysetmTime.wYear;
  182. ocout << "-";
  183. ocout << obSysetmTime.wMonth;
  184. ocout << "-";
  185. ocout << obSysetmTime.wDay;
  186. ocout << " ";
  187. ocout << obSysetmTime.wHour;
  188. ocout << ":";
  189. ocout << obSysetmTime.wMinute;
  190. ocout << ":";
  191. ocout << obSysetmTime.wSecond;
  192. ocout << ", ";
  193. ocout << fTemp;
  194. ocout << ")" << endl;
  195. }
  196. else if (nFlag == 0) //队列多了 替换
  197. {
  198. map< double, float>::iterator iter = m_mapTemp.begin();
  199. m_mapTemp.erase(iter);
  200. PrintTime();
  201. ocout << "Remove the first element" << endl;
  202. m_mapTemp.insert(make_pair(dTime, fTemp));
  203. VariantTimeToSystemTime(dTime, &obSysetmTime);
  204. PrintTime();
  205. ocout << "added to the temprature list:";
  206. ocout << "(";
  207. ocout << obSysetmTime.wYear;
  208. ocout << "-";
  209. ocout << obSysetmTime.wMonth;
  210. ocout << "-";
  211. ocout << obSysetmTime.wDay;
  212. ocout << " ";
  213. ocout << obSysetmTime.wHour;
  214. ocout << ":";
  215. ocout << obSysetmTime.wMinute;
  216. ocout << ":";
  217. ocout << obSysetmTime.wSecond;
  218. ocout << ", ";
  219. ocout << fTemp;
  220. ocout << ")" << endl;
  221. }
  222. else
  223. {
  224. m_mapTemp.clear();
  225. PrintTime();
  226. ocout << "end = -4" << endl;
  227. ocout.close();
  228. return -4;
  229. }
  230. map< double, float>::iterator iter = m_mapTemp.begin();
  231. map< double, float>::iterator iter1 = m_mapTemp.begin();
  232. iter1 = ++iter1;
  233. CTime time1;
  234. CTime time2;
  235. if (iter == iter1)
  236. {
  237. PrintTime();
  238. ocout << "end = -10" << endl;
  239. ocout.close();
  240. return -10;
  241. }
  242. float fMean = 0.0f;
  243. for (iter, iter1; iter != m_mapTemp.end() && iter1 != m_mapTemp.end(); iter++, iter1++)
  244. {
  245. //cout << "iter1->second=" << iter1->second << endl;
  246. //cout << "iter->second=" << iter->second << endl;
  247. VariantTimeToSystemTime(iter1->first, &obSysetmTime1);
  248. time1 = CTime((int)obSysetmTime1.wYear, (int)obSysetmTime1.wMonth, (int)obSysetmTime1.wDay, (int)obSysetmTime1.wHour, (int)obSysetmTime1.wMinute,
  249. (int)obSysetmTime1.wSecond, (int)obSysetmTime1.wMilliseconds);
  250. VariantTimeToSystemTime(iter->first, &obSysetmTime2);
  251. time2 = CTime((int)obSysetmTime2.wYear, (int)obSysetmTime2.wMonth, (int)obSysetmTime2.wDay, (int)obSysetmTime2.wHour, (int)obSysetmTime2.wMinute,
  252. (int)obSysetmTime2.wSecond, (int)obSysetmTime2.wMilliseconds);
  253. CTimeSpan timeSpan = time1 - time2;
  254. if (timeSpan.GetTotalSeconds() < 1)
  255. {
  256. //d << "timeSpan.GetTotalSeconds()" << timeSpan.GetTotalSeconds() << endl;
  257. return -12;
  258. }
  259. fMean += fabs((iter1->second - iter->second) / (timeSpan.GetTotalSeconds()));
  260. m_dqTempGradient.push_back(fMean);
  261. }
  262. fMean = fMean / m_dqTempGradient.size();
  263. float fStdDev = 0.0f;
  264. for (unsigned int i = 0; i <m_dqTempGradient.size(); i++)
  265. {
  266. fStdDev += (m_dqTempGradient[i] - fMean) * (m_dqTempGradient[i] - fMean);
  267. }
  268. fStdDev = sqrt(fStdDev / m_dqTempGradient.size());
  269. PrintTime();
  270. ocout << "calculation result:" << endl;
  271. PrintTime();
  272. ocout << "Map_Temperature_Mean=" << fabs(fMean) << endl;
  273. PrintTime();
  274. ocout << "Map_Temperature_StdDev=" << fStdDev << endl;
  275. PrintTime();
  276. ocout << "GradientMeanCondition=" << m_fGradientMean << endl;
  277. PrintTime();
  278. ocout << "GradientStdDevCondition=" << m_fGradientStdDev << endl;
  279. //ocout << "test end = fit the condition" << endl;
  280. //ocout.close();
  281. //return 1;
  282. if (fabs(fMean) < m_fGradientMean && fStdDev < m_fGradientStdDev)
  283. {
  284. PrintTime();
  285. ocout << "test end = fit the condition" << endl;
  286. ocout.close();
  287. return 1;
  288. }
  289. else
  290. {
  291. PrintTime();
  292. ocout << "test end = not fit the condition" << endl;
  293. ocout.close();
  294. return 0;
  295. }
  296. }
  297. bool TemperCheckImp::clearMapAndLog()
  298. {
  299. if (!m_mapTemp.empty())
  300. {
  301. m_mapTemp.clear();
  302. if (!m_mapTemp.empty())
  303. return false;
  304. }
  305. if (!m_dqTemp.empty())
  306. {
  307. m_dqTemp.clear();
  308. if (!m_dqTemp.empty())
  309. return false;
  310. }
  311. if (!m_dqTempGradient.empty())
  312. {
  313. m_dqTempGradient.clear();
  314. if (!m_dqTempGradient.empty())
  315. return false;
  316. }
  317. ofstream ocout;
  318. const time_t t = time(NULL);
  319. struct tm* current_time = localtime(&t);
  320. string file = "TemperatureCheck";
  321. string timelog;
  322. stringstream stream1;
  323. stringstream stream2;
  324. stringstream stream3;
  325. stream1 << (current_time->tm_year + 1900);
  326. stream2 << (current_time->tm_mon + 1);
  327. stream3 << (current_time->tm_mday);
  328. timelog = stream1.str() + stream2.str() + stream3.str() + +".log";
  329. string Path = m_strLogPath + file + timelog;
  330. ocout.open(Path);
  331. //PrintTime();
  332. //ocout << "clear map and log" << endl;
  333. ocout.close();
  334. return true;
  335. }
  336. void TemperCheckImp::PrintTime()
  337. {
  338. const time_t t = time(NULL);
  339. struct tm* current_time = localtime(&t);
  340. ofstream ocout;
  341. string file = "TemperatureCheck";
  342. string timelog;
  343. stringstream stream1;
  344. stringstream stream2;
  345. stringstream stream3;
  346. stream1 << (current_time->tm_year + 1900);
  347. stream2 << (current_time->tm_mon + 1);
  348. stream3 << (current_time->tm_mday);
  349. timelog = stream1.str() + stream2.str() + stream3.str() + +".log";
  350. string Path = m_strLogPath + file + timelog;
  351. ocout.open(Path, ios::app);
  352. ocout << (current_time->tm_year + 1900) << "-" << (current_time->tm_mon + 1) << "-" << current_time->tm_mday << " " <<
  353. current_time->tm_hour << ":" << current_time->tm_min << ":" << current_time->tm_sec << "[INFO]: ";
  354. }