www.pudn.com > PressMonitor_q.zip > MonitorDoc.cpp
// MonitorDoc.cpp : implementation of the CMonitorDoc class
//
#include "stdafx.h"
#include "Monitor.h"
#include "MonitorDoc.h"
#include "Hint.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static CString strFault[23]={"压机下软限位","压机下软减速",
"压机上软减速","压机上软限位","主油箱液位低",
"主油箱液位高","充液罐液位低","充液罐液位高",
"循环泵压力超高","主泵冲洗压力超高或冲洗管路断开",
"压机急停","操作机Ⅰ急停","压机主泵软启动器故障",
"操作机主泵软启动器故障","控制泵压力超高","操作机Ⅱ急停",
"压机液压系统油温高于60℃","压机液压系统油温低于15℃",
"充液罐压力高于0.6MPa","充液罐压力低于0.3MPa",
"充液罐温度传感器工作异常","充液罐压力传感器工作异常",
"压机压力传感器工作异常"
};
/////////////////////////////////////////////////////////////////////////////
// CMonitorDoc
IMPLEMENT_DYNCREATE(CMonitorDoc, CDocument)
BEGIN_MESSAGE_MAP(CMonitorDoc, CDocument)
//{{AFX_MSG_MAP(CMonitorDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMonitorDoc construction/destruction
CMonitorDoc::CMonitorDoc()
{
m_pTable=new CDaoTableDef(&m_db);
m_bDBOpen=FALSE;
m_pSocketSer = NULL;
m_hCom=OpenComm();
// m_pGridCtrl=NULL;
// m_pDB = NULL;
// m_pSet = NULL;
m_RecordCount = 0 ;
m_bUpdateView = true;
m_bAddItemflag[0] = true;
m_bAddItemflag[1] = true;
m_strDataNote[0] = "9999";
m_strDataNote[1] = "9999";
for(int i=0;i<23;i++)
{
m_bRecordFaultContinue[i]=FALSE;
}
InitDB(); //初始化故障数据库
///////////////////////////////////////////initialize Report form database
m_pFormDB=new CDaoDatabase;
m_bFormOpen=FALSE;
InitFormDB();
m_nCurrentFormNum=0;
m_nCurrentClass=1;
}
CMonitorDoc::~CMonitorDoc()
{
CloseDB();
if (m_pTable!=NULL) {
delete m_pTable;
}
if (m_pSocketSer!=NULL) {
delete m_pSocketSer;
}
CloseFormDB();
if ( m_pFormTable!=NULL ) {
delete m_pFormTable;
}
if ( m_pFormDB!=NULL ) {
delete m_pFormDB;
}
CloseComm();
}
BOOL CMonitorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
m_pSocketSer = new CSocketSer(this);
int port = 5000;
if(m_pSocketSer->Create(port, SOCK_DGRAM, FD_READ | FD_WRITE, NULL) == SOCKET_ERROR)
AfxMessageBox("Fail to create the recieve socket!");
InitDocument();
// InitDB();
return TRUE;
}
///////////////////////////////////////////////////////////////////
//DataBase Functions
/*
void CMonitorDoc::OnDbLocation()
{
{
if (AfxMessageBox(_T("你确实需要重新定位故障数据库所在位置?"),MB_OKCANCEL)==IDCANCEL) {
return;
}
// CFileDialog dlg(TRUE,".mdb","db_fault.mdb",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
// "数据库文件(*.mdb)");
CFileDialog dlg(TRUE,".mdb","*.mdb");
if (dlg.DoModal() == IDOK)
{
CloseDB();
OpenDB(dlg.GetPathName());
if (m_bDBOpen)
{
SetDatabasePath(dlg.GetPathName());
UpdateAllViews(NULL, HINT_DB_OPENED, NULL);
}
}
}
}
*/
BOOL CMonitorDoc::IsContainMdbFile(CString str)
{
int len=str.GetLength();
int mid=str.Find(".mdb",len-5);
if (mid!=-1) {
return TRUE;
}
else
return FALSE;
}
void CMonitorDoc::InitDB()
{
/*
CString strDbPath;
strDbPath = GetDatabasePath();
CFile dbFile;
if (dbFile.Open(strDbPath,CFile::modeRead)) {
dbFile.Close();
OpenDB(strDbPath);
return;
}
*/
CString execphelth=AfxGetApp()->m_pszHelpFilePath;
CString exeName=AfxGetApp()->m_pszAppName;
CString exePath=execphelth.Left(execphelth.GetLength()-exeName.GetLength()-5);
int len=exePath.GetLength();
char c=exePath[len-1];
while(1)
{
// exePath=exePath.Left(exePath.GetLength()-6);
exePath=exePath.Left(exePath.GetLength()-1);
len=exePath.GetLength();
if (len==1) {
break;
}
c=exePath[len-1];
if (c=='\\'){
break;
}
}
exePath=exePath+"DataBase\\"+"fault.mdb";
///////////////////////////////////////////
//如果打开默认的文件,将路径纪录下
CFile myFile;
if (myFile.Open(exePath,CFile::modeRead)) {
// SetDatabasePath(myFile.GetFilePath()); //禁止向注册表中写
myFile.Close();
OpenDB(exePath);
}
else
{
//static int m=0;
// m++;
// if (m!=1)
AfxMessageBox("在默认路径上没有发现数据库!\n请重新指定数据库!");
}
//////////////////////////////////////////////
// AfxMessageBox("test!");
return;
}
BOOL CMonitorDoc::OpenDB(CString lpszPathName)
{
CloseDB();
try
{
m_db.Open(lpszPathName);//,FALSE,FALSE,_T(""));
}
catch (CDaoException *e) {
AfxMessageBox("打开数据库错误:"+e->m_pErrorInfo->m_strDescription+
"\n警告:请运行数据库菜单下的重新定位数据库命令,\n以重新定位数据库");
m_bDBOpen=FALSE;
//if (e) {
// delete e;
// }
e->Delete();
return FALSE;
}
catch(CMemoryException *e)
{
AfxMessageBox("打开数据库内存错误");
e->ReportError();
m_bDBOpen=FALSE;
//if (e) {
// delete e;
// }
e->Delete();
return FALSE;
}
try
{
m_pTable->Open("fault");
}
catch (CDaoException *e) {
AfxMessageBox("打开表格错误:"+e->m_pErrorInfo->m_strDescription);
m_bDBOpen=FALSE;
//if (e) {
// delete e;
// }
e->Delete();
return FALSE;
}
catch(CMemoryException *e)
{
AfxMessageBox("打开数据库内存错误");
e->ReportError();
m_bDBOpen=FALSE;
if (e) {
delete e;
}
return FALSE;
}
try
{
m_RecordSet.Open(m_pTable);
}
catch (CDaoException *e) {
AfxMessageBox("打开记录集错误:"+e->m_pErrorInfo->m_strDescription);
m_bDBOpen=FALSE;
//if (e) {
// delete e;
// }
e->Delete();
return FALSE;
}
catch(CMemoryException *e)
{
AfxMessageBox("打开数据库内存错误");
e->ReportError();
m_bDBOpen=FALSE;
if (e) {
delete e;
}
return FALSE;
}
m_bDBOpen=TRUE;
return TRUE;
}
void CMonitorDoc::CloseDB()
{
if(m_RecordSet.IsOpen())
{
m_RecordSet.Close();
}
if(m_pTable->IsOpen())
{
m_pTable->Close();
}
if (m_db.IsOpen()) {
m_db.Close();
}
m_bDBOpen = FALSE;
}
void CMonitorDoc::CloseFormDB()
{
if( m_FormRecordSet.IsOpen())
{
m_FormRecordSet.Close();
}
if (m_pFormTable!=NULL) {
if( m_pFormTable->IsOpen())
{
m_pFormTable->Close();
}
}
if (m_pFormDB!=NULL) {
if ( m_pFormDB->IsOpen()) {
m_pFormDB->Close();
}
}
m_bFormOpen = FALSE;
}
void CMonitorDoc::OnFaultOccur(int i)
{
TRACE("////////////////////////OnFaultOccur()\n");
if (!m_bDBOpen) {
// AfxMessageBox("数据库没有打开,错误信息无法添加!\n请重新定位数据库!");
return;
}
if (m_RecordSet.GetRecordCount()!=0) {
m_RecordSet.MoveLast();
}
if(m_RecordSet.CanAppend()==0)
{
AfxMessageBox("数据库不能动态增加");
return;
}
COleVariant VariantSet;
COleDateTime time;//=COleDateTime::GetCurrentTime();;
for(int test=0;test<23;test++)
{
if (i==test)
{
m_RecordSet.AddNew();
CString strSeral;
strSeral.Format("%d",i+1);
m_RecordSet.SetFieldValue(1,(LPCSTR)strSeral);
m_RecordSet.SetFieldValue(2,time.GetCurrentTime());
m_RecordSet.SetFieldValue(3,(LPCSTR)strFault[i]);
m_RecordSet.Update();
m_RecordCount=m_RecordSet.GetRecordCount();
// UpdateAllViews(NULL,HINT_NEW_RECORD, NULL);
UpdateAllViews(NULL,HINT_DB_OPENED, NULL);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CMonitorDoc serialization
void CMonitorDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
for(int i=0;iReceiveFrom((char*)&strRecv, sizeof(strRecv), strRecvIP, nRecvPort);
if((nRes != SOCKET_ERROR) && (nRecvPort = 5000))
{
m_DataReal.SetDataReal(strRecv);
ShiftDatasets();
SetModifiedFlag();
if(m_bUpdateView)
UpdateAllViews(NULL,HINT_DATA_RENEW);
PurgeComm(m_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
static i;
i++;
if( i == 5){
SendComData(m_DataReal.Forging_Display_Pos/*Data_Real[FORGING_POS]*/);
i = 0;
}
RecordFault();
}
}
void CMonitorDoc::RecordFault()
{
CString toolRecordFault="状态报警:" ;
int numFault=0;
CString strNum;
unsigned short statusAlert[23];
statusAlert[0]=m_DataReal.InPlcStatus.Bit.Lower_Limite ;
statusAlert[1]=m_DataReal.InPlcStatus.Bit.Lower_Decelerate ;
statusAlert[2]=m_DataReal.InPlcStatus.Bit.Upper_Decelerate ;
statusAlert[3]=m_DataReal.InPlcStatus.Bit.Upper_Limite ;
statusAlert[4]=m_DataReal.InPlcStatus.Bit.Yewei_Low ;
statusAlert[5]=m_DataReal.InPlcStatus.Bit.Yewei_High ;
statusAlert[6]=m_DataReal.InPlcStatus.Bit.CY_Yewei_Low ;
statusAlert[7]=m_DataReal.InPlcStatus.Bit.CY_Yewei_High ;
statusAlert[8]=m_DataReal.InPlcStatus.Bit.XH_Yali_High ;
statusAlert[9]=m_DataReal.InPlcStatus.Bit.CX_Yali_High ;
statusAlert[10]=m_DataReal.InPlcStatus.Bit.Forging_EStop ;
statusAlert[11]=m_DataReal.InPlcStatus.Bit.Robot_EStop ;
statusAlert[12]=m_DataReal.InPlcStatus.Bit.F_Soft_Starter ;
statusAlert[13]=m_DataReal.InPlcStatus.Bit.R_Soft_Starter ;
statusAlert[14]=m_DataReal.InPlcStatus.Bit.KZP_Yali_High ;
statusAlert[15]=m_DataReal.InPlcStatus.Bit.Robot2_EStop ;
statusAlert[16]=m_DataReal.OutPlc.Bit.Forging_T60 ;
statusAlert[17]=m_DataReal.OutPlc.Bit.Forging_T15 ;
statusAlert[18]=m_DataReal.OutPlc.Bit.Kettle_06MPa ;
statusAlert[19]=m_DataReal.OutPlc.Bit.Kettle_03MPa ;
statusAlert[20]=m_DataReal.OutPlc.Bit.Kettle_TSensor_Ok ;
statusAlert[21]=m_DataReal.OutPlc.Bit.Kettle_PSensor_Ok ;
statusAlert[22]=m_DataReal.OutPlc.Bit.Sys_PSensor_Ok ;
for(int i=0;i<23;i++)
{
if (statusAlert[i]==1) {
if (!m_bRecordFaultContinue[i]) {
OnFaultOccur(i);
}
m_bRecordFaultContinue[i]=TRUE;
//numFault+=1;
numFault=i+1;
strNum.Format("%d",numFault);
if (i>=1) {
toolRecordFault=toolRecordFault+" "+strNum+strFault[i];
}
else
toolRecordFault=toolRecordFault+strNum+strFault[i];
}
else
m_bRecordFaultContinue[i]=FALSE;
}
if (numFault==0) {
toolRecordFault=toolRecordFault+"无记录" ;
GetMainFrame()->m_wndFault.SetTextColor(RGB(255,255,255));
}
if (m_strToolRecordFault==toolRecordFault) {
return;
}
else
{
GetMainFrame()->m_wndFault.SetText(toolRecordFault);
GetMainFrame()->m_wndFault.SetTextColor(RGB(255,0,0));
}
m_strToolRecordFault=toolRecordFault;
}
void CMonitorDoc::DeleteContents()
{
CDocument::DeleteContents();
}
void CMonitorDoc::InitDocument()
{
CTime time;
time=time.GetCurrentTime();
m_DataReal.SetDataReal();
for(int i=0;im_pszHelpFilePath;
CString exeName=AfxGetApp()->m_pszAppName;
CString exePath=execphelth.Left(execphelth.GetLength()-exeName.GetLength()-5);
int len=exePath.GetLength();
char c=exePath[len-1];
while(1)
{
// exePath=exePath.Left(exePath.GetLength()-6);
exePath=exePath.Left(exePath.GetLength()-1);
len=exePath.GetLength();
if (len==1) {
break;
}
c=exePath[len-1];
if (c=='\\'){
break;
}
}
m_strDatabaseSaveDirectory=exePath+"DataBase\\" ;
CTime DBtime=CTime::GetCurrentTime();
CString strDB;
strDB.Format("db%d.mdb",DBtime.GetYear());
exePath=m_strDatabaseSaveDirectory+strDB;
CFile dbFile;
if (dbFile.Open(exePath,CFile::modeRead)) {
dbFile.Close();
////////////////////////////////////////////Open Database
try
{
m_pFormDB->Open( (LPCTSTR)exePath );
}
catch (CDaoException *e) {
AfxMessageBox("打开数据库错误:"+e->m_pErrorInfo->m_strDescription);
m_bFormOpen=FALSE;
//if (e) {
// delete e;
// }
e->Delete();
return;
}
}
else
{
try{
m_pFormDB->Create( (LPCTSTR)exePath,dbLangGeneral,dbVersion30);
}
catch (CDaoException *e) {
AfxMessageBox("创建数据库错误:"+e->m_pErrorInfo->m_strDescription);
m_bFormOpen=FALSE;
//if (e) {
// delete e;
// }
e->Delete();
return;
}
}
///////////////////////////////////Initialize table
if ( !InitTable(DBtime) ) {
m_bFormOpen=FALSE;
return;
}
////////////////////////////////////////////////////建立记录集与表格的联系
if ( !m_pFormTable) {
m_bFormOpen=FALSE;
return;
}
try
{
m_FormRecordSet.Open(m_pFormTable,dbOpenDynaset);
}
catch (CDaoException *e) {
AfxMessageBox("打开记录集错误:"+e->m_pErrorInfo->m_strDescription);
m_bFormOpen=FALSE;
e->Delete();
return;
}
m_bFormOpen=TRUE;
}
BOOL CMonitorDoc::InitTable(CTime DBtime)
{
CString strMY;
if ( !m_pFormDB ) {
return FALSE;
}
//////////////////////////////////////////Open or create Table
m_pFormTable=new CDaoTableDef(m_pFormDB);
strMY.Format("%d",DBtime.GetMonth() );
try
{
m_pFormTable->Open(strMY);
}
catch (CDaoException *e) {
if( InCreateTable(strMY) ) //打不开,就新创建一个表格
{
e->Delete();
return TRUE;
}
else
{
e->Delete();
return FALSE;
}
}
return TRUE;
}
BOOL CMonitorDoc::InCreateTable(CString tableName)
{
if (!m_pFormTable) {
return FALSE;
}
try
{
m_pFormTable->Create( (LPCTSTR)tableName );
}
catch (CDaoException *e) {
AfxMessageBox("创建空表格错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "班",dbText,4 );
}
catch (CDaoException *e) {
AfxMessageBox("创建班属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "序号",dbText,8 );
}
catch (CDaoException *e) {
AfxMessageBox("创建序号属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "代号",dbText,10 );
}
catch (CDaoException *e) {
AfxMessageBox("创建轴号属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "轴号",dbText,8 );
}
catch (CDaoException *e) {
AfxMessageBox("创建代号属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "熔炼号",dbText,10 );
}
catch (CDaoException *e) {
AfxMessageBox("创建熔炼号属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "始锻温度",dbText,8 );
}
catch (CDaoException *e) {
AfxMessageBox("创建始锻温度属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "终锻温度",dbText,8 );
}
catch (CDaoException *e) {
AfxMessageBox("创建终锻温度属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "记事",dbText,127 );
}
catch (CDaoException *e) {
AfxMessageBox("创建备注属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "日期",dbDate,8 );
}
catch (CDaoException *e) {
AfxMessageBox("创建日期属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
try
{
m_pFormTable->CreateField( "我的日期",dbText,10 );
}
catch (CDaoException *e) {
AfxMessageBox("创建我的日期属性错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
m_pFormTable->Append();
return TRUE;
}