ScanDll.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "stdafx.h"
  2. #include "BatchInfo.h"
  3. #include "Log.h"
  4. //#include "qcloud.h"
  5. #include "CrashDumper.h"
  6. #include <tuple>
  7. #include "ScanDll.h"
  8. #include "_dump.h"
  9. #include "../Util/typedef_struct.h"
  10. CString GetExePath();
  11. CString AnsiToUnicode(const std::string& str);
  12. std::string UnicodeToAnsi(const CString& str);
  13. CLog g_log;
  14. bool CreateDir(char* path);
  15. HWND g_MsgWnd = NULL;
  16. string g_SchoolId = "";
  17. string g_ServerUrl = "";
  18. string g_Indexdb="";
  19. void ReadConfig()
  20. {
  21. CString exe_path = GetExePath();
  22. CString _ini_file = exe_path + _T("\\config.ini");
  23. TCHAR sz_buf1[MAX_PATH] = { 0 };
  24. GetPrivateProfileString(_T("USER"), _T("school_id"), _T(""), sz_buf1, sizeof(sz_buf1) / sizeof(TCHAR), _ini_file);
  25. g_SchoolId = UnicodeToAnsi(sz_buf1);
  26. memset(sz_buf1, 0, MAX_PATH);
  27. GetPrivateProfileString(_T("USER"), _T("server_url"), _T(""), sz_buf1, sizeof(sz_buf1) / sizeof(TCHAR), _ini_file);
  28. g_ServerUrl = UnicodeToAnsi(sz_buf1);
  29. }
  30. batch_info GetBatchInfo(int batch_id);
  31. SMARTEVALUATIONLOGIC_API bool InitScanDll(HWND hWnd)
  32. {
  33. _FCDump::StartDump();
  34. g_MsgWnd = hWnd;
  35. ReadConfig();
  36. CString exe_path = GetExePath();
  37. string logDir = UnicodeToAnsi(exe_path + _T("\\log\\"));
  38. CreateDir((char*)logDir.c_str());
  39. CString time_string;
  40. time_string.Format(_T("%s\\log\\exchandle.log"), exe_path);
  41. if (!g_log.Init(UnicodeToAnsi(time_string).c_str(), LogLvlInfo))
  42. {
  43. return false;
  44. }
  45. g_Indexdb = UnicodeToAnsi(exe_path + _T("\\index.db3"));
  46. return true;
  47. }
  48. SMARTEVALUATIONLOGIC_API bool StartScan(HWND wnd, int batch_id)
  49. {
  50. _FCDump::StartDump();
  51. ReadConfig();
  52. g_MsgWnd = wnd;
  53. CString exe_path = GetExePath();
  54. string logDir = UnicodeToAnsi(exe_path + _T("\\log\\"));
  55. CreateDir((char*)logDir.c_str());
  56. g_Indexdb = UnicodeToAnsi(exe_path + _T("\\index.db3"));
  57. CString time_string;
  58. SYSTEMTIME systime;
  59. GetLocalTime(&systime);
  60. time_string.Format(_T("%s\\log\\批次%d_____%4d-%02d-%02d %02d %02d %02d.log"), exe_path, batch_id, systime.wYear,
  61. systime.wMonth,
  62. systime.wDay, systime.wHour, systime.wMinute, systime.wSecond);
  63. //g_log.Init(UnicodeToAnsi(time_string).c_str(), LogLvlInfo);
  64. g_log.Init((logDir+"scan.log").c_str(), LogLvlInfo);
  65. CBatch_Server batch_server;
  66. batch_server.StartScan(batch_id);
  67. return true;
  68. }
  69. SMARTEVALUATIONLOGIC_API bool ReScan(int batch_id, int paper_id, char* page0, char* page1,string exam_id)
  70. {
  71. ReadConfig();
  72. CBatch_Server batch_server;
  73. batch_server.ReScan(batch_id, paper_id, page0, page1, exam_id);
  74. return true;
  75. }
  76. SMARTEVALUATIONLOGIC_API bool TestDb()
  77. {
  78. CppSQLite3DB index_db;
  79. try
  80. {
  81. index_db.open("D:/1cutimg/index.db3");
  82. }
  83. catch (CException* e)
  84. {
  85. g_log.PutMsg(LogLvlFailed, "开启index.db3失败 ");
  86. return false;
  87. }
  88. char sql[1200];
  89. int i = 100;
  90. while (true)
  91. {
  92. sprintf_s(sql, 1200, "update batchs set state = %d",i++);
  93. index_db.execDML(sql);
  94. Sleep(100);
  95. }
  96. }
  97. SMARTEVALUATIONLOGIC_API vector<batch_info> GetFailedBatch()
  98. {
  99. vector<batch_info> infos;
  100. CppSQLite3DB index_db;
  101. try
  102. {
  103. index_db.open(g_Indexdb.c_str());
  104. }
  105. catch (CException* e)
  106. {
  107. g_log.PutMsg(LogLvlFailed, "开启index.db3失败 ");
  108. return infos;
  109. }
  110. CppSQLite3Query query0;
  111. query0 = index_db.execQuery("select * from batchs where state > 0");
  112. if (!query0.eof())
  113. {
  114. batch_info info;
  115. info.id = query0.getIntField("id");
  116. strcpy_s(info.create_time, query0.getStringField("create_time"));
  117. strcpy_s(info.batchdb_path, query0.getStringField("batchdb_path"));
  118. strcpy_s(info.work_dir, query0.getStringField("work_dir"));
  119. info.total_cnt = query0.getIntField("total_cnt");
  120. info.success_cnt = query0.getIntField("success_cnt");
  121. infos.push_back(info);
  122. }
  123. query0.finalize();
  124. return infos;
  125. }
  126. SMARTEVALUATIONLOGIC_API bool ReScan(int batch_id, vector<int> paper_ids, string exam_id)
  127. {
  128. ReadConfig();
  129. return true;
  130. }
  131. static bool uploading = false;
  132. unsigned _stdcall ReUploadThread(void* param)
  133. {
  134. CppSQLite3DB index_db;
  135. try
  136. {
  137. index_db.open(g_Indexdb.c_str());
  138. }
  139. catch (CException* e)
  140. {
  141. g_log.PutMsg(LogLvlFailed, "ReUploadThread 开启index.db3失败 ");
  142. uploading = false;
  143. return false;
  144. }
  145. CppSQLite3Query query0;
  146. char sql[256];//upload_type =0 先扫描 后上传 =1 边扫边上传
  147. sprintf_s(sql, "select * from batchs where state & %d > 0 and state != -1", batch_exc_scan_excption);
  148. query0 = index_db.execQuery(sql);
  149. vector<tuple<int, string, string>> batchs;
  150. while (!query0.eof())
  151. {
  152. int batch_id = query0.getInt64Field("id");
  153. string batch_path = query0.getStringField("batchdb_path");
  154. string work_dir = query0.getStringField("work_dir");
  155. batchs.push_back(std::make_tuple(batch_id, batch_path, work_dir));
  156. query0.nextRow();
  157. }
  158. query0.finalize();
  159. index_db.close();
  160. for (auto& it : batchs)
  161. {
  162. CBatch_Server binfo;
  163. binfo.m_batch_id = std::to_string(get<0>(it));
  164. binfo.m_batchDbPath = get<1>(it);
  165. binfo.m_work_dir = get<2>(it);
  166. binfo.ReUploadAllFailed();
  167. ::PostMessage(g_MsgWnd, UM_ULOAD_SUCCESS, NULL, NULL);
  168. }
  169. uploading = false;
  170. return true;
  171. }
  172. SMARTEVALUATIONLOGIC_API bool ReUploadAllFailed()
  173. {
  174. if (!uploading)
  175. {
  176. uploading = true;
  177. HANDLE h_upload = (HANDLE)_beginthreadex(NULL, 0, ReUploadThread, NULL, NULL, NULL);
  178. if (h_upload != NULL)
  179. {
  180. CloseHandle(h_upload);
  181. }
  182. else
  183. {
  184. uploading = false;
  185. }
  186. }
  187. return true;
  188. }
  189. SMARTEVALUATIONLOGIC_API bool ReBatchScan(int batch_id, string exam_id) // 批量识别
  190. {
  191. CBatch_Server batch_server;
  192. batch_server.ReBatchScan(batch_id, exam_id);
  193. return true;
  194. }