// DetcGD10Dev.cpp: implementation of the CDetcGD10Dev class. // ////////////////////////////////////////////////////////////////////// #include "geomative.h" #include "DetcGD10Dev.h" #include #include "GD10OperCmd.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif #define GD10DEVICE _T("GD20") extern SYSTEMTIME g_sysCurTime; extern int g_iUILanguage; extern CGeoMativeApp theApp; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CDetcGD10Dev* CDetcGD10Dev::m_pDetcDev = NULL; CDetcGD10Dev::CDetcGD10Dev() { m_bGD10DevIsCon = false; m_strDevAddr = _T(""); m_pLogFile = NULL; m_pLogFile = fopen("LOG\\detect_gd20_log.txt","ab+"); if (NULL == m_pLogFile) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("打开 detect_gd20_log.txt 失败.")); else MessageBoxEx(NULL, _T("Open detect_gd20_log.txt failed!!!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } } CDetcGD10Dev::~CDetcGD10Dev() { fclose(m_pLogFile); m_pLogFile = NULL; } CDetcGD10Dev* CDetcGD10Dev::GetInstance() { if (NULL == m_pDetcDev) { m_pDetcDev = new CDetcGD10Dev(); } return m_pDetcDev; } CString CDetcGD10Dev::GetGD10DevAddr() { return m_strDevAddr; } void CDetcGD10Dev::DetectGD10Dev() { m_strDevAddr.Empty(); PrintLog(_T("Now is ready to find gd20-device!")); CString strDevName = (EN_MULTI_CHANNEL == theApp.m_ucIsMultiChannel) ? GD10DEVICE : _T("GD10"); m_strDevAddr = FindUsbDevice(strDevName); m_bGD10DevIsCon = m_strDevAddr.IsEmpty() ? false : true; } bool CDetcGD10Dev::CompareDriverName(CString strDriName,CString strDriverAddr) { bool bRes = false; if (strDriverAddr.IsEmpty()) { PrintLog(_T("Driver address can not be empty!")); return bRes; } TCHAR chVolName[MAX_PATH+1]; TCHAR chFileSys[MAX_PATH+1]; memset(chVolName, 0, sizeof(TCHAR)*(MAX_PATH+1)); memset(chFileSys, 0, sizeof(TCHAR)*(MAX_PATH+1)); CString strText = _T(""); if (0 == GetVolumeInformation(strDriverAddr,chVolName,MAX_PATH+1,NULL,NULL,NULL,chFileSys,MAX_PATH+1)) { strText.Format(_T("Get Volume Information failed, errorno = %d"),GetLastError()); PrintLog(strText); } else { CString strVal = chVolName; strText.Empty(); strText.Format(_T("Get Volume Information success, volume_name = %s"),strVal); bRes = (0 == strVal.Compare(strDriName)) ? true : false; } return bRes; } CString CDetcGD10Dev::FindUsbDevice(CString strDevName) { UINT DiskType; CString strLog; CString strText = _T(""); CString strDir = _T(""); size_t szAllDriveStrings = GetLogicalDriveStrings(0,NULL); if (0 == szAllDriveStrings) { ASSERT(0 == szAllDriveStrings); return strDir; } char *pDriveStrings = new char[szAllDriveStrings + sizeof(_T( " "))]; char *pAddr = pDriveStrings; GetLogicalDriveStrings(szAllDriveStrings,pDriveStrings); size_t szDriveString = strlen(pDriveStrings); while(szDriveString > 0) { DiskType=GetDriveType(pDriveStrings); switch(DiskType) { case DRIVE_REMOVABLE: { strDir.Empty(); strDir.Format(_T("%s"),pDriveStrings); strText.Empty(); strText.Format(_T("detect removable drive(%s), ready to parser device"),strDir); PrintLog(strText); //如果名字相等则认为匹配成功 if (CompareDriverName(strDevName, strDir)) { delete[]pAddr; return strDir; } else { //规避有的电脑怎么改,u盘都不是GD10/GD20的问题 by quyx 20180517 //"\SD\equipment\equipment.xml" struct _stat fileStat; CString str = "\SD\\equipment"; CString strPath = strDir + str; if ((_stat(strPath.GetBuffer(0), &fileStat) == 0) && (fileStat.st_mode & _S_IFDIR)) { delete[]pAddr; return strDir; } } break; } case DRIVE_FIXED: break; case DRIVE_REMOTE: break; case DRIVE_CDROM: break; case DRIVE_NO_ROOT_DIR: { strText.Empty(); strText.Format(_T("Unknow driver string (%s)"),pDriveStrings); MessageBoxEx(NULL, strText, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); break; // return _T(""); } default: break; } pDriveStrings += szDriveString + 1; szDriveString = strlen(pDriveStrings); } delete []pAddr; return _T(""); } void CDetcGD10Dev::PrintLog(CString strLog) { if (NULL == m_pLogFile) { MessageBox(NULL, "can't write device_log, log File point is NULL", "LOG_ERROR", MB_OK); return; } CString strOutPut = _T(""); strOutPut.Format(_T("%04d-%02d-%02d %02d:%02d:%02d.%03d %s \r\n"),g_sysCurTime.wYear, g_sysCurTime.wMonth, g_sysCurTime.wDay, g_sysCurTime.wHour, g_sysCurTime.wMinute, g_sysCurTime.wSecond, g_sysCurTime.wMilliseconds, strLog.GetBuffer(0)); fwrite(strOutPut.GetBuffer(0), 1, strOutPut.GetLength(), m_pLogFile); fflush(m_pLogFile); }