📄 historydata.cpp
字号:
// HistoryData.cpp : implementation file
//
#include "stdafx.h"
#include "HistoryData.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHistoryData
CHistData *m_pHistData[MAX_HIST_POINT]; // this point Array for Current Data in Memory
CHistProperty *m_pHistProperty;
time_t *m_pHistTime;
short m_nPointCount=0;
short m_nHistCurPos=0,m_nHistNum=0,m_nHistPosBegin=0,m_nHistSavePos=0;
CCurveCommandSet m_nCurveCommandSet;
CCurveDrawStruct *m_pCurveDrawStruct;
char m_nCurveGroupName[MAX_CURVE_GROUP][16];
CHistoryData::CHistoryData()
{
}
CHistoryData::~CHistoryData()
{
}
BEGIN_MESSAGE_MAP(CHistoryData, CWnd)
//{{AFX_MSG_MAP(CHistoryData)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHistoryData message handlers
void CHistoryData::IncrementHistData()
{
CHistData *pHistData;
CHistProperty *pHistProperty;
for(int i=0;i<m_nPointCount;i++)
{
pHistProperty=m_pHistProperty+i;
pHistData=m_pHistData[i]+m_nHistCurPos;
float nTempAnaval;
//*******Get Current HistData Begin*******
aps=aps1+pHistProperty->nFdcsNo*(MAX_CHNUM+1)+pHistProperty->nCh;
nTempAnaval=aps->anaVal;
//*******Get Current HistData End*********
if ((fabs(nTempAnaval)>99999.0)||((fabs(nTempAnaval)<1e-5)&&(nTempAnaval!=0.0)))
{
// AfxMessageBox("IncrementHistData");
nTempAnaval=0;
}
pHistData->nCurAnaval=nTempAnaval;
}
CTime nTime;
nTime=CTime::GetCurrentTime();
time_t *pHistTime;
pHistTime=m_pHistTime+m_nHistCurPos;
*pHistTime=nTime.GetTime();
m_nHistCurPos++;
m_nHistCurPos%=MAX_MEMORY_HISTNUM;
m_nHistNum++;
if (m_nHistNum>MAX_MEMORY_HISTNUM)
{
m_nHistPosBegin++;
m_nHistNum=MAX_MEMORY_HISTNUM;
m_nHistPosBegin%=MAX_MEMORY_HISTNUM;
}
}
void CHistoryData::SaveHistData()
{
CHistData *pHistData;
CHistProperty *pHistProperty;
time_t *pHistTime;
short nCurDay;
CString nFileName;
CTime nTime;
CFile nFile;
CFileException e;
CFileStatus nStatus;
BOOL bSuc;
if (m_nHistSavePos==m_nHistCurPos) return;
char nCode[16];
// m_nFileCritSect.Lock();
{
for(int i=0;i<m_nPointCount;i++)
{
pHistProperty=m_pHistProperty+i;
pHistTime=m_pHistTime+m_nHistSavePos;
nTime=*pHistTime;
nCurDay=nTime.GetDay();
//******* Get Point code begin***********
if (GetCode(nCode,pHistProperty->nFdcsNo,pHistProperty->nCh)==RESULT_ERROR)continue;
//********Get point code end*********
nFileName.Format(".\\History\\%4s\\D%02d.his",nCode,nTime.GetDay());
if (nFile.GetStatus(nFileName,nStatus))
{
if(nStatus.m_mtime.GetMonth()!= nTime.GetMonth())
{
bSuc=nFile.Open(nFileName,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary,&e);
}
else
{
bSuc=nFile.Open(nFileName,CFile::modeReadWrite|CFile::typeBinary,&e);
}
}
else
{
bSuc=nFile.Open(nFileName,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary,&e);
}
short nHistSavePos=m_nHistSavePos;
BOOL bInsteadFile=FALSE;
if (bSuc)
{
nFile.SeekToEnd();
while (nHistSavePos!=m_nHistCurPos)
{
pHistData=m_pHistData[i]+nHistSavePos;
pHistTime=m_pHistTime+nHistSavePos;
nTime=*pHistTime;
short nDay=nTime.GetDay();
if (nCurDay!=nDay)
{
bInsteadFile=TRUE;
break;
}
nFile.Write(pHistTime,sizeof(time_t));
nFile.Write(&pHistData->nCurAnaval,sizeof(float));
nHistSavePos++;nHistSavePos%=MAX_MEMORY_HISTNUM;
}
nFile.Close();
}
if (bInsteadFile)
{
nTime=*(m_pHistTime+nHistSavePos);
nCurDay=nTime.GetDay();
nFileName.Format(".\\History\\%4s\\D%02d.his",nCode,nTime.GetDay());
if (nFile.GetStatus(nFileName,nStatus))
{
if(nStatus.m_mtime.GetMonth()!= nTime.GetMonth())
{
bSuc=nFile.Open(nFileName,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary,&e);
}
else
{
bSuc=nFile.Open(nFileName,CFile::modeReadWrite|CFile::typeBinary,&e);
}
}
else
{
bSuc=nFile.Open(nFileName,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary,&e);
}
if (bSuc)
{
nFile.SeekToEnd();
while (nHistSavePos!=m_nHistCurPos)
{
pHistData=m_pHistData[i]+nHistSavePos;
pHistTime=m_pHistTime+nHistSavePos;
nFile.Write(pHistTime,sizeof(time_t));
nFile.Write(&pHistData->nCurAnaval,sizeof(float));
nHistSavePos++;nHistSavePos%=MAX_MEMORY_HISTNUM;
}
nFile.Close();
}
}
}
m_nHistSavePos=m_nHistCurPos;
}
// m_nFileCritSect.Unlock();
}
short CHistoryData::GetMemoryHistAnaval(short nNo, float *nAnavalArray, time_t *nTimeArray, short *nCount)
{
CHistData *nHistData;
time_t *nCurTime;
if (nNo>=m_nPointCount) return RESULT_ERROR;
short nHistNum;
nHistNum=m_nHistNum;
for(int i=0;i<nHistNum;i++)
{
short nCurPos=m_nHistPosBegin+i;
nCurPos%=MAX_MEMORY_HISTNUM;
nHistData=m_pHistData[nNo]+nCurPos;
nCurTime=m_pHistTime+nCurPos;
*(nAnavalArray+i)=nHistData->nCurAnaval;
*(nTimeArray+i)=*nCurTime;
}
*nCount=nHistNum;
return RESULT_NORMAL;
}
void CHistoryData::InitData()
{
CHistData *pItem,*pItem1;
CHistProperty *pHistProperty;
m_nPointCount=0;
m_pHistProperty=new CHistProperty[MAX_HIST_POINT];
for (int j=0;j<MAX_HIST_POINT;j++)
{
pHistProperty=m_pHistProperty+j;
memset((CHistProperty*)pHistProperty,0,sizeof(CHistProperty));
}
time_t *pHistTime;
m_pHistTime= new time_t[MAX_MEMORY_HISTNUM];
for(j=0;j<MAX_MEMORY_HISTNUM;j++)
{
pHistTime=m_pHistTime+j;
memset((time_t*)pHistTime,0,sizeof(time_t));
}
CCurveDrawStruct *pCurveDrawStruct;
CCurveSetFileStruct nCurveFileStruct;
m_pCurveDrawStruct=new CCurveDrawStruct[MAX_CURVE_GROUP*MAX_CURVE_NUMPERGROUP];
for (int i=0;i<MAX_CURVE_GROUP*MAX_CURVE_NUMPERGROUP;i++)
{
pCurveDrawStruct=m_pCurveDrawStruct+i;
memset(pCurveDrawStruct,0,sizeof(CCurveDrawStruct));
pCurveDrawStruct->nNo=-1;
}
CString nFileName;
nFileName.Format("%s",".\\table\\CurveCommandSet.txt");
FILE *ftpA;
if ((ftpA=fopen(nFileName,"r"))==NULL) return;
while (fscanf(ftpA,"%hd %hd",&m_nCurveCommandSet.nTimeInterval,&m_nCurveCommandSet.nTimePixel)!=EOF)
{
fscanf(ftpA,"%hd %hd %ld %hd",&m_nCurveCommandSet.nPrimNumX,&m_nCurveCommandSet.nPrimNumY,&m_nCurveCommandSet.nPrimColor,&m_nCurveCommandSet.nPrimWidth);
fscanf(ftpA,"%hd %hd %ld %hd",&m_nCurveCommandSet.nSecondNumX,&m_nCurveCommandSet.nSecondNumY,&m_nCurveCommandSet.nSecondColor,&m_nCurveCommandSet.nSecondWidth);
fscanf(ftpA,"%ld %hd %ld",&m_nCurveCommandSet.nBackColor,&m_nCurveCommandSet.nIdLineWidth,&m_nCurveCommandSet.nIdLineColor);
fscanf(ftpA,"%hd %s",&m_nCurveCommandSet.nTimeCoordNum,m_nCurveCommandSet.nFontName);
fscanf(ftpA,"%hd %ld %ld",&m_nCurveCommandSet.nFontSize,&m_nCurveCommandSet.nFontColor,&m_nCurveCommandSet.nFontBackColor);
}
fclose(ftpA);
if ((ftpA=fopen(".\\table\\CurveSet.txt","r"))==NULL)
{
AfxMessageBox("Can not open CurveSet.txt");
return;
};
while (fscanf(ftpA,"%s",nCurveFileStruct.nCode)!=EOF)
{
fscanf(ftpA,"%hd %hd %hd %hd %hd %hd %f %f",
&nCurveFileStruct.nGroupNo,&nCurveFileStruct.nInsideNo,
&nCurveFileStruct.nRed,&nCurveFileStruct.nGreen,&nCurveFileStruct.nBlue,
&nCurveFileStruct.nWidth,&nCurveFileStruct.nUpper,&nCurveFileStruct.nDown);
if ((nCurveFileStruct.nGroupNo<=0)||(nCurveFileStruct.nGroupNo>MAX_CURVE_GROUP)) continue;
if ((nCurveFileStruct.nInsideNo<=0)||(nCurveFileStruct.nInsideNo>MAX_CURVE_NUMPERGROUP)) continue;
pCurveDrawStruct=m_pCurveDrawStruct+(nCurveFileStruct.nGroupNo-1)*MAX_CURVE_NUMPERGROUP+nCurveFileStruct.nInsideNo-1;
pCurveDrawStruct->nColor=RGB(nCurveFileStruct.nRed,nCurveFileStruct.nGreen,nCurveFileStruct.nBlue);
pCurveDrawStruct->nWidth=nCurveFileStruct.nWidth;
pCurveDrawStruct->nUpper=nCurveFileStruct.nUpper;
pCurveDrawStruct->nDown=nCurveFileStruct.nDown;
pCurveDrawStruct->nNo=m_nPointCount;
pHistProperty=m_pHistProperty+m_nPointCount;
GetAddChByCode(nCurveFileStruct.nCode,&pHistProperty->nFdcsNo,&pHistProperty->nCh);
m_nPointCount++;
}
fclose(ftpA);
CreateHistoryPath();
for(i=0;i<m_nPointCount;i++)
{
pItem=new CHistData[MAX_MEMORY_HISTNUM];
for(j=0;j<MAX_MEMORY_HISTNUM;j++)
{
pItem1=pItem+j;
memset((CHistData*)pItem1,0,sizeof(CHistData));
}
m_pHistData[i]=pItem;
}
ReadFileHistData();
char nStr[16];
if ((ftpA=fopen(".\\table\\CurveGroupName.txt","r+"))!=NULL)
{
short nCount=0;
while (fscanf(ftpA,"%16s",nStr)!=EOF)
{
strcpy(m_nCurveGroupName[nCount],nStr);
nCount++;
if (nCount>=MAX_CURVE_GROUP) break;
}
fclose(ftpA);
}
}
void CHistoryData::DestoryData()
{
SaveHistData();
for (int i=0;i<m_nPointCount;i++)
{
if (m_pHistData[i]!=NULL) delete[] m_pHistData[i];
}
delete[] m_pHistProperty;
delete[] m_pHistTime;
delete[] m_pCurveDrawStruct;
}
short CHistoryData::GetHistDataArray(short nNo, short nTimeInterval,short nSum,float *pCurAnaval,time_t *pTime)
{
short nPos,nHistNum,nCount,nLocSum;
CHistData *nHistData;
time_t nCurTime,nMemBeginTime,nMemEndTime;
CHistProperty *pHistProperty;
if (nNo>=m_nPointCount) return 0;
nCount=0;
time_t nFindTime[MAX_MEMORY_HISTNUM+10];
memcpy((time_t*)nFindTime,(time_t*)pTime,nSum*sizeof(time_t));
nHistNum=m_nHistNum;
nCurTime=nFindTime[0];
nLocSum=nSum;
if (nHistNum>0)
{
nMemEndTime=*(m_pHistTime+(m_nHistPosBegin+nHistNum-1)%MAX_MEMORY_HISTNUM);
nMemBeginTime=*(m_pHistTime+(m_nHistPosBegin+0)%MAX_MEMORY_HISTNUM);
}
short nPerRangePointCount=0;
float nTempFloat=0;
if ((nCurTime<=nMemEndTime)&&(nCurTime>=nMemBeginTime)&&(nHistNum>0))
{
for(int i=nHistNum-1;i>=0;i--)
{
nCurTime=nFindTime[nSum-nLocSum];
time_t nTime;
nTime=*(m_pHistTime+(m_nHistPosBegin+i)%MAX_MEMORY_HISTNUM);
nCurTime=nFindTime[nSum-nLocSum];
if (InTimeRange(nTime,nCurTime,nTimeInterval))
{
nHistData=m_pHistData[nNo]+(m_nHistPosBegin+i)%MAX_MEMORY_HISTNUM;
nTempFloat+=nHistData->nCurAnaval;
nPerRangePointCount++;
}
else
{
if (nPerRangePointCount>0)
{
*(pCurAnaval+nCount)=nTempFloat/nPerRangePointCount;
*(pTime+nCount)=nFindTime[nSum-nLocSum];
i++;
nLocSum--;
nCount++;
if (nCount>=nSum) break;
}
else
{
if (i==0)
{
nLocSum--;
}
else if (nTime<nCurTime)
{
while (nTime<nCurTime)
{
if (InTimeRange(nTime,nCurTime,nTimeInterval)) break;
nCurTime=nFindTime[nSum-nLocSum];
nLocSum--;
if (nLocSum==0) break;
}
}
}
nPerRangePointCount=0;
nTempFloat=0;
}
};
}
while (nLocSum>0)
{
// if ((nCurTime<nMemBeginTime)||(nHistNum==0))
pHistProperty=m_pHistProperty+nNo;
CTime nLocTime,nLocTime2;
nLocTime=nCurTime;
CString nFileName;
char nCode[8];
GetCode(nCode,pHistProperty->nFdcsNo,pHistProperty->nCh);
nFileName.Format(".\\History\\%4s\\D%02d.his",nCode,nLocTime.GetDay());
CFile nFile;
CFileStatus nStatus;
if (nFile.GetStatus(nFileName,nStatus))
{
if(nStatus.m_mtime.GetMonth()!= nLocTime.GetMonth())
{
if (nPerRangePointCount>0)
{
*(pCurAnaval+nCount)=nTempFloat/nPerRangePointCount;
*(pTime+nCount)=nFindTime[nSum-nLocSum];
nCount++;
nLocSum--;
nPerRangePointCount=0;
nTempFloat=0.0;
}
nLocTime=nFindTime[nSum-nLocSum];
nLocSum-=(nLocTime.GetHour()*3600+nLocTime.GetMinute()*60+nLocTime.GetSecond())/nTimeInterval;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -