123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- // JHUnzipDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "HJUnzip.h"
- #include "HJUnzipDlg.h"
- #include "afxdialogex.h"
- #include "../jsonLib/json/json.h"
- #include "../ZLibWrapLib/ZLibWrapLib.h"
- #include "../Util/Util.h"
- #include "../Util/typedef_struct.h"
- #include "UnZipService.h"
- #include "shlwapi.h"
- #include <io.h>
- #include <stdio.h>
- #include <stdlib.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- extern HWND g_hMain;
- #define TIMER_HEART 0x1000
- CLog g_log;
- extern CDataBaseService g_dataBaseService;
- void RestoreBatchDB(int resid, wstring strPath);
- CHJUnzipDlg::CHJUnzipDlg(CWnd* pParent /*=NULL*/)
- : CDialogEx(CHJUnzipDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- InitializeCriticalSection(&m_criticalIdsLock);
- }
- void CHJUnzipDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CHJUnzipDlg, CDialogEx)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_COPYDATA()
- ON_WM_TIMER()
- END_MESSAGE_MAP()
- BOOL CHJUnzipDlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW); //隐藏任务栏
- WINDOWPLACEMENT wp;
- wp.length = sizeof(WINDOWPLACEMENT);
- wp.flags = WPF_RESTORETOMAXIMIZED;
- wp.showCmd = SW_HIDE;
- SetWindowPlacement(&wp);
- GetModuleDir(m_strModuleDir);
- wstring strConfigPath = m_strModuleDir + L"config.ini";
- WCHAR share_dir[MAX_PATH] = { 0 };
- GetPrivateProfileString(_T("USER"), _T("share_path"), _T(""), share_dir, MAX_PATH, strConfigPath.c_str());
- m_strShareDir = share_dir;
- m_strScanExePath = m_strModuleDir;
- m_strScanExePath.append(L"ScanExe.exe");
- wstring _logdir(m_strModuleDir);
- _logdir.append(L"log");
- if (!PathIsDirectory(_logdir.c_str()))
- {
- ::CreateDirectory(_logdir.c_str(), 0);
- }
- wchar_t _logpath[1024] = { 0 };
- SYSTEMTIME systime;
- GetLocalTime(&systime);
- _stprintf(_logpath, L"%slog\\unzip_%04d%02d%02d.log", m_strModuleDir.c_str(),
- systime.wYear, systime.wMonth, systime.wDay);
- g_log.Init(UnicodeToGB2312(_logpath).c_str(), LogLvlInfo);
- HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, NULL, SHARE_MEMORY_NAME);
- if (hMapFile)
- {
- LPVOID lpBase = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
- share_memory_block* _data_block = (share_memory_block*)lpBase;
- char szHandle[64] = { 0 };
- sprintf(szHandle, "%p", reinterpret_cast<long>(GetSafeHwnd()));
- strcpy(_data_block->unzip_handle, szHandle);
- UnmapViewOfFile(lpBase);
- CloseHandle(hMapFile);
- }
- SetTimer(TIMER_HEART, 10 * 1000, NULL);
- wstring _zipfiledir(m_strModuleDir);
- _zipfiledir.append(L"zipfile");
- if (!PathIsDirectory(_zipfiledir.c_str()))
- {
- ::CreateDirectory(_zipfiledir.c_str(), 0);
- }
- wstring _indexdir(m_strModuleDir);
- _indexdir.append(L"index.db3");
- if (!PathFileExists(_indexdir.c_str()))
- {
- RestoreBatchDB(IDR_DB_INDEX, _indexdir);
- }
- vector<int> vct;
- CUnZipService _unzip(_indexdir, vct);
- for (auto& iter : vct)
- {
- m_queueIds.push({ iter, 0, 0, 0 });
- }
- DetectZipFile();
- return TRUE;
- }
- DWORD WINAPI ThreadZipFile(LPVOID lpThreadParameter)
- {
- CHJUnzipDlg* _service = (CHJUnzipDlg*)lpThreadParameter;
- _service->DoDetectZipFile();
- return 0;
- }
- DWORD WINAPI ThreadScan(LPVOID lpThreadParameter)
- {
- CHJUnzipDlg* _service = (CHJUnzipDlg*)lpThreadParameter;
- _service->DoThreadScan();
- return 0;
- }
- void CHJUnzipDlg::DetectZipFile()
- {
- m_hThreadDetect = CreateThread(NULL, 0, ThreadZipFile, this, 0, 0);
- m_hThreadScan = CreateThread(NULL, 0, ThreadScan, this, 0, 0);
- }
- void CHJUnzipDlg::DoDetectZipFile()
- {
- while (true)
- {
- CFileFind finder;
- CString path;
- if (PathFileExists(m_strShareDir.c_str()))
- {
- path.Format(_T("%s/*.zip"), m_strShareDir.c_str());
- bool bWorking = finder.FindFile(path);
- if (bWorking)
- {
- finder.FindNextFile();
-
- wchar_t _zipfiledir[1024] = { 0 };
- _stprintf(_zipfiledir, L"%szipfile\\%s_%lld\\", m_strModuleDir.c_str(), finder.GetFileTitle(), std::time(0));
- ::CreateDirectory(_zipfiledir, 0);
- wstring _indexdbpath(m_strModuleDir);
- _indexdbpath.append(L"index.db3");
- wstring _batchdbpath(_zipfiledir);
- _batchdbpath.append(L"batch.db3");
- RestoreBatchDB(IDR_DB_BATCH, _batchdbpath);
- wstring _imagepath(_zipfiledir);
- _imagepath.append(L"papers");
- ::CreateDirectory(_imagepath.c_str(), 0);
- wstring _zipfilepath(_zipfiledir);
- _zipfilepath.append(finder.GetFileName());
-
- wstring _sharefilename(finder.GetFileTitle());
- wstring _sharefilepath(finder.GetFilePath());
-
- g_log.PutMsg(LogLvlInfo, "开始解压:%s", UnicodeToGB2312(_sharefilepath.c_str()).c_str());
- BOOL unzip_rlt = FALSE;
- int unzip_count = 3;
- while (unzip_count > 0)
- {
- unzip_rlt = ZipExtract(finder.GetFilePath(), _imagepath.c_str());
- if (unzip_rlt)
- {
- break;
- }
- else
- {
- unzip_count--;
- Sleep(60 * 1000);
- }
- }
- g_log.PutMsg(LogLvlInfo, "解压结束:%s,结果:%s", UnicodeToGB2312(_sharefilepath.c_str()).c_str(), unzip_rlt ? "成功" : "失败");
-
- wstring strZipPath = finder.GetFileTitle();
- string _zip_path = UnicodeToGB2312(strZipPath.c_str());
- char szParam[1024] = { 0 };
- sprintf(szParam, "【%s】解压%s", _zip_path.c_str(), unzip_rlt ? "成功" : "失败");
- SetShareMemoryData(szParam);
-
- CUnZipService _Service(_indexdbpath, _batchdbpath, _imagepath, _zipfiledir, _sharefilename, _sharefilepath);
- _Service.InsertBatchInfo();
- if (unzip_rlt)
- {
- int _last_id(0);
- bool _rlt = _Service.ParseImageDir(_last_id);
- if (_rlt)
- {
- ::EnterCriticalSection(&m_criticalIdsLock);
- m_queueIds.push({ _last_id, 0, 0, 0 });
- ::LeaveCriticalSection(&m_criticalIdsLock);
- sprintf(szParam, "【%s】入库成功", _zip_path.c_str());
- }
- else
- {
- sprintf(szParam, "【%s】试卷非偶数倍(仅支持JPG格式的图片)", _zip_path.c_str());
- }
- SetShareMemoryData(szParam);
- }
- else
- {
- _Service.InsertBatchFail(batch_exc_unzip, 0);
- }
- BOOL move_rlt = FALSE;
- int move_count = 3;
- while (move_count > 0)
- {
- move_rlt = MoveFile(finder.GetFilePath(), _zipfilepath.c_str());
- if (move_rlt)
- {
- break;
- }
- else
- {
- move_rlt--;
- Sleep(5 * 1000);
- }
- }
- g_log.PutMsg(LogLvlInfo, "入库成功:%s", UnicodeToGB2312(_sharefilepath.c_str()).c_str());
- }
- }
- Sleep(10000);
- }
- }
- void CHJUnzipDlg::DoThreadScan()
- {
- while (true)
- {
- ::EnterCriticalSection(&m_criticalIdsLock);
- if (!m_queueIds.empty())
- {
- tagScanPaper& _scanpaper = m_queueIds.front();
- if (_scanpaper.scan_umbers == 0)
- {
- _scanpaper.scan_time = std::time(0);
- _scanpaper.scan_umbers = 1;
- RunScanExe(_scanpaper.batch_id);
- char info[256] = { 0 };
- sprintf(info, "扫描批次号:%d 开始", _scanpaper.batch_id);
- g_log.PutMsg(LogLvlInfo, info);
- SetShareMemoryData(info);
- }
- else if ((std::time(0) - _scanpaper.scan_time) > 30)
- {
- wstring _indexdbpath(m_strModuleDir);
- _indexdbpath.append(L"index.db3");
- if (_scanpaper.paper_cnt == 0)
- {
- g_dataBaseService.GetBatchCount(_indexdbpath, _scanpaper.batch_id, _scanpaper.paper_cnt);
- }
- if (_scanpaper.scan_umbers < _scanpaper.paper_cnt + 1)
- {
- _scanpaper.scan_time = std::time(0);
- _scanpaper.scan_umbers++;
- RunScanExe(_scanpaper.batch_id);
-
- char info[256] = { 0 };
- sprintf(info, "扫描批次号:%d 崩溃,第%d次重启ScanExe.exe", _scanpaper.batch_id, _scanpaper.scan_umbers);
- g_log.PutMsg(LogLvlFailed, info);
- SetShareMemoryData(info);
- }
- else
- {
- int batch_id = _scanpaper.batch_id;
- m_queueIds.pop();
- KillProgress(L"ScanExe.exe");
- char info[256] = { 0 };
- sprintf(info, "扫描批次号:%d 崩溃,重试%d次仍崩溃,放弃本批次扫描", _scanpaper.batch_id, _scanpaper.scan_umbers);
- g_log.PutMsg(LogLvlFailed, info);
- SetShareMemoryData(info);
- g_dataBaseService.UpdateBatchSchemaErro(_indexdbpath, batch_id);
- }
- }
- }
- ::LeaveCriticalSection(&m_criticalIdsLock);
-
- Sleep(5000);
- }
- }
- void CHJUnzipDlg::RunScanExe(int batch_id)
- {
- KillProgress(L"ScanExe.exe");
-
- TCHAR szParam[1024] = { 0 };
- _stprintf(szParam, _T("window=%p batchid=%d"), reinterpret_cast<long>(GetSafeHwnd()), batch_id);
-
- PROCESS_INFORMATION _ProcStruct;
- STARTUPINFO StartInfo;
- memset(&StartInfo, 0, sizeof(STARTUPINFO));
- StartInfo.cb = sizeof(STARTUPINFO);
- if (::CreateProcess(
- (LPCTSTR)m_strScanExePath.c_str(),
- szParam,
- NULL,
- NULL,
- FALSE,
- CREATE_NO_WINDOW,
- NULL,
- NULL,
- &StartInfo,
- &_ProcStruct))
- {
- CloseHandle(_ProcStruct.hProcess);
- CloseHandle(_ProcStruct.hThread);
- }
- g_log.PutMsg(LogLvlInfo, "启动扫描程序,参数:%s", UnicodeToGB2312(szParam).c_str());
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CHJUnzipDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作区矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialogEx::OnPaint();
- }
- }
- BOOL CHJUnzipDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
- {
- if (pCopyDataStruct && pCopyDataStruct->lpData && pCopyDataStruct->cbData > 0)
- {
- ::EnterCriticalSection(&m_criticalIdsLock);
- std::string str((char*)pCopyDataStruct->lpData, pCopyDataStruct->cbData);
- if (SCANEXE_HEART == str)
- {
- tagScanPaper& _scanpaper = m_queueIds.front();
- _scanpaper.scan_time = std::time(0);
- }
- else if (SCANEXE_DONE == str)
- {
- if (!m_queueIds.empty())
- {
- tagScanPaper _scanpaper = m_queueIds.front();
- m_queueIds.pop();
- KillProgress(L"ScanExe.exe");
- char info[256] = { 0 };
- sprintf(info, "扫描批次号:%d 完成,一共尝试了%d次扫描", _scanpaper.batch_id, _scanpaper.scan_umbers);
- g_log.PutMsg(LogLvlInfo, info);
- SetShareMemoryData(info);
- }
- }
- else if (str.find(UNHANDL_RESCAN) != std::string::npos)
- {
- vector<string> _vct;
- split(str, ":", _vct);
- if (_vct.size() > 1)
- {
- m_queueIds.push({ atoi(_vct[1].c_str()), 0, 0, 0 });
- }
- }
- ::LeaveCriticalSection(&m_criticalIdsLock);
- }
- return CWnd::OnCopyData(pWnd, pCopyDataStruct);
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CHJUnzipDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CHJUnzipDlg::OnTimer(UINT_PTR nIDEvent)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- switch (nIDEvent)
- {
- default:
- break;
- case TIMER_HEART:
- {
- HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, NULL, SHARE_MEMORY_NAME);
- if (hMapFile)
- {
- LPVOID lpBase = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
- share_memory_block* _data_block = (share_memory_block*)lpBase;
- _data_block->unzip_tickcount = std::time(0);
- UnmapViewOfFile(lpBase);
- CloseHandle(hMapFile);
- }
- else
- {
- HANDLE hself = GetCurrentProcess();
- TerminateProcess(hself, 0);
- ::WaitForSingleObject(hself, INFINITE);
- CloseHandle(hself);
- }
- }
- break;
- }
- }
- void CHJUnzipDlg::SetShareMemoryData(char* szParam)
- {
- HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, NULL, SHARE_MEMORY_NAME);
- if (hMapFile)
- {
- LPVOID lpBase = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
- share_memory_block* _data_block = (share_memory_block*)lpBase;
- _data_block->unzip_tickcount = std::time(0);
- strcpy(_data_block->msg_info, szParam);
- UnmapViewOfFile(lpBase);
- CloseHandle(hMapFile);
- }
- else
- {
- HANDLE hself = GetCurrentProcess();
- TerminateProcess(hself, 0);
- ::WaitForSingleObject(hself, INFINITE);
- CloseHandle(hself);
- }
- }
- void RestoreBatchDB(int resid, wstring strPath)
- {
- HRSRC hrsrc = FindResource(theApp.m_hInstance, MAKEINTRESOURCE(resid), L"db");
- HANDLE hglob = LoadResource(theApp.m_hInstance, hrsrc);
- char* _buf = (char*)LockResource(hglob);
- unsigned int _len = SizeofResource(theApp.m_hInstance, hrsrc);
- CFile file;
- file.Open(strPath.c_str(), CFile::modeCreate | CFile::modeWrite);
- file.Write(_buf, _len);
- file.Close();
- FreeResource(hglob);
- }
|