From 8bee9bb982473fdb2b01e3a648e76b472b873cd0 Mon Sep 17 00:00:00 2001 From: wyh Date: Wed, 22 Nov 2023 13:16:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=A8=E7=A6=81=E8=AE=A1=E5=88=92=EF=BC=8C80?= =?UTF-8?q?%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iot/card-model.go | 4 +- iot/model/plan-model.go | 66 ++++++ iot/oauth_test.go | 169 +++++++-------- iot/plan-iot.go | 440 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 582 insertions(+), 97 deletions(-) create mode 100644 iot/model/plan-model.go create mode 100644 iot/plan-iot.go diff --git a/iot/card-model.go b/iot/card-model.go index cf1d663..4dec57e 100644 --- a/iot/card-model.go +++ b/iot/card-model.go @@ -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, } diff --git a/iot/model/plan-model.go b/iot/model/plan-model.go new file mode 100644 index 0000000..b3845ef --- /dev/null +++ b/iot/model/plan-model.go @@ -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"` +} diff --git a/iot/oauth_test.go b/iot/oauth_test.go index 1d2dd92..8c6e2ad 100644 --- a/iot/oauth_test.go +++ b/iot/oauth_test.go @@ -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) */ diff --git a/iot/plan-iot.go b/iot/plan-iot.go new file mode 100644 index 0000000..72d9b5e --- /dev/null +++ b/iot/plan-iot.go @@ -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 +}