mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-30 10:21:26 +08:00
接入萤石摄像头后端
This commit is contained in:
57
pkg/ys/device.go
Normal file
57
pkg/ys/device.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package ys
|
||||
|
||||
import (
|
||||
"pandax/pkg/tool"
|
||||
)
|
||||
|
||||
const (
|
||||
//设备列表
|
||||
DEVICELIST = "https://open.ys7.com/api/lapp/device/list"
|
||||
//获取指定设备的通道信息
|
||||
DEVICECHANNELLIST = "https://open.ys7.com/api/lapp/device/camera/list"
|
||||
// 获取播放地址
|
||||
DEVICELIVEADDRESS = "https://open.ys7.com/api/lapp/v2/live/address/get"
|
||||
)
|
||||
|
||||
// GetDeviceList 获取设备列表
|
||||
func (ys *Ys) GetDeviceList(pageNum, pageSize int) (devices []Device, total int64, err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["pageStart"] = pageNum
|
||||
params["pageSize"] = pageSize
|
||||
status, err := ys.authorizeRequset("POST", DEVICELIST, params, &devices) //获取用户下的设备列表
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var page Page
|
||||
err = tool.InterfaceToStruct(status.Page, &page)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return devices, int64(page.Total), nil
|
||||
}
|
||||
|
||||
// GetDeviceChannelList 获取指定设备的通道信息
|
||||
func (ys *Ys) GetDeviceChannelList(deviceSerial string) (cameras []Channel, err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["deviceSerial"] = deviceSerial
|
||||
_, err = ys.authorizeRequset("POST", DEVICECHANNELLIST, params, &cameras)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cameras, nil
|
||||
}
|
||||
|
||||
// GetDeviceLiveAddress 获取指定设备通道的播放地址
|
||||
func (ys *Ys) GetDeviceLiveAddress(deviceSerial string, channelNo int) (live []LiveAddress, err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["deviceSerial"] = deviceSerial
|
||||
params["channelNo"] = channelNo
|
||||
params["protocol"] = 1 //流播放协议,1-ezopen、2-hls、3-rtmp、4-flv,默认为1
|
||||
params["type"] = "1" //地址的类型,1-预览,2-本地录像回放,3-云存储录像回放,非必选,默认为1;回放仅支持rtmp、ezopen、flv协议
|
||||
params["quality"] = 1 //视频清晰度,1-高清(主码流)、2-流畅(子码流)
|
||||
_, err = ys.authorizeRequset("POST", DEVICELIVEADDRESS, params, &live)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return live, nil
|
||||
}
|
||||
37
pkg/ys/ptz.go
Normal file
37
pkg/ys/ptz.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package ys
|
||||
|
||||
const (
|
||||
//云台控制开始
|
||||
URLPTZSTAR = "https://open.ys7.com/api/lapp/device/ptz/start"
|
||||
//云台控制结束
|
||||
URLPTZSTOP = "https://open.ys7.com/api/lapp/device/ptz/stop"
|
||||
)
|
||||
|
||||
// StartPtz 开始云台控制
|
||||
func (ys *Ys) StartPtz(deviceSerial string, channelNo, direction, speed int) (err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["deviceSerial"] = deviceSerial
|
||||
params["channelNo"] = channelNo
|
||||
params["direction"] = direction
|
||||
params["speed"] = speed
|
||||
|
||||
_, err = ys.authorizeRequset("POST", URLPTZSTAR, params, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StopPtz 停止云台转动
|
||||
func (ys *Ys) StopPtz(deviceSerial string, channelNo, direction int) (err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["deviceSerial"] = deviceSerial
|
||||
params["channelNo"] = channelNo
|
||||
params["direction"] = direction
|
||||
|
||||
_, err = ys.authorizeRequset("POST", URLPTZSTOP, params, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
49
pkg/ys/type.go
Normal file
49
pkg/ys/type.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package ys
|
||||
|
||||
type AccessToken struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
ExpireTime int64 `json:"expireTime"`
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
Page interface{} `json:"page"`
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
Total float64 `json:"total"`
|
||||
Page float64 `json:"page"`
|
||||
Size float64 `json:"size"`
|
||||
}
|
||||
|
||||
// Device 萤石设备数据结构
|
||||
type Device struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
DeviceName string `json:"deviceName"`
|
||||
DeviceType string `json:"deviceType"`
|
||||
Status int `json:"status"`
|
||||
Defence int `json:"defence"`
|
||||
DeviceVersion string `json:"deviceVersion"`
|
||||
}
|
||||
|
||||
// Channel 萤石摄像头通道数据结构
|
||||
type Channel struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
IpcSerial string `json:"ipcSerial"`
|
||||
ChannelNo int `json:"channelNo"`
|
||||
ChannelName string `json:"channelName"`
|
||||
PicURL string `json:"picUrl"`
|
||||
IsShared string `json:"isShared"`
|
||||
VideoLevel int `json:"videoLevel"`
|
||||
IsEncrypt int `json:"isEncrypt"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
// LiveAddress 播放地址
|
||||
type LiveAddress struct {
|
||||
Id string `json:"id"`
|
||||
Url string `json:"url"`
|
||||
ExpireTime string `json:"expireTime"`
|
||||
}
|
||||
94
pkg/ys/ys.go
Normal file
94
pkg/ys/ys.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package ys
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
//[用户]获取accessToken
|
||||
ACCESSTOKEN = "https://open.ys7.com/api/lapp/token/get"
|
||||
)
|
||||
|
||||
type Ys struct {
|
||||
AppKey string
|
||||
Secret string
|
||||
AccessToken string
|
||||
ExpireTime int64
|
||||
}
|
||||
|
||||
func (ys *Ys) GetAccessToken() error {
|
||||
params := make(map[string]interface{})
|
||||
params["appKey"] = ys.AppKey
|
||||
params["appSecret"] = ys.Secret
|
||||
ac := &AccessToken{}
|
||||
_, err := ys.requset("POST", ACCESSTOKEN, params, &ac)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ys.AccessToken = ac.AccessToken
|
||||
ys.ExpireTime = ac.ExpireTime
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ys *Ys) requset(method, url string, params map[string]interface{}, data interface{}) (status *Status, err error) {
|
||||
defer func() {
|
||||
if Rerr := recover(); Rerr != nil {
|
||||
err = errors.New("recover error")
|
||||
return
|
||||
}
|
||||
}()
|
||||
var r http.Request
|
||||
r.ParseForm()
|
||||
for k, v := range params {
|
||||
r.Form.Add(k, fmt.Sprint(v))
|
||||
}
|
||||
req, err := http.NewRequest(method, url, strings.NewReader(r.Form.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
client := &http.Client{Timeout: 60 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
buf, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var res = Status{
|
||||
Data: data,
|
||||
}
|
||||
err = json.Unmarshal(buf, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != "200" {
|
||||
return status, errors.New(res.Msg)
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (ys *Ys) authorizeRequset(method, url string, params map[string]interface{}, data interface{}) (status *Status, err error) {
|
||||
exTime := time.Unix(ys.ExpireTime/1000, 0)
|
||||
if exTime.Unix() < time.Now().Unix() || ys.AccessToken == "" {
|
||||
ys.GetAccessToken()
|
||||
}
|
||||
defer func() {
|
||||
if Rerr := recover(); Rerr != nil {
|
||||
err = errors.New("recover error")
|
||||
return
|
||||
}
|
||||
}()
|
||||
params["accessToken"] = ys.AccessToken
|
||||
status, err = ys.requset(method, url, params, data)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user