// DevManager.cpp: implementation of the CDevManager class. // ////////////////////////////////////////////////////////////////////// #include "geomative.h" #include "DevManager.h" #include "FileOperTools.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif extern float tcp_ntohf(float f); extern CString g_szMacAddress; extern int g_iUILanguage; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// extern CGeoMativeApp theApp; extern HHOOK hHook; extern LRESULT __stdcall CBTHookProc(long nCode, WPARAM wParam, LPARAM lParam); CDevManager::CDevManager(_ConnectionPtr& pConnection) { ASSERT(NULL != pConnection); m_pConnection = pConnection; m_devLinkList.DeleteAll(); m_setRemoteDev.clear(); OutputDebugString(_T("zm: CDevManager::CDevManager() m_devLinkList delete all \n")); } CDevManager::~CDevManager() { int iDevIndex = (int)VAL_ZERO; iDevIndex++; while (NULL != m_devLinkList.Find(iDevIndex)) { delete m_devLinkList.Find(iDevIndex); iDevIndex++; } m_devLinkList.DeleteAll(); OutputDebugString(_T("zm: CDevManager::~CDevManager() m_devLinkList delete all \n")); } CDevice* CDevManager::GetDeviceByID(DWORD dwDevID) { CDevice* pDev = NULL; int iDevIndex = (int)VAL_ZERO; // DWORD dwDevID = (DWORD)VAL_ZERO; // dwDevID = m_handleProcessor.GetIDFromHandle(dwHandle); int iDevCount = m_devLinkList.Length(); CString strLog; for (iDevIndex = 1; iDevIndex <= iDevCount; iDevIndex++) { pDev = m_devLinkList.Find(iDevIndex); if (pDev != NULL && pDev->m_dwID == dwDevID) { strLog.Format(_T("zm:m_devLinkList size=%d CDevManager::GetDeviceByID(%d) 找到设备%s\n"), iDevCount, iDevIndex, pDev->m_szDevSN); OutputDebugString(strLog); return pDev; } } return NULL; /*pDev = m_devLinkList.Find(++iDevIndex); while (NULL != pDev) { if (pDev->m_dwID == dwDevID) { return pDev; } pDev = m_devLinkList.Find(++iDevIndex); } return pDev;*/ } CDevice* CDevManager::GetDevice(DWORD dwHandle) { CDevice* pDev = NULL; pDev = m_devLinkList.Get(dwHandle); CString strLog; strLog.Format(_T("zm:CDevManager::GetDevice 通过dwHandle=%d,获取设备%s\n"), dwHandle, pDev->m_szDevSN); OutputDebugString(strLog); return pDev; } CDevice* CDevManager::GetRegisterDevice(CString szDevSN, bool &bIsRegister) { bIsRegister = false; _RecordsetPtr pRecDevID = NULL; int iDevIndex = (int)VAL_ZERO; CDevice* pDev = NULL; CString szSql = _T(""); pDev = m_devLinkList.Find(++iDevIndex); while (NULL != pDev) { if (pDev->m_szDevSN == szDevSN)//若设备没注册,不会进入 { bIsRegister = true; CStringArray szMacList; theApp.m_pProManager->m_guCodeCreator.GetMacList(&szMacList); //for循环解决网卡位置变换的问题 for (int i = 0; i < szMacList.GetSize(); i++) { if (pDev->m_szMacAddress == szMacList.GetAt(i)) { g_szMacAddress = pDev->m_szMacAddress; return pDev; } } return NULL; } pDev = m_devLinkList.Find(++iDevIndex); } return NULL; } CDevice* CDevManager::GetDevice(CString szDevSN) { _RecordsetPtr pRecDevID = NULL; int iDevIndex = (int)VAL_ZERO; CDevice* pDev = NULL; CString szSql = _T(""); pDev = m_devLinkList.Find(++iDevIndex); while (NULL != pDev) { if (0 == strcmp(pDev->m_szDevSN,szDevSN))//若设备没注册,不会进入 { CStringArray szMacList; theApp.m_pProManager->m_guCodeCreator.GetMacList(&szMacList); //for循环解决网卡位置变换的问题 for (int i = 0; i < szMacList.GetSize(); i++) { if (pDev->m_szMacAddress == szMacList.GetAt(i)) { g_szMacAddress = pDev->m_szMacAddress; return pDev; } } return NULL; } pDev = m_devLinkList.Find(++iDevIndex); } return NULL; } void CDevManager::InitialDevLinkList() { int iDevIndex = (int)VAL_ZERO; DWORD dwDevHandle = (DWORD)VAL_ZERO; CString szSql = _T(""); CDevice* pDev = NULL; _RecordsetPtr pRecDevID = NULL; iDevIndex++; while (NULL != m_devLinkList.Find(iDevIndex)) { delete m_devLinkList.Find(iDevIndex); iDevIndex++; } m_devLinkList.DeleteAll(); OutputDebugString(_T("zm: CDevManager::InitialDevLinkList() m_devLinkList delete all \n")); pRecDevID.CreateInstance(_uuidof(Recordset)); pRecDevID->Open(_T("select ID,SN,MacAddress from device order by ID"), _variant_t((IDispatch*)m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); CString strLog; while ((short)VAL_ZERO == pRecDevID->adoEOF) { pDev = new CDevice(pRecDevID->GetCollect(_T("ID")).ulVal, m_pConnection); if (NULL != pDev) { pDev->m_szDevSN = (LPCTSTR)(_bstr_t)pRecDevID->GetCollect(_T("SN")); pDev->m_szMacAddress = (LPCTSTR)(_bstr_t)pRecDevID->GetCollect(_T("MacAddress")); // short ucDevType = (pRecDevID->GetCollect(_T("DevType")).vt == VT_NULL ) ? 0 : pRecDevID->GetCollect(_T("DevType")).iVal; pDev->m_uState = PZ_STATE_OFFLINE; pDev->m_uType = PZ_STYLE_DEV; dwDevHandle = m_handleProcessor.GenerateHandle((DWORD)pRecDevID->GetCollect(_T("ID")).ulVal, PZ_STYLE_DEV); strLog.Format(_T("zm:CDevManager::InitialDevLinkList() m_devLinkList.Add(%s)\n"),pDev->m_szDevSN); OutputDebugString(strLog); m_devLinkList.Add(dwDevHandle, pDev); } pRecDevID->MoveNext(); } pRecDevID->Close(); } BOOL CDevManager::AddDevice(CDevice* const pDev) { _RecordsetPtr pRecDev = NULL; _CommandPtr pCmdIns = NULL; _CommandPtr pCmdUpd = NULL; CString szSql = _T(""); DWORD dwDeID = (DWORD)VAL_ZERO; if (NULL != pDev) { pRecDev.CreateInstance(_uuidof(Recordset)); pCmdIns.CreateInstance(_uuidof(Command)); pCmdIns->ActiveConnection = m_pConnection; pCmdUpd.CreateInstance(_uuidof(Command)); pCmdUpd->ActiveConnection = m_pConnection; try { m_pConnection->BeginTrans(); szSql.Empty(); szSql.Format(_T("insert into device(DEname,modelNO,SN,MacAddress) values('%s','%s','%s','%s')"), pDev->m_szDevName, pDev->m_szModelNO, pDev->m_szDevSN,g_szMacAddress); pCmdIns->CommandText = szSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); szSql.Empty(); szSql.Format(_T("select ID from device where SN = '%s'"), pDev->m_szDevSN); pRecDev->Open(szSql.AllocSysString(), _variant_t((IDispatch*)m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); dwDeID = pRecDev->GetCollect(_T("ID")).ulVal; pRecDev->Close(); szSql.Empty(); szSql.Format(_T("insert into desetting(DEID,Ifrequency,numformat,Tsys,Mvoltage,Msys,maxRG,Tzone,STdelay,PSmode,PBtone,Ttone,Bvoltage,dimmer,lang) " "values(%u,0,0,0,0,0,0,0,0,0,0,0,0,0,0)"), dwDeID); pCmdIns->CommandText = szSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); szSql.Empty(); szSql.Format(_T("insert into acds(DEID,M0ST,M0TW,M1ST,M1TW,M2ST,M2TW,M3ST,M3TW,M4ST,M4TW,M5ST,M5TW,M6ST,M6TW,M7ST,M7TW,M8ST,M8TW,M9ST,M9TW, ") _T("M10ST,M10TW,M11ST,M11TW,M12ST,M12TW,M13ST,M13TW,M14ST,M14TW,M15ST,M15TW,M16ST,M16TW,M17ST,M17TW,M18ST,M18TW,M19ST,M19TW) ") _T("values(%u,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)"), dwDeID); pCmdIns->CommandText = szSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); szSql.Empty(); szSql.Format(_T("update td set DEID = %u where DESN = '%s'"), dwDeID, pDev->m_szDevSN); pCmdUpd->CommandText = szSql.AllocSysString(); pCmdUpd->Execute(NULL, NULL, adCmdText); pDev->SetID(dwDeID); SetDeviceHandle(pDev); m_pConnection->CommitTrans(); } catch (_com_error e) { CString strInfo=_T(""); hHook = SetWindowsHookEx(WH_CBT,(HOOKPROC)CBTHookProc,AfxGetInstanceHandle(),NULL); if (LANG_ZHCN == g_iUILanguage) { strInfo.Format(_T("添加设备异常 %s"), (LPCTSTR)e.Description()); AfxMessageBox(strInfo); } else { strInfo.Format(_T("AddDevice exception,msg = %s"), (LPCTSTR)e.Description()); MessageBoxEx(NULL, strInfo, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } m_pConnection->RollbackTrans(); return FALSE; } } return TRUE; } BOOL CDevManager::SetDeviceHandle(CDevice* const pDev) { DWORD dwDevHandle = (DWORD)VAL_ZERO; if (NULL == pDev) { return FALSE; } dwDevHandle = m_handleProcessor.GenerateHandle(pDev->m_dwID, PZ_STYLE_DEV); m_devLinkList.SetHandle(pDev, dwDevHandle); return TRUE; } void CDevManager::DeleteObjInMem(DWORD dwHandle) { CDevice* pDev = NULL; pDev = m_devLinkList.Get(dwHandle); CString strLog; strLog.Format(_T("zm:CDevManager::DeleteObjInMem(DWORD dwHandle) m_devLinkList.Delete(%s)\n"), pDev->m_szDevSN); OutputDebugString(strLog); delete pDev; m_devLinkList.Delete(dwHandle); } void CDevManager::DeleteObjInMem(CString szDevSN) { CDevice* pDev = NULL; pDev = this->GetDevice(szDevSN); if (pDev == NULL) { //ASSERT(pDev != NULL); return; } CString strLog; strLog.Format(_T("zm:CDevManager::DeleteObjInMem(CString szDevSN) m_devLinkList.Delete(%s)\n"), pDev->m_szDevSN); OutputDebugString(strLog); m_devLinkList.Delete((CDevice*)pDev); delete pDev; } void CDevManager::AddObjInMem(CString szDevSN) { CDevice* pDev = NULL; DWORD dwDevHandle = (DWORD)VAL_ZERO; pDev = new CDevice((DWORD)VAL_ZERO, m_pConnection); pDev->m_szMacAddress = theApp.m_pProManager->m_guCodeCreator.GenerateMacAddress(); pDev->m_szDevSN = szDevSN; pDev->m_uState = PZ_STATE_NEW; pDev->m_uType = PZ_STYLE_DEV; dwDevHandle = m_handleProcessor.GenerateHandle(pDev->m_dwID, PZ_STYLE_DEV); CString strLog; strLog.Format(_T("zm:CDevManager::AddObjInMem(CString szDevSN) m_devLinkList.Add(%s)"), pDev->m_szDevSN); OutputDebugString(strLog); m_devLinkList.Add(dwDevHandle, pDev); } void CDevManager::AddOfflineObjInMem(CString szDevSN) { CDevice* pDev = NULL; DWORD dwDevHandle = (DWORD)VAL_ZERO; CString strLog; pDev = new CDevice((DWORD)VAL_ZERO, m_pConnection); if (NULL == pDev) { CFileOperTools::GetInstance()->WriteComLog(_T("zm:CDevManager::AddOfflineObjInMem NULL == pDev")); return; } _RecordsetPtr pRecDev = NULL; pRecDev.CreateInstance(_uuidof(Recordset)); CString strSql; strSql.Format(_T("select ID,SN from device where SN='%s'"), szDevSN); pRecDev->Open(strSql.AllocSysString(), _variant_t((IDispatch*)m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if ((long)VAL_ZERO == pRecDev->GetRecordCount()) { return; } pDev->m_dwID = (DWORD)pRecDev->GetCollect(_T("ID")).ulVal; pRecDev->Close(); pDev->m_szMacAddress = theApp.m_pProManager->m_guCodeCreator.GenerateMacAddress(); pDev->m_szDevSN = szDevSN; pDev->m_uState = PZ_STATE_OFFLINE; pDev->m_uType = PZ_STYLE_DEV; dwDevHandle = m_handleProcessor.GenerateHandle(pDev->m_dwID, PZ_STYLE_DEV); strLog.Format(_T("zm:CDevManager::AddOfflineObjInMem(CString szDevSN) m_devLinkList.Add(%s)\n"), pDev->m_szDevSN); OutputDebugString(strLog); m_devLinkList.Add(dwDevHandle, pDev); } BOOL CDevManager::DeleteDevice(DWORD dwHandle) { CString szSql = _T(""); _CommandPtr pCmdDel = NULL; _CommandPtr pCmdUpd = NULL; DWORD dwID = (DWORD)VAL_ZERO; try { m_pConnection->BeginTrans(); dwID = this->m_handleProcessor.GetIDFromHandle(dwHandle); this->DeleteObjInMem(dwHandle); pCmdDel.CreateInstance(_uuidof(Command)); pCmdDel->ActiveConnection = m_pConnection; pCmdUpd.CreateInstance(_uuidof(Command)); pCmdUpd->ActiveConnection = m_pConnection; szSql.Empty(); szSql.Format(_T("delete from acds where DEID = %u"), dwID); pCmdDel->CommandText = szSql.AllocSysString(); pCmdDel->Execute(NULL, NULL, adCmdText); szSql.Empty(); szSql.Format(_T("delete from desetting where DEID = %u"), dwID); pCmdDel->CommandText = szSql.AllocSysString(); pCmdDel->Execute(NULL, NULL, adCmdText); szSql.Empty(); szSql.Format(_T("delete from device where ID = %u"), dwID); pCmdDel->CommandText = szSql.AllocSysString(); pCmdDel->Execute(NULL, NULL, adCmdText); szSql.Empty(); szSql.Format(_T("update td set DEID = 0 where DEID = %u"), dwID); pCmdUpd->CommandText = szSql.AllocSysString(); pCmdUpd->Execute(NULL, NULL, adCmdText); m_pConnection->CommitTrans(); return TRUE; } catch (...) { m_pConnection->RollbackTrans(); return FALSE; } } BOOL CDevManager::DeleteDevice(CString strDev) { CString szSql = _T(""); _CommandPtr pCmdDel = NULL; if (strDev.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("删除设备时设备名不能为空")); else MessageBoxEx(NULL, _T("Devcie name can not be NULL in Delete device!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return FALSE; } try { m_pConnection->BeginTrans(); pCmdDel.CreateInstance(_uuidof(Command)); pCmdDel->ActiveConnection = m_pConnection; szSql.Empty(); szSql.Format(_T("delete from device where SN = '%s'"), strDev); pCmdDel->CommandText = szSql.AllocSysString(); pCmdDel->Execute(NULL, NULL, adCmdText); m_pConnection->CommitTrans(); return TRUE; } catch (_com_error e) { CString strInfo = _T(""); hHook = SetWindowsHookEx(WH_CBT,(HOOKPROC)CBTHookProc,AfxGetInstanceHandle(),NULL); if (LANG_ZHCN == g_iUILanguage) { strInfo.Format(_T("删除设备异常 %s"), (LPCTSTR)e.Description()); AfxMessageBox(strInfo); } else { strInfo.Format(_T("DeleteDevice exception,msg = %s"), (LPCTSTR)e.Description()); MessageBoxEx(NULL, strInfo, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } m_pConnection->RollbackTrans(); return FALSE; } } BOOL CDevManager::GetOLDevList(CPtrArray *pOLDevList) { CString strLog; strLog.Format(_T("zm:CDevManager::GetOLDevList获取在线设备列表 begin.....\n")); OutputDebugString(strLog); CDevice* pDev = NULL; int iDevIndex = (int)VAL_ZERO; pDev = m_devLinkList.Find(++iDevIndex); while (NULL != pDev) { if ((PZ_STATE_ONLINE == pDev->m_uState) || (PZ_STATE_NEW == pDev->m_uState)) { pOLDevList->Add(pDev); strLog.Format(_T("zm:CDevManager::GetOLDevList 新增设备 DevSN=%s iDevIndex=%d\n"), pDev->m_szDevSN, iDevIndex); OutputDebugString(strLog); } pDev = m_devLinkList.Find(++iDevIndex); } strLog.Format(_T("zm:CDevManager::GetOLDevList获取在线设备列表 end.....\n")); OutputDebugString(strLog); return TRUE; } BOOL CDevManager::GetFLDevList(CPtrArray* pFLDevList) { OutputDebugString(_T("zm:CDevManager::GetFLDevList() 添加离线设备 begin.............\n")); int iIndex = (int)VAL_ZERO; _RecordsetPtr pRecDev = NULL; CDevice* pDevice = NULL; pFLDevList->RemoveAll(); iIndex = (int)VAL_ZERO; pRecDev.CreateInstance(_uuidof(Recordset)); /*//过滤离线数据库中的在线设备 //获取远端在线设备 std::set setRemoteDev = theApp.m_pDevManager->GetRemoteDeviceInfo(); std::list listOnlineDev; if (setRemoteDev.size() > 0) { CString strSN,strLog; int iDevStyle = 0; std::set::iterator iter; for (iter = setRemoteDev.begin(); iter != setRemoteDev.end(); iter++) { strSN.Empty(); if (iter->ucDevType == EN_DEV_GD10) { strSN.Format(_T("SN%u"), iter->uiDevID); listOnlineDev.push_back(strSN); strLog.Format(_T("zm: 当前在线的GD设备 DevSN=%s\n"), strSN); OutputDebugString(strLog); } else if (iter->ucDevType == EN_DEV_PLC) { strSN.Format(_T("PLC%u"), iter->uiDevID); listOnlineDev.push_back(strSN); strLog.Format(_T("zm: 当前在线的PLC设备 PLCID = %s\n"), strSN); OutputDebugString(strLog); } } } */ pRecDev->Open(_T("select ID,SN from device order by ID"), _variant_t((IDispatch*)m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); CString strLog; DWORD dwDevID = 0; CString strDevSN; while ((short)VAL_ZERO == pRecDev->adoEOF) { pDevice = NULL; dwDevID = (DWORD)pRecDev->GetCollect(_T("ID")).ulVal; strDevSN = (LPCTSTR)(_bstr_t)pRecDev->GetCollect(_T("SN")); strLog.Format(_T("zm:设备数据库ID=%d,DevSN=%s\n"), dwDevID,strDevSN); OutputDebugString(strLog); pDevice = this->GetDeviceByID(dwDevID); if (pDevice != NULL) { if (PZ_STATE_OFFLINE == pDevice->m_uState) { strLog.Format(_T("zm:CDevManager::GetFLDevList() 添加离线设备 =%s\n"), pDevice->m_szDevSN); OutputDebugString(strLog); /*std::list::iterator iter = listOnlineDev.begin(); for (iter; iter != listOnlineDev.end(); iter++) { if (strcmp(*iter, pDevice->m_szDevSN) == 0) { break; } } if (iter == listOnlineDev.end())*/ pFLDevList->Add(pDevice); } } pRecDev->MoveNext(); } pRecDev->Close(); OutputDebugString(_T("zm:CDevManager::GetFLDevList() 添加离线设备 end.............\n")); return TRUE; } void CDevManager::UpdateDevInfo(STSynDevParam stDevParam, BYTE bRemoteDeveTyp) { _CommandPtr pCmdUpd = NULL; CString szSql = _T(""); _ConnectionPtr pConnection = m_pConnection; pCmdUpd.CreateInstance(_uuidof(Command)); pCmdUpd->ActiveConnection = m_pConnection; pConnection->BeginTrans(); //存入数据 STSynDevParam *DevPar = (STSynDevParam*)&stDevParam; CString Hwv; Hwv.Format("V%u.%u", DevPar->ucHWVer[0], DevPar->ucHWVer[1]); CString Swv; Swv.Format("V%u.%u.%u_T%u", DevPar->ucSWVer[0], DevPar->ucSWVer[1], DevPar->ucSWVer[2], DevPar->ucSWVer[3]); CString szDevSN; CString szBatVol; int iGrTh; szDevSN.Format(_T("SN%u"), ntohl(DevPar->uiMachineID)); iGrTh = ntohl(DevPar->iGrThreshold); szBatVol.Format("%.2f", (tcp_ntohf(DevPar->fBatteryVol))); /*if (EN_DEV_CLOUND == bRemoteDeveTyp) { szBatVol.Format("%.2f", (tcp_ntohf(DevPar->fBatteryVol))); } else { szBatVol.Format("%.2f", DevPar->fBatteryVol); }*/ int ucDownLoadNum; //下发数不能小于通道数 if (DevPar->ucDownLoadNum < DevPar->ucChannelNum){ ucDownLoadNum = DevPar->ucChannelNum; } try { //写入数据库 szSql.Empty(); szSql.Format(_T("update device set HWV = '%s', SWV = '%s', BatVol = %s, PowerFreq = %d, CableType = %d, GrTh = %d,\ BatAlarm = %d, Stack = %d, ChanNum = %d, DownNum = %d where SN = '%s'"), Hwv, Swv, szBatVol, DevPar->ucPowerFreq, \ DevPar->ucCableType,iGrTh, DevPar->ucBatteryAlarm, DevPar->ucStacking, \ DevPar->ucChannelNum, ucDownLoadNum, szDevSN); CFileOperTools::GetInstance()->WriteComLog(szSql); pCmdUpd->CommandText = szSql.AllocSysString(); pCmdUpd->Execute(NULL, NULL, adCmdText); } catch (_com_error e) { // 显示错误信息 AfxMessageBox(e.Description()); } pConnection->CommitTrans(); } //DEL void CDevManager::ModifyTimeWindow(DWORD f_handle) //DEL { //DEL CString szSql = _T(""); //DEL //DEL DWORD dwID = (DWORD)VAL_ZERO; //DEL //DEL dwID = this->m_handleProcessor.GetIDFromHandle(f_handle); //DEL //DEL CUpDateTimeWindow timeWindow; //DEL timeWindow.m_dwID = dwID; //DEL timeWindow.m_pConnection = m_pConnection; //DEL timeWindow.DoModal(); //DEL //DEL //DEL //DEL }