chenshijun
2019-06-24 30eff761eaf84b7133d0bd15190fd26ed8bfc464
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
}