天天看點

Go語言實作Onvif用戶端-3、擷取網絡接口token

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>
           
Go語言實作Onvif用戶端-3、擷取網絡接口token