其他分享
首页 > 其他分享> > ONVIF协议客户端

ONVIF协议客户端

作者:互联网

前几天跟大家聊了一些关于ONVIF的一些基础知识,它的工作原理以及优势。今天安徽思蔷信息科技为带大家了解一下simpleonvif

百度云盘下载地址:链接:https://pan.baidu.com/s/1xMkeXRZjrVCyiTbivedCxA

提取码:8hx3

1.SimpleOnvif的功能

提供支持Windows、Linux、arm、Android、iOS等各种平台的SDK库,方便集成,二次开发。

2.onvif的搜索实现

std::map<std::string, std::string> OnvifService::GetDiscoveryDevices()

{

std::map<std::string, std::string> devs;

SimpleMutex lock(&mutex_);

std::map<std::string, ONVIF_DEVICE*>::iterator it = OnvifDeviceMap_.begin();

while (it != OnvifDeviceMap_.end())

{

ONVIF_DEVICE* dev = it->second;

char url[256] = {0};

snprintf(url, 255, "%s://%s:%d%s", dev->binfo.XAddr.https ? "https" : "http", dev->binfo.XAddr.host, dev->binfo.XAddr.port, dev->binfo.XAddr.url);

devs.insert(std::make_pair(dev->binfo.XAddr.host, url));

it++;

}

return devs;

}

3.onvif ptz实现

bool OnvifService::StartPtz(SimplePTZCMDType command, unsigned int speed, const std::string& host, const std::string& username, const std::string& password, const std::string& deviceUrl)

{

ONVIF_DEVICE manualDevice;

ONVIF_DEVICE* device = getDevice(&manualDevice, host, username, password, deviceUrl);

BOOL ret = false;

if (!device)

{

printf("==%s==Line:%d >> Get device Fail continue to find Device!!!\n", __FUNCTION__,__LINE__);

//return false;

}

else

{

onvif_SetAuthInfo(device, username.c_str(), password.c_str());

}

ContinuousMove_REQ req;

memset(&req, 0, sizeof(req));

#ifdef Simple_ONVIF_SPEED

float onvif_speed = 0.0;

if ((speed > 100) || (speed < 0))

{

printf("==%s==Line:%d >> Speed=%d,out of the range\n", __FUNCTION__, __LINE__, speed);

return false;

}

onvif_speed = float(speed / 100.0);

#endif

switch (command)

{

case Simple_PTZ_CMD_TYPE_LEFT:

req.Velocity.PanTiltFlag = 1;

#ifdef Simple_ONVIF_SPEED

req.Velocity.PanTilt.x = -(onvif_speed);

#else

req.Velocity.PanTilt.x = -1;

#endif // Simple_ONVIF_SPEED

req.Velocity.PanTilt.y = 0;

break;

case Simple_PTZ_CMD_TYPE_RIGHT:

req.Velocity.PanTiltFlag = 1;

#ifdef Simple_ONVIF_SPEED

req.Velocity.PanTilt.x = onvif_speed;

#else

req.Velocity.PanTilt.x = 1;

#endif

req.Velocity.PanTilt.y = 0;

break;

case Simple_PTZ_CMD_TYPE_UP:

req.Velocity.PanTiltFlag = 1;

req.Velocity.PanTilt.x = 0;

#ifdef Simple_ONVIF_SPEED

req.Velocity.PanTilt.y = onvif_speed;

#else

req.Velocity.PanTilt.y = 1;

#endif

break;

case Simple_PTZ_CMD_TYPE_DOWN:

req.Velocity.PanTiltFlag = 1;

req.Velocity.PanTilt.x = 0;

#ifdef Simple_ONVIF_SPEED

req.Velocity.PanTilt.y = -(onvif_speed);

#else

req.Velocity.PanTilt.y = -1;

#endif

break;

case Simple_PTZ_CMD_TYPE_ZOOMIN:

req.Velocity.ZoomFlag = 1;

#ifdef Simple_ONVIF_SPEED

req.Velocity.Zoom.x = -(onvif_speed);

#else

req.Velocity.Zoom.x = -1;

#endif

break;

case Simple_PTZ_CMD_TYPE_ZOOMOUT:

req.Velocity.ZoomFlag = 1;

#ifdef Simple_ONVIF_SPEED

req.Velocity.Zoom.x = onvif_speed;

#else

req.Velocity.Zoom.x = 1;

#endif

break;

default:

if(manualDevice.local_ip != 0)

{

onvif_free_device(device);

device = NULL;

}

return false;

}

// printf("==%s==Line:%d >>:PTZ info: Speed=[%d],PanTiltFlag=[%d],PanTilt.x=[%f],PanTilt.y=[%f],onvif_speed=[%f]\n", __FUNCTION__,__LINE__, speed, req.Velocity.PanTiltFlag, req.Velocity.PanTilt.x, req.Velocity.PanTilt.y,onvif_speed);

if (device && device->profiles && strlen(device->profiles->token) > 0)

{

strcpy(req.ProfileToken, device->profiles->token);

printf("==%s==Line%d >>:Start to Move device token=%s,name:%s\n", __FUNCTION__,__LINE__, device->profiles->token, device->profiles->name);

ret = onvif_ContinuousMove(device, &req, NULL);

printf("==%s==Line%d >>:Move device End ret=[%d].token:[%s],name:[%s].\n", __FUNCTION__, __LINE__, ret, device->profiles->token, device->profiles->name);

}

else

{

ONVIF_DEVICE* device_find = NULL;

if (OnvifDeviceMap_.find(host) != OnvifDeviceMap_.end())

{

SimpleMutex lock(&mutex_);

device_find = OnvifDeviceMap_[host];

}

if (!device_find)

{

printf("==%s==Line:%d>> Find device Failed PTZ start Failed End!!!!\n", __FUNCTION__,__LINE__);

return false;

}

onvif_SetAuthInfo(device_find, username.c_str(), password.c_str());

BOOL getCapabilities = false;

getCapabilities = GetCapabilities(device_find);

printf("==%s==:Line:%d >> dev ip:[%s] getCapabilities = %d\n", __FUNCTION__, __LINE__, device_find->binfo.XAddr.host, getCapabilities);

if (!getCapabilities)

{

getCapabilities = GetServices(device_find);

printf("==%s==:Line:%d >> dev ip:[%s] GetServices = %d\n", __FUNCTION__, __LINE__, device_find->binfo.XAddr.host, getCapabilities);

}

if (getCapabilities && GetProfiles(device_find) )

{

printf("==%s==:Line:%d >> dev ip:[%s] GetProfiles Success\n", __FUNCTION__, __LINE__, device_find->binfo.XAddr.host);

if (device_find->profiles && strlen(device_find->profiles->token) > 0)

{

strcpy(req.ProfileToken, device_find->profiles->token);

printf("==%s==Line%d >>:Start to Move find_device token:[%s],name:[%s]\n", __FUNCTION__, __LINE__, device_find->profiles->token, device_find->profiles->name);

ret = onvif_ContinuousMove(device_find, &req, NULL);

printf("==%s==Line%d >>:Move device End ret=[%d].token:[%s],name:[%s].\n", __FUNCTION__, __LINE__, ret,device_find->profiles->token, device_find->profiles->name);

}

}

else

{

ret = false;

printf("%s:Line:%d dev ip:[%s] GetProfiles Failed, Failed to Move\n", __FUNCTION__, __LINE__, device_find->binfo.XAddr.host);

}

}

 

if (manualDevice.local_ip != 0)

{

onvif_free_device(device);

}

return ret;

}

标签:__,协议,onvif,req,device,Velocity,ONVIF,find,客户端
来源: https://blog.csdn.net/qinlin2018/article/details/117000532