264 lines
7.2 KiB
Go
264 lines
7.2 KiB
Go
package isc
|
||
|
||
import (
|
||
"bytes"
|
||
"crypto/hmac"
|
||
"crypto/sha256"
|
||
"crypto/tls"
|
||
"encoding/base64"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io/ioutil"
|
||
"math/rand"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
var AppKey = "22422818" //海康平台的key
|
||
var AppSecret = "smyUkLf2Rd9VOkLV9Ndv"
|
||
|
||
//var AppKey = "22422818" //海康平台的key
|
||
//var AppSecret = "smyUkLf2Rd9VOkLV9Ndv"
|
||
|
||
var Protocol = "https://"
|
||
var HOST = "192.168.1.153"
|
||
var PORT = "2443"
|
||
var reqError = "2003"
|
||
var reqErrorMsg = "请求失败"
|
||
var Ok = "0"
|
||
var Method = "POST"
|
||
|
||
func GetStatusList() {
|
||
config := map[string]interface{}{}
|
||
config["includeSubNode"] = 1
|
||
config["pageNo"] = 1
|
||
config["pageSize"] = 1000
|
||
config["regionId"] = "root000000"
|
||
//config["indexCodes"] = []string{"7cbd5a4c2dab46579b09c297cf649721", "7ec4f9689dc1466d813c801a5a0686b2"}
|
||
t := CameraStatus{Code: reqError, Msg: reqErrorMsg}
|
||
//content, _ := request("/api/nms/v1/online/camera/get", config)
|
||
content, _ := request("/api/nms/v1/online/camera/get", config)
|
||
fmt.Println("获取设备状态")
|
||
_ = json.Unmarshal([]byte(content), &t)
|
||
for _, v := range t.Data.List {
|
||
if ca, ok := DbCameraMap[v.IndexCode]; ok {
|
||
ca.Status = v.Online
|
||
DbCameraMap[v.IndexCode] = ca
|
||
}
|
||
//fmt.Println("v.Cn", v.Cn)
|
||
//fmt.Println("v.Online", v.Online)
|
||
//fmt.Println("v.DeviceIndexCode", v.DeviceIndexCode)
|
||
//fmt.Println("v.IndexCode", v.IndexCode)
|
||
//fmt.Println("v.CollectTime", v.CollectTime)
|
||
//fmt.Println("************************")
|
||
}
|
||
|
||
}
|
||
|
||
/*抓图接口*/
|
||
func ManualCapture(cameraIndexCode string) CameraCap {
|
||
config := map[string]interface{}{}
|
||
config["cameraIndexCode"] = cameraIndexCode
|
||
t := CameraCap{Code: reqError, Msg: reqErrorMsg}
|
||
content, _ := request("/api/video/v1/manualCapture", config)
|
||
_ = json.Unmarshal([]byte(content), &t)
|
||
return t
|
||
}
|
||
|
||
/*
|
||
云台控制
|
||
action number 0-开始 ,1-停止
|
||
注:GOTO_PRESET命令下填任意值均可转到预置点,建议填0即可
|
||
command string 不区分大小写
|
||
说明:
|
||
LEFT 左转
|
||
RIGHT右转
|
||
UP 上转
|
||
DOWN 下转
|
||
ZOOM_IN 焦距变大
|
||
ZOOM_OUT 焦距变小
|
||
LEFT_UP 左上
|
||
LEFT_DOWN 左下
|
||
RIGHT_UP 右上
|
||
RIGHT_DOWN 右下
|
||
FOCUS_NEAR 焦点前移
|
||
FOCUS_FAR 焦点后移
|
||
IRIS_ENLARGE 光圈扩大
|
||
IRIS_REDUCE 光圈缩小
|
||
WIPER_SWITCH 接通雨刷开关
|
||
START_RECORD_TRACK 开始记录轨迹
|
||
STOP_RECORD_TRACK 停止记录轨迹
|
||
START_TRACK 开始轨迹
|
||
STOP_TRACK 停止轨迹;
|
||
以下命令presetIndex不可为空:
|
||
GOTO_PRESET到预置点
|
||
*/
|
||
func CtrlPtz(cameraIndexCode string, action int, command string) Usual {
|
||
config := map[string]interface{}{}
|
||
config["cameraIndexCode"] = cameraIndexCode
|
||
config["action"] = action
|
||
config["command"] = command
|
||
config["speed"] = 50 //默认速度为50
|
||
t := Usual{Code: reqError, Msg: reqErrorMsg}
|
||
content, _ := request("/api/video/v1/ptzs/controlling", config)
|
||
_ = json.Unmarshal([]byte(content), &t)
|
||
return t
|
||
}
|
||
|
||
/*获取对讲地址*/
|
||
func GettalkURLs(cameraIndexCode string) CameraTalk {
|
||
config := map[string]interface{}{}
|
||
config["cameraIndexCode"] = cameraIndexCode
|
||
config["protocol"] = "ws"
|
||
t := CameraTalk{Code: reqError, Msg: reqErrorMsg}
|
||
content, _ := request("/api/video/v1/cameras/talkURLs", config)
|
||
_ = json.Unmarshal([]byte(content), &t)
|
||
return t
|
||
}
|
||
|
||
/*获取预览地址 ws协议*/
|
||
func GetpreviewURLs(cameraIndexCode string) CameraPreview {
|
||
config := map[string]interface{}{}
|
||
config["cameraIndexCode"] = cameraIndexCode
|
||
config["protocol"] = "ws"
|
||
t := CameraPreview{Code: reqError, Msg: reqErrorMsg}
|
||
content, _ := request("/api/video/v1/cameras/previewURLs", config)
|
||
//fmt.Println(api)
|
||
//content, _ := request(api, config)
|
||
fmt.Println(content)
|
||
_ = json.Unmarshal([]byte(content), &t)
|
||
return t
|
||
}
|
||
|
||
/*获取监控点列表*/
|
||
func GetCameraList(pageNo, pageSize int) {
|
||
fmt.Println("开始获取第一页数据")
|
||
config := map[string]interface{}{}
|
||
config["pageNo"] = pageNo
|
||
config["pageSize"] = pageSize
|
||
t := CameraData{Code: reqError, Msg: reqErrorMsg}
|
||
content, err := request("/api/resource/v1/cameras", config)
|
||
if err != nil {
|
||
return
|
||
}
|
||
fmt.Println("数据完成")
|
||
fmt.Println(content)
|
||
err = json.Unmarshal([]byte(content), &t)
|
||
if err != nil {
|
||
fmt.Println("数据反序列化失败")
|
||
fmt.Println(err)
|
||
return
|
||
}
|
||
if t.Code == Ok {
|
||
fmt.Println("监控点总数", t.Data.Total)
|
||
fmt.Println("页码", t.Data.PageNo)
|
||
fmt.Println("当前页数量", t.Data.PageSize)
|
||
//fmt.Println(t.Data.List)
|
||
for _, v := range t.Data.List {
|
||
fmt.Println(v.CameraIndexCode, v.Name, v.Status)
|
||
//if v.Status == 1 {
|
||
// ca = v
|
||
//}
|
||
}
|
||
} else {
|
||
fmt.Println("数据获取失败")
|
||
fmt.Println(t)
|
||
}
|
||
|
||
}
|
||
|
||
// 请求
|
||
func request(url string, body map[string]interface{}) (string, error) {
|
||
configData, _ := json.Marshal(body)
|
||
param := bytes.NewBuffer(configData)
|
||
tr := &http.Transport{
|
||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||
}
|
||
client := &http.Client{Transport: tr}
|
||
url = "/artemis" + url
|
||
headers, _ := getSignature(url, Method)
|
||
url = Protocol + HOST + ":" + PORT + url
|
||
fmt.Println(url)
|
||
|
||
// 2. 创建请求实例
|
||
req, err := http.NewRequest(Method, url, param)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
//header
|
||
|
||
for i := 0; i < len(headers); i++ {
|
||
//fmt.Println(headers[i].Key, headers[i].Val)
|
||
req.Header.Add(headers[i].Key, headers[i].Val)
|
||
}
|
||
//发送请求
|
||
res, err := client.Do(req)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
defer res.Body.Close()
|
||
content, err := ioutil.ReadAll(res.Body)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
return string(content), nil
|
||
}
|
||
func getSignature(url, method string) ([]Header, string) {
|
||
var now = time.Now()
|
||
var headers = []Header{
|
||
{Key: "Accept", Val: "*/*"},
|
||
{Key: "Content-Type", Val: "application/json"},
|
||
{Key: "Date", Val: now.Format("2006-01-02 15:04:05")},
|
||
{Key: "x-ca-key", Val: AppKey, Use: true},
|
||
{Key: "x-ca-nonce", Val: RandAllString(32), Use: true},
|
||
{Key: "x-ca-timestamp", Val: strconv.Itoa(int(now.UnixNano() / 1e6)), Use: true},
|
||
}
|
||
var arr = append(headers, Header{Key: "url", Val: url})
|
||
var arr2 = append([]Header{{Val: method, Key: "method"}}, arr...)
|
||
var str []string
|
||
for i := 0; i < len(arr2); i++ {
|
||
if arr2[i].Use {
|
||
str = append(str, arr2[i].Key+":"+arr2[i].Val)
|
||
} else {
|
||
str = append(str, arr2[i].Val)
|
||
}
|
||
}
|
||
sign := ComputeHmac256(strings.Join(str, "\n"), AppSecret)
|
||
headers = append(headers, Header{Key: "x-ca-signature-headers", Val: "x-ca-key,x-ca-nonce,x-ca-timestamp"}, Header{Key: "x-ca-signature", Val: sign})
|
||
return headers, sign
|
||
}
|
||
|
||
func ComputeHmac256(message string, secret string) string {
|
||
key := []byte(secret)
|
||
h := hmac.New(sha256.New, key)
|
||
h.Write([]byte(message))
|
||
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||
}
|
||
|
||
type Header struct {
|
||
Key string `json:"key"`
|
||
Val string `json:"val"`
|
||
Use bool `json:"use"`
|
||
}
|
||
|
||
/*
|
||
RandAllString 生成随机字符串([a~zA~Z0~9])
|
||
|
||
lenNum 长度
|
||
*/
|
||
func RandAllString(lenNum int) string {
|
||
var CHARS = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
|
||
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
|
||
|
||
str := strings.Builder{}
|
||
length := len(CHARS)
|
||
for i := 0; i < lenNum; i++ {
|
||
l := CHARS[rand.Intn(length)]
|
||
str.WriteString(l)
|
||
}
|
||
return str.String()
|
||
}
|