📄 devmgr.cpp
字号:
// DevMgr.cpp: implementation of the CDevMgr class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "netsdkdemo.h"
#include "DevMgr.h"
#include <algorithm>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Get CDevMgr object
//////////////////////////////////////////////////////////////////////
CDevMgr& CDevMgr::GetDevMgr(void)
{
static CDevMgr devmgr;
return devmgr;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDevMgr::CDevMgr()
{
::InitializeCriticalSection(&m_csDev);
::InitializeCriticalSection(&m_csAlarm);
}
CDevMgr::~CDevMgr()
{
list<DeviceNode*>::iterator it = m_lstDevice.begin();
for(; it != m_lstDevice.end(); ++it)
{
if (*it)
{
delete (*it);
}
}
m_lstDevice.clear();
::DeleteCriticalSection(&m_csDev);
::DeleteCriticalSection(&m_csAlarm);
}
void CDevMgr::For_EachDev(int (* cbForEach)(const DeviceNode& node, DWORD dwUser), DWORD dwUser)
{
::EnterCriticalSection(&m_csDev);
list<DeviceNode*>::iterator it = m_lstDevice.begin();
for(; it != m_lstDevice.end(); ++it)
{
int nRet = cbForEach(**it, dwUser);
if (1 == nRet)
{
break;
}
}
::LeaveCriticalSection(&m_csDev);
}
int CDevMgr::GetDev(LONG lLoginID, DeviceNode& node)
{
int nRet = -1;
::EnterCriticalSection(&m_csDev);
list<DeviceNode*>::iterator it =
find_if(m_lstDevice.begin(), m_lstDevice.end(),
CDevMgr::SearchDevByHandle(lLoginID));
if (it != m_lstDevice.end())
{
memcpy(&node, *it, sizeof(DeviceNode));
nRet = 0;
}
::LeaveCriticalSection(&m_csDev);
return nRet;
}
int CDevMgr::SetAlarmInfo(LONG lLoginID, LONG lCommand, char *pchDVRIP, LONG nDVRPort,
char *pBuf, DWORD dwBufLen)
{
int nRet = -1;
::EnterCriticalSection(&m_csAlarm);
list<DeviceNode*>::iterator it =
find_if(m_lstDevice.begin(), m_lstDevice.end(),
CDevMgr::SearchDevByHandle(lLoginID));
if (it != m_lstDevice.end())
{
switch(lCommand)
{
case COMM_ALARM:
{
NET_CLIENT_STATE *State = (NET_CLIENT_STATE *)pBuf;
if(!State)
{
break;
}
//设备列表中信息刷新
(*it)->State.cState.channelcount = State->channelcount;
(*it)->State.cState.alarminputcount = State->alarminputcount;
memcpy((*it)->State.cState.alarm, State->alarm, 16);
memcpy((*it)->State.cState.motiondection, State->motiondection, 16);
memcpy((*it)->State.cState.videolost, State->videolost, 16);
nRet = 0;
break;
}
case SHELTER_ALARM:
{
memcpy((*it)->State.shelter, pBuf, 16);
break;
}
case DISK_FULL_ALARM:
{
(*it)->State.dFull = *(DWORD*)pBuf;
break;
}
case DISK_ERROR_ALARM:
{
(*it)->State.dError = *(DWORD*)pBuf;
break;
}
default:
nRet = -1;
break;
}
}
::LeaveCriticalSection(&m_csAlarm);
return nRet;
}
int CDevMgr::GetAlarmInfo(LONG lLoginID, DEV_STATE *des)
{
int nRet = -1;
::EnterCriticalSection(&m_csAlarm);
list<DeviceNode*>::iterator it =
find_if(m_lstDevice.begin(), m_lstDevice.end(),
CDevMgr::SearchDevByHandle(lLoginID));
if (it != m_lstDevice.end())
{
memcpy(des, &(*it)->State, sizeof(DEV_STATE));
nRet = 0;
}
::LeaveCriticalSection(&m_csAlarm);
return nRet;
}
//add node to list
int CDevMgr::PushBack(DeviceNode* node)
{
if (!node)
{
return -1;
}
int nRet = 0;
::EnterCriticalSection(&m_csDev);
memset(&node->State, 0, sizeof(DEV_STATE));
node->State.cState.channelcount = node->Info.byChanNum;
node->State.cState.alarminputcount = node->Info.byAlarmInPortNum;
m_lstDevice.push_back(node);
::LeaveCriticalSection(&m_csDev);
return nRet;
}
//delete node from list
int CDevMgr::DelNode(LONG lLoginID)
{
int nRet = -1;
::EnterCriticalSection(&m_csDev);
list<DeviceNode*>::iterator it =
find_if(m_lstDevice.begin(), m_lstDevice.end(),
SearchDevByHandle(lLoginID));
if (it != m_lstDevice.end())
{
delete *it;
m_lstDevice.erase(it);
nRet = 0;
}
::LeaveCriticalSection(&m_csDev);
return nRet;
}
BOOL CDevMgr::IsOnline(LONG lLoginID)
{
BOOL b = FALSE;
::EnterCriticalSection(&m_csDev);
list<DeviceNode*>::iterator it =
find_if(m_lstDevice.begin(), m_lstDevice.end(),
CDevMgr::SearchDevByHandle(lLoginID));
if (it != m_lstDevice.end())
{
b = TRUE;
}
::LeaveCriticalSection(&m_csDev);
return b;
}
BOOL CDevMgr::IsOnline(DeviceNode* node)
{
BOOL b = FALSE;
::EnterCriticalSection(&m_csDev);
list<DeviceNode*>::iterator it =
find(m_lstDevice.begin(), m_lstDevice.end(), node);
if (it != m_lstDevice.end())
{
b = TRUE;
}
::LeaveCriticalSection(&m_csDev);
return b;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -