海康ISAPI对接门禁设备 实现用户周计划权限
海康ISAPI对接门禁设备 删除用户信息
海康ISAPI对接门禁设备 添加用户信息和下发人脸
本文主要是记录一下对接的部分信息。
通过周计划模板来控制用户刷脸的有效时间段。
一周七天一天最多支持八个时间段。
例如:周一 8:00~10:00 13:00~16:00 19:00~21:00
周二 8:00~10:30 13:00~16:30 19:00~21:30
…
周末 8:00~10:30 13:00~16:30 19:00~21:30
每周在指定时间段内刷脸是有效的,其他时间段内无效。
比如:配合闸机可以控制人员在指定时间段进出。
本项目采用的队列进行异步执行。
//通过设备能力集查询设备支持 周模板个数
private static final int WEEK_SIZE = 128;
//通过设备能力集查询设备支持计划个数
private static final int PLAN_TEMPLATE_SIZE = 255;
IotForbidDayTypeEnum 权限类型
/**
* 权限类型
*/
@Getter
@AllArgsConstructor
public enum IotForbidDayTypeEnum {
/**
* 不限制权限
*/
EVERY_DAY("1", "不限制权限"),
/**
* 禁止权限
*/
NO_EVERY_DAY("2", "禁止权限"),
/**
* 工作日禁止权限
*/
NO_WORKING_DAY("3", "工作日禁止权限"),
/**
* 周末禁止权限
*/
NO_WEEKEND("4", "周末禁止权限"),
/**
* 周权限设置
*/
WEEKLY_LIMIT_SETTING("6", "周权限设置"),
/**
* 天权限设置
*/
DAY_PERMISSION_SETTINGS("5", "天权限设置"),
;
private String code;
private String message;
public static String getDayTypeMessage(String code) {
return Arrays.stream(IotForbidDayTypeEnum.values())
.filter(response -> Objects.equals(code, response.getCode()))
.findFirst()
.map(IotForbidDayTypeEnum::getMessage)
.orElse(StrUtil.EMPTY);
}
}
// 根据不同权限执行, 设备中是存放的用户唯一标识是用户id。
/**
* 执行逻辑
* 此处代码将不需要已经删除。
*/
public void send(IotCommonMsgBodyParam msgBody) {
// 设备信息
IotCameraParam camera = JSONUtil.toBean(msgBody.getAccount(), IotCameraParam.class);
try {
// 权限参数
IotAuthTypeParam iotAuthTypeParam = JSONUtil.toBean(msgBody.getBody(), IotAuthTypeParam.class);
if (ObjectUtil.isNull(iotAuthTypeParam)) {
bodyResult = msgBody.toResult(500, "时间参数为空");
return;
}
List<Long> userInfos = iotAuthTypeParam.getUserIds();
if (CollUtil.isEmpty(userInfos)) {
bodyResult = msgBody.toResult(500, "用户信息获取失败");
return;
}
// 计划模板id
String planTemplateId;
if (IotForbidDayTypeEnum.WEEKLY_LIMIT_SETTING.getCode().equals(iotAuthTypeParam.getAuthType())) {
IotAuthWeekDataParam authWeekDataParam = JSONUtil.toBean(JSONUtil.toJsonStr(iotAuthTypeParam.getData()), IotAuthWeekDataParam.class);
log.info("AuthTypeData:{}", JSONUtil.toJsonStr(authWeekDataParam));
List<IotWeekInfoParam> weekDateList = authWeekDataParam.getDateList();
if (CollUtil.isEmpty(weekDateList)) {
msgBody.toResult(500, "时间参数为空");
return;
}
boolean checkDateSum = checkDateSum(weekDateList);
if (checkDateSum) {
bodyResult = msgBody.toResult(500, "每天最多只能设置8个时间段");
return;
}
// 获取模板id
IotUserRightWeekPlanCfgParam weekPlanCfgParam = this.analysisAuth(weekDateList);
planTemplateId = getTemplatAuth(camera, weekPlanCfgParam);
if (ObjectUtil.isNull(planTemplateId)) {
bodyResult = msgBody.toResult(500, "权限添加失败");
return;
}
} else if (IotForbidDayTypeEnum.EVERY_DAY.getCode().equals(iotAuthTypeParam.getAuthType())) {
planTemplateId = "1";
} else if (IotForbidDayTypeEnum.NO_EVERY_DAY.getCode().equals(iotAuthTypeParam.getAuthType())) {
// 获取模板id
IotUserRightWeekPlanCfgParam weekPlanCfgParam = this.prohibitPermissions();
planTemplateId = getTemplatAuth(camera, weekPlanCfgParam);
if (ObjectUtil.isNull(planTemplateId)) {
bodyResult = msgBody.toResult(500, "权限添加失败");
return;
}
} else if (IotForbidDayTypeEnum.NO_WORKING_DAY.getCode().equals(iotAuthTypeParam.getAuthType())) {
// 获取模板id
IotUserRightWeekPlanCfgParam weekPlanCfgParam = this.analysisProhibitWorkingDays();
planTemplateId = getTemplatAuth(camera, weekPlanCfgParam);
if (ObjectUtil.isNull(planTemplateId)) {
bodyResult = msgBody.toResult(500, "权限添加失败");
return;
}
} else if (IotForbidDayTypeEnum.NO_WEEKEND.getCode().equals(iotAuthTypeParam.getAuthType())) {
// 获取模板id
IotUserRightWeekPlanCfgParam weekPlanCfgParam = this.analysisProhibitedWeekends();
planTemplateId = getTemplatAuth(camera, weekPlanCfgParam);
if (ObjectUtil.isNull(planTemplateId)) {
bodyResult = msgBody.toResult(500, "权限添加失败");
return;
}
}
// 执行修改用户权限
List<IotCommDataResult> commDataResults = new ArrayList<>(); // 存放返回结果
for (Long userId : userInfos) {
IotCommDataResult commDataResult = hkIsapiService.setUpUserAuth(camera, userId, planTemplateId);
commDataResults.add(commDataResult);
ThreadUtil.safeSleep(200);
}
} catch (Exception e) {
bodyResult = msgBody.toResult(500, "指令下发失败:" + e.getMessage());
log.error("Arcsoft authType msg: {},e", JSONUtil.toJsonStr(msgBody), e);
}
}
/**
* 校验
* 一天时间段是否超过八个
*/
private boolean checkDateSum(List<IotWeekInfoParam> weekDateList) {
Map<Integer, List<IotWeekInfoParam>> weekMap = weekDateList.stream().collect(Collectors.groupingBy(IotWeekInfoParam::getWeekNum));
for (Map.Entry<Integer, List<IotWeekInfoParam>> map : weekMap.entrySet()) {
if (map.getValue().size() > 8) {
return true;
}
}
return false;
}
/**
* 计划模板
* 获取模板id
*/
private String getTemplatAuth(IotCameraParam camera, IotUserRightWeekPlanCfgParam weekPlanCfgParam) {
try {
String planTemplateId = String.valueOf(2);
boolean flag = true;
for (int i = 2; i < PLAN_TEMPLATE_SIZE; i++) {
try {
IotUserRightPlanTemplateResult planTemplate = hkIsapiService.getPlanTemplate(camera, String.valueOf