package gb28181api
|
|
import (
|
"bytes"
|
"encoding/json"
|
"fmt"
|
"strconv"
|
)
|
|
func SmartPrintStruct(src interface{}) string {
|
b, err := json.Marshal(src)
|
if err != nil {
|
return fmt.Sprintf("%+v", src)
|
}
|
var out bytes.Buffer
|
err = json.Indent(&out, b, "", " ")
|
if err != nil {
|
return fmt.Sprintf("%+v", src)
|
}
|
return out.String()
|
}
|
|
type Gb28181Api struct{}
|
|
//SetPlatformServerInfo 设置服务器信息
|
func (api Gb28181Api) SetPlatformServerInfo(serverInfo GbServerInfo) bool {
|
url := BASIC_URL + DATA_URL_PREFIX + "/set_platform_server"
|
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)
|
if err != nil {
|
return false
|
}
|
|
var res GbResult
|
// fmt.Println("body", string(body))
|
if err = json.Unmarshal(body, &res); err != nil {
|
fmt.Println("jsonErr:", err)
|
return false
|
}
|
|
if res.ErrCode != 0 {
|
fmt.Println("errcode: ", res.ErrCode, " errdesc: ", res.ErrDesc)
|
return false
|
}
|
|
return true
|
}
|
|
type retGbServerInfo struct {
|
GbServerInfo
|
GbResult
|
}
|
|
//GetPlatformServerInfo 获取服务器信息
|
func (api Gb28181Api) GetPlatformServerInfo() (GbServerInfo, bool) {
|
url := BASIC_URL + DATA_URL_PREFIX + "/get_platform_server"
|
client := NewClient()
|
|
var info GbServerInfo
|
body, err := client.DoGetRequest(url, nil, nil)
|
if err != nil {
|
fmt.Println("err:", err)
|
return info, false
|
}
|
|
//解析retGbServerInfo
|
var res retGbServerInfo
|
if err = json.Unmarshal(body, &res); err != nil {
|
fmt.Println("jsonErr:", err)
|
return info, false
|
}
|
if res.ErrCode != 0 {
|
fmt.Println("errcode: ", res.ErrCode, " errdesc: ", res.ErrDesc)
|
return info, false
|
}
|
|
return res.GbServerInfo, true
|
}
|
|
//GetDevicesByPageNO 按页获取下级设备(平台或摄像机)列表
|
//pageNo (数字) 指定获取第几页,第一次为1,返回总页数,总条数等,再根据这些信息去再次查询,直到取完全部
|
func (api Gb28181Api) GetDevicesByPageNO(pageNo int) (DevicesInOnePage, bool) {
|
url := BASIC_URL + DATA_URL_PREFIX + "/get_all_device/" + strconv.Itoa(pageNo)
|
client := NewClient()
|
|
var devicesPerPage DevicesInOnePage
|
body, err := client.DoGetRequest(url, nil, nil)
|
if err != nil {
|
fmt.Println("err:", err)
|
return devicesPerPage, false
|
}
|
|
//解析 DevicesInOnePage
|
if err = json.Unmarshal(body, &devicesPerPage); err != nil {
|
fmt.Println("jsonErr:", err)
|
return devicesPerPage, false
|
}
|
if devicesPerPage.ErrCode != 0 {
|
fmt.Println("errcode: ", devicesPerPage.ErrCode, " errdesc: ", devicesPerPage.ErrDesc)
|
return devicesPerPage, false
|
}
|
|
return devicesPerPage, true
|
}
|
|
//GetAllDevices 获取全部下级设备(平台或摄像机)信息列表
|
func (api Gb28181Api) GetAllDevices() ([]DeviceInfo, bool) {
|
var deviceSlice []DeviceInfo
|
devicesPerPage, flag := api.GetDevicesByPageNO(1)
|
if !flag {
|
fmt.Println("GetDevicesByPageNO Error, deviceSlice is nil")
|
return deviceSlice, false
|
}
|
deviceSlice = devicesPerPage.Data
|
|
for i := 1; i < devicesPerPage.TotalPage; i++ {
|
devicesPerPage, flag := api.GetDevicesByPageNO(i + 1)
|
if !flag {
|
fmt.Println("GetDevicesByPageNO Error,pageno:" + strconv.Itoa(i+1) + " ,deviceSlice is not completed")
|
return deviceSlice, false
|
}
|
deviceSlice = append(deviceSlice, devicesPerPage.Data...)
|
}
|
|
return deviceSlice, true
|
}
|
|
//GetCamsByDevAndPage 按页获取下级设备的摄像机列表
|
//devID (字符串) 注册的设备的20位id
|
//pageNo (数字) 指定获取第几页,第一次为1,返回总页数,总条数等,再根据这些信息去再次查询,直到取完全部
|
//srcType (字符串) "all"-表示获取设备的所有资源 "node"-父节点下的第一级资源
|
//注意:根据"restype"字段,忽略资源组,只保留通道资源------"restype":(数字)1-通道资源 2-资源组
|
func (api Gb28181Api) GetCamsByDevAndPage(devID string, srcType string, pageNo int) (CamerasInOnePage, bool) {
|
url := BASIC_URL + DATA_URL_PREFIX + "/get_all_channel/" + devID + "/" + srcType + "/" + strconv.Itoa(pageNo)
|
client := NewClient()
|
|
var camerasPerPage CamerasInOnePage
|
body, err := client.DoGetRequest(url, nil, nil)
|
if err != nil {
|
fmt.Println("err:", err)
|
return camerasPerPage, false
|
}
|
|
//解析 CamerasInOnePage
|
if err = json.Unmarshal(body, &camerasPerPage); err != nil {
|
fmt.Println("jsonErr:", err)
|
return camerasPerPage, false
|
}
|
if camerasPerPage.ErrCode != 0 {
|
fmt.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 {
|
fmt.Println("v.restype == 2")
|
camerasPerPage.Data = append(camerasPerPage.Data[:i], camerasPerPage.Data[i+1:]...)
|
}
|
}
|
}
|
|
return camerasPerPage, true
|
}
|
|
//GetAllCamerasByDevID 获取全部下级设备的摄像机列表
|
//devID (字符串) 注册的设备的20位id
|
func (api Gb28181Api) GetAllCamerasByDevID(devID string) ([]CameraInfo, bool) {
|
var cameraSlice []CameraInfo
|
camerasPerPage, flag := api.GetCamsByDevAndPage(devID, "all", 1)
|
if !flag {
|
fmt.Println("GetCamsByDevAndPage Error, deviceSlice is nil")
|
return cameraSlice, false
|
}
|
cameraSlice = camerasPerPage.Data
|
|
for i := 1; i < camerasPerPage.TotalPage; i++ {
|
camerasPerPage, flag := api.GetCamsByDevAndPage(devID, "all", i+1)
|
if !flag {
|
fmt.Println("GetCamsByDevAndPage Error! devID:" + devID + ",type:all,pageno:" + strconv.Itoa(i+1) + " ,cameraSlice is not completed")
|
return cameraSlice, false
|
}
|
cameraSlice = append(cameraSlice, camerasPerPage.Data...)
|
}
|
|
return cameraSlice, true
|
}
|
|
//SetCameraPtz 2.3 PTZ云台控制
|
//"channelid": (字符串) 通道20位编号
|
//"ptztype": (字符串) 控制类型:上"up",下"down",左"left",右"right",左上"leftup",左下"leftdown",右上"rightup",右下"rightdown",镜头近"zoomin",镜头远"zoomout",
|
// 焦距远"focusfar",焦距近"focusnear", 设置预置位"setpos",调预置位"callpos",停止"stop"
|
//"ptzparam": (数字) 参数,速度范围为1-255
|
func (api Gb28181Api) SetCameraPtz(chanID string, ptzType string, ptzParam int) bool {
|
url := BASIC_URL + "/vss/ptz/" + ptzType + "/" + strconv.Itoa(ptzParam) + "/" + chanID
|
client := NewClient()
|
|
body, err := client.DoGetRequest(url, nil, nil)
|
if err != nil {
|
fmt.Println("err:", err)
|
return false
|
}
|
|
//解析 CamerasInOnePage
|
var res GbResult
|
if err = json.Unmarshal(body, &res); err != nil {
|
fmt.Println("jsonErr:", err)
|
return false
|
}
|
if res.ErrCode != 0 {
|
fmt.Println("errcode: ", res.ErrCode, " errdesc: ", res.ErrDesc)
|
return false
|
}
|
|
return true
|
}
|