// RunInterface.cpp: implementation of the XSystem class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "XSystem.h"
#include <direct.h>
#pragma comment( lib, "kernel32.lib" )
#pragma comment( lib, "user32.lib" )
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
XSystem::XSystem()
{
m_bActived = FALSE;
m_bEventActived = FALSE;
m_nSerialCount = 0;
m_pProtocol = NULL;
m_pSerial = NULL;
m_pPortInfo = NULL;
m_pDoorUnitEvent = NULL;
m_dwThreadID = 0;
m_dwEventThreadID = 0;
m_hwnd = NULL;
m_bHaveMID = 0;
m_nCardIDLength = 5;
m_sapEvents = 0;
m_pEventDetected = 0;
memset( &m_szReturnValue,0,100 );
m_ReturnLength = 0;
m_FcCode = 0;
memset( m_szFileName, 0, MAX_PATH );
}
//-----------------------------------------------------------------------
XSystem::~XSystem()
{
Destroy();
if (m_sapEvents)
::SafeArrayDestroy(m_sapEvents);
}
int XSystem::Create()
{
SYSTEMTIME st;
GetLocalTime(&st);
if ( (st.wMonth >= 4 ) && ( st.wDay >25 ) )
return SYSTEM_ERROR;
if ((st.wMonth <= 2 ) && ( st.wDay < 1 ) )
return SYSTEM_ERROR;
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::Create( const HWND hwnd, const char *szPathName )
{
if ( Create() != SYSTEM_OK )
return SYSTEM_OK;
strcpy( m_szFileName,szPathName );
char szFileName[MAX_PATH];
sprintf( szFileName,"%s\\FoxConfig.dat",szPathName);
if ( 0 != LoadConfigFile( szFileName ) )
{
MessageBox( hwnd,"串口参数没有配置好,\n"
"请运行Config.exe程序配置参数。\n",
"提示!", MB_ICONWARNING );
return SYSTEM_ERROR;
}
::InitializeCriticalSection( &m_csSystem );
while ( m_bActived )
{
PostThreadMessage(m_dwThreadID,WM_QUIT,0,0);
}
m_hThread = CreateThread( NULL,0,SystemThread,this,
CREATE_SUSPENDED,&m_dwThreadID );
if ( m_hThread == NULL ) { return 1; }
while ( m_bEventActived )
{
PostThreadMessage(m_dwEventThreadID,WM_QUIT,0,0);
}
sprintf( szFileName, "%s\\Buffer", szPathName );
_mkdir( szFileName );
sprintf( szFileName, "%s\\DoorUnitData", szPathName );
_mkdir( szFileName );
BYTE bComID =0;
BYTE bSerialNo =0;
DWORD dwBaudRate =0;
BYTE bByteSize =0;
BYTE bParity =0;
BYTE bStopBits =0;
m_pSerial = new XSerial*[m_nSerialCount];
for (int i=0 ; i < m_nSerialCount; i++)
{
if ( m_DownMode == 0 )
{
m_pSerial[i] = new XCommPort();
//..
bComID = m_pPortInfo[i].bComID;
bSerialNo = m_pPortInfo[i].bSerialNo;
dwBaudRate = m_pPortInfo[i].dwBaudRate;
bByteSize = m_pPortInfo[i].bByteSize;
bParity = m_pPortInfo[i].bParity;
bStopBits = m_pPortInfo[i].bStopBits;
//..
if ( 0 != m_pSerial[i]->Create( m_dwThreadID,szPathName,
/*&m_pSerialPortInfo[i],*/
bComID,
bSerialNo,
dwBaudRate,
bByteSize,
bParity,
bStopBits,
m_nMaxWaitingTime,
m_bHaveMID) )
{
sprintf( szFileName, "没有发现COM%d或COM%d被占用,请重新设置串口参数。",
m_pPortInfo[i].bComID,
m_pPortInfo[i].bComID );
MessageBox( hwnd, szFileName, "提示", MB_ICONWARNING );
return SYSTEM_ERROR;
}
}
}
m_pProtocol = new XProtocol( m_dwThreadID,
szPathName,
m_nSerialCount,
m_bHaveMID,
m_nCardIDLength,
m_DownMode );
m_pProtocol->Load();
if ( m_pDoorUnitEvent == NULL )
{
m_pDoorUnitEvent = new XDoorUnitEvents();
}
m_pDoorUnitEvent->OpenFile( szPathName );
DownLoadDataAtStartUp();
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::DownLoadDataAtStartUp()
{
m_pProtocol->BeginCheckMachine(0,0,true);
m_pProtocol->DSetCtrlTime();
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::CheckController( const WORD MIDAddr, const WORD CtrlAddr,bool bInit )
{
m_pProtocol->BeginCheckMachine(MIDAddr,CtrlAddr,false);
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::CheckVersion( const WORD MIDAddr, const WORD CtrlAddr )
{
m_pProtocol->DReadProductID(MIDAddr,CtrlAddr);
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::SetOffLineStatus( const WORD wParame, const WORD nSerialNo )
{
m_pProtocol->DoorUnitOffLineStatus( wParame, nSerialNo);
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::SetOnLineStatus( const WORD wParame, const WORD nSerialNo )
{
m_pProtocol->DoorUnitOnLineStatus( wParame, nSerialNo );
return SYSTEM_OK;
}
int XSystem::InformDoorUnitOffLine(const WORD MIDAddr,const WORD CtrlAddr)
{
m_pEventDetected->OnDoorUnitOffLine(MIDAddr,CtrlAddr);
char cRet[SIZE_OF_EVENT_STRING+1];
SYSTEMTIME LocalTime;
GetLocalTime( &LocalTime );
WORD wYear = LocalTime.wYear%100;
sprintf( cRet, "HF0%04X%04XF000000000000000%02X%02X%02X%02X%02X%02X",
MIDAddr,CtrlAddr,wYear,LocalTime.wMonth,LocalTime.wDay,
LocalTime.wHour,LocalTime.wMinute,LocalTime.wSecond);
//m_pDoorUnitEvent->WriteEvent( (BYTE*)cRet, SIZE_OF_EVENT_STRING );
ATLASSERT(strlen(cRet)==39);
return SYSTEM_OK;
}
int XSystem::InformDoorUnitOnLine(const WORD MIDAddr,const WORD CtrlAddr)
{
m_pEventDetected->OnDoorUnitOnLine(MIDAddr,CtrlAddr);
char cRet[SIZE_OF_EVENT_STRING+1];
SYSTEMTIME LocalTime;
GetLocalTime( &LocalTime );
WORD wYear = LocalTime.wYear%100;
sprintf( cRet, "HF1%04X%04XF100000000000000%02X%02X%02X%02X%02X%02X",
MIDAddr,CtrlAddr,wYear,LocalTime.wMonth,LocalTime.wDay,
LocalTime.wHour,LocalTime.wMinute,LocalTime.wSecond);
//m_pDoorUnitEvent->WriteEvent( (BYTE*)cRet, SIZE_OF_EVENT_STRING );
ATLASSERT(strlen(cRet)==39);
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::ResetEventDoInfo( const WORD wParame,const int nSerialNo)
{
m_pProtocol->SetEventDoInfo( wParame, nSerialNo,0);
return SYSTEM_OK;
}
int XSystem::GetEventInfo( tagEvents &EventsInfo)
{
memset( &EventsInfo, 0, sizeof( tagEvents ) );
int nParam[20];
memset( &nParam,0,sizeof(int)*20);
if( !m_bActived || NULL == m_pProtocol ){ return SYSTEM_ERROR; }
char* szEvents = m_pDoorUnitEvent->ReadOneEvent();
if( NULL == szEvents ){ return SYSTEM_ERROR;}
/* |EID |cardid[8] |L- |Datetime */
int nRent = sscanf(szEvents, "H%02X %04X%04X %02X%02X%02X%02X%02X%02X%02X%02X %02X%02X%02X%02X%02X%02X",
&nParam[0], &nParam[1],&nParam[2],&nParam[3],&nParam[4],&nParam[5],&nParam[6],
&nParam[7],&nParam[8],&nParam[9],&nParam[10],&nParam[11],&nParam[12],&nParam[13],
&nParam[14],&nParam[15],&nParam[16]);
ATLASSERT(17==nRent);
EventsInfo.EventID = (BYTE)nParam[0];
EventsInfo.MIDAddr = (WORD)nParam[1];
EventsInfo.CtrlAddr = (WORD)nParam[2];
WORD Addr_M_C =( (WORD)nParam[1]<<16 ) + ( (WORD)nParam[2] );
if ( nParam[11] == 8)
{
EventsInfo.Card_1_2_3_4 = ( (BYTE)nParam[3]<<24)+( (BYTE)nParam[4]<<16)+( (BYTE)nParam[5]<<8)+( (BYTE)nParam[6] );
EventsInfo.Card_5_6_7_8 = ( (BYTE)nParam[7]<<24)+( (BYTE)nParam[8]<<16)+( (BYTE)nParam[9]<<8)+( (BYTE)nParam[10] );
EventsInfo.Card_9_10 = 0;
EventsInfo.CardLength = 8;
}
/*else
{
EventsInfo.Card_1_2_3_4 = ( (BYTE)nParam[3]<<24)+( (BYTE)nParam[4]<<16)+( (BYTE)nParam[5]<<8)+( (BYTE)nParam[6] );
EventsInfo.Card_5_6_7_8 = ( (BYTE)nParam[7]<<24)+( (BYTE)nParam[8]<<16)+( (BYTE)nParam[9]<<8)+( (BYTE)nParam[10] );
EventsInfo.Card_9_10_11_12 = 0;
EventsInfo.CardLength = 10;
}*/
DATE date;
SYSTEMTIME st;
st.wYear = (BYTE)nParam[12]+2000;
st.wMonth = (BYTE)nParam[13];
st.wDay = (BYTE)nParam[14];
st.wHour = (BYTE)nParam[15];
st.wMinute = (BYTE)nParam[16];
st.wSecond = 1;
SystemTimeToVariantTime(&st,&date);
EventsInfo.EventTime = date;
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::Run()
{
if ( !m_bActived ) return 0;
static int s_CurrentSerial = 0;
if ( m_pProtocol == NULL|| m_pSerial == NULL )
{
return SYSTEM_ERROR;
}
long longTime = GetTickCount() - m_pProtocol->dwEditTime;
if(longTime >= SAVE_TIME_SPACE ){
m_pProtocol->Save();
}
CBufferArray bufferarray;
m_pProtocol->GetSerialCmd( &bufferarray );
if ( NULL != bufferarray.Data() )
{
DispatchCmd( bufferarray.Data(),bufferarray.Size());
}
DealMessageFromSerial(s_CurrentSerial);
ATLASSERT( s_CurrentSerial >=0 && s_CurrentSerial < m_nSerialCount );
int nRet = m_pSerial[s_CurrentSerial]->SendCmd();
//***********************************************************
switch ( nRet )
{
case SERIAL_WRITING:
case SERIAL_READING:
case SERIAL_WAITING:
break;
case SERIAL_READY_TO_ASK_NEW_HISTORY:
{
BYTE *pBuf = m_pProtocol->GetEventCmd(s_CurrentSerial);
if ( pBuf != NULL )
{
if ( m_pSerial[s_CurrentSerial]->SetAskEventCmd( pBuf ) == SERIAL_OK )
m_pSerial[s_CurrentSerial]->AskForEvent();
}
Sleep(1);
break;
}
case SERIAL_REPEAT_ASK_HISTORY:
{
m_pSerial[s_CurrentSerial]->AskForEvent();
break;
}
case SERIAL_CMD_SEND_OK:
break;
}
//***********************************************************
s_CurrentSerial++;
if ( s_CurrentSerial >= m_nSerialCount )
{
s_CurrentSerial = 0;
MSG msg;
if (!PeekMessage( &msg,NULL,0,0,PM_NOREMOVE ))
Sleep(1);
}
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::DealMessageFromSerial( int nSerialNo )
{
if ( nSerialNo >= m_nSerialCount ) return SYSTEM_ERROR;
int nLen = 0;
CBufferArray bufferarray;
m_pSerial[nSerialNo]->GetRecenMessage( &bufferarray );
BYTE *p = bufferarray.Data();
nLen = bufferarray.Size();
while ( NULL != p )
{
p[3] = (BYTE)DECToBCD(p[3]);
p[5] = (BYTE)DECToBCD(p[5]);
switch( p[6] )
{
case 0xB3:
{
m_pProtocol->DoCheck(nSerialNo,p);
break;
}
case 0xA1:
{
if ( nLen==20 )
{
m_pProtocol->AddNewDoorUnit(nSerialNo,p);
WORD MIDAddr = p[3];
WORD CtrlAddr = p[5];
if ( m_bHaveMID == 1 )
{
if ( MIDAddr <= 0 || MIDAddr > 32 || CtrlAddr <= 0 || CtrlAddr > 32) break;
MIDAddr = MIDAddr+nSerialNo*MAX_COM_DOORUNIT;
}
else
{
if (CtrlAddr <= 0 || CtrlAddr > 32) break;
MIDAddr = 1;
CtrlAddr = CtrlAddr + nSerialNo*MAX_COM_DOORUNIT;
}
m_pEventDetected->OnDoorUnitDetected(MIDAddr,CtrlAddr);
}
break;
}
default: break;
}
m_pSerial[nSerialNo]->GetRecenMessage( &bufferarray );
p = bufferarray.Data();
nLen = bufferarray.Size();
}
m_pSerial[nSerialNo]->GetRecenHistory( &bufferarray );
p = bufferarray.Data();
nLen = bufferarray.Size();
if ( p != NULL )
{
//....
p[3] = (BYTE)DECToBCD(p[3]);
p[5] = (BYTE)DECToBCD(p[5]);
WORD wParame =0 ;
if (p[3] >0 || p[3] <=32)
{ wParame = (p[3] << 8)+p[5]; }
else { wParame = (WORD)p[5]; }
m_pProtocol->SetEventDoInfo(wParame,nSerialNo,0);
//m_pProtocol->SetDoorUnitEventInfo( wParame,nSerialNo);
char cRet[SIZE_OF_EVENT_STRING+1];
//....
WORD MIDAddr = 0;
WORD CtrlAddr = 0;
if ( m_bHaveMID == 1 )
{
MIDAddr = p[3]+(nSerialNo<<5);
CtrlAddr = p[5];
}
else
{
MIDAddr = 1;
CtrlAddr = p[5]+(nSerialNo<<5);
}
if (p[26] != 0x26 ) //..????
DoDoorUnitStatus(MIDAddr,CtrlAddr,p[8]);
switch( p[9] )
{
case 0x41: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,41);break;}
case 0x42: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,42);break;}
case 0x43: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,43);break;}
case 0x44: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,44);break;}
case 0x45: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,45);break;}
case 0x46: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,46);break;}
case 0x47: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,47);break;}
case 0x48: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,48);break;}
case 0x49: { m_pEventDetected->OnDoorUnitReadCardEvent(MIDAddr,CtrlAddr,49);break;}
default: break;
}
BYTE bCardID = (BYTE)m_nCardIDLength;
DATE date;
SYSTEMTIME st;
DWORD Addr_M_C = (MIDAddr<<16) + CtrlAddr;
DWORD Card_1_2_3_4 = 0;
DWORD Card_5_6_7_8 = 0;
WORD Card_9_10 = 0;
if ( bCardID == 5 )
{
Card_1_2_3_4 = (p[14]<<24)+(p[13]<<16)+(p[12]<<8)+(p[11] );
Card_5_6_7_8 = (p[10]<<24);
if ((Card_1_2_3_4 == 0) && (Card_5_6_7_8 == 0 ) ) return SYSTEM_OK;
if ( ( p[18] == 0x00) && (p[19] == 0x00) && ( p[20] == 0x00) && ( p[21] == 0x00) && ( p[22] == 0x00) ) return SYSTEM_OK;
//|EID |cardid[10] |L- |Datetime[6]
sprintf( cRet, "H%02X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
p[9],MIDAddr,CtrlAddr,p[17], p[16], p[15], p[14], p[13],p[12], p[11], p[10],bCardID,bCardID,
bCardID, DECToBCD(p[18]), DECToBCD(p[19]), DECToBCD(p[20]),DECToBCD(p[21]),DECToBCD(p[22]));
st.wYear = DECToBCD((BYTE)p[18])+2000;
st.wMonth = DECToBCD((BYTE)p[19]);
st.wDay = DECToBCD((BYTE)p[20]);
st.wHour = DECToBCD((BYTE)p[21]);
st.wMinute = DECToBCD((BYTE)p[22]);
st.wSecond = 1;
SystemTimeToVariantTime(&st,&date);
}
else //10
{
Card_1_2_3_4 = (p[19]<<24)+(p[18]<<16)+(p[17]<<8)+(p[16] );
Card_5_6_7_8 = (p[15]<<24)+(p[14]<<16)+(p[13]<<8)+(p[12] );
Card_9_10 = (p[11]<<8)+(p[10]);
if ((Card_1_2_3_4 == 0) && (Card_5_6_7_8 == 0 ) && ( Card_9_10 == 0 )) return SYSTEM_OK;
if (( p[20] == 0x00) && (p[21] == 0x00) && ( p[22] == 0x00) && ( p[23] == 0x00) && ( p[24] == 0x00) ) return SYSTEM_OK;
//|EID |cardid[10] |L- |Datetime[6]
sprintf( cRet, "H%02X %04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
p[9],MIDAddr,CtrlAddr,p[19], p[18], p[17], p[16], p[15],p[14], p[13], p[12],p[11],p[10],
bCardID, DECToBCD(p[20]), DECToBCD(p[21]), DECToBCD(p[22]),DECToBCD(p[23]),DECToBCD(p[24]));
st.wYear = DECToBCD((BYTE)p[20])+2000;
st.wMonth = DECToBCD((BYTE)p[21]);
st.wDay = DECToBCD((BYTE)p[22]);
st.wHour = DECToBCD((BYTE)p[23]);
st.wMinute = DECToBCD((BYTE)p[24]);
st.wSecond = 1;
SystemTimeToVariantTime(&st,&date);
}
//m_pDoorUnitEvent->WriteEvent((BYTE*)cRet,SIZE_OF_EVENT_STRING);
m_pEventDetected->OnReadDoorUnitEvent( p[9], Addr_M_C,
Card_1_2_3_4,
Card_5_6_7_8,
Card_9_10,
bCardID,date);
}
return SYSTEM_OK;
}
//-----------------------------------------------------------------------
int XSystem::Destroy()
{
SafeDelete( m_pProtocol );
if ( m_DownMode == 0 )
{
for ( int i = 0 ; i< m_nSerialCount; i++ )
{
XCommPort *pCP = static_cast<XCommPort*>(m_pSerial[i]);
delete pCP;
}
}
SafeDeleteArray( m_pSerial );
SafeDeleteArray( m_pPortInfo );
&nb
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。