444 lines
14 KiB
Go
444 lines
14 KiB
Go
package cloud
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
|
||
"myschools.me/suguo/hikvision/model"
|
||
)
|
||
|
||
/*
|
||
基础服务能力 > 设备管理 > 设备接入管理
|
||
*/
|
||
|
||
const (
|
||
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"
|
||
|
||
// 设备校时配置
|
||
TIME_BASE = "https://api2.hik-cloud.com/api/v1/device/isapi/system/time"
|
||
TIME_NTPCFG = TIME_BASE + "/ntpServers"
|
||
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 := BaseCloudHttpRequest(access_token, "POST", ACCESS_REGISTER, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeviceRegisterRes
|
||
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, nil
|
||
}
|
||
|
||
// 从某一分组内删除设备
|
||
func DeivceDelete(access_token *string, deviceSerial string) (*model.BaseOperationRes, error) {
|
||
resp, err := BaseCloudHttpRequest(access_token, "POST", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_DELETE, deviceSerial), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *model.BaseOperationRes
|
||
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, nil
|
||
}
|
||
|
||
type DeivceUpdateReq struct {
|
||
DeviceSerial string `json:"deviceSerial"` //设备序列号
|
||
DeviceName string `json:"deviceName"` //设备名称
|
||
}
|
||
|
||
// 该接口用于修改设备名称
|
||
func DeivceUpdate(access_token *string, req *DeivceUpdateReq) (*model.BaseOperationRes, error) {
|
||
resp, err := BaseCloudHttpRequest(access_token, "POST", ACCESS_UPDATE, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *model.BaseOperationRes
|
||
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, nil
|
||
}
|
||
|
||
// 该接口用于根据设备序列号获取单个设备详细信息
|
||
func DeivceDetail(access_token *string, deviceSerial string) (*DeviceData, error) {
|
||
resp, err := BaseCloudHttpRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_DETAIL, deviceSerial), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeviceRegisterRes
|
||
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
|
||
}
|
||
|
||
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 := BaseCloudHttpRequest(access_token, "GET", url, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeivceListRes
|
||
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
|
||
}
|
||
|
||
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) {
|
||
|
||
resp, err := BaseCloudHttpRequest(access_token, "GET", ACCESS_ACCOUNT, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeivceNumRes
|
||
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.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) {
|
||
|
||
resp, err := BaseCloudHttpRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", ACCESS_STATUS, deviceserial), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeivceStatusReq
|
||
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
|
||
}
|
||
|
||
// 该接口用于重启设备,请谨慎操作
|
||
func DeivceReboot(access_token *string, deviceserial string) (*model.BaseOperationRes, error) {
|
||
params := &struct {
|
||
DeviceSerial string `json:"deviceSerial"`
|
||
}{
|
||
DeviceSerial: deviceserial,
|
||
}
|
||
|
||
resp, err := BaseCloudHttpRequest(access_token, "POST", ACCESS_REBOOT, params)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *model.BaseOperationRes
|
||
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, nil
|
||
}
|
||
|
||
/*
|
||
确权流程说明
|
||
设备接入政策进行了如下调整:
|
||
(1)设备首次激活,需要在规定的时间(72小时)内上线平台,否则,上线时需要走确权流程;
|
||
(2)设备重新注册平台,删除设备并重新添加设备超过规定时间(72小时)需要走确权流程;
|
||
|
||
什么时候需要进行确权
|
||
注册设备接口返回内容code=60058时,需要进行设备确权操作。
|
||
*/
|
||
|
||
// 下线确认
|
||
func DeivceOffline(access_token *string, deviceserial string) (*model.BaseOperationRes, error) {
|
||
|
||
resp, err := BaseCloudHttpRequest(access_token, "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
|
||
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, nil
|
||
}
|
||
|
||
// 上线线确认
|
||
func DeivceOnline(access_token *string, deviceserial string) (*model.BaseOperationRes, error) {
|
||
|
||
resp, err := BaseCloudHttpRequest(access_token, "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
|
||
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, 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 := BaseCloudHttpRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", TIME_BASE, deviceserial), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeviceGetTimeModeRes
|
||
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
|
||
}
|
||
|
||
type DeviceSetTimeModeReq struct {
|
||
DeviceSerial string `json:"deviceSerial"`
|
||
TimeMode string `json:"timeMode"`
|
||
}
|
||
|
||
// 该接口用于配置设备校时模式
|
||
func DeviceSetTimeMode(access_token *string, req *DeviceSetTimeModeReq) (*model.BaseOperationRes, error) {
|
||
resp, err := BaseCloudHttpRequest(access_token, "POST", TIME_BASE, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *model.BaseOperationRes
|
||
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, nil
|
||
}
|
||
|
||
// 该接口用于获取设备当前NTP服务器配置
|
||
func DeviceGetNTPCfg(access_token *string, deviceserial string) (*DeviceSetNTPCfgServer, error) {
|
||
resp, err := BaseCloudHttpRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s", TIME_NTPCFG, deviceserial), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeviceGetNTPServerRes
|
||
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
|
||
}
|
||
|
||
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 := BaseCloudHttpRequest(access_token, "POST", TIME_NTPCFG, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *model.BaseOperationRes
|
||
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, 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 := BaseCloudHttpRequest(access_token, "GET", fmt.Sprintf("%s?deviceSerial=%s&ntpServerId=%d", TIM_NTPSERVER_CFG, deviceserial, ntpserverid), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *DeviceGetNTPServerRes
|
||
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
|
||
}
|
||
|
||
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 := BaseCloudHttpRequest(access_token, "POST", TIM_NTPSERVER_CFG, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
var result *model.BaseOperationRes
|
||
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, nil
|
||
}
|