初始
This commit is contained in:
32
third/isc/api.go
Normal file
32
third/isc/api.go
Normal file
@ -0,0 +1,32 @@
|
||||
package isc
|
||||
|
||||
type camera struct {
|
||||
CameraIndexCode string `json:"cameraIndexCode"`
|
||||
CameraName string `json:"cameraName"`
|
||||
Marker_id string `json:"marker_id"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
var ca Camera
|
||||
var cameras []Camera
|
||||
|
||||
var DbCameraMap = make(map[string]camera)
|
||||
|
||||
func InitIscApi() {
|
||||
//getAllCameraFromDb()
|
||||
//getCameraFromIsc()
|
||||
//GetStatusList()
|
||||
//createTimer()
|
||||
|
||||
//GetCameraList(1, 20)
|
||||
}
|
||||
|
||||
type Ret struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func getCameraFromIsc() {
|
||||
GetCameraList(1, 1000)
|
||||
}
|
263
third/isc/isc_auth.go
Normal file
263
third/isc/isc_auth.go
Normal file
@ -0,0 +1,263 @@
|
||||
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()
|
||||
}
|
92
third/isc/struct.go
Normal file
92
third/isc/struct.go
Normal file
@ -0,0 +1,92 @@
|
||||
package isc
|
||||
|
||||
// 通用结构体
|
||||
type Usual struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
type Camera struct {
|
||||
CameraIndexCode string `json:"cameraIndexCode"` //监控点编号
|
||||
Name string `json:"name"` //监控点名称
|
||||
CameraType int `json:"cameraType"` //监控点类型
|
||||
CameraTypeName string `json:"cameraTypeName"` //监控点类型名称
|
||||
Longitude string `json:"longitude"` //监控点类型名称
|
||||
Latitude string `json:"latitude"` //监控点类型名称
|
||||
Altitude string `json:"altitude"` //监控点类型名称
|
||||
ChannelNo string `json:"channelNo"` //监控点类型名称
|
||||
Status int `json:"status"` //监控点在线状态 在线状态(0-未知,1-在线,2-离线),扩展字段,暂不使用
|
||||
}
|
||||
|
||||
// 监控点列表信息结构
|
||||
type CameraData struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data struct {
|
||||
Total int `json:"total"`
|
||||
PageNo int `json:"pageNo"`
|
||||
PageSize int `json:"pageSize"`
|
||||
List []Camera `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// 监控点的预览接口返回值结构
|
||||
type CameraPreview struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data struct {
|
||||
Url string `json:"url"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
//监控点的对讲接口返回值结构
|
||||
|
||||
type CameraTalk struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data struct {
|
||||
Url string `json:"url"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
//监控点的抓图接口返回值结构
|
||||
|
||||
type CameraCap struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data struct {
|
||||
PicUrl string `json:"picUrl"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// 监控点的在线状态接口返回值结构
|
||||
type statusList struct {
|
||||
Online int `json:"online"`
|
||||
IndexCode string `json:"indexCode"`
|
||||
DeviceIndexCode string `json:"deviceIndexCode"`
|
||||
Cn string `json:"cn"`
|
||||
CollectTime string `json:"collectTime"`
|
||||
//"deviceType": "HIK%2FDS-9116HW-ST%2F-AF-DVR",
|
||||
//"deviceIndexCode": null,
|
||||
//"regionIndexCode": "ce91c758-5af4-4539-845a",
|
||||
//"collectTime": "2018-12-28T10:21:40.000+08:00",
|
||||
//"regionName": "NMS自动化",
|
||||
//"indexCode": "82896441ced946d5a51c6d6ca8e65851",
|
||||
//"cn": "Onvif-IPC(10.67.172.13 )",
|
||||
//"treatyType": "1",
|
||||
//"manufacturer": "hikvision",
|
||||
//"ip": null,
|
||||
//"port": null,
|
||||
//"online": 1
|
||||
}
|
||||
type CameraStatus struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data struct {
|
||||
PageNo int `json:"pageNo"`
|
||||
PageSize int `json:"pageSize"`
|
||||
TotalPage int `json:"totalPage"`
|
||||
Total int `json:"total"`
|
||||
List []statusList `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
Reference in New Issue
Block a user