调整代码
This commit is contained in:
parent
d5978b95f2
commit
f2cdfefb5e
|
|
@ -1,10 +1,10 @@
|
|||
package cloud
|
||||
package iot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"myschools.me/suguo/hikvision/model"
|
||||
"myschools.me/suguo/hikvision/iot/model"
|
||||
)
|
||||
|
||||
/*
|
||||
|
|
@ -28,40 +28,30 @@ const (
|
|||
TIM_NTPSERVER_CFG = TIME_BASE + "/ntpServers/config"
|
||||
)
|
||||
|
||||
type DeviceRegister struct {
|
||||
DeviceSerial string `json:"deviceSerial"` //设备序列号
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
ValidateCode string `json:"validateCode"` //验证码
|
||||
}
|
||||
|
||||
type DeviceRegisterRes struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data DeviceData `json:"data"`
|
||||
}
|
||||
|
||||
type DeviceData struct {
|
||||
DeviceId string `json:"deviceId"` //设备id
|
||||
DeviceName string `json:"deviceName"` //型号
|
||||
DeviceModel string `json:"deviceModel"` //设备名称
|
||||
DeviceSerial string `json:"deviceSerial"` //设备序列号
|
||||
DeviceStatus int `json:"deviceStatus"` //0-离线,1-在线
|
||||
GroupId string `json:"groupId"` //组id
|
||||
IsEncrypt int `json:"isEncrypt"` //设备加密状态 0-关闭 1-开启
|
||||
|
||||
// 当使用设备接入详情接口获取的信息才会拥有版本
|
||||
DeviceVersion string `json:"deviceVersion"`
|
||||
}
|
||||
|
||||
// 注册设备到对应分组内。
|
||||
//
|
||||
// 注册设备时首先会将设备添加到平台,然后异步同步设备通道。如果设备添加成功而同步设备通道失败,则可以先获取设备列表信息,再手动调用通道同步接口同步设备下的通道。\
|
||||
func DeivceRegister(access_token *string, req *DeviceRegister) (*DeviceRegisterRes, error) {
|
||||
resp, err := hikvisionRequest(access_token, "POST", ACCESS_REGISTER, req)
|
||||
func DeivceRegister(deviceserial, groupno, validatecode string) (*model.DeviceData, error) {
|
||||
|
||||
req := &struct {
|
||||
DeviceSerial string `json:"deviceSerial"` //设备序列号
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
ValidateCode string `json:"validateCode"` //验证码
|
||||
}{
|
||||
|
||||
DeviceSerial: deviceserial,
|
||||
GroupNo: groupno,
|
||||
ValidateCode: validatecode,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", ACCESS_REGISTER, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeviceRegisterRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.DeviceData `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -69,16 +59,16 @@ func DeivceRegister(access_token *string, req *DeviceRegister) (*DeviceRegisterR
|
|||
if result.Code != 200 {
|
||||
return nil, fmt.Errorf("result errcode:%d errmsg:%s", result.Code, result.Message)
|
||||
}
|
||||
return result, nil
|
||||
return &result.Data, nil
|
||||
}
|
||||
|
||||
// 从某一分组内删除设备
|
||||
func DeivceDelete(access_token *string, deviceSerial string) (*model.BaseOperationRes, error) {
|
||||
resp, err := hikvisionRequest(access_token, "POST", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_DELETE, deviceSerial), nil)
|
||||
func DeivceDelete(deviceSerial string) (*model.Hikvision, error) {
|
||||
resp, err := hikvisionRequest("POST", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_DELETE, deviceSerial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -89,18 +79,22 @@ func DeivceDelete(access_token *string, deviceSerial string) (*model.BaseOperati
|
|||
return result, nil
|
||||
}
|
||||
|
||||
type DeivceUpdateReq struct {
|
||||
DeviceSerial string `json:"deviceSerial"` //设备序列号
|
||||
DeviceName string `json:"deviceName"` //设备名称
|
||||
}
|
||||
|
||||
// 该接口用于修改设备名称
|
||||
func DeivceUpdate(access_token *string, req *DeivceUpdateReq) (*model.BaseOperationRes, error) {
|
||||
resp, err := hikvisionRequest(access_token, "POST", ACCESS_UPDATE, req)
|
||||
func DeivceUpdate(deviceserial, devicename string) (*model.Hikvision, error) {
|
||||
|
||||
req := &struct {
|
||||
DeviceSerial string `json:"deviceSerial"` //设备序列号
|
||||
DeviceName string `json:"deviceName"` //设备名称
|
||||
}{
|
||||
DeviceSerial: deviceserial,
|
||||
DeviceName: devicename,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", ACCESS_UPDATE, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -112,12 +106,15 @@ func DeivceUpdate(access_token *string, req *DeivceUpdateReq) (*model.BaseOperat
|
|||
}
|
||||
|
||||
// 该接口用于根据设备序列号获取单个设备详细信息
|
||||
func DeivceDetail(access_token *string, deviceSerial string) (*DeviceData, error) {
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_DETAIL, deviceSerial), nil)
|
||||
func DeivceDetail(deviceSerial string) (*model.DeviceData, error) {
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_DETAIL, deviceSerial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeviceRegisterRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.DeviceData `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -128,33 +125,17 @@ func DeivceDetail(access_token *string, deviceSerial string) (*DeviceData, error
|
|||
return &result.Data, nil
|
||||
}
|
||||
|
||||
type DeivceListReq struct {
|
||||
GroupNo string //必填
|
||||
pageNo int //必填,分页页数 >=1
|
||||
pageSize int //必填,分页记录数 1-999
|
||||
}
|
||||
|
||||
type DeivceListRes struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data DeivceListData `json:"data"`
|
||||
}
|
||||
|
||||
type DeivceListData struct {
|
||||
PageNo int `json:"pageNo"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Total int `json:"total"`
|
||||
Rows []DeviceData `json:"rows"`
|
||||
}
|
||||
|
||||
// 该接口用于查询某组下设备列表信息
|
||||
func DeivceList(access_token *string, req *DeivceListReq) (*DeivceListData, error) {
|
||||
url := fmt.Sprintf("%s?groupNo=%s&pageNo=%d&pageSize=%d", ACCESS_LIST, req.GroupNo, req.pageNo, req.pageSize)
|
||||
resp, err := hikvisionRequest(access_token, "GET", url, nil)
|
||||
// 该接口用于查询某组下设备列表信息 ,组编号,页数,页行数
|
||||
func DeivceList(groupno string, pageno, pagesize int) (*model.DeivceListData, error) {
|
||||
url := fmt.Sprintf("%s?groupNo=%s&pageNo=%d&pageSize=%d", ACCESS_LIST, groupno, pageno, pagesize)
|
||||
resp, err := hikvisionRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeivceListRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.DeivceListData `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -165,24 +146,19 @@ func DeivceList(access_token *string, req *DeivceListReq) (*DeivceListData, erro
|
|||
return &result.Data, nil
|
||||
}
|
||||
|
||||
type DeivceNumRes struct {
|
||||
Code int `json:"code"`
|
||||
Data DeivceNumData `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type DeivceNumData struct {
|
||||
TotalCount int `json:"totalCount"`
|
||||
}
|
||||
|
||||
// 该接口用于获取用户接入的设备总数
|
||||
func DeivceNum(access_token *string) (*int, error) {
|
||||
func DeivceNum() (*int, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "GET", ACCESS_ACCOUNT, nil)
|
||||
resp, err := hikvisionRequest("GET", ACCESS_ACCOUNT, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeivceNumRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data struct {
|
||||
TotalCount int `json:"totalCount"`
|
||||
} `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -193,33 +169,17 @@ func DeivceNum(access_token *string) (*int, error) {
|
|||
return &result.Data.TotalCount, nil
|
||||
}
|
||||
|
||||
type DeivceStatusReq struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data DeivceStatusData `json:"data"`
|
||||
}
|
||||
|
||||
type DeivceStatusData struct {
|
||||
PrivacyStatus int `json:"privacyStatus"`
|
||||
PirStatus int `json:"pirStatus"`
|
||||
AlarmSoundMode int `json:"alarmSoundMode"`
|
||||
BattryStatus int `json:"battryStatus"`
|
||||
LockSignal int `json:"lockSignal"`
|
||||
DiskNum int `json:"diskNum"`
|
||||
DiskState string `json:"diskState"`
|
||||
CloudStatus int `json:"cloudStatus"`
|
||||
NvrDiskNum int `json:"nvrDiskNum"`
|
||||
NvrDiskState string `json:"nvrDiskState"`
|
||||
}
|
||||
|
||||
// 该接口用于查询设备的状态信息,目前仅支持萤石设备 /*测试未通过*/
|
||||
func DeivceStatus(access_token *string, deviceserial string) (*DeivceStatusData, error) {
|
||||
func DeivceStatus(deviceserial string) (*model.DeivceStatusData, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_STATUS, deviceserial), nil)
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_STATUS, deviceserial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeivceStatusReq
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.DeivceStatusData `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -231,18 +191,18 @@ func DeivceStatus(access_token *string, deviceserial string) (*DeivceStatusData,
|
|||
}
|
||||
|
||||
// 该接口用于重启设备,请谨慎操作
|
||||
func DeivceReboot(access_token *string, deviceserial string) (*model.BaseOperationRes, error) {
|
||||
func DeivceReboot(deviceserial string) (*model.Hikvision, error) {
|
||||
params := &struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
}{
|
||||
DeviceSerial: deviceserial,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "POST", ACCESS_REBOOT, params)
|
||||
resp, err := hikvisionRequest("POST", ACCESS_REBOOT, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -264,13 +224,13 @@ func DeivceReboot(access_token *string, deviceserial string) (*model.BaseOperati
|
|||
*/
|
||||
|
||||
// 下线确认
|
||||
func DeivceOffline(access_token *string, deviceserial string) (*model.BaseOperationRes, error) {
|
||||
func DeivceOffline(deviceserial string) (*model.Hikvision, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("https://api2.hik-cloud.com/v1/carrier/wing/endpoint/confirm/right/offlineconfirm?deviceSerial=%s", deviceserial), nil)
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("https://api2.hik-cloud.com/v1/carrier/wing/endpoint/confirm/right/offlineconfirm?deviceSerial=%s", deviceserial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -282,13 +242,13 @@ func DeivceOffline(access_token *string, deviceserial string) (*model.BaseOperat
|
|||
}
|
||||
|
||||
// 上线线确认
|
||||
func DeivceOnline(access_token *string, deviceserial string) (*model.BaseOperationRes, error) {
|
||||
func DeivceOnline(deviceserial string) (*model.Hikvision, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("https://api2.hik-cloud.com/v1/carrier/wing/endpoint/confirm/right/onlineconfirm?deviceSerial=%s", deviceserial), nil)
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("https://api2.hik-cloud.com/v1/carrier/wing/endpoint/confirm/right/onlineconfirm?deviceSerial=%s", deviceserial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -299,23 +259,18 @@ func DeivceOnline(access_token *string, deviceserial string) (*model.BaseOperati
|
|||
return result, nil
|
||||
}
|
||||
|
||||
type DeviceGetTimeModeRes struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data DeviceGetTimeModeData `json:"data"`
|
||||
}
|
||||
|
||||
type DeviceGetTimeModeData struct {
|
||||
TimeMode string `json:"timeMode"`
|
||||
}
|
||||
|
||||
// 该接口用于获取设备当前校时配置
|
||||
func DeviceGetTimeMode(access_token *string, deviceserial string) (*DeviceGetTimeModeData, error) {
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", TIME_BASE, deviceserial), nil)
|
||||
func DeviceGetTimeMode(deviceserial string) (*string, error) {
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("%s?deviceSerial=%s", TIME_BASE, deviceserial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeviceGetTimeModeRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data struct {
|
||||
TimeMode string `json:"timeMode"`
|
||||
} `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -323,21 +278,25 @@ func DeviceGetTimeMode(access_token *string, deviceserial string) (*DeviceGetTim
|
|||
if result.Code != 200 {
|
||||
return nil, fmt.Errorf("result errcode:%d errmsg:%s", result.Code, result.Message)
|
||||
}
|
||||
return &result.Data, nil
|
||||
}
|
||||
|
||||
type DeviceSetTimeModeReq struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
TimeMode string `json:"timeMode"`
|
||||
return &result.Data.TimeMode, nil
|
||||
}
|
||||
|
||||
// 该接口用于配置设备校时模式
|
||||
func DeviceSetTimeMode(access_token *string, req *DeviceSetTimeModeReq) (*model.BaseOperationRes, error) {
|
||||
resp, err := hikvisionRequest(access_token, "POST", TIME_BASE, req)
|
||||
func DeviceSetTimeMode(deviceSerial, timeMode string) (*model.Hikvision, error) {
|
||||
|
||||
req := &struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
TimeMode string `json:"timeMode"`
|
||||
}{
|
||||
DeviceSerial: deviceSerial,
|
||||
TimeMode: timeMode,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", TIME_BASE, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -349,12 +308,15 @@ func DeviceSetTimeMode(access_token *string, req *DeviceSetTimeModeReq) (*model.
|
|||
}
|
||||
|
||||
// 该接口用于获取设备当前NTP服务器配置
|
||||
func DeviceGetNTPCfg(access_token *string, deviceserial string) (*DeviceSetNTPCfgServer, error) {
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", TIME_NTPCFG, deviceserial), nil)
|
||||
func DeviceGetNTPCfg(deviceserial string) (*model.DeviceSetNTPCfgServer, error) {
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("%s?deviceSerial=%s", TIME_NTPCFG, deviceserial), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeviceGetNTPServerRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.DeviceSetNTPCfgServer `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -365,27 +327,22 @@ func DeviceGetNTPCfg(access_token *string, deviceserial string) (*DeviceSetNTPCf
|
|||
return &result.Data, nil
|
||||
}
|
||||
|
||||
type DeviceSetNTPCfgReq struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
NtpServer DeviceSetNTPCfgServer `json:"ntpServer"`
|
||||
}
|
||||
|
||||
type DeviceSetNTPCfgServer struct {
|
||||
Id string `json:"id"`
|
||||
AddressingFormatType string `json:"addressingFormatType"`
|
||||
HostName string `json:"hostName"`
|
||||
IpAddress string `json:"ipAddress"`
|
||||
PortNo int `json:"portNo"`
|
||||
SynchronizeInterval int `json:"synchronizeInterval"`
|
||||
}
|
||||
|
||||
// 该接口用于配置设备NTP服务器参数
|
||||
func DeviceSetNTPCfg(access_token *string, req *DeviceSetNTPCfgReq) (*model.BaseOperationRes, error) {
|
||||
resp, err := hikvisionRequest(access_token, "POST", TIME_NTPCFG, req)
|
||||
func DeviceSetNTPCfg(deviceSerial string, ntpserver *model.DeviceSetNTPCfgServer) (*model.Hikvision, error) {
|
||||
|
||||
req := &struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
NtpServer model.DeviceSetNTPCfgServer `json:"ntpServer"`
|
||||
}{
|
||||
DeviceSerial: deviceSerial,
|
||||
NtpServer: *ntpserver,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", TIME_NTPCFG, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -396,19 +353,16 @@ func DeviceSetNTPCfg(access_token *string, req *DeviceSetNTPCfgReq) (*model.Base
|
|||
return result, nil
|
||||
}
|
||||
|
||||
type DeviceGetNTPServerRes struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data DeviceSetNTPCfgServer `json:"data"`
|
||||
}
|
||||
|
||||
// 该接口用于获取设备指定NTP服务器配置
|
||||
func DeviceGetNTPServerCfg(access_token *string, deviceserial string, ntpserverid int) (*DeviceSetNTPCfgServer, error) {
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s&ntpServerId=%d", TIM_NTPSERVER_CFG, deviceserial, ntpserverid), nil)
|
||||
func DeviceGetNTPServerCfg(deviceserial string, ntpserverid int) (*model.DeviceSetNTPCfgServer, error) {
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("%s?deviceSerial=%s&ntpServerId=%d", TIM_NTPSERVER_CFG, deviceserial, ntpserverid), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *DeviceGetNTPServerRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.DeviceSetNTPCfgServer `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -419,19 +373,13 @@ func DeviceGetNTPServerCfg(access_token *string, deviceserial string, ntpserveri
|
|||
return &result.Data, nil
|
||||
}
|
||||
|
||||
type DeviceSetNTPServerCfgReq struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
NtpServerID int `json:"ntpServerId"`
|
||||
NtpServer DeviceSetNTPCfgServer `json:"ntpServer"`
|
||||
}
|
||||
|
||||
// 该接口用于配置设备指定NTP服务器参数
|
||||
func DeviceSetNTPServerCfg(access_token *string, req DeviceSetNTPServerCfgReq) (*model.BaseOperationRes, error) {
|
||||
resp, err := hikvisionRequest(access_token, "POST", TIM_NTPSERVER_CFG, req)
|
||||
func DeviceSetNTPServerCfg(req model.DeviceSetNTPServerCfgReq) (*model.Hikvision, error) {
|
||||
resp, err := hikvisionRequest("POST", TIM_NTPSERVER_CFG, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
package cloud
|
||||
package iot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"myschools.me/suguo/hikvision/model"
|
||||
"myschools.me/suguo/hikvision/iot/model"
|
||||
)
|
||||
|
||||
/*
|
||||
|
|
@ -22,20 +22,24 @@ const (
|
|||
GROUP_CHILD_LIST = GROUP_BASE + "/actions/childrenList"
|
||||
)
|
||||
|
||||
type GroupAddReq struct {
|
||||
GroupName string `json:"groupName"` //组名称
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
ParentNo string `json:"parentNo"` //父节点组编号
|
||||
}
|
||||
|
||||
// 该接口用于通过编号来新增组。最多支持3000个组,最多支持5层嵌套。
|
||||
func GroupAdd(access_token *string, req *GroupAddReq) (*model.BaseOperationRes, error) {
|
||||
func GroupAdd(groupname, groupno, parentno string) (*model.Hikvision, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "POST", GROUP_ADD, req)
|
||||
req := &struct {
|
||||
GroupName string `json:"groupName"` //组名称
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
ParentNo string `json:"parentNo"` //父节点组编号
|
||||
}{
|
||||
GroupName: groupname,
|
||||
GroupNo: groupno,
|
||||
ParentNo: parentno,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", GROUP_ADD, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -46,19 +50,22 @@ func GroupAdd(access_token *string, req *GroupAddReq) (*model.BaseOperationRes,
|
|||
return result, nil
|
||||
}
|
||||
|
||||
type GroupUpdateReq struct {
|
||||
GroupName string `json:"groupName"` //组名称
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
}
|
||||
|
||||
// 该接口用于通过组编号来删除组。组下面挂有下级节点或者设备的不可以删除,需清空后进行删除。
|
||||
func GroupUpdate(access_token *string, req *GroupUpdateReq) (*model.BaseOperationRes, error) {
|
||||
func GroupUpdate(groupname, groupno string) (*model.Hikvision, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "POST", GROUP_UPDATE, req)
|
||||
req := &struct {
|
||||
GroupName string `json:"groupName"` //组名称
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
}{
|
||||
GroupName: groupname,
|
||||
GroupNo: groupno,
|
||||
}
|
||||
|
||||
resp, err := hikvisionRequest("POST", GROUP_UPDATE, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -69,13 +76,13 @@ func GroupUpdate(access_token *string, req *GroupUpdateReq) (*model.BaseOperatio
|
|||
}
|
||||
|
||||
// 该接口用于通过组编号来删除组。组下面挂有下级节点或者设备的不可以删除,需清空后进行删除。
|
||||
func GroupRemove(access_token *string, groupNo string) (*model.BaseOperationRes, error) {
|
||||
func GroupRemove(groupNo string) (*model.Hikvision, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "POST", fmt.Sprintf("%s?groupNo=%s", GROUP_REMOVE, groupNo), nil)
|
||||
resp, err := hikvisionRequest("POST", fmt.Sprintf("%s?groupNo=%s", GROUP_REMOVE, groupNo), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *model.BaseOperationRes
|
||||
var result *model.Hikvision
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -85,27 +92,17 @@ func GroupRemove(access_token *string, groupNo string) (*model.BaseOperationRes,
|
|||
return result, nil
|
||||
}
|
||||
|
||||
type GroupGetRes struct {
|
||||
Code int `json:"code"`
|
||||
Data Group `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
GroupId string `json:"groupId"` //组ID
|
||||
GroupName string `json:"groupName"` //组名称
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
ParentId string `json:"parentId"` //父ID
|
||||
}
|
||||
|
||||
// 该接口用于通过组编号获取组的详细信息
|
||||
func CloudGetGroup(access_token *string, groupNo string) (*Group, error) {
|
||||
func GroupDetail(groupNo string) (*model.Group, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("%s?groupNo=%s", GROUP_DETAIL, groupNo), nil)
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("%s?groupNo=%s", GROUP_DETAIL, groupNo), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *GroupGetRes
|
||||
var result = &struct {
|
||||
model.Hikvision
|
||||
Data model.Group `json:"data"`
|
||||
}{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -115,20 +112,14 @@ func CloudGetGroup(access_token *string, groupNo string) (*Group, error) {
|
|||
return &result.Data, nil
|
||||
}
|
||||
|
||||
type Groups struct {
|
||||
Code int `json:"code"`
|
||||
Data []Group `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// 该接口用于获取所有组。
|
||||
func GroupGetAll(access_token *string) ([]Group, error) {
|
||||
func GroupGetAll() ([]model.Group, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "GET", GROUP_LISTALL, nil)
|
||||
resp, err := hikvisionRequest("GET", GROUP_LISTALL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *Groups
|
||||
var result *model.Groups
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -139,13 +130,13 @@ func GroupGetAll(access_token *string) ([]Group, error) {
|
|||
}
|
||||
|
||||
// 该接口用于通过组编号查找某组的下级节点。如果父节点为空,则查询根节点。
|
||||
func GroupChildList(access_token *string, groupNo string) ([]Group, error) {
|
||||
func GroupChildList(groupNo string) ([]model.Group, error) {
|
||||
|
||||
resp, err := hikvisionRequest(access_token, "GET", fmt.Sprintf("%s?parentNo=%s", GROUP_CHILD_LIST, groupNo), nil)
|
||||
resp, err := hikvisionRequest("GET", fmt.Sprintf("%s?parentNo=%s", GROUP_CHILD_LIST, groupNo), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *Groups
|
||||
var result *model.Groups
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package cloud
|
||||
package iot
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -25,6 +26,13 @@ func init() {
|
|||
if baseURL == "" {
|
||||
baseURL = "https://api2.hik-cloud.com/oauth"
|
||||
}
|
||||
// clientID = os.Getenv("HIK_IOT_CLIENTID")
|
||||
// clientSecret = os.Getenv("HIK_IOT_CLIENTSECRET")
|
||||
// scope = os.Getenv("HIK_IOT_SCOPE")
|
||||
|
||||
clientID = "164edfd71b744dd88c945dbd5ae30a9a"
|
||||
clientSecret = "2f580d932d7d46029a2091ece681035c"
|
||||
scope = "yy"
|
||||
}
|
||||
|
||||
// 基础云眸http请求
|
||||
|
|
@ -43,7 +51,7 @@ func hikvisionRequest(method string, url string, body interface{}) ([]byte, erro
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("authorization", fmt.Sprintf(" bearer %s", token))
|
||||
req.Header.Add("authorization", fmt.Sprintf(" bearer %s", *token))
|
||||
|
||||
if strings.ToLower(method) == "post" {
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
|
@ -77,17 +85,55 @@ type OauthResponse struct {
|
|||
// 客户端认证获取access_token
|
||||
func hikvisionOauth() (*string, error) {
|
||||
|
||||
if expired.Before(time.Now()) {
|
||||
if expired.After(time.Now()) {
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Add("client_id", clientID)
|
||||
params.Add("client_secret", clientSecret)
|
||||
params.Add("grant_type", "client_credentials")
|
||||
params.Add("scope", scope)
|
||||
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
|
||||
grant_type string
|
||||
scope string
|
||||
}{
|
||||
client_id: clientID,
|
||||
client_secret: clientSecret,
|
||||
grant_type: "client_credentials",
|
||||
scope: scope,
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf(`%s/token`, baseURL), strings.NewReader(string(params.Encode())))
|
||||
resp, err := hikvisionRequestUrlencoded("POST", fmt.Sprintf(`%s/token`, baseURL), params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result *OauthResponse
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token = result.AccessToken
|
||||
expired = time.Now().Add(time.Duration(result.ExpiresIn) * time.Second)
|
||||
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
// 客户端认证获取access_token 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()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -117,13 +163,40 @@ func hikvisionOauth() (*string, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *OauthResponse
|
||||
if err := json.Unmarshal(respBody, &result); err != nil {
|
||||
return nil, err
|
||||
|
||||
return respBody, nil
|
||||
}
|
||||
|
||||
// 解析结构体数据,写入到url.values中
|
||||
func hikvisioninspectStruct(u interface{}) (url.Values, error) {
|
||||
values := url.Values{}
|
||||
|
||||
typev := reflect.TypeOf(u)
|
||||
|
||||
if typev.Kind() != reflect.Struct {
|
||||
return nil, errors.New("typev.Kind() is not a reflect.Struct")
|
||||
}
|
||||
|
||||
token = result.AccessToken
|
||||
expired = time.Now().Add(time.Duration(result.ExpiresIn) * time.Second)
|
||||
|
||||
return &token, nil
|
||||
for i := 0; i < typev.NumField(); i++ {
|
||||
v := reflect.ValueOf(u)
|
||||
field := v.FieldByName(typev.Field(i).Name)
|
||||
// typev := reflect.TypeOf(u).FieldByName()
|
||||
switch field.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
values.Add(typev.Field(i).Name, fmt.Sprintf("%d", field.Int()))
|
||||
// fmt.Printf("field:%s type:%s value:%d\n", typev.Field(i).Name, field.Type().Name(), field.Int())
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
values.Add(typev.Field(i).Name, fmt.Sprintf("%d", field.Uint()))
|
||||
// fmt.Printf("field:%s type:%s value:%d\n", typev.Field(i).Name, field.Type().Name(), field.Uint())
|
||||
case reflect.Bool:
|
||||
values.Add(typev.Field(i).Name, fmt.Sprintf("%v", field.Bool()))
|
||||
// fmt.Printf("field:%s type:%s value:%t\n", typev.Field(i).Name, field.Type().Name(), field.Bool())
|
||||
case reflect.String:
|
||||
values.Add(typev.Field(i).Name, field.String())
|
||||
// fmt.Printf("field:%s type:%s value:%q\n", typev.Field(i).Name, field.Type().Name(), field.String())
|
||||
// default:
|
||||
// fmt.Printf("field:%s unhandled kind:%s\n", typev.Field(i).Name, field.Kind())
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package model
|
||||
|
||||
type DeviceData struct {
|
||||
DeviceId string `json:"deviceId"` //设备id
|
||||
DeviceName string `json:"deviceName"` //型号
|
||||
DeviceModel string `json:"deviceModel"` //设备名称
|
||||
DeviceSerial string `json:"deviceSerial"` //设备序列号
|
||||
DeviceStatus int `json:"deviceStatus"` //0-离线,1-在线
|
||||
GroupId string `json:"groupId"` //组id
|
||||
IsEncrypt int `json:"isEncrypt"` //设备加密状态 0-关闭 1-开启
|
||||
|
||||
// 当使用设备接入详情接口获取的信息才会拥有版本
|
||||
DeviceVersion string `json:"deviceVersion"`
|
||||
}
|
||||
|
||||
type DeivceListData struct {
|
||||
PageNo int `json:"pageNo"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Total int `json:"total"`
|
||||
Rows []DeviceData `json:"rows"`
|
||||
}
|
||||
|
||||
type DeivceStatusData struct {
|
||||
PrivacyStatus int `json:"privacyStatus"`
|
||||
PirStatus int `json:"pirStatus"`
|
||||
AlarmSoundMode int `json:"alarmSoundMode"`
|
||||
BattryStatus int `json:"battryStatus"`
|
||||
LockSignal int `json:"lockSignal"`
|
||||
DiskNum int `json:"diskNum"`
|
||||
DiskState string `json:"diskState"`
|
||||
CloudStatus int `json:"cloudStatus"`
|
||||
NvrDiskNum int `json:"nvrDiskNum"`
|
||||
NvrDiskState string `json:"nvrDiskState"`
|
||||
}
|
||||
|
||||
type DeviceSetNTPCfgServer struct {
|
||||
Id string `json:"id"`
|
||||
AddressingFormatType string `json:"addressingFormatType"`
|
||||
HostName string `json:"hostName"`
|
||||
IpAddress string `json:"ipAddress"`
|
||||
PortNo int `json:"portNo"`
|
||||
SynchronizeInterval int `json:"synchronizeInterval"`
|
||||
}
|
||||
|
||||
type DeviceSetNTPServerCfgReq struct {
|
||||
DeviceSerial string `json:"deviceSerial"`
|
||||
NtpServerID int `json:"ntpServerId"`
|
||||
NtpServer DeviceSetNTPCfgServer `json:"ntpServer"`
|
||||
}
|
||||
|
|
@ -4,3 +4,10 @@ type Groups struct {
|
|||
Hikvision
|
||||
Data []Group `json:"data"`
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
GroupId string `json:"groupId"` //组ID
|
||||
GroupName string `json:"groupName"` //组名称
|
||||
GroupNo string `json:"groupNo"` //组编号
|
||||
ParentId string `json:"parentId"` //父ID
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package cloud
|
||||
package iot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -7,12 +7,12 @@ import (
|
|||
)
|
||||
|
||||
func TestOauth(t *testing.T) {
|
||||
serial := "AF8534579"
|
||||
accesstoken := "a08a8044-3770-41f5-b5a5-048004a723ac"
|
||||
result, err := DeivceReboot(&accesstoken, serial)
|
||||
// serial := "AF8534579"
|
||||
// accesstoken := "a08a8044-3770-41f5-b5a5-048004a723ac"
|
||||
result, err := DeivceList("02", 1, 20)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(result)
|
||||
fmt.Println(*result)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue