IdentifyService.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #include "stdafx.h"
  2. #include "IdentifyService.h"
  3. #include "FolderFeeder.h"
  4. #include "feeder.h"
  5. #include "Identifor.h"
  6. #include "ResultUploader.h"
  7. #include "basic_struct.h"
  8. #include "SeparateThreadTwainFeeder.h"
  9. #include "ImgUploader.h"
  10. #include "basic_struct_result.h"
  11. #include "StudentMatcher.h"
  12. #include "CManualMatchFeeder.h"
  13. #include "RescanFromServerFeeder.h"
  14. #include "DataBaseManagerHolder.h"
  15. #include "BatchService.h"
  16. //#include "Log4cplusInitalizer.h"
  17. #include "ServerConfig.h"
  18. extern int g_my_continue_scan;
  19. CIdentifyService::CIdentifyService(CBatchService* batchService) :batch_service(batchService),
  20. taskManager(new CIdentifyTaskManager()),
  21. /*送入器*/
  22. m_twain_feeder(0),
  23. m_server_feeder(0),
  24. m_auto_schema_feeder(0),
  25. m_folder_feeder(0),
  26. m_manualmatch_feeder(0),
  27. isMainService(false)
  28. {
  29. isReIdentify = false;
  30. m_bErrorHandle = false;
  31. m_bBianSBBianUpload = false;
  32. m_nOnlineScanType = -1;
  33. }
  34. CIdentifyService::~CIdentifyService()
  35. {
  36. }
  37. ServiceState CIdentifyService::OnStarting(void)
  38. {
  39. m_abnormal_stop = FALSE;
  40. unsigned _int64 i64FreeBytesToCaller;
  41. unsigned _int64 i64TotalBytes;
  42. unsigned _int64 i64FreeBytes;
  43. BOOL check_success = GetDiskFreeSpaceEx(m_bantch_dir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes);
  44. DWORD error = GetLastError();
  45. //空余空间小于2G提示不能扫描
  46. const unsigned long long limit = 1024Ui64 * 1024 * 1024 * 2;
  47. if (check_success){
  48. if (isMainService&&i64FreeBytesToCaller < limit){
  49. IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG;
  50. msg->type = 1;
  51. strcpy(msg->msg, "当前空余空间不足2G,请清理磁盘再进行扫描");
  52. PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0);
  53. return stoping;
  54. }
  55. }
  56. else{
  57. IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG;
  58. msg->type = 1;
  59. strcpy(msg->msg, "检查磁盘剩余空间失败!");
  60. PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0);
  61. return stoping;
  62. }
  63. if (g_my_continue_scan == 1 && !isReIdentify)
  64. {
  65. //发送消息恢复之前扫描结果界面
  66. EnterCriticalSection(db_lock);
  67. CppSQLite3Query query = bantch_db->execQuery("select * from message order by student_paper_id");
  68. while (!query.eof()){
  69. if (query.getIntField("message_type") == WM_IDENTIFOR_PAPER_STATE_CHAGED)
  70. {
  71. if (m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)query.getIntField("student_paper_id"));
  72. }
  73. else if (query.getIntField("message_type") == WM_IDENTIFOR_REPORT)
  74. {
  75. IDENTIFY_MSG * msg = new IDENTIFY_MSG;
  76. msg->class_id = query.getIntField("class_id");
  77. msg->class_total = query.getIntField("class_total");
  78. strcpy(msg->class_info, query.getStringField("class_info"));
  79. msg->exceptions = query.getIntField("exceptions");
  80. msg->identified = identified;
  81. msg->paper_id = query.getIntField("student_paper_id");
  82. msg->score = query.getFloatField("score");
  83. msg->student_id = query.getIntField("student_id");
  84. msg->locationID = 0;
  85. strcpy(msg->student_name, query.getStringField("student_name"));
  86. strcpy(msg->examnumber, query.getStringField("examnumber"));
  87. ::PostMessage(m_hwnd, WM_IDENTIFOR_REPORT, (WPARAM)msg, NULL);
  88. }
  89. query.nextRow();
  90. }
  91. if (m_feeder_type == Folder)
  92. {
  93. 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))");
  94. if (!query1.eof())
  95. {
  96. char sql[512];
  97. if (m_bBianSBBianUpload){
  98. sprintf_s(sql, "SELECT * FROM student_paper where student_paper_id = %d", query1.getIntField("student_paper_id"));
  99. }
  100. else{
  101. sprintf_s(sql, "SELECT * FROM student_paper where student_paper_id = %d and ret_upload = 0", query1.getIntField("student_paper_id"));
  102. }
  103. CppSQLite3Query query2 = bantch_db->execQuery(sql);
  104. if (!query2.eof()){
  105. std::string stmpstr = query1.getStringField("img_oldpath");
  106. CString namePath = CA2T(stmpstr.c_str());
  107. m_folder_feeder.get()->SetReStartConfig(namePath, true);
  108. }
  109. else{
  110. m_folder_feeder.get()->SetReStartConfig(_T(""), false);
  111. }
  112. }
  113. else
  114. {
  115. m_folder_feeder.get()->SetReStartConfig(_T(""), false);
  116. }
  117. }
  118. LeaveCriticalSection(db_lock);
  119. }
  120. else
  121. {
  122. if (m_feeder_type == Folder)
  123. {
  124. m_folder_feeder.get()->SetReStartConfig(_T(""), false);
  125. }
  126. EnterCriticalSection(db_lock);
  127. bantch_db->execDML("Delete from message where 1 = 1");
  128. LeaveCriticalSection(db_lock);
  129. //清除原来消息
  130. }
  131. switch (m_feeder_type){
  132. case Folder: m_current_feeder = m_folder_feeder; break;
  133. case Scanner: m_current_feeder = m_twain_feeder; break;
  134. case AdjustBatch: m_current_feeder = m_server_feeder; break;
  135. case AdjustExaminne: m_current_feeder = m_server_feeder; break;
  136. case ManualMatch:m_current_feeder = m_manualmatch_feeder; break;
  137. default:m_current_feeder = NULL; break;
  138. }
  139. if (m_current_feeder != NULL){
  140. m_current_feeder->Start();
  141. m_current_feeder->SetHandlerWnd(m_hwnd);
  142. m_identifor->SetExcImgDir(m_excimg_dir);
  143. CIdentifyTask* main_task = taskManager->createTask();
  144. main_task->SetFeeder(m_current_feeder.get());
  145. taskManager->setMainTask(main_task);
  146. }
  147. m_identifor->LoadSchema(batch_service->GeSchema());
  148. m_identifor->Start();
  149. m_result_handler->SetDataBase(db_lock, bantch_db);
  150. m_result_handler->SetImgClipsDir(m_img_clips_dir);
  151. m_result_handler->SetIdentifor(m_identifor.get());
  152. m_result_handler->SetIdentifyService(this);
  153. m_result_handler->Start();
  154. m_student_matcher->SetDataBase(db_lock, bantch_db);
  155. m_student_matcher->SetResultHandler(m_result_handler.get());
  156. Sleep(10);
  157. m_student_matcher->setExamId(m_examId);
  158. m_student_matcher->Start();
  159. return running;
  160. }
  161. ServiceState CIdentifyService::OnRunning(void)
  162. {
  163. ServiceState nextState = IService::OnRunning();
  164. if (nextState == pausing) return nextState;
  165. if (nextState == stoping) {
  166. m_current_feeder->Stop();
  167. return nextState;
  168. }
  169. if (m_identifor->GetServiceSate() == stoped&&m_result_handler->GetServiceSate() == stoped){
  170. return stoping;
  171. }
  172. Sleep(10);
  173. return running;
  174. }
  175. ServiceState CIdentifyService::OnPausing(void)
  176. {
  177. m_current_feeder->Pause();
  178. while (m_current_feeder->GetServiceSate() == pausing || m_current_feeder->GetServiceSate() == running){
  179. Sleep(10);
  180. }
  181. ServiceState nextState = IService::OnPausing();
  182. //if (isMainService&&nextState == paused&&m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAUSED, 0, 0);
  183. return nextState;
  184. }
  185. ServiceState CIdentifyService::OnResuming(void)
  186. {
  187. m_current_feeder->Resume();
  188. while (m_current_feeder->GetServiceSate() == resuming){
  189. Sleep(10);
  190. }
  191. ServiceState nextState = IService::OnResuming();
  192. //if (isMainService&&nextState == running&&m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_RESUMED, 0, 0);
  193. return nextState;
  194. }
  195. ServiceState CIdentifyService::OnStoping(void)
  196. {
  197. if (m_current_feeder != NULL&&m_current_feeder->GetServiceSate() != stoped){
  198. m_current_feeder->Stop();
  199. }
  200. if (m_result_handler->GetServiceSate() != stoped){
  201. m_result_handler->Stop();
  202. }
  203. while ((m_current_feeder != NULL&&m_current_feeder->GetServiceSate() != stoped) || m_result_handler->GetServiceSate() != stoped){
  204. Sleep(10);
  205. }
  206. while (m_student_matcher->GetServiceSate() != stoped){
  207. Sleep(10);
  208. }
  209. /*char sql[128];
  210. sprintf_s(sql, "update database set database_state =%d where database_id =%d", DATABASE_STATE_SCAN_OVER, m_database_id);
  211. EnterCriticalSection(database_db_lock);
  212. database_db->execDML("begin transaction");
  213. database_db->execDML(sql);
  214. database_db->execDML("commit transaction");
  215. LeaveCriticalSection(database_db_lock);*/
  216. //if (m_hwnd != NULL&&m_abnormal_stop == FALSE)::PostMessage(m_hwnd, WM_IDENTIFOR_COMPLETE, 0, 0);
  217. ServiceState nextState = IService::OnStoping();
  218. if (isReIdentify&&nextState == stoped&&m_hwnd != NULL){
  219. ::PostMessage(m_hwnd, WM_IDENTIFOR_STOPED, 0, 0);
  220. }
  221. return nextState;
  222. }
  223. void CIdentifyService::SetFolderPageNumber(int nCount)
  224. {
  225. if(m_folder_feeder)
  226. {
  227. m_folder_feeder->SetPageNumber(nCount);
  228. }
  229. }
  230. void CIdentifyService::ReadScan(FEEDER_TYPE feederType, const ScanParam & param)
  231. {
  232. isReIdentify = false;
  233. m_examId = param.examId;
  234. m_examCourseId = param.examCourseId;
  235. m_examName = param.examName;
  236. m_courseName = param.courseName;
  237. // Log4cplusInitalizer::initalize();
  238. m_identifor->Reset();
  239. m_feeder_type = feederType;
  240. m_result_handler->SetDataBase(db_lock, bantch_db);
  241. m_student_matcher->SetDataBase(db_lock, bantch_db);
  242. m_student_matcher->SetImgDir(m_norimg_dir, m_excimg_dir);
  243. switch (feederType&FEEDER_TYPE_MASK)
  244. {
  245. case Folder:
  246. m_folder_feeder.reset(new CFolderFeeder());
  247. m_folder_feeder->SetFolder(param.img_dir);
  248. break;
  249. case Scanner:
  250. m_twain_feeder = CSeparateThreadTwainFeeder::Get();
  251. m_twain_feeder->SetScannerIndex(param.scanner_id);
  252. break;
  253. case AdjustBatch:
  254. m_server_feeder.reset(new CRescanFromServerFeeder());
  255. m_server_feeder->SetImageDownloadDir(m_img_down_load_dir);
  256. m_server_feeder->SetParam(param.examId, param.examCourseIdOld, CString(param.batch.c_str()));
  257. break;
  258. case AdjustExaminne:
  259. m_server_feeder.reset(new CRescanFromServerFeeder());
  260. m_server_feeder->SetImageDownloadDir(m_img_down_load_dir);
  261. m_server_feeder->SetParam(param.examId, param.examCourseIdOld, CString(param.batch.c_str()), CString(param.examinneid.c_str()));
  262. break;
  263. }
  264. }
  265. int CIdentifyService::SetHandlerWnd(HWND hwnd)
  266. {
  267. m_hwnd = hwnd;
  268. if (m_result_handler.get()){
  269. m_result_handler->SetHandlerWnd(m_hwnd);
  270. }
  271. if (m_student_matcher.get()){
  272. m_student_matcher->SetHandlerWnd(m_hwnd);
  273. }
  274. return 1;
  275. }
  276. int CIdentifyService::ReIdentify(const int paper_id, const std::vector<img_param>& params)
  277. {
  278. isReIdentify = true;
  279. CppSQLite3Statement statm = bantch_db->compileStatement("update student_paper set paper_state =:paper_state where student_paper_id=:student_paper_id");
  280. statm.bind(":paper_state", EX_SHIJUANCHONGXINSHIBIEZHONG);
  281. statm.bind(":student_paper_id", paper_id);
  282. statm.execDML();
  283. m_manualmatch_feeder.reset(new CManualMatchFeeder());
  284. m_manualmatch_feeder->SetImgList(params);
  285. m_feeder_type = (FEEDER_TYPE)(ManualMatch | (m_feeder_type&AutoSchemaFlag));
  286. CIdentifyTask* task = taskManager->createTask();
  287. m_manualmatch_feeder->SetHandlerWnd(m_hwnd);
  288. m_manualmatch_feeder->Start();
  289. task->SetFeeder(m_manualmatch_feeder.get());
  290. taskManager->ExcuTask(task);
  291. return Start();
  292. }
  293. int CIdentifyService::continueScan(const std::string& img_dir)
  294. {
  295. m_identifor->Reset();
  296. m_result_handler->SetDataBase(db_lock, bantch_db);
  297. m_student_matcher->SetDataBase(db_lock, bantch_db);
  298. m_student_matcher->SetImgDir(m_norimg_dir, m_excimg_dir);
  299. m_folder_feeder->SetFolder(img_dir);
  300. return 1;
  301. }
  302. 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)
  303. {
  304. m_bantch_dir = bantch_dir;
  305. m_norimg_dir = norimg_dir;
  306. m_excimg_dir = excimg_dir;
  307. m_img_clips_dir = img_clips_dir;
  308. m_img_alynasis_dir = img_alynasis_dir;
  309. m_img_alynasis_result_dir = img_alynasis_result_dir;
  310. m_img_down_load_dir = img_down_load_dir;
  311. m_muban_img_dir = muban_img_dir;
  312. return 1;
  313. }
  314. void CIdentifyService::setSchema(boost::shared_ptr<const identify::schema::ISCH_Schema> schema, boost::shared_ptr<const HANDLE_INFO> handleInfo)
  315. {
  316. this->m_schema = schema;
  317. this->m_handleInfo = handleInfo;
  318. m_identifor->LoadSchema(m_schema);
  319. }
  320. BOOL CIdentifyService::SupportPause(void)
  321. {
  322. return m_current_feeder.get() ? m_current_feeder->SupportPause() : FALSE;
  323. }
  324. void CIdentifyService::setMainService(bool isMainService)
  325. {
  326. this->isMainService = isMainService;
  327. }
  328. const boost::shared_ptr<const HANDLE_INFO>& CIdentifyService::GetHandleInfo()
  329. {
  330. return batch_service->GetHandleInfo();
  331. }
  332. void CIdentifyService::SetQr(int student_paper_id,bool bUseQr, const std::string&strQR)
  333. {
  334. m_identifor->SetUseQr(bUseQr, strQR);
  335. m_result_handler->SetUseQr(bUseQr, strQR);
  336. m_student_matcher->SetUseQr(student_paper_id,bUseQr, strQR);
  337. }
  338. void CIdentifyService::InitService()
  339. {
  340. m_identifor = boost::make_shared<CIdentifor>();
  341. m_result_handler = boost::make_shared<CResultHandler>();
  342. m_student_matcher = boost::make_shared<CStudentMatcher>();
  343. m_identifor->IdentifyTaskManager(taskManager.get());
  344. m_result_handler->IdentifyTaskManager(taskManager.get());
  345. }
  346. void CIdentifyService::SerErrorHandle(bool bErrorHandle){
  347. m_bErrorHandle = bErrorHandle;
  348. m_student_matcher->SetErrorHandle(m_bErrorHandle);
  349. m_result_handler->SetErrorHandle(m_bErrorHandle);
  350. }