hikvision/iot/plan-iot.go

441 lines
13 KiB
Go

package iot
import (
"encoding/json"
"fmt"
"myschools.me/suguo/hikvision/iot/model"
)
var (
plan_base = "https://api2.hik-cloud.com/api/v1/open/access/plans"
plan_week = plan_base + "/weeks"
plan_week_cfg = plan_week + "/config"
plan_holiday = plan_base + "/holidays"
plan_holiday_cfg = plan_holiday + "/config"
plan_holiday_groupcfg = plan_holiday + "/groups/config"
plan_template = plan_base + "/templates"
plan_template_cfg = plan_template + "/config"
plan_template_cfgpol = plan_template + "/config/polymerization"
)
// 周计划模板添加/更新 customPlanId不存在 新增,存在更新
func PLANWEEKTempleteCreateOrUpdate(customplanid string, enable bool, weekplan []model.PlanWeek) (*model.Hikvision, error) {
req := &model.PlanWeekRow{
CustomPlanId: customplanid,
Enable: enable,
WeekPlan: weekplan,
}
resp, err := hikvisionRequest("POST", plan_week, req)
if err != nil {
return nil, err
}
var result *model.Hikvision
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 PLANWEEKTempleteGetAll(pageNo, pageSize int) (*model.PlanWeekDetail, error) {
url := fmt.Sprintf("%s?pageNo=%d&pageSize=%d", plan_week, pageNo, pageSize)
resp, err := hikvisionRequest("GET", url, nil)
if err != nil {
return nil, err
}
var result = &struct {
model.Hikvision
Data model.PlanWeekDetail `json:"data"`
}{}
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 PLANWEEKTempleteDelete(customplanid string) (*model.Hikvision, error) {
url := fmt.Sprintf("%s?customPlanId=%s", plan_week, customplanid)
resp, err := hikvisionRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
var result = &model.Hikvision{}
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
}
// 假日计划模板添加/更新 时间格式为 2019-08-09
func PLANHolidayTempleteCreateOrUpdate(customplanid string, enable bool, begindate, enddate string, plan []model.PlanPort) (*model.Hikvision, error) {
req := &model.PlanHolidayRow{
CustomPlanId: customplanid,
Enable: enable,
BeginDate: begindate,
EndDate: enddate,
HolidayPlan: plan,
}
resp, err := hikvisionRequest("POST", plan_holiday, req)
if err != nil {
return nil, err
}
var result *model.Hikvision
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 PLANHolidayTempleteGetAll(pageNo, pageSize int) (*model.PlanHolidayDetail, error) {
url := fmt.Sprintf("%s?pageNo=%d&pageSize=%d", plan_holiday, pageNo, pageSize)
resp, err := hikvisionRequest("GET", url, nil)
if err != nil {
return nil, err
}
var result = &struct {
model.Hikvision
Data model.PlanHolidayDetail `json:"data"`
}{}
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 PLANHolidayTempleteDelete(customplanid string) (*model.Hikvision, error) {
url := fmt.Sprintf("%s?customPlanId=%s", plan_holiday, customplanid)
resp, err := hikvisionRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
var result = &model.Hikvision{}
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
}
// 周计划配置
//
// 配置设备的周计划 configType:userRight-人员权限
func PlanWeekConfigSet(configtype, deviceid string, planno int, customplanid string) (*model.Hikvision, error) {
req := &struct {
ConfigType string `json:"configType"`
DeviceId string `json:"deviceId"`
PlanNo int `json:"planNo"`
CustomPlanId string `json:"customPlanId"`
}{
ConfigType: configtype,
DeviceId: deviceid,
PlanNo: planno,
CustomPlanId: customplanid,
}
resp, err := hikvisionRequest("POST", plan_week_cfg, req)
if err != nil {
return nil, err
}
var result *model.Hikvision
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 PlanWeekConfigGet(configtype, deviceid string, planno int) (*model.PlanWeekRow, error) {
url := fmt.Sprintf("%s?configType=%s&deviceId=%s&planNo=%d", plan_week_cfg, configtype, deviceid, planno)
resp, err := hikvisionRequest("GET", url, nil)
if err != nil {
return nil, err
}
var result = &struct {
model.Hikvision
Data model.PlanWeekRow `json:"data"`
}{}
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
}
// 假日计划配置
//
// 配置设备的假日计划 configtype: userRight-人员权限
func PlanHolidayConfigSet(configtype, deviceid string, planno int, customplanid string) (*model.Hikvision, error) {
req := &struct {
ConfigType string `json:"configType"`
DeviceId string `json:"deviceId"`
PlanNo int `json:"planNo"`
CustomPlanId string `json:"customPlanId"`
}{
ConfigType: configtype,
DeviceId: deviceid,
PlanNo: planno,
CustomPlanId: customplanid,
}
resp, err := hikvisionRequest("POST", plan_holiday_cfg, req)
if err != nil {
return nil, err
}
var result *model.Hikvision
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 PlanHolidayConfigGet(configtype, deviceid string, planno int) (*model.PlanHolidayRow, error) {
url := fmt.Sprintf("%s?configType=%s&deviceId=%s&planNo=%d", plan_holiday_cfg, configtype, deviceid, planno)
resp, err := hikvisionRequest("GET", url, nil)
if err != nil {
return nil, err
}
var result = &struct {
model.Hikvision
Data model.PlanHolidayRow `json:"data"`
}{}
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 PlanHolidayGroupConfigSet(configtype, deviceid string, groupno int, enable bool, groupname, holidayplanno string) (*model.Hikvision, error) {
req := &struct {
ConfigType string `json:"configType"`
DeviceId string `json:"deviceId"`
GroupNo int `json:"groupNo"`
Enable bool `json:"enable"`
GroupName string `json:"groupName"`
HolidayPlanNo string `json:"holidayPlanNo"`
}{
ConfigType: configtype,
DeviceId: deviceid,
GroupNo: groupno,
Enable: enable,
GroupName: groupname,
HolidayPlanNo: holidayplanno,
}
resp, err := hikvisionRequest("POST", plan_holiday_groupcfg, req)
if err != nil {
return nil, err
}
var result *model.Hikvision
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 PlanHolidayGroupConfigGet(configtype, deviceid string, groupno int) (*model.PlanGroupConfig, error) {
url := fmt.Sprintf("%s?configType=%s&deviceId=%s&groupNo=%d", plan_holiday_cfg, configtype, deviceid, groupno)
resp, err := hikvisionRequest("GET", url, nil)
if err != nil {
return nil, err
}
var result = &struct {
model.Hikvision
Data model.PlanGroupConfig `json:"data"`
}{}
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 PlanGlobalConfig(configtype, deviceid string, templateno int, enable bool, templetename string, weekplanno int, holidayplanno string) (*model.Hikvision, error) {
req := &struct {
ConfigType string `json:"configType"`
DeviceId string `json:"deviceId"`
TemplateNo int `json:"templateNo"`
Enable bool `json:"enable"`
TemplateName string `json:"templateName"`
WeekPlanNo int `json:"weekPlanNo"`
HolidayGroupNo string `json:"holidayGroupNo"`
}{
ConfigType: configtype,
DeviceId: deviceid,
TemplateNo: templateno,
Enable: enable,
TemplateName: templetename,
WeekPlanNo: weekplanno,
HolidayGroupNo: holidayplanno,
}
resp, err := hikvisionRequest("POST", plan_template_cfg, req)
if err != nil {
return nil, err
}
var result *model.Hikvision
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 PlanGlobalPolymerizationConfig(configtype, deviceid string, templateno int, enable bool, templetename string, weekplanno model.PlanChild, holidayplanno []model.PlanHolidayGroup) (*model.Hikvision, error) {
req := &struct {
ConfigType string `json:"configType"`
DeviceId string `json:"deviceId"`
TemplateNo int `json:"templateNo"`
Enable bool `json:"enable"`
TemplateName string `json:"templateName"`
WeekPlan model.PlanChild `json:"weekPlan"`
HolidayPlanGroup []model.PlanHolidayGroup `json:"holidayPlanGroup"`
}{
ConfigType: configtype,
DeviceId: deviceid,
TemplateNo: templateno,
Enable: enable,
TemplateName: templetename,
WeekPlan: weekplanno,
HolidayPlanGroup: holidayplanno,
}
resp, err := hikvisionRequest("POST", plan_template_cfgpol, req)
if err != nil {
return nil, err
}
var result *model.Hikvision
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 PlanGlobalConfigGet(configtype, deviceid string, templateno int) (*model.PlanGlobalConfig, error) {
url := fmt.Sprintf("%s?configType=%s&deviceId=%s&templateNo=%d", plan_template_cfg, configtype, deviceid, templateno)
resp, err := hikvisionRequest("GET", url, nil)
if err != nil {
return nil, err
}
var result = &struct {
model.Hikvision
Data model.PlanGlobalConfig `json:"data"`
}{}
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 PlanGlobalConfigDelete(configtype, deviceid string, templateno int) (*model.PlanGlobalConfig, error) {
url := fmt.Sprintf("%s?configType=%s&deviceId=%s&templateNo=%d", plan_template_cfg, configtype, deviceid, templateno)
resp, err := hikvisionRequest("GET", url, nil)
if err != nil {
return nil, err
}
var result = &struct {
model.Hikvision
Data model.PlanGlobalConfig `json:"data"`
}{}
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
}