门禁计划,80%

This commit is contained in:
wyh 2023-11-22 13:16:04 +08:00
parent 3fd6f52a1e
commit 8bee9bb982
4 changed files with 582 additions and 97 deletions

View File

@ -16,7 +16,9 @@ const (
// 新增卡片
func CardCreate(cards []model.CardCreateReq) (*model.Hikvision, error) {
req := &struct{ Cards []model.CardCreateReq }{
req := &struct {
Cards []model.CardCreateReq `json:"cards"`
}{
Cards: cards,
}

66
iot/model/plan-model.go Normal file
View File

@ -0,0 +1,66 @@
package model
type PlanWeekRow struct {
CustomPlanId string `json:"customPlanId,omitempty"`
Enable bool `json:"enable"`
WeekPlan []PlanWeek `json:"weekPlan"`
}
type PlanWeek struct {
Week string `json:"week"` //标准星期格式首字母大写Monday-星期一
TimeSegment []PlanPort `json:"timeSegment"`
}
type PlanPort struct {
Index int `json:"index"` //时间段编号从1开始范围1-8
Enable bool `json:"enable"` // 是否使能true-使能false-不使能
BeginTime string `json:"beginTime"` // 时间格式为 08:00:00
EndTime string `json:"endTime"` // 时间格式为 08:00:00
}
type PlanWeekDetail struct {
PageNo int `json:"pageNo"`
PageSize int `json:"pageSize"`
Total int `json:"total"`
Rows []PlanWeekRow `json:"rows"`
}
type PlanHolidayDetail struct {
PageNo int `json:"pageNo"`
PageSize int `json:"pageSize"`
Total int `json:"total"`
Rows []PlanHolidayRow `json:"rows"`
}
type PlanHolidayRow struct {
CustomPlanId string `json:"customPlanId,omitempty"`
Enable bool `json:"enable"`
BeginDate string `json:"beginDate"` // 时间格式为 2019-08-09
EndDate string `json:"endDate"` // 时间格式为 2019-08-09
HolidayPlan []PlanPort `json:"holidayPlan"`
}
type PlanGroupConfig struct {
Enable bool `json:"enable"`
GroupName string `json:"groupName"`
HolidayPlanNo string `json:"holidayPlanNo"`
}
type PlanChild struct {
PlanNo int `json:"planNo"`
CustomPlanId string `json:"customPlanId"`
}
type PlanHolidayGroup struct {
GroupNo int `json:"groupNo"`
Enable bool `json:"enable"`
GroupName string `json:"groupName"`
HolidayPlanList []PlanChild `json:"holidayPlanList"`
}
type PlanGlobalConfig struct {
Enable bool `json:"enable"`
TemplateName string `json:"templateName"`
WeekPlanNo int `json:"weekPlanNo"`
HolidayGroupNo string `json:"holidayGroupNo"`
}

View File

@ -1,111 +1,88 @@
package iot
import (
"fmt"
"log"
"testing"
)
func TestOauth(t *testing.T) {
// g1, err := GroupGetAll()
// if err != nil {
// log.Println(err)
// return
// }
// fmt.Println(g1)
data, err := GroupDetail("02")
if err != nil {
log.Println(err)
return
}
fmt.Println(data)
// data, err := DeviceRegister("AF8534579", "02", "213222")
// if err != nil {
// log.Println(err)
// return
// }
// fmt.Println(data)
// result, err := DeviceSetNTPCfg("AF8534579", &model.DeviceSetNTPCfgServer{
// Id: "1",
// AddressingFormatType: "hostname",
// HostName: "time.ys7.com",
// IpAddress: "",
// PortNo: 123,
// SynchronizeInterval: 60,
// })
// if err != nil {
// log.Println(err)
// return
// }
// fmt.Println(*result)
// q, err := DeviceSetNTPServerCfg(&model.DeviceSetNTPServerCfgReq{
// DeviceSerial: "AF8534579",
// NtpServerID: 1,
// NtpServer: model.DeviceSetNTPCfgServer{
// Id: "1",
// AddressingFormatType: "hostname",
// HostName: "time.ys7.com",
// IpAddress: "",
// PortNo: 123,
// SynchronizeInterval: 60,
// },
// })
// if err != nil {
// log.Println(err)
// return
// }
// res, err := DeviceGetNTPServerCfg("AF8534579", 1)
// fmt.Println(res)
// fmt.Println(q)
}
/*
// result, err := PLANWEEKCreateOrUpdate("222", true, []model.PlanWeek{
// {
// Week: "Monday",
// TimeSegment: []model.PlanPort{
// {
// Index: 1,
// Enable: true,
// BeginTime: "08:00:00",
// EndTime: "10:30:00",
// },
// },
// },
// })
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(result)
result, err := CardCreate([]model.CardCreateReq{{
CardNo: "004",
CardType: "normalCard",
EmployeeNo: "02",
}, {
CardNo: "005",
CardType: "normalCard",
EmployeeNo: "02",
}})
if err != nil {
log.Fatal(err.Error())
}
// r1, err := PLANWEEKGetAll(1, 20)
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(r1)
fmt.Println("person ", result)
// r2, err := PLANWEEKDelete("111")
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(r2)
imgfile, err := os.ReadFile("../image.jpg")
if err != nil {
log.Fatal(err.Error())
}
imgbase64 := base64.StdEncoding.EncodeToString(imgfile)
p, err := PersonDetail("02")
if err != nil {
log.Fatal(err)
}
fmt.Println(p)
person, err := PersonFaceUpdate("02", imgbase64, true)
if err != nil {
log.Fatal(err.Error())
}
p, err = PersonDetail("02")
if err != nil {
log.Fatal(err)
}
r, err := PersonFaceDelete("02")
if err != nil {
log.Fatal(err)
}
fmt.Println(r)
fmt.Println(p)
fmt.Println(person)
// r3, err := PLANWEEKGetAll(1, 20)
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(r3)
*/
/*
// result, err := PLANHolidayCreateOrUpdate("555", true, "2023-11-22", "2023-11-25", []model.PlanPort{
// {
// Index: 1,
// Enable: true,
// BeginTime: "08:00:00",
// EndTime: "10:30:00",
// },
// })
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(result)
// r1, err := PLANHolidayGetAll(1, 20)
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(r1)
// r2, err := PLANHolidayDelete("555")
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(r2)
// r3, err := PLANHolidayGetAll(1, 20)
// if err != nil {
// log.Fatal(err)
// return
// }
// fmt.Println(r3)
*/

440
iot/plan-iot.go Normal file
View File

@ -0,0 +1,440 @@
package iot
import (
"encoding/json"
"fmt"
"myschools.me/suguo/hikvision/iot/model"
)
const (
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
}