123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #include "stdafx.h"
- #include "DataManager.h"
- #include "DBInterface.h"
- #include "MachineMgrBase.h"
- #include "FpClockCtrlMgr.h"
- #include "FKAttendCtrlMgr.h"
- #define INSERT_DATA _T("insert into lonindata([no],[datetime])values('%d','%s')")
- CDataManager::CDataManager()
- {
- _hThread = NULL;
- _hThread = NULL;
- _hCtrl1 = NULL;
- m_bThreadRuning = FALSE;
- m_bThreadRuning1 = FALSE;
- }
- CDataManager::~CDataManager()
- {
-
- }
- /*-------------------------------实时提交 begin-------------------------------*/
- //启动
- int CDataManager::StartRealTimeSubmitThread()
- {
- AutoThreadSection aSection(&s_critSection);
- if (_hThread) return -1;
- _hThread = CreateThread(NULL, 0, RealTimeSubmitThread, this, 0, NULL);
- if (_hThread == NULL)
- {
- LOG4C((LOG_NOTICE, "创建线程失败"));
- return -1;
- }
- CloseHandle(_hThread);
- _hThread = NULL;
- m_bThreadRuning = TRUE;
- return 1;
- }
- //结束
- int CDataManager::EndofRealTimeSubmitThread()
- {
- if (_hThread)
- WaitForSingleObject(_hThread, INFINITE);
- m_bThreadRuning = FALSE;
- return 1;
- }
- int CDataManager::RealTimeSubmitWork()
- {
- if(g_pCMachineMgrBase == NULL)
- {
- LOG4C((LOG_NOTICE, "指纹机没连接成功"));
- return 0;
- }
- if(CDBInterface::GetInstance()->IsOpen())
- {
- //读取数据前先设置ReadMark为TRUE
- g_pCMachineMgrBase->SetReadMark(TRUE);
- if(g_pCMachineMgrBase->SetMachineEnable(FALSE))
- {
- // LOG4C((LOG_NOTICE, "实时提交启动"));
- std::vector<STGeneralLogData> vtGeneralLogData;
- if(g_pCMachineMgrBase->ReadGeneralLogData(&vtGeneralLogData) != -1)
- WriteIntoDatabase(&vtGeneralLogData);
- }
- g_pCMachineMgrBase->SetMachineEnable(TRUE);
- }
- AutoThreadSection aSection(&s_critSection);
- m_bThreadRuning = FALSE;
- return 1;
- }
- DWORD CDataManager::RealTimeSubmitThread(LPVOID lpParam)
- {
- CDataManager* p = (CDataManager*)lpParam;
- if(p)
- p->RealTimeSubmitWork();
- return 0;
- }
- /*-------------------------------实时提交 end-------------------------------*/
- /*-------------------------------全部提交 begin-------------------------------*/
- //启动
- int CDataManager::StartSubmitAllThread()
- {
- _hCtrl1 = CreateEvent(NULL, TRUE, FALSE, NULL); // 无信号事件;
- if (_hCtrl1 == NULL)
- {
- LOG4C((LOG_NOTICE, "创建事件失败"));
- //PostMessage(g_hwnd,MSG_SHOWPROMPTING,0,0);
- return 0;
- }
- _hThread1 = CreateThread(NULL, 0, SubmitAllDataThread, this, 0, NULL);
- if (_hThread1 == NULL)
- {
- LOG4C((LOG_NOTICE, "创建线程失败"));
- return 0;
- }
- m_bThreadRuning1 = TRUE;
- return 1;
- }
- //结束
- int CDataManager::EndofSubmitAllThread()
- {
- // 1.关闭线程------------------------;;
- if (_hCtrl1)
- SetEvent(_hCtrl1);
- if (_hThread1)
- {
- if (WaitForSingleObject(_hThread1, INFINITE) != WAIT_TIMEOUT)
- CloseHandle(_hThread1);
- }
- _hThread1 = NULL;
- m_bThreadRuning1 = FALSE;
- if (_hCtrl1)
- CloseHandle(_hCtrl1);
- _hCtrl1 = NULL;
- return 1;
- }
- //
- int CDataManager::SubmitAllWork()
- {
- if(g_pCMachineMgrBase == NULL)
- return 0;
- // 1.因为全部提交数据量比较大,时间长,先设置指纹机不可用;
- if (g_pCMachineMgrBase->SetMachineEnable(FALSE) == FALSE)
- {
- if (_hCtrl1)
- CloseHandle(_hCtrl1);
- _hCtrl1 = NULL;
- if (_hThread1)
- CloseHandle(_hThread1);
- _hThread1 = NULL;
- AutoThreadSection aSection(&s_critSection);
- m_bThreadRuning1 = FALSE;
- return 0;
- }
- // 3.循环读取全部记录;
- std::vector<STGeneralLogData> vtGeneralLogData;
- if(g_pCMachineMgrBase->ReadAllGlLogData(&vtGeneralLogData) != -1)
- WriteIntoDatabase(&vtGeneralLogData);
- g_pCMachineMgrBase->SetMachineEnable(TRUE);
- if (_hCtrl1)
- CloseHandle(_hCtrl1);
- if (_hThread1)
- CloseHandle(_hThread1);
- _hCtrl1 = NULL;
- _hThread1 = NULL;
- AutoThreadSection aSection(&s_critSection);
- m_bThreadRuning1 = FALSE;
- return 1;
- }
- DWORD CDataManager::SubmitAllDataThread(LPVOID lpParam)
- {
- CDataManager* p = (CDataManager*)lpParam;
- if(p)
- p->SubmitAllWork();
- return 0;
- }
- /*-------------------------------全部提交 end-------------------------------*/
- DWORD CDataManager::WriteIntoDatabase(std::vector<STGeneralLogData>* pVData)
- {
- if(pVData == NULL)
- return 0;
- CString strSQL;
- if (CDBInterface::GetInstance()->IsOpen())
- {
- std::vector<STGeneralLogData>::iterator it = pVData->begin();
- DWORD dwInsertCount = 0;
- for(; it != pVData->end(); )
- {
- COleDateTime tagOleTime(it->dwYear, it->dwMonth, it->dwDay, it->dwHour, it->dwMinute, it->nVerifyMode);
- SendMessage(g_hwnd, MSG_SHOWREALTIMELOG, (WPARAM)&it->nEnrollNumber, (LPARAM)&tagOleTime);
- // PostMessage(g_hwnd, MSG_SHOWREALTIMELOG, (WPARAM)&it->nEnrollNumber, (LPARAM)&tagOleTime);
- strSQL.Format(INSERT_DATA, it->nEnrollNumber, tagOleTime.Format(_T("%Y-%m-%d %H:%M:%S")));
- ++it;
- if (CDBInterface::GetInstance()->Execute(strSQL) != 0)
- continue;
- dwInsertCount++;
- Sleep(10);
- }
- return dwInsertCount;
- }
- return 0;
- }
- DWORD CDataManager::ReadOldLog(std::vector<STAllLog>& vtAllLog)
- {
- CString strSQL;
- std::vector<STAllLog>::iterator it;
- if( CDBInterface::GetInstance()->OpenDatabase() )
- {
- DWORD dwInsertCount = 0;
- for ( it = vtAllLog.begin(); it != vtAllLog.end(); it++)
- {
- //COleDateTime tagOleTime(it->nDateTime);
- //PostMessage(g_hwnd,MSG_SHOWREALTIMELOG,(WPARAM)&it->nEnrollNumber,(LPARAM)&it->nDateTime);
- strSQL.Format(_T("insert into lonindata([no],[datetime])values('%s','%s')"),it->strEnrollNumber,it->strDateTime);
- // LOG4C((LOG_NOTICE,"%s",strSQL));
- if( CDBInterface::GetInstance()->Execute(strSQL.GetBuffer(0)) != 0)
- {
- strSQL.ReleaseBuffer();
- continue;
- }
- dwInsertCount++;
- strSQL.ReleaseBuffer();
- }
- strSQL.Format(_T("提交AllLog.txt完毕,去除重复数据,实际提交=%d条..."),dwInsertCount);
- SendMessage(g_hwnd, MSG_SHOWPROMPTING,(WPARAM)&strSQL, 0);
- return dwInsertCount;
- }
- return 0;
- }
- BOOL CDataManager::IsSubmitAllThreadRuning()
- {
- AutoThreadSection aSection(&s_critSection);
- return m_bThreadRuning1;
- }
|