#include "stdafx.h" #include "IdentifyService.h" #include "FolderFeeder.h" #include "feeder.h" #include "Identifor.h" #include "ResultUploader.h" #include "basic_struct.h" #include "SeparateThreadTwainFeeder.h" #include "ImgUploader.h" #include "basic_struct_result.h" #include "StudentMatcher.h" #include "CManualMatchFeeder.h" #include "RescanFromServerFeeder.h" #include "DataBaseManagerHolder.h" #include "BatchService.h" //#include "Log4cplusInitalizer.h" #include "ServerConfig.h" extern int g_my_continue_scan; CIdentifyService::CIdentifyService(CBatchService* batchService) :batch_service(batchService), taskManager(new CIdentifyTaskManager()), /*送入器*/ m_twain_feeder(0), m_server_feeder(0), m_auto_schema_feeder(0), m_folder_feeder(0), m_manualmatch_feeder(0), isMainService(false) { isReIdentify = false; m_bErrorHandle = false; m_bBianSBBianUpload = false; m_nOnlineScanType = -1; } CIdentifyService::~CIdentifyService() { } ServiceState CIdentifyService::OnStarting(void) { m_abnormal_stop = FALSE; unsigned _int64 i64FreeBytesToCaller; unsigned _int64 i64TotalBytes; unsigned _int64 i64FreeBytes; BOOL check_success = GetDiskFreeSpaceEx(m_bantch_dir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes); DWORD error = GetLastError(); //空余空间小于2G提示不能扫描 const unsigned long long limit = 1024Ui64 * 1024 * 1024 * 2; if (check_success){ if (isMainService&&i64FreeBytesToCaller < limit){ IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG; msg->type = 1; strcpy(msg->msg, "当前空余空间不足2G,请清理磁盘再进行扫描"); PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0); return stoping; } } else{ IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG; msg->type = 1; strcpy(msg->msg, "检查磁盘剩余空间失败!"); PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0); return stoping; } if (g_my_continue_scan == 1 && !isReIdentify) { //发送消息恢复之前扫描结果界面 EnterCriticalSection(db_lock); CppSQLite3Query query = bantch_db->execQuery("select * from message order by student_paper_id"); while (!query.eof()){ if (query.getIntField("message_type") == WM_IDENTIFOR_PAPER_STATE_CHAGED) { if (m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)query.getIntField("student_paper_id")); } else if (query.getIntField("message_type") == WM_IDENTIFOR_REPORT) { IDENTIFY_MSG * msg = new IDENTIFY_MSG; msg->class_id = query.getIntField("class_id"); msg->class_total = query.getIntField("class_total"); strcpy(msg->class_info, query.getStringField("class_info")); msg->exceptions = query.getIntField("exceptions"); msg->identified = identified; msg->paper_id = query.getIntField("student_paper_id"); msg->score = query.getFloatField("score"); msg->student_id = query.getIntField("student_id"); msg->locationID = 0; strcpy(msg->student_name, query.getStringField("student_name")); strcpy(msg->examnumber, query.getStringField("examnumber")); ::PostMessage(m_hwnd, WM_IDENTIFOR_REPORT, (WPARAM)msg, NULL); } query.nextRow(); } if (m_feeder_type == Folder) { CppSQLite3Query query1 = bantch_db->execQuery("SELECT * FROM page where page_id = (SELECT max(page_id) FROM page where student_paper_id = (select max(student_paper_id) from student_paper))"); if (!query1.eof()) { char sql[512]; if (m_bBianSBBianUpload){ sprintf_s(sql, "SELECT * FROM student_paper where student_paper_id = %d", query1.getIntField("student_paper_id")); } else{ sprintf_s(sql, "SELECT * FROM student_paper where student_paper_id = %d and ret_upload = 0", query1.getIntField("student_paper_id")); } CppSQLite3Query query2 = bantch_db->execQuery(sql); if (!query2.eof()){ std::string stmpstr = query1.getStringField("img_oldpath"); CString namePath = CA2T(stmpstr.c_str()); m_folder_feeder.get()->SetReStartConfig(namePath, true); } else{ m_folder_feeder.get()->SetReStartConfig(_T(""), false); } } else { m_folder_feeder.get()->SetReStartConfig(_T(""), false); } } LeaveCriticalSection(db_lock); } else { if (m_feeder_type == Folder) { m_folder_feeder.get()->SetReStartConfig(_T(""), false); } EnterCriticalSection(db_lock); bantch_db->execDML("Delete from message where 1 = 1"); LeaveCriticalSection(db_lock); //清除原来消息 } switch (m_feeder_type){ case Folder: m_current_feeder = m_folder_feeder; break; case Scanner: m_current_feeder = m_twain_feeder; break; case AdjustBatch: m_current_feeder = m_server_feeder; break; case AdjustExaminne: m_current_feeder = m_server_feeder; break; case ManualMatch:m_current_feeder = m_manualmatch_feeder; break; default:m_current_feeder = NULL; break; } if (m_current_feeder != NULL){ m_current_feeder->Start(); m_current_feeder->SetHandlerWnd(m_hwnd); m_identifor->SetExcImgDir(m_excimg_dir); CIdentifyTask* main_task = taskManager->createTask(); main_task->SetFeeder(m_current_feeder.get()); taskManager->setMainTask(main_task); } m_identifor->LoadSchema(batch_service->GeSchema()); m_identifor->Start(); m_result_handler->SetDataBase(db_lock, bantch_db); m_result_handler->SetImgClipsDir(m_img_clips_dir); m_result_handler->SetIdentifor(m_identifor.get()); m_result_handler->SetIdentifyService(this); m_result_handler->Start(); m_student_matcher->SetDataBase(db_lock, bantch_db); m_student_matcher->SetResultHandler(m_result_handler.get()); Sleep(10); m_student_matcher->setExamId(m_examId); m_student_matcher->Start(); return running; } ServiceState CIdentifyService::OnRunning(void) { ServiceState nextState = IService::OnRunning(); if (nextState == pausing) return nextState; if (nextState == stoping) { m_current_feeder->Stop(); return nextState; } if (m_identifor->GetServiceSate() == stoped&&m_result_handler->GetServiceSate() == stoped){ return stoping; } Sleep(10); return running; } ServiceState CIdentifyService::OnPausing(void) { m_current_feeder->Pause(); while (m_current_feeder->GetServiceSate() == pausing || m_current_feeder->GetServiceSate() == running){ Sleep(10); } ServiceState nextState = IService::OnPausing(); //if (isMainService&&nextState == paused&&m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAUSED, 0, 0); return nextState; } ServiceState CIdentifyService::OnResuming(void) { m_current_feeder->Resume(); while (m_current_feeder->GetServiceSate() == resuming){ Sleep(10); } ServiceState nextState = IService::OnResuming(); //if (isMainService&&nextState == running&&m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_RESUMED, 0, 0); return nextState; } ServiceState CIdentifyService::OnStoping(void) { if (m_current_feeder != NULL&&m_current_feeder->GetServiceSate() != stoped){ m_current_feeder->Stop(); } if (m_result_handler->GetServiceSate() != stoped){ m_result_handler->Stop(); } while ((m_current_feeder != NULL&&m_current_feeder->GetServiceSate() != stoped) || m_result_handler->GetServiceSate() != stoped){ Sleep(10); } while (m_student_matcher->GetServiceSate() != stoped){ Sleep(10); } /*char sql[128]; sprintf_s(sql, "update database set database_state =%d where database_id =%d", DATABASE_STATE_SCAN_OVER, m_database_id); EnterCriticalSection(database_db_lock); database_db->execDML("begin transaction"); database_db->execDML(sql); database_db->execDML("commit transaction"); LeaveCriticalSection(database_db_lock);*/ //if (m_hwnd != NULL&&m_abnormal_stop == FALSE)::PostMessage(m_hwnd, WM_IDENTIFOR_COMPLETE, 0, 0); ServiceState nextState = IService::OnStoping(); if (isReIdentify&&nextState == stoped&&m_hwnd != NULL){ ::PostMessage(m_hwnd, WM_IDENTIFOR_STOPED, 0, 0); } return nextState; } void CIdentifyService::SetFolderPageNumber(int nCount) { if(m_folder_feeder) { m_folder_feeder->SetPageNumber(nCount); } } void CIdentifyService::ReadScan(FEEDER_TYPE feederType, const ScanParam & param) { isReIdentify = false; m_examId = param.examId; m_examCourseId = param.examCourseId; m_examName = param.examName; m_courseName = param.courseName; // Log4cplusInitalizer::initalize(); m_identifor->Reset(); m_feeder_type = feederType; m_result_handler->SetDataBase(db_lock, bantch_db); m_student_matcher->SetDataBase(db_lock, bantch_db); m_student_matcher->SetImgDir(m_norimg_dir, m_excimg_dir); switch (feederType&FEEDER_TYPE_MASK) { case Folder: m_folder_feeder.reset(new CFolderFeeder()); m_folder_feeder->SetFolder(param.img_dir); break; case Scanner: m_twain_feeder = CSeparateThreadTwainFeeder::Get(); m_twain_feeder->SetScannerIndex(param.scanner_id); break; case AdjustBatch: m_server_feeder.reset(new CRescanFromServerFeeder()); m_server_feeder->SetImageDownloadDir(m_img_down_load_dir); m_server_feeder->SetParam(param.examId, param.examCourseIdOld, CString(param.batch.c_str())); break; case AdjustExaminne: m_server_feeder.reset(new CRescanFromServerFeeder()); m_server_feeder->SetImageDownloadDir(m_img_down_load_dir); m_server_feeder->SetParam(param.examId, param.examCourseIdOld, CString(param.batch.c_str()), CString(param.examinneid.c_str())); break; } } int CIdentifyService::SetHandlerWnd(HWND hwnd) { m_hwnd = hwnd; if (m_result_handler.get()){ m_result_handler->SetHandlerWnd(m_hwnd); } if (m_student_matcher.get()){ m_student_matcher->SetHandlerWnd(m_hwnd); } return 1; } int CIdentifyService::ReIdentify(const int paper_id, const std::vector& params) { isReIdentify = true; CppSQLite3Statement statm = bantch_db->compileStatement("update student_paper set paper_state =:paper_state where student_paper_id=:student_paper_id"); statm.bind(":paper_state", EX_SHIJUANCHONGXINSHIBIEZHONG); statm.bind(":student_paper_id", paper_id); statm.execDML(); m_manualmatch_feeder.reset(new CManualMatchFeeder()); m_manualmatch_feeder->SetImgList(params); m_feeder_type = (FEEDER_TYPE)(ManualMatch | (m_feeder_type&AutoSchemaFlag)); CIdentifyTask* task = taskManager->createTask(); m_manualmatch_feeder->SetHandlerWnd(m_hwnd); m_manualmatch_feeder->Start(); task->SetFeeder(m_manualmatch_feeder.get()); taskManager->ExcuTask(task); return Start(); } int CIdentifyService::continueScan(const std::string& img_dir) { m_identifor->Reset(); m_result_handler->SetDataBase(db_lock, bantch_db); m_student_matcher->SetDataBase(db_lock, bantch_db); m_student_matcher->SetImgDir(m_norimg_dir, m_excimg_dir); m_folder_feeder->SetFolder(img_dir); return 1; } int CIdentifyService::SetDirs(const CString& bantch_dir, const CString&norimg_dir, const CString&excimg_dir, CString img_clips_dir, const CString&img_alynasis_dir, const CString&img_alynasis_result_dir, const CString&img_down_load_dir, const std::string& muban_img_dir) { m_bantch_dir = bantch_dir; m_norimg_dir = norimg_dir; m_excimg_dir = excimg_dir; m_img_clips_dir = img_clips_dir; m_img_alynasis_dir = img_alynasis_dir; m_img_alynasis_result_dir = img_alynasis_result_dir; m_img_down_load_dir = img_down_load_dir; m_muban_img_dir = muban_img_dir; return 1; } void CIdentifyService::setSchema(boost::shared_ptr schema, boost::shared_ptr handleInfo) { this->m_schema = schema; this->m_handleInfo = handleInfo; m_identifor->LoadSchema(m_schema); } BOOL CIdentifyService::SupportPause(void) { return m_current_feeder.get() ? m_current_feeder->SupportPause() : FALSE; } void CIdentifyService::setMainService(bool isMainService) { this->isMainService = isMainService; } const boost::shared_ptr& CIdentifyService::GetHandleInfo() { return batch_service->GetHandleInfo(); } void CIdentifyService::SetQr(int student_paper_id,bool bUseQr, const std::string&strQR) { m_identifor->SetUseQr(bUseQr, strQR); m_result_handler->SetUseQr(bUseQr, strQR); m_student_matcher->SetUseQr(student_paper_id,bUseQr, strQR); } void CIdentifyService::InitService() { m_identifor = boost::make_shared(); m_result_handler = boost::make_shared(); m_student_matcher = boost::make_shared(); m_identifor->IdentifyTaskManager(taskManager.get()); m_result_handler->IdentifyTaskManager(taskManager.get()); } void CIdentifyService::SerErrorHandle(bool bErrorHandle){ m_bErrorHandle = bErrorHandle; m_student_matcher->SetErrorHandle(m_bErrorHandle); m_result_handler->SetErrorHandle(m_bErrorHandle); }