From 366c867c373a4328588bdb02fb3e61f362409953 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 14 十二月 2021 15:30:12 +0800
Subject: [PATCH] GetAllCamerasAndGroupsByDevID ret nil []DomainUnit

---
 gb28181Api.go |  249 +++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 218 insertions(+), 31 deletions(-)

diff --git a/gb28181Api.go b/gb28181Api.go
index 7b0ad94..a74be0e 100644
--- a/gb28181Api.go
+++ b/gb28181Api.go
@@ -6,6 +6,7 @@
 	"fmt"
 	"log"
 	"strconv"
+	urlLib "net/url"
 )
 
 func SmartPrintStruct(src interface{}) string {
@@ -25,23 +26,27 @@
 
 //SetPlatformServerInfo 璁剧疆鏈嶅姟鍣ㄤ俊鎭�
 func (api Gb28181Api) SetPlatformServerInfo(serverInfo GbServerInfo) bool {
-	url := BASIC_URL + DATA_URL_PREFIX + "/set_platform_server"
+	serverInfo.GbUsername = serverInfo.PublicID   //閴存潈鍚嶅瓧涓嶇敓鏁堬紝鏈�濂藉拰璁惧id涓�鑷�
+
+	u := urlLib.Values{}
+	u.Set("gbsvrname", serverInfo.Name)
+	u.Set("gbsvrid", serverInfo.PublicID)
+	u.Set("gbsvrport", strconv.Itoa(serverInfo.GbServerPort))
+	u.Set("registerauth", strconv.FormatBool(serverInfo.RegisterAuth))
+	u.Set("gbusername", serverInfo.GbUsername)
+	u.Set("gbpasswd", serverInfo.GbPasswd)
+	str := u.Encode()
+	url := BASIC_URL + DATA_URL_PREFIX + "/set_platform_server?" + str
+	log.Println("url:", url)
+
 	client := NewClient()
-
-	paramBody := make(map[string]string, 0)
-	paramBody["gbsvrid"] = serverInfo.PublicID
-	paramBody["gbsvrport"] = strconv.Itoa(serverInfo.GbServerPort)
-	paramBody["gbsvrname"] = serverInfo.Name
-	paramBody["rtspsvrport"] = strconv.Itoa(serverInfo.RtspServrPort)
-
-	// fmt.Println("url:", url)
-	body, err := client.DoGetRequest(url, paramBody, nil)
+	body, err := client.DoGetRequest(url, nil, nil)
 	if err != nil {
 		return false
 	}
 
 	var res GbResult
-	// fmt.Println("body", string(body))
+	log.Println("body:", string(body[:]))
 	if err = json.Unmarshal(body, &res); err != nil {
 		log.Println("jsonErr:", err)
 		return false
@@ -88,7 +93,7 @@
 
 //GetDevicesByPageNO 鎸夐〉鑾峰彇涓嬬骇璁惧锛堝钩鍙版垨鎽勫儚鏈猴級鍒楄〃
 //pageNo (鏁板瓧)   鎸囧畾鑾峰彇绗嚑椤碉紝绗竴娆′负1锛岃繑鍥炴�婚〉鏁帮紝鎬绘潯鏁扮瓑锛屽啀鏍规嵁杩欎簺淇℃伅鍘诲啀娆℃煡璇紝鐩村埌鍙栧畬鍏ㄩ儴
-func (api Gb28181Api) GetDevicesByPageNO(pageNo int) (DevicesInOnePage, bool) {
+func (api Gb28181Api) getDevicesByPageNO(pageNo int) (DevicesInOnePage, bool) {
 	url := BASIC_URL + DATA_URL_PREFIX + "/get_all_device/" + strconv.Itoa(pageNo)
 	client := NewClient()
 
@@ -115,7 +120,7 @@
 //GetAllDevices 鑾峰彇鍏ㄩ儴涓嬬骇璁惧锛堝钩鍙版垨鎽勫儚鏈猴級淇℃伅鍒楄〃
 func (api Gb28181Api) GetAllDevices() ([]DeviceInfo, bool) {
 	var deviceSlice []DeviceInfo
-	devicesPerPage, flag := api.GetDevicesByPageNO(1)
+	devicesPerPage, flag := api.getDevicesByPageNO(1)
 	if !flag {
 		log.Println("GetDevicesByPageNO Error, deviceSlice is nil")
 		return deviceSlice, false
@@ -123,7 +128,7 @@
 	deviceSlice = devicesPerPage.Data
 
 	for i := 1; i < devicesPerPage.TotalPage; i++ {
-		devicesPerPage, flag := api.GetDevicesByPageNO(i + 1)
+		devicesPerPage, flag := api.getDevicesByPageNO(i + 1)
 		if !flag {
 			log.Println("GetDevicesByPageNO Error,pageno:" + strconv.Itoa(i+1) + " ,deviceSlice is not completed")
 			return deviceSlice, false
@@ -139,7 +144,7 @@
 //pageNo (鏁板瓧)   鎸囧畾鑾峰彇绗嚑椤碉紝绗竴娆′负1锛岃繑鍥炴�婚〉鏁帮紝鎬绘潯鏁扮瓑锛屽啀鏍规嵁杩欎簺淇℃伅鍘诲啀娆℃煡璇紝鐩村埌鍙栧畬鍏ㄩ儴
 //srcType (瀛楃涓�) "all"-琛ㄧず鑾峰彇璁惧鐨勬墍鏈夎祫婧�  "node"-鐖惰妭鐐逛笅鐨勭涓�绾ц祫婧�
 //娉ㄦ剰锛氭牴鎹�"restype"瀛楁锛屽拷鐣ヨ祫婧愮粍锛屽彧淇濈暀閫氶亾璧勬簮------"restype"锛�(鏁板瓧)1-閫氶亾璧勬簮 2-璧勬簮缁�
-func (api Gb28181Api) GetCamsByDevAndPage(devID string, srcType string, pageNo int) (CamerasInOnePage, bool) {
+func (api Gb28181Api) getCamsAndGroupsByDevAndPage(devID string, srcType string, pageNo int) (CamerasInOnePage, bool) {
 	url := BASIC_URL + DATA_URL_PREFIX + "/get_all_channel/" + devID + "/" + srcType + "/" + strconv.Itoa(pageNo)
 	client := NewClient()
 
@@ -159,40 +164,50 @@
 		log.Println("errcode: ", camerasPerPage.ErrCode, " errdesc: ", camerasPerPage.ErrDesc)
 		return camerasPerPage, false
 	}
-
+	/*
 	if len(camerasPerPage.Data) > 0 {
-		for i, v := range camerasPerPage.Data {
-			if v.ResType == 2 {
-				log.Println("v.restype == 2")
+		for i := 0; i < len(camerasPerPage.Data); i++ {
+			if camerasPerPage.Data[i].ResType == 2 {
+				di := camerasPerPage.Data[i]
+				camerasPerPage.Groups = append(camerasPerPage.Groups, DomainUnit{
+					DevPubID: di.DevPubID,
+					ResType: 2,
+					PublicID: di.PublicID,
+					Name: di.Name,
+					ParentID: di.ParentID,
+				})
 				camerasPerPage.Data = append(camerasPerPage.Data[:i], camerasPerPage.Data[i+1:]...)
+				i--
 			}
 		}
-	}
+	}*/
 
 	return camerasPerPage, true
 }
 
 //GetAllCamerasByDevID 鑾峰彇鍏ㄩ儴涓嬬骇璁惧鐨勬憚鍍忔満鍒楄〃
 //devID  (瀛楃涓�)  娉ㄥ唽鐨勮澶囩殑20浣峣d
-func (api Gb28181Api) GetAllCamerasByDevID(devID string) ([]CameraInfo, bool) {
+func (api Gb28181Api) GetAllCamerasAndGroupsByDevID(devID string) ([]CameraInfo, []DomainUnit, bool) {
 	var cameraSlice []CameraInfo
-	camerasPerPage, flag := api.GetCamsByDevAndPage(devID, "all", 1)
+	var groups 		[]DomainUnit
+	camerasPerPage, flag := api.getCamsAndGroupsByDevAndPage(devID, "all", 1)
 	if !flag {
 		log.Println("GetCamsByDevAndPage Error, deviceSlice is nil")
-		return cameraSlice, false
+		return cameraSlice, groups, false
 	}
 	cameraSlice = camerasPerPage.Data
-
+	groups = camerasPerPage.Groups
 	for i := 1; i < camerasPerPage.TotalPage; i++ {
-		camerasPerPage, flag := api.GetCamsByDevAndPage(devID, "all", i+1)
+		camerasPerPage, flag := api.getCamsAndGroupsByDevAndPage(devID, "all", i+1)
 		if !flag {
 			log.Println("GetCamsByDevAndPage Error! devID:" + devID + ",type:all,pageno:" + strconv.Itoa(i+1) + " ,cameraSlice is not completed")
-			return cameraSlice, false
+			return cameraSlice, groups, false
 		}
 		cameraSlice = append(cameraSlice, camerasPerPage.Data...)
+		groups = append(groups, camerasPerPage.Groups...)
 	}
 
-	return cameraSlice, true
+	return cameraSlice, groups, true
 }
 
 //GetAllDomainUnit get all domainunit 鑾峰彇鎵�鏈夎鏀垮尯鍩�
@@ -221,8 +236,9 @@
 }
 
 //GetAllCamerasByDomainUnit  get all cameras by domainunit 鑾峰彇鎵�鏈夎鏀垮尯鍩熶笅鐨勮澶�
-func (api Gb28181Api) GetAllCamerasByDomainUnitAndPage(domainpubid string, pageNo int) (CamerasInOnePage, bool) {
+func (api Gb28181Api) getAllCamerasByDomainUnitAndPage(domainpubid string, pageNo int) (CamerasInOnePage, bool) {
 	url := BASIC_URL + DATA_URL_PREFIX + "/get_domainunit_channel/" + domainpubid + "/" + strconv.Itoa(pageNo)
+	log.Println("getAllCamerasByDomainUnitAndPage:", url)
 	client := NewClient()
 
 	var camerasPerPage CamerasInOnePage
@@ -248,7 +264,7 @@
 //GetAllCamerasByDevID 鑾峰彇鎸囧畾琛屾斂鍖哄煙鐨勬憚鍍忔満鍒楄〃
 func (api Gb28181Api) GetAllCamerasByDomainUnit(domainpubid string) ([]CameraInfo, bool) {
 	var cameraSlice []CameraInfo
-	camerasPerPage, flag := api.GetAllCamerasByDomainUnitAndPage(domainpubid, 1)
+	camerasPerPage, flag := api.getAllCamerasByDomainUnitAndPage(domainpubid, 1)
 	if !flag {
 		log.Println("GetCamsByDevAndPage Error, deviceSlice is nil")
 		return cameraSlice, false
@@ -256,7 +272,7 @@
 	cameraSlice = camerasPerPage.Data
 
 	for i := 1; i < camerasPerPage.TotalPage; i++ {
-		camerasPerPage, flag := api.GetAllCamerasByDomainUnitAndPage(domainpubid, i+1)
+		camerasPerPage, flag := api.getAllCamerasByDomainUnitAndPage(domainpubid, i+1)
 		if !flag {
 			log.Println("GetCamsByDevAndPage Error! devID:" + domainpubid + ",type:all,pageno:" + strconv.Itoa(i+1) + " ,cameraSlice is not completed")
 			return cameraSlice, false
@@ -265,6 +281,178 @@
 	}
 
 	return cameraSlice, true
+}
+
+//getAllGroupBygroupIDAndpage get all group by groupID ang page 鎸夐〉鍜岀粍id鑾峰彇涓嬪睘鍒嗙粍
+func (api Gb28181Api) getAllGroupBygroupIDAndpage(groupID string, pageNo int) ([]DomainUnit, bool) {
+	url := BASIC_URL + DATA_URL_PREFIX + "/get_all_channel/" + groupID + "/node/" + strconv.Itoa(pageNo)
+	client := NewClient()
+
+	var camerasPerPage CamerasInOnePage
+	body, err := client.DoGetRequest(url, nil, nil)
+	if err != nil {
+		log.Println("err:", err)
+		return nil, false
+	}
+
+	if camerasPerPage.ErrCode != 0 {
+		log.Println("errcode: ", camerasPerPage.ErrCode, " errdesc: ", camerasPerPage.ErrDesc)
+		return nil, false
+	}
+
+	//瑙f瀽 cameras
+	//log.Println("body:", body)
+	if err = json.Unmarshal(body, &camerasPerPage); err != nil {
+		log.Println("jsonErr:", err)
+		return nil, false
+	}
+
+	//cameraInfo 杞� DomainUnit
+	var dmUnits []DomainUnit
+	var tmpUnit DomainUnit
+	for _, cam := range camerasPerPage.Data {
+		if cam.ResType == 2 {
+			tmpUnit.Name = cam.Name
+			tmpUnit.DevPubID = cam.DevPubID
+			tmpUnit.OnlineNum = 0
+			tmpUnit.ParentID = cam.ParentID
+			tmpUnit.PublicID = cam.PublicID
+			tmpUnit.ResType = cam.ResType
+			tmpUnit.TotalNum = 0
+			dmUnits = append(dmUnits, tmpUnit)
+		}
+	}
+
+	log.Println("dmUnits:", dmUnits)
+	return dmUnits, true
+}
+
+//GetAllGroupByDevID  鎸夌粍id鑾峰彇涓嬪睘鍒嗙粍
+func (api Gb28181Api) getAllGroupByGroupID(groupID string) []DomainUnit {
+	var dmUnitAll []DomainUnit
+	pageNo := 1
+	for {
+		dmUnitsPerPage, flag := api.getAllGroupBygroupIDAndpage(groupID, pageNo)
+		if !flag || dmUnitsPerPage == nil {
+			break
+		}
+		dmUnitAll = append(dmUnitAll, dmUnitsPerPage...)
+		pageNo++
+	}
+
+	return dmUnitAll
+}
+
+//
+func (api Gb28181Api) getGroupsByGroupID(groupID string, groupsAll *[]DomainUnit) {
+	if groupID != "" {
+		groups := api.getAllGroupByGroupID(groupID)
+		if groups == nil {
+			return
+		}
+		if len(groups) > 0 {
+			*groupsAll = append(*groupsAll, groups...)
+			for _, groupid := range groups {
+				api.getGroupsByGroupID(groupid.PublicID, groupsAll)
+			}
+		}
+	}
+}
+
+//GetAllGroupByDevID get all group by devid 鎸夎澶噄d鑾峰彇鍒嗙粍
+func (api Gb28181Api) GetAllGroupBydevID(devID string) []DomainUnit {
+	var dmUnitAll []DomainUnit
+
+	api.getGroupsByGroupID(devID, &dmUnitAll)
+
+	log.Println("dmUnitAll:", dmUnitAll)
+	return dmUnitAll
+}
+
+//getAllCamerasByGroupIDAndPage 鎸夊垎缁勫拰椤佃幏鍙栨憚鍍忔満鍒楄〃
+func (api Gb28181Api) getAllCamerasByGroupIDAndPage(groupID string, pageNo int) (CamerasInOnePage, bool) {
+	url := BASIC_URL + DATA_URL_PREFIX + "/get_all_channel/" + groupID + "/node/" + strconv.Itoa(pageNo)
+	client := NewClient()
+
+	var camerasPerPage CamerasInOnePage
+	body, err := client.DoGetRequest(url, nil, nil)
+	if err != nil {
+		log.Println("err:", err)
+		return camerasPerPage, false
+	}
+
+	//瑙f瀽 cameras
+	if err = json.Unmarshal(body, &camerasPerPage); err != nil {
+		log.Println("jsonErr:", err)
+		return camerasPerPage, false
+	}
+
+	if len(camerasPerPage.Data) > 0 {
+		for i := 0; i < len(camerasPerPage.Data); i++ {
+			if camerasPerPage.Data[i].ResType == 2 {
+				camerasPerPage.Data = append(camerasPerPage.Data[:i], camerasPerPage.Data[i+1:]...)
+				i--
+			}
+		}
+	}
+
+	return camerasPerPage, true
+}
+
+//GetAllGroupByDevID get all group by devid 鎸夎澶噄d鑾峰彇鍒嗙粍
+func (api Gb28181Api) GetAllCamerasByGroupID(groupID string) ([]CameraInfo, bool) {
+
+	var cameraSlice []CameraInfo
+	camerasPerPage, flag := api.getAllCamerasByGroupIDAndPage(groupID, 1)
+	if !flag {
+		log.Println("GetCamsByDevAndPage Error, deviceSlice is nil")
+		return cameraSlice, false
+	}
+	cameraSlice = camerasPerPage.Data
+
+	for i := 1; i < camerasPerPage.TotalPage; i++ {
+		camerasPerPage, flag := api.getAllCamerasByGroupIDAndPage(groupID, i+1)
+		if !flag {
+			log.Println("getAllCamerasByGroupIDAndPage Error! devID:" + groupID + ",type:all,pageno:" + strconv.Itoa(i+1) + " ,cameraSlice is not completed")
+			return cameraSlice, false
+		}
+		cameraSlice = append(cameraSlice, camerasPerPage.Data...)
+	}
+
+	return cameraSlice, true
+}
+
+//updateResourceByID 鏇存柊涓嬬骇骞冲彴璧勬簮
+func (api Gb28181Api) updateResourceByID(devID string) bool {
+	url := BASIC_URL + "/vss/get_catalog/" + devID
+	client := NewClient()
+
+	body, err := client.DoGetRequest(url, nil, nil)
+	if err != nil {
+		log.Println("err:", err)
+		return false
+	}
+
+	var res GbResult
+	if err = json.Unmarshal(body, &res); err != nil {
+		log.Println("jsonErr:", err)
+		return false
+	}
+	if res.ErrCode != 0 {
+		log.Println("errcode: ", res.ErrCode, " errdesc: ", res.ErrDesc)
+		return false
+	}
+
+	return true
+}
+
+func (api Gb28181Api) UpdateAllResource() {
+	devices, b := api.GetAllDevices()
+	if b && len(devices) > 0 {
+		for _, device := range devices {
+			api.updateResourceByID(device.PublicID)
+		}
+	}
 }
 
 //SetCameraPtz 2.3 PTZ浜戝彴鎺у埗
@@ -282,7 +470,6 @@
 		return false
 	}
 
-	//瑙f瀽 CamerasInOnePage
 	var res GbResult
 	if err = json.Unmarshal(body, &res); err != nil {
 		log.Println("jsonErr:", err)

--
Gitblit v1.8.0