Go语言实现Onvif客户端:3、获取网络接口token
文章目录
-
- Go语言实现Onvif客户端:3、获取网络接口token
-
- 1、思路
- 2、代码
上两节已经通过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>
