123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- #include "stdafx.h"
- #include "TemperCheckImp.h"
- TemperCheckImp::TemperCheckImp()
- {
- //ofstream ocout;
- //ocout.open("D:\\default constructor.txt");
- //ocout << "begin" << endl;
- //ocout << "end" << endl;
- //ocout.close();
- }
- TemperCheckImp::TemperCheckImp(float fSetTempHigh, float fSetTempLow, float fSetTempStdDev, float fSetGradientMean, float fSetGradientStdDev, int nSetQueueSize, char* path)
- {
- m_fTempHigh = fSetTempHigh;
- m_fTempLow = fSetTempLow;
- m_fTempStdDev = fSetTempStdDev;
- m_nQueueSize = nSetQueueSize;
- m_fGradientMean = fSetGradientMean;
- m_fGradientStdDev = fSetGradientStdDev;
- m_strLogPath = path;
- m_dqTemp.clear();// = new deque <float>;
- m_dqTempGradient.clear(); //= new deque <float>;
- m_mapTemp.clear();// = new map <double, float >;
- //ofstream ocout;
- //string file = "TempeCheckMethod2.log";
- //string Path = *LogPath + file;
- //ocout.open(Path);
- ////ocout.open(Path, ios::app);
- //PrintTime();
- //ocout << "test begin:" << endl;
- //PrintTime();
- //ocout << "the example of temperature map is:(time, temperature)" << endl;
- }
- TemperCheckImp::~TemperCheckImp(void)
- {
- m_dqTemp.clear();
- m_dqTempGradient.clear();
- m_mapTemp.clear();
- //ofstream ocout;
- //ocout.open("D:\\destructor.txt");
- //ocout << "begin" << endl;
- //if (LogPath)
- //{
- // delete LogPath;
- //}
- //if (dqTemp)
- //{
- // delete dqTemp;
- //}
- //if (dqTempGradient)
- //{
- // delete dqTempGradient;
- //}
- //if (mapTemp)
- //{
- // delete mapTemp;
- //}
- //ocout << "end" << endl;
- //ocout.close();
- }
- int TemperCheckImp::TempeCheckMethod1(float fTemp)
- {
- if (m_fTempHigh < m_fTempLow || m_fTempStdDev < 0 || m_nQueueSize < 1)
- return -1;
- int nFlag = m_nQueueSize - (int)m_dqTemp.size();
- if (nFlag > 1)
- {
- m_dqTemp.push_back(fTemp);
- return -2;
- }
- else if (nFlag == 1)
- {
- m_dqTemp.push_back(fTemp);
- }
- else if (nFlag == 0)
- {
- m_dqTemp.pop_front();
- m_dqTemp.push_back(fTemp);
- }
- else
- {
- m_dqTemp.clear();
- return -3;
- }
- //#pragma omp parallel for
- for (int i = 0; i < m_nQueueSize; i++)
- {
- if (m_dqTemp[i] < m_fTempLow || m_dqTemp[i] > m_fTempHigh)
- return 0;
- }
- float fSum = 0.0f;
- //#pragma omp parallel for
- for (int i = 0; i < m_nQueueSize; i++)
- {
- fSum += m_dqTemp[i];
- }
- float fMean = fSum / m_nQueueSize;
- float fStdDev = 0.0f;
- //#pragma omp parallel for
- for (int i = 0; i < m_nQueueSize; i++)
- {
- fStdDev += (m_dqTemp[i] - fMean)*(m_dqTemp[i] - fMean);
- }
- fStdDev = sqrt(fStdDev / m_nQueueSize);
- if (fStdDev < m_fTempStdDev)
- return 1;
- else
- return 0;
- }
- int TemperCheckImp::TempeCheckMethod2(float fTemp, double dTime)
- {
- SYSTEMTIME obSysetmTime;
- SYSTEMTIME obSysetmTime1;
- SYSTEMTIME obSysetmTime2;
- const time_t t = time(NULL);
- struct tm* current_time = localtime( &t);
- ofstream ocout;
- string file = "TemperatureCheck";
- string time;
- stringstream stream1;
- stringstream stream2;
- stringstream stream3;
- stream1 << (current_time->tm_year + 1900);
- stream2 << (current_time->tm_mon + 1);
- stream3 << (current_time->tm_mday);
- time = stream1.str() + stream2.str() + stream3.str() + +".log";
- string Path = m_strLogPath + file + time;
- //ocout.open(Path);
- ocout.open(Path, ios::app);
- if (!ocout)
- return -11;
- //const time_t t = time(NULL);
- //struct tm* current_time = localtime(&t);
- //ocout << "test begin:" << endl;
- //ocout << "the example of temperature map is:(time--- temperature)" << endl;
- if (m_fTempHigh < m_fTempLow || m_fTempStdDev < 0 || m_nQueueSize < 2 || dTime < 43022 || dTime > 50000 || m_fGradientStdDev < 0)
- {
- PrintTime();
- ocout << "end = -1" << endl;
- ocout.close();
- return -1;
- }
- if (fTemp<m_fTempLow || fTemp>m_fTempHigh)
- {
- PrintTime();
- ocout << "end = -2" << endl;
- ocout.close();
- return -2;
- }
- int nFlag = m_nQueueSize - (int)m_mapTemp.size();
- if (nFlag > 1)
- {
- m_mapTemp.insert(make_pair(dTime, fTemp));
- VariantTimeToSystemTime(dTime, &obSysetmTime);
- PrintTime();
- ocout << "added to the temprature list:";
- ocout << "(";
- ocout << obSysetmTime.wYear;
- ocout << "-";
- ocout << obSysetmTime.wMonth;
- ocout << "-";
- ocout << obSysetmTime.wDay;
- ocout << " ";
- ocout << obSysetmTime.wHour;
- ocout << ":";
- ocout << obSysetmTime.wMinute;
- ocout << ":";
- ocout << obSysetmTime.wSecond;
- ocout << ", ";
- ocout << fTemp;
- ocout << ")" << endl;
- ocout.close();
- return -3;
- }
- else if (nFlag == 1) //队列正好满了
- {
- m_mapTemp.insert(make_pair(dTime, fTemp));
- VariantTimeToSystemTime(dTime, &obSysetmTime);
- PrintTime();
- ocout << "added to the temprature list:";
- ocout << "(";
- ocout << obSysetmTime.wYear;
- ocout << "-";
- ocout << obSysetmTime.wMonth;
- ocout << "-";
- ocout << obSysetmTime.wDay;
- ocout << " ";
- ocout << obSysetmTime.wHour;
- ocout << ":";
- ocout << obSysetmTime.wMinute;
- ocout << ":";
- ocout << obSysetmTime.wSecond;
- ocout << ", ";
- ocout << fTemp;
- ocout << ")" << endl;
- }
- else if (nFlag == 0) //队列多了 替换
- {
- map< double, float>::iterator iter = m_mapTemp.begin();
- m_mapTemp.erase(iter);
- PrintTime();
- ocout << "Remove the first element" << endl;
- m_mapTemp.insert(make_pair(dTime, fTemp));
- VariantTimeToSystemTime(dTime, &obSysetmTime);
- PrintTime();
- ocout << "added to the temprature list:";
- ocout << "(";
- ocout << obSysetmTime.wYear;
- ocout << "-";
- ocout << obSysetmTime.wMonth;
- ocout << "-";
- ocout << obSysetmTime.wDay;
- ocout << " ";
- ocout << obSysetmTime.wHour;
- ocout << ":";
- ocout << obSysetmTime.wMinute;
- ocout << ":";
- ocout << obSysetmTime.wSecond;
- ocout << ", ";
- ocout << fTemp;
- ocout << ")" << endl;
- }
- else
- {
- m_mapTemp.clear();
- PrintTime();
- ocout << "end = -4" << endl;
- ocout.close();
- return -4;
- }
- map< double, float>::iterator iter = m_mapTemp.begin();
- map< double, float>::iterator iter1 = m_mapTemp.begin();
- iter1 = ++iter1;
- CTime time1;
- CTime time2;
- if (iter == iter1)
- {
- PrintTime();
- ocout << "end = -10" << endl;
- ocout.close();
- return -10;
- }
- float fMean = 0.0f;
- for (iter, iter1; iter != m_mapTemp.end(), iter1 != m_mapTemp.end(); iter++, iter1++)
- {
- //cout << "iter1->second=" << iter1->second << endl;
- //cout << "iter->second=" << iter->second << endl;
- VariantTimeToSystemTime(iter1->first, &obSysetmTime1);
- time1 = CTime((int)obSysetmTime1.wYear, (int)obSysetmTime1.wMonth, (int)obSysetmTime1.wDay, (int)obSysetmTime1.wHour, (int)obSysetmTime1.wMinute,
- (int)obSysetmTime1.wSecond, (int)obSysetmTime1.wMilliseconds);
- VariantTimeToSystemTime(iter->first, &obSysetmTime2);
- time2 = CTime((int)obSysetmTime2.wYear, (int)obSysetmTime2.wMonth, (int)obSysetmTime2.wDay, (int)obSysetmTime2.wHour, (int)obSysetmTime2.wMinute,
- (int)obSysetmTime2.wSecond, (int)obSysetmTime2.wMilliseconds);
- CTimeSpan timeSpan = time1 - time2;
- if (timeSpan.GetTotalSeconds() < 1)
- {
- //d << "timeSpan.GetTotalSeconds()" << timeSpan.GetTotalSeconds() << endl;
- return -12;
- }
- fMean += fabs((iter1->second - iter->second) / (timeSpan.GetTotalSeconds()));
- m_dqTempGradient.push_back(fMean);
- }
- fMean = fMean / m_dqTempGradient.size();
- float fStdDev = 0.0f;
- for (unsigned int i = 0; i <m_dqTempGradient.size(); i++)
- {
- fStdDev += (m_dqTempGradient[i] - fMean) * (m_dqTempGradient[i] - fMean);
- }
- fStdDev = sqrt(fStdDev / m_dqTempGradient.size());
- PrintTime();
- ocout << "calculation result:" << endl;
- PrintTime();
- ocout << "Map_Temperature_Mean=" << fabs(fMean) << endl;
- PrintTime();
- ocout << "Map_Temperature_StdDev=" << fStdDev << endl;
- PrintTime();
- ocout << "GradientMeanCondition=" << m_fGradientMean << endl;
- PrintTime();
- ocout << "GradientStdDevCondition=" << m_fGradientStdDev << endl;
- //ocout << "test end = fit the condition" << endl;
- //ocout.close();
- //return 1;
- if (fabs(fMean) < m_fGradientMean && fStdDev < m_fGradientStdDev)
- {
- PrintTime();
- ocout << "test end = fit the condition" << endl;
- ocout.close();
- return 1;
- }
- else
- {
- PrintTime();
- ocout << "test end = not fit the condition" << endl;
- ocout.close();
- return 0;
- }
- }
- bool TemperCheckImp::clearMapAndLog()
- {
- if (!m_mapTemp.empty())
- {
- m_mapTemp.clear();
- if (!m_mapTemp.empty())
- return false;
- }
- if (!m_dqTemp.empty())
- {
- m_dqTemp.clear();
- if (!m_dqTemp.empty())
- return false;
- }
- if (!m_dqTempGradient.empty())
- {
- m_dqTempGradient.clear();
- if (!m_dqTempGradient.empty())
- return false;
- }
- ofstream ocout;
- const time_t t = time(NULL);
- struct tm* current_time = localtime(&t);
- string file = "TemperatureCheck";
- string timelog;
- stringstream stream1;
- stringstream stream2;
- stringstream stream3;
- stream1 << (current_time->tm_year + 1900);
- stream2 << (current_time->tm_mon + 1);
- stream3 << (current_time->tm_mday);
- timelog = stream1.str() + stream2.str() + stream3.str() + +".log";
- string Path = m_strLogPath + file + timelog;
- ocout.open(Path);
- //PrintTime();
- //ocout << "clear map and log" << endl;
- ocout.close();
- return true;
- }
- void TemperCheckImp::PrintTime()
- {
- const time_t t = time(NULL);
- struct tm* current_time = localtime(&t);
- ofstream ocout;
- string file = "TemperatureCheck";
- string timelog;
- stringstream stream1;
- stringstream stream2;
- stringstream stream3;
- stream1 << (current_time->tm_year + 1900);
- stream2 << (current_time->tm_mon + 1);
- stream3 << (current_time->tm_mday);
- timelog = stream1.str() + stream2.str() + stream3.str() + +".log";
- string Path = m_strLogPath + file + timelog;
- ocout.open(Path, ios::app);
- ocout << (current_time->tm_year + 1900) << "-" << (current_time->tm_mon + 1) << "-" << current_time->tm_mday << " " <<
- current_time->tm_hour << ":" << current_time->tm_min << ":" << current_time->tm_sec << "[INFO]: ";
- }
|