Go语言实现Onvif客户端-3、获取网络接口token

Go语言实现Onvif客户端:3、获取网络接口token



上两节已经通过1、搜索设备、2、获取设备能力、3、鉴权、4、设备信息匹配几步找到了我们的设备并且获取了一部分操作设备的能力,接下来我们实现获取网络接口token的功能,为接下来对设备网络信息配置做准备。

1、思路

  • 发送请求获取网络接口的token
  • 解析回复的xml数据获取对应网络接口token

2、代码

参考客户端结构体(建议自行封装,最好先设计接口,然后根据不同业务需求实现不同接口这样来封装):

type GoOnvifClient struct {
	//搜索到的设备ip端口的字符串数组
	devices []ov.Device
	//获取到的设备信息
	DevInfo DeviceInfo
	//选择要筛选的设备的信息,提供设备类型或者设备序列号任意信息即可
	selectDevInfo DeviceInfo
	//选择要进行onvif交互的设备,从搜索到的设备中选择一个
	dev ov.Device
	//是否打印获取到的response中的soap信息
	isPrintRespSoap bool
	//需要获取的模块的能力,包括PTZ云台控制、Media流媒体控制等,一般直接选择All即可
	cateGoryName string
	//鉴权认证的用户名
	loginUserName string
	//鉴权认证使用的密码
	loginPassWord string
	//需要创建的用户名
	createUserName string
	//需要创建用户对应的密码
	createUserPassWord string
	//创建的用户的等级,包括 Administrator、Operator、User、Anonymous、Extended
	createUserLevel string
	//网络接口的token,通过获取网络接口获取,设置网络时需要使用
	networkInterfaceToken string
	//获取到的profile token,不同码流的token不同,一般会有三种码流
	profilesToken [5]string
	//选中要获取的流的token以及要进行PTZ的token,一般选择获取到的profiles token的一个
	localSelectProfileToken string
	//ptz控制的速度
	ptzSpeed float64
	//ptz控制的方向:1:上;2:下;3:左;4:右;5:左上;6:左下;7:右上;8:右下;9:停
	direction PTZDirection
	//ptz移动模式,1:连续移动,0:断续移动,连续移动时选择方向不停的话则会一直移动,否则移动一次后会停下来
	ptzMoveMode MoveMode
	//预置点信息
	presetInfo PresetInfo
	//要配置的网络信息
	netWorkConfigInfo NetWorkConfigInfo
}
/**
 * @Description: 从xml中读取网络token
 * @time: 2021-03-26 09:55:52
 * @receiver client
 * @param message
 * @return returnInfo
 */
func (client *GoOnvifClient) getNetWorkTokenFromXml(message string) returnInfo {
	doc := etree.NewDocument()
	if err := doc.ReadFromString(message); err != nil {
		return returnInfo{GetNetWorkInfoErr, "read network xml info failed."}
	}
	root := doc.SelectElement("Envelope")
	if root == nil {
		return returnInfo{GetNetWorkInfoErr, "read network xml info failed."}
	}
	token := root.FindElements("./Body/GetNetworkInterfacesResponse/NetworkInterfaces")
	for _, res := range token {
		client.networkInterfaceToken = res.SelectAttr("token").Value
	}

	if client.networkInterfaceToken == "" {
		return returnInfo{GetNetWorkInfoErr, "read network xml info failed."}
	}

	return returnInfo{OK, "get network token success"}
}

/**
 * @Description: 调用获取网络信息接口获取网络接口token
 * @time: 2021-03-26 09:57:48
 * @receiver client
 * @return returnInfo
 */
func (client *GoOnvifClient) getNetWokToken() returnInfo {
	getNetWorkToken := device.GetNetworkInterfaces{}
	res := client.sendReqGetResp(GetNetWorkInfoErr, getNetWorkToken)

	return client.getNetWorkTokenFromXml(res.Info)
}

获取到对应token为eth0,下面是对应的soap xml信息:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tt="http://www.onvif.org/ver10/schema" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:trt="http://www.onvif.org/ver10/media/wsdl" xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl" xmlns:tev="http://www.onvif.org/ver10/events/wsdl" xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl" xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl" xmlns:tst="http://www.onvif.org/ver10/storage/wsdl" xmlns:ter="http://www.onvif.org/ver10/error" xmlns:dn="http://www.onvif.org/ver10/network/wsdl" xmlns:tns1="http://www.onvif.org/ver10/topics" xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl" xmlns:wsoap12="http://schemas.xmlsoap.org/wsdl/soap12" xmlns:http="http://schemas.xmlsoap.org/wsdl/http" xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery" xmlns:wsadis="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wstop="http://docs.oasis-open.org/wsn/t-1" xmlns:wsrf-bf="http://docs.oasis-open.org/wsrf/bf-2" xmlns:wsntw="http://docs.oasis-open.org/wsn/bw-2" xmlns:wsrf-rw="http://docs.oasis-open.org/wsrf/rw-2" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsrf-r="http://docs.oasis-open.org/wsrf/r-2" xmlns:trc="http://www.onvif.org/ver10/recording/wsdl" xmlns:tse="http://www.onvif.org/ver10/search/wsdl" xmlns:trp="http://www.onvif.org/ver10/replay/wsdl" xmlns:tnshik="http://www.hikvision.com/2011/event/topics" xmlns:hikwsd="http://www.onvifext.com/onvif/ext/ver10/wsdl" xmlns:hikxsd="http://www.onvifext.com/onvif/ext/ver10/schema" xmlns:tas="http://www.onvif.org/ver10/advancedsecurity/wsdl" xmlns:tr2="http://www.onvif.org/ver20/media/wsdl" xmlns:axt="http://www.onvif.org/ver20/analytics">
	<env:Body>
		<tds:GetNetworkInterfacesResponse>
			<tds:NetworkInterfaces token="eth0">
				<tt:Enabled>true</tt:Enabled>
				<tt:Info>
					<tt:Name>eth0</tt:Name>
					<tt:HwAddress>10:12:fb:82:42:55</tt:HwAddress>
					<tt:MTU>1500</tt:MTU>
				</tt:Info>
				<tt:Link>
					<tt:AdminSettings>
						<tt:AutoNegotiation>true</tt:AutoNegotiation>
						<tt:Speed>100</tt:Speed>
						<tt:Duplex>Full</tt:Duplex>
					</tt:AdminSettings>
					<tt:OperSettings>
						<tt:AutoNegotiation>true</tt:AutoNegotiation>
						<tt:Speed>100</tt:Speed>
						<tt:Duplex>Full</tt:Duplex>
					</tt:OperSettings>
					<tt:InterfaceType>0</tt:InterfaceType>
				</tt:Link>
				<tt:IPv4>
					<tt:Enabled>true</tt:Enabled>
					<tt:Config>
						<tt:Manual>
							<tt:Address>40.40.40.101</tt:Address>
							<tt:PrefixLength>24</tt:PrefixLength>
						</tt:Manual>
						<tt:DHCP>false</tt:DHCP>
					</tt:Config>
				</tt:IPv4>
				<tt:IPv6>
					<tt:Enabled>true</tt:Enabled>
					<tt:Config>
						<tt:AcceptRouterAdvert>false</tt:AcceptRouterAdvert>
						<tt:DHCP>Off</tt:DHCP>
						<tt:LinkLocal>
							<tt:Address>fe80::1212:fbff:fe82:4255</tt:Address>
							<tt:PrefixLength>64</tt:PrefixLength>
						</tt:LinkLocal>
						<tt:FromDHCP>
							<tt:Address>fe80::1212:fbff:fe82:4255</tt:Address>
							<tt:PrefixLength>64</tt:PrefixLength>
						</tt:FromDHCP>
					</tt:Config>
				</tt:IPv6>
			</tds:NetworkInterfaces>
		</tds:GetNetworkInterfacesResponse>
	</env:Body>
</env:Envelope>

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
实现一个 C# 的 Onvif 客户端,你需要遵循以下步骤: 1. 引用 Onvif 标准的 WSDL 文件 Onvif 标准定义了一组 Web Services Description Language (WSDL) 文件,你需要将这些文件添加到你的 C# 项目中。这些文件包含了 Onvif 标准所定义的服务接口和数据类型。 2. 生成客户端代理类 使用 Visual Studio 或者命令行工具,基于 WSDL 文件生成客户端代理类,这样你就可以使用 C# 代码调用 Onvif 标准定义的服务接口。 3. 创建客户端对象 使用生成的客户端代理类创建客户端对象,你需要提供 Onvif 设备的 IP 地址、端口号、用户名和密码等信息。 4. 调用服务接口 使用客户端对象调用 Onvif 标准定义的服务接口,例如获取设备信息、控制云台、获取视频流等操作。 下面是一个简单的示例代码,演示了如何使用 C# 实现 Onvif 客户端: ```csharp using System; using System.ServiceModel; using onvif.services; namespace onvif_client { class Program { static void Main(string[] args) { // Onvif 设备的 IP 地址、端口号、用户名和密码 string address = "http://192.168.1.100/onvif/device_service"; string username = "admin"; string password = "admin"; // 创建服务绑定和客户端对象 var binding = new BasicHttpBinding(); var endpoint = new EndpointAddress(address); var client = new DeviceClient(binding, endpoint); // 设置验证凭据 client.ClientCredentials.UserName.UserName = username; client.ClientCredentials.UserName.Password = password; try { // 调用服务接口 var response = client.GetDeviceInformation(new GetDeviceInformationRequest()); // 打印设备信息 Console.WriteLine("Manufacturer: {0}", response.Manufacturer); Console.WriteLine("Model: {0}", response.Model); Console.WriteLine("Serial Number: {0}", response.SerialNumber); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); } finally { // 关闭客户端 client.Close(); } } } } ``` 以上示例代码演示了如何使用 Onvif 标准定义的 GetDeviceInformation 接口获取设备信息。你可以根据需要调用其他的服务接口,实现更加复杂的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昵称系统有问题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值