?? alertview.cpp
字號(hào):
// AlertView.cpp : implementation of the CAlertView class
//
#include "stdafx.h"
#include "Alert.h"
#include "AlertDoc.h"
#include "AlertView.h"
#include "WinIO.h"
#include "CommSettingDlg.h"
#include "SetParaDlg.h"
#include "LogDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAlertView
IMPLEMENT_DYNCREATE(CAlertView, CFormView)
BEGIN_MESSAGE_MAP(CAlertView, CFormView)
//{{AFX_MSG_MAP(CAlertView)
ON_WM_TIMER()
ON_UPDATE_COMMAND_UI(ID_ALERT_START, OnUpdateAlertStart)
ON_COMMAND(ID_ALERT_START, OnAlertStart)
ON_COMMAND(ID_SETTINGS, OnSettings)
ON_COMMAND(ID_LOG_MANAGE, OnLogManage)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAlertView construction/destruction
CAlertView::CAlertView()
: CFormView(CAlertView::IDD)
{
//{{AFX_DATA_INIT(CAlertView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CAlertView::~CAlertView()
{
}
void CAlertView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAlertView)
DDX_Control(pDX, IDC_LIST_TODAY, m_logList);
DDX_Control(pDX, IDC_MSCOMM1, m_Comm1);
DDX_Control(pDX, IDC_MSCOMM2, m_Comm2);
//}}AFX_DATA_MAP
}
BOOL CAlertView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CAlertView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
m_logList.InsertColumn(0,"布控名稱");
m_logList.InsertColumn(1,"時(shí)間");
m_logList.InsertColumn(2,"動(dòng)作");
CRect rect;
m_logList.GetClientRect(&rect);
m_logList.SetColumnWidth(0,rect.Width()/5);
m_logList.SetColumnWidth(1,rect.Width()*2/5);
m_logList.SetColumnWidth(2,rect.Width()*2/5);
CTime date=CTime::GetCurrentTime();
CString strDate=date.Format("%Y-%m-%d");
CString strSql="select * from log where date >= #"+strDate+"#";
m_bstrSQL = strSql.AllocSysString();
m_pRsLog.CreateInstance(_uuidof(Recordset)); //初始化Recordset指針
CAlertApp* pApp=(CAlertApp*)::AfxGetApp();
//adOpenStatic:靜態(tài) adLockOptimistic樂觀封鎖法 adCmdText:文本查詢語句
m_pRsLog->Open(m_bstrSQL,(IDispatch*)pApp->m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
m_cntLog = m_pRsLog->RecordCount;
ShowLog(); //顯示日志表中今日的記錄
m_pRsLog->Close();
m_pRsLog=NULL;
initcpara(); //初始化端口
//啟動(dòng)檢測(cè)系統(tǒng)
m_bAction=FALSE;
m_nTimer = SetTimer(1, 5000, 0);
}
/////////////////////////////////////////////////////////////////////////////
// CAlertView printing
BOOL CAlertView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CAlertView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CAlertView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CAlertView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CAlertView diagnostics
#ifdef _DEBUG
void CAlertView::AssertValid() const
{
CFormView::AssertValid();
}
void CAlertView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CAlertDoc* CAlertView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAlertDoc)));
return (CAlertDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAlertView message handlers
void CAlertView::ShowLog()
{
if(m_pRsLog==NULL)
return;
int i=0;
CString strText;
_variant_t bfname,date,time,cjname; //VARIANT數(shù)據(jù)類型
m_logList.DeleteAllItems();
if(!m_pRsLog->RecordCount)
return;
m_pRsLog->MoveFirst();
while(!m_pRsLog->adoEOF)//遍歷所有記錄
{
// strText.Format(TEXT("item %d"), i);
int nItem;
bfname = m_pRsLog->GetCollect("bfname");//得到字段"bfname"的值
if(bfname.vt!=VT_NULL)
nItem=m_logList.InsertItem(0,_bstr_t(bfname));
date = m_pRsLog->GetCollect("date"); //得到字段"date"的值
time = m_pRsLog->GetCollect("time"); //得到字段"time"的值
if(date.vt!=VT_NULL && time.vt!=VT_NULL)
m_logList.SetItemText(nItem,1,_bstr_t(date)+_bstr_t(" ")+_bstr_t(time));
cjname = m_pRsLog->GetCollect("cjname");//得到字段"cjname"的值
if(cjname.vt!=VT_NULL)
m_logList.SetItemText(nItem,2,_bstr_t(cjname));
m_pRsLog->MoveNext();//轉(zhuǎn)到下一條紀(jì)錄
i++;
}
}
void CAlertView::initcpara()
{
TRY
{
initcom(); //初始化串口
}
CATCH(CException,e)
{
e->ReportError();
}
END_CATCH
BOOL bResult = InitializeWinIo(); //初始化WinIO庫
if (!bResult)
{
::AfxMessageBox("WINIO庫初始化失敗");
}
getLpt(); //從數(shù)據(jù)庫中得到可以使用的并口編號(hào),并賦予數(shù)組lptPort
}
void CAlertView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
if(m_bAction==TRUE)
{
SpyOn(); //對(duì)布防設(shè)置進(jìn)行監(jiān)控
updateLog(); //更新日志
}
}
CFormView::OnTimer(nIDEvent);
}
void CAlertView::OnUpdateAlertStart(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bAction);
}
void CAlertView::OnAlertStart()
{
// TODO: Add your command handler code here
m_bAction=!m_bAction;
//工具欄圖標(biāo)問題未處理
}
//監(jiān)控子程序:從各監(jiān)控端口取數(shù)據(jù)判斷觸發(fā)事件是否發(fā)生,并給以處警
void CAlertView::SpyOn()
{
if(m_lptInbf)
{
for(int i=0;i<lptPortArray.GetSize();i++)
{
DWORD portVal;
GetPortVal(lptPortArray[i], &portVal, 1);
processLook((BYTE)portVal, FALSE);
}
}
}
//提取端口內(nèi)容,判斷觸發(fā)事件的發(fā)生,找出對(duì)應(yīng)設(shè)置響應(yīng),并記錄入日志
void CAlertView::processLook(BYTE br, BOOL c)
{
CString s;
if(c)
s = "select * from bufangset where property='串口'";
else
s = "select * from bufangset where property='并口'";
_RecordsetPtr r;
r.CreateInstance(_uuidof(Recordset));
CAlertApp* pApp=(CAlertApp*)::AfxGetApp();
//adOpenStatic:靜態(tài) adLockOptimistic樂觀封鎖法 adCmdText:文本查詢語句
_bstr_t bs=s.AllocSysString();
r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
if(r->RecordCount==0)
goto finish;
r->MoveFirst();
while(!r->adoEOF)
{
//若有事件發(fā)生,則響應(yīng)該觸發(fā)事件
CString s1,s2;
_bstr_t bs1,bs2;
_bstr_t bfname,cjname;
_RecordsetPtr r1,r2;
int count;
BYTE bfcode= BYTE(r->GetCollect("bfcode"));
bfname= _bstr_t(r->GetCollect("bfname"));
if(!br || bfcode!= br)
continue;
//找出對(duì)應(yīng)處警設(shè)置
r1.CreateInstance(_uuidof(Recordset));
bs1="select cjname from bftocj where bfname = '" +bfname +"'";
r1->Open(bs1,(IDispatch*)pApp->m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
cjname=_bstr_t(r1->GetCollect("cjname"));
CString temp=LPTSTR(cjname);
count=analystRs(temp);
//記入日志
r2.CreateInstance(_uuidof(Recordset));
s2="select cjname from log";
bs2=s2.AllocSysString();
r2->Open(bs2,(IDispatch*)pApp->m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
r2->AddNew();
r2->PutCollect("bfname",bfname);
r2->PutCollect("cjname",cjname);
COleDateTime t=COleDateTime::GetCurrentTime();
t.Format("%Y-%m-%d");
r2->PutCollect("date",COleVariant(t));
t.Format("%H:%M:%S");
r2->PutCollect("time",COleVariant(t));
r2->Update();
r2->Close();
r1->Close();
r2=r1=NULL;
//予以處警響應(yīng)
for(int i=0;i<count;i++)
{
r1.CreateInstance(_uuidof(Recordset));
bs1="select * from chujingset where cjname = '" +strcj[i] +"'";
r1->Open(bs1,(IDispatch*)pApp->m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
while(!r1->adoEOF)
{
_bstr_t property=_bstr_t(r1->GetCollect("property"));
if(property==_bstr_t("串口"))
{
if(m_Comm1.GetPortOpen())
{
char telnum[20];
itoa(long(r1->GetCollect("telnumber")),telnum,10);
char out[100];
strcpy(out,"ATDT");
strcat(out,telnum);
int len=strlen(out);
out[len]=0x0d;
out[len+1]=0x0a;
out[len+2]=0;
m_Comm1.SetOutput(_variant_t(out));
}
}
if(property==_bstr_t("并口"))
{
BOOL res;
_bstr_t typeact=_bstr_t(r1->GetCollect("typeact"));
if(typeact==_bstr_t("撥號(hào)"))
res=SetPortVal(long(r1->GetCollect("number")),long(r1->GetCollect("telnumber")),1); //只寫一字節(jié)?
else //發(fā)碼
res=SetPortVal(long(r1->GetCollect("number")),long(r1->GetCollect("infocode")),1);
if(res==FALSE)
::AfxMessageBox("WinIO 庫未成功安裝");
}
r1->MoveNext();
}
r1->Close();
r1=NULL;
}
r->MoveNext();
}
finish:
r->Close();
r=NULL;
}
//觸發(fā)器源+事件+處警動(dòng)作
//日志更新
void CAlertView::updateLog()
{
m_pRsLog.CreateInstance(_uuidof(Recordset)); //初始化Recordset指針
CAlertApp* pApp=(CAlertApp*)::AfxGetApp();
//adOpenStatic:靜態(tài) adLockOptimistic樂觀封鎖法 adCmdText:文本查詢語句
m_pRsLog->Open(m_bstrSQL,(IDispatch*)pApp->m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
if(m_cntLog!=m_pRsLog->RecordCount)
{
m_cntLog=m_pRsLog->RecordCount; //顯示日志表中今日的記錄
ShowLog();
}
m_pRsLog->Close();
m_pRsLog=NULL;
}
int CAlertView::analystRs(CString cjname)
{
int count=0; //count:紀(jì)錄獲取的字符串個(gè)數(shù)
strcj.RemoveAll();
strcj.Add("");
for(int i=0;i<cjname.GetLength();i++)
{
if(cjname[i]!='@')
strcj[count]+=cjname[i];
else
{
count+=1;
strcj.Add("");
}
}
if(strcj[count]=="")
{
strcj.RemoveAt(count);
count--;
}
return count+1;
}
//初始化COM口
void CAlertView::initcom()
{
//初始化處警撥號(hào)使用的com口(控件:mscomm1)
_RecordsetPtr r;
r.CreateInstance(_uuidof(Recordset));
CString s= "select distinct number from chujingset where property='串口'";
_bstr_t bs=s.AllocSysString();
CAlertApp* pApp=(CAlertApp*)::AfxGetApp();
//adOpenStatic:靜態(tài) adLockOptimistic樂觀封鎖法 adCmdText:文本查詢語句
r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
CString strcom,commSetting,commHandShaking;
int commport = (BYTE)r->GetCollect("number"); //處警表里面使用的com口端口號(hào)
itoa(commport,strcom.GetBuffer(10),10);
strcom.ReleaseBuffer();
r->Close();
r=NULL;
commSetting = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commSetting");
commHandShaking = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commHandshaking");
while((commSetting=="")||(commHandShaking==""))
{
CCommSettingDlg dlg;
dlg.m_strChoose=CString("串口")+strcom;
dlg.DoModal();
commSetting = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commSetting");
commHandShaking = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commHandshaking");
}
m_Comm1.SetCommPort(commport);
long shakehand=atol(commHandShaking.GetBuffer(50));
commHandShaking.ReleaseBuffer();
m_Comm1.SetSettings(commSetting);
m_Comm1.SetHandshaking(shakehand);
TRY
{
m_Comm1.SetPortOpen(TRUE);
}
CATCH(CException,e)
{
e->ReportError();
}
END_CATCH
//如果布防設(shè)置中使用到com口的話,則初始化該com口
//控件:mscomm2
r.CreateInstance(_uuidof(Recordset));
s="select distinct number from bufangset where property='串口'";
bs=s.AllocSysString();
r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
commport = (BYTE)r->GetCollect("number"); //布防表里使用的com口端口號(hào)
itoa(commport,strcom.GetBuffer(10),10);
strcom.ReleaseBuffer();
if(r->RecordCount==1)
{
m_comInbf=TRUE;
commSetting = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commSetting");
commHandShaking = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commHandshaking");
while((commSetting=="")||(commHandShaking==""))
{
CCommSettingDlg dlg;
dlg.m_strChoose=CString("串口")+strcom;
dlg.DoModal();
commSetting = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commSetting");
commHandShaking = GetSetting(CString("PortSetting\\Com")+strcom+"\\", "commHandshaking");
}
m_Comm2.SetCommPort(commport);
shakehand=atol(commHandShaking.GetBuffer(50));
commHandShaking.ReleaseBuffer();
m_Comm2.SetSettings(commSetting);
m_Comm2.SetHandshaking(shakehand);
m_Comm2.SetInputMode(1);
m_Comm2.SetPortOpen(TRUE);
}
else
m_comInbf=FALSE;
r->Close();
r=NULL;
}
void CAlertView::getLpt()
{
_RecordsetPtr r;
r.CreateInstance(_uuidof(Recordset));
CString s="select distinct number from bufangset where property='并口' and promote=true";
_bstr_t bs=s.AllocSysString();
CAlertApp* pApp=(CAlertApp*)::AfxGetApp();
r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenStatic, adLockOptimistic, adCmdText);
//若在布防設(shè)置表中select出的并口項(xiàng)數(shù)量為零則判斷系統(tǒng)不使用并口,反之使用
lptPortArray.RemoveAll();
if(r->RecordCount==0)
m_lptInbf=FALSE;
else
{
//若有并口則開一個(gè)并口端口數(shù)組,并給數(shù)組賦值
m_lptInbf=TRUE;
TRY
{
r->MoveFirst();
for(int i=0; i<r->RecordCount; i++)
{
lptPortArray.Add((long)r->GetCollect("Number"));
r->MoveNext();
}
}
CATCH(CException,e)
{
e->ReportError();
}
END_CATCH
}
r->Close();
r=NULL;
}
BEGIN_EVENTSINK_MAP(CAlertView, CFormView)
//{{AFX_EVENTSINK_MAP(CAlertView)
ON_EVENT(CAlertView, IDC_MSCOMM2, 1 /* OnComm */, OnOnCommMscomm2, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CAlertView::OnOnCommMscomm2()
{
// TODO: Add your control notification handler code here
if(m_Comm2.GetCommEvent()==2) //事件值為2表示接收緩沖區(qū)內(nèi)有字符
{
CString str=VARIANT2str(m_Comm2.GetInput());
for(int i=0; i<str.GetLength();i++)
processLook(str[i],TRUE);
}
}
void CAlertView::OnSettings()
{
// TODO: Add your command handler code here
m_bAction=FALSE;
CSetParaDlg dlg;
dlg.DoModal();
// m_bAction=TRUE;
}
void CAlertView::OnLogManage()
{
// TODO: Add your command handler code here
CLogDlg dlg(this);
dlg.DoModal();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -