调整修改
This commit is contained in:
parent
3b1f5a5649
commit
ab788e0fba
|
|
@ -12,15 +12,12 @@ import (
|
|||
*/
|
||||
|
||||
var (
|
||||
access_base = "https://api2.hik-cloud.com/api/v1/open/basic/devices"
|
||||
access_register = access_base + "/create"
|
||||
access_delete = access_base + "/delete"
|
||||
access_update = access_base + "/update"
|
||||
access_detail = access_base + "/get"
|
||||
access_list = access_base + "/list"
|
||||
access_account = access_base + "/actions/deviceCount"
|
||||
access_status = access_base + "https://api2.hik-cloud.com/api/v1/ezviz/devices/queryDeviceStatus"
|
||||
access_reboot = access_base + "/actions/system/reboot"
|
||||
access_base = "https://api2.hik-cloud.com/api/v1/open/basic/devices"
|
||||
access_detail = access_base + "/get"
|
||||
access_list = access_base + "/list"
|
||||
access_account = access_base + "/actions/deviceCount"
|
||||
access_status = access_base + "https://api2.hik-cloud.com/api/v1/ezviz/devices/queryDeviceStatus"
|
||||
access_reboot = access_base + "/actions/system/reboot"
|
||||
|
||||
// 设备校时配置
|
||||
time_base = "https://api2.hik-cloud.com/api/v1/device/isapi/system/time"
|
||||
|
|
@ -44,27 +41,21 @@ func DeviceRegister(deviceserial, groupno, validatecode string) (*model.DeviceDa
|
|||
ValidateCode: validatecode,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", access_register, req)
|
||||
resp, err := hikvisionRequest("POST", "/api/v1/open/basic/devices/create", req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.DeviceData `json:"data"`
|
||||
}{}
|
||||
result := &model.DeviceData{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Code != 200 {
|
||||
return nil, fmt.Errorf("result errcode:%d errmsg:%s", result.Code, result.Message)
|
||||
}
|
||||
return &result.Data, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 从某一分组内删除设备
|
||||
func DeviceDelete(deviceSerial string) (*model.Hikvision, error) {
|
||||
resp, err := hikvisionRequest("POST", fmt.Sprintf("%s?deviceSerial=%s", access_delete, deviceSerial), nil)
|
||||
resp, err := hikvisionRequest("POST", fmt.Sprintf("/api/v1/open/basic/devices/delete?deviceSerial=%s", deviceSerial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -90,7 +81,7 @@ func DeviceUpdate(deviceserial, devicename string) (*model.Hikvision, error) {
|
|||
DeviceName: devicename,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", access_update, req)
|
||||
resp, err := hikvisionRequest("POST", `/api/v1/open/basic/devices/update`, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,32 +22,32 @@ var (
|
|||
)
|
||||
|
||||
func init() {
|
||||
baseURL = os.Getenv("HIK_IOT_URL")
|
||||
baseURL = os.Getenv("IOT_URL")
|
||||
if baseURL == "" {
|
||||
baseURL = "https://api2.hik-cloud.com/oauth"
|
||||
baseURL = "https://api2.hik-cloud.com"
|
||||
}
|
||||
clientID = os.Getenv("HIK_IOT_CLIENTID")
|
||||
clientID = os.Getenv("IOT_CLIENTID")
|
||||
if clientID == "" {
|
||||
clientID = "164edfd71b744dd88c945dbd5ae30a9a"
|
||||
}
|
||||
clientSecret = os.Getenv("HIK_IOT_CLIENTSECRET")
|
||||
clientSecret = os.Getenv("IOT_CLIENTSECRET")
|
||||
if clientSecret == "" {
|
||||
clientSecret = "2f580d932d7d46029a2091ece681035c"
|
||||
}
|
||||
scope = os.Getenv("HIK_IOT_SCOPE")
|
||||
scope = os.Getenv("IOT_SCOPE")
|
||||
if scope == "" {
|
||||
scope = "yy"
|
||||
}
|
||||
}
|
||||
|
||||
// 基础云眸http请求
|
||||
// 基础云眸http请求 公共方法
|
||||
func hikvisionRequest(method string, url string, body interface{}) ([]byte, error) {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reqbody := strings.NewReader(string(b))
|
||||
req, err := http.NewRequest(method, url, reqbody)
|
||||
req, err := http.NewRequest(method, baseURL+url, reqbody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -80,29 +80,13 @@ func hikvisionRequest(method string, url string, body interface{}) ([]byte, erro
|
|||
var token string
|
||||
var expired time.Time
|
||||
|
||||
type OauthResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
// 客户端认证获取access_token
|
||||
// 客户端认证获取access_token,公共方法
|
||||
func hikvisionOauth() (*string, error) {
|
||||
|
||||
if expired.After(time.Now()) {
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
if clientID == "" {
|
||||
return nil, errors.New("env HIK_IOT_CLIENTID is not set ")
|
||||
}
|
||||
if clientSecret == "" {
|
||||
return nil, errors.New("env HIK_IOT_CLIENTSECRET is not set ")
|
||||
}
|
||||
if scope == "" {
|
||||
return nil, errors.New("env HIK_IOT_SCOPE is not set ")
|
||||
}
|
||||
params := struct {
|
||||
client_id string
|
||||
client_secret string
|
||||
|
|
@ -115,12 +99,18 @@ func hikvisionOauth() (*string, error) {
|
|||
scope: scope,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequestUrlencoded("POST", fmt.Sprintf(`%s/token`, baseURL), params)
|
||||
resp, err := hikvisionRequestUrlencoded("POST", `/oauth/token`, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result *OauthResponse
|
||||
result := &struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
Scope string `json:"scope"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -131,14 +121,14 @@ func hikvisionOauth() (*string, error) {
|
|||
return &token, nil
|
||||
}
|
||||
|
||||
// 客户端认证获取access_token application/x-www-form-urlencoded
|
||||
// 使用form请求 application/x-www-form-urlencoded,公共方法
|
||||
func hikvisionRequestUrlencoded(method string, url string, body interface{}) ([]byte, error) {
|
||||
|
||||
reader, err := hikvisioninspectStruct(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf(`%s/token`, baseURL), strings.NewReader(reader.Encode()))
|
||||
req, err := http.NewRequest("POST", baseURL+url, strings.NewReader(reader.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package model
|
||||
|
||||
type DeviceData struct {
|
||||
Hikvision
|
||||
DeviceId string `json:"deviceId"` //设备id
|
||||
DeviceName string `json:"deviceName"` //型号
|
||||
DeviceModel string `json:"deviceModel"` //设备名称
|
||||
|
|
|
|||
Loading…
Reference in New Issue