#1,根据下载海康官网的设备管理工具4200
官网连接
#2,然后开始SDK集成,SDK的初始化及登录同上一文章
文章链接
#3,对于这个系列的设备有部分配置需要我们设置-如(修改卡加密校验配置, 卡权限模板配置)
##3.1 下面展示设备登录成功后,==卡权限模板配置, ==(因为设备的默认权限配置时段不清楚,需要我们配置全天24小时有限,否则刷卡会提示不在有效期)
/**
* 修改卡加密校验
*
* @param lUserId
*/
/**
* 卡计划模板配置 (以卡为中心)
*
* @param lUserID 用户登录句柄
* @param iPlanTemplateNumber 计划模板编号,从1开始,最大值从门禁能力集获取
*/
public static ReturnMessage setCardTemplate(int lUserID, int iPlanTemplateNumber) throws Exception {
// 设置卡权限计划模板参数
HCNetSDK.NET_DVR_PLAN_TEMPLATE_COND struPlanCond = new HCNetSDK.NET_DVR_PLAN_TEMPLATE_COND();
struPlanCond.dwSize = struPlanCond.size();
// 计划模板编号,从1开始,最大值从门禁能力集获取
struPlanCond.dwPlanTemplateNumber = iPlanTemplateNumber;
// 就地控制器序号[1,64],0表示门禁主机
struPlanCond.wLocalControllerID = 0;
struPlanCond.write();
HCNetSDK.NET_DVR_PLAN_TEMPLATE struPlanTemCfg = new HCNetSDK.NET_DVR_PLAN_TEMPLATE();
struPlanTemCfg.dwSize = struPlanTemCfg.size();
// 是否使能:0- 否,1- 是
struPlanTemCfg.byEnable = 1;
// 周计划编号,0表示无效
struPlanTemCfg.dwWeekPlanNo = 2;
// 假日组编号,按值表示,采用紧凑型排列,中间遇到0则后续无效
struPlanTemCfg.dwHolidayGroupNo[0] = 0;
ReturnMessage returnMessage = new ReturnMessage();
returnMessage.setlUserId(lUserID);
byte[] byTemplateName;
int code = 6;
if (iCharEncodeType == code) {
byTemplateName = "CardTemplatePlan_2".getBytes("utf-8");
} else {
byTemplateName = "CardTemplatePlan_2".getBytes("GBK");
}
// 计划模板名称
for (int i = 0; i < HCNetSDK.NAME_LEN; i++) {
struPlanTemCfg.byTemplateName[i] = 0;
}
System.arraycopy(byTemplateName, 0, struPlanTemCfg.byTemplateName, 0, byTemplateName.length);
struPlanTemCfg.write();
IntByReference pInt = new IntByReference(0);
Pointer lpStatusList = pInt.getPointer();
if (false == hCNetSDK.NET_DVR_SetDeviceConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_RIGHT_PLAN_TEMPLATE_V50, 1, struPlanCond.getPointer(), struPlanCond.size(), lpStatusList, struPlanTemCfg.getPointer(), struPlanTemCfg.size())) {
returnMessage.setStatusCode(hCNetSDK.NET_DVR_GetLastError());
returnMessage.setStatusCodeMsg(CommonUtil.getLastErrorMsg(hCNetSDK.NET_DVR_GetLastError()));
return returnMessage;
}
// 获取卡权限周计划参数
HCNetSDK.NET_DVR_WEEK_PLAN_COND struWeekPlanCond = new HCNetSDK.NET_DVR_WEEK_PLAN_COND();
struWeekPlanCond.dwSize = struWeekPlanCond.size();
struWeekPlanCond.dwWeekPlanNumber = 2;
struWeekPlanCond.wLocalControllerID = 0;
HCNetSDK.NET_DVR_WEEK_PLAN_CFG struWeekPlanCfg = new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();
struWeekPlanCond.write();
struWeekPlanCfg.write();
Pointer lpCond = struWeekPlanCond.getPointer();
Pointer lpInbuferCfg = struWeekPlanCfg.getPointer();
if (false == hCNetSDK.NET_DVR_GetDeviceConfig(lUserID, HCNetSDK.NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size())) {
returnMessage.setStatusCode(hCNetSDK.NET_DVR_GetLastError());
returnMessage.setStatusCodeMsg(CommonUtil.getLastErrorMsg(hCNetSDK.NET_DVR_GetLastError()));
return returnMessage;
}
struWeekPlanCfg.read();
struWeekPlanCfg.byEnable = 1; //是否使能:0- 否,1- 是
// 避免时间段交叉,先初始化, 七天八个时段*
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 8; j++) {
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].byEnable = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byHour = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byMinute = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.bySecond = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byMinute = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.bySecond = 0;
}
}
// 一周7天,全天24小时
for (int i = 0; i < 7; i++) {
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].byEnable = 1;
// 开始时间
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byHour = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byMinute = 0;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.bySecond = 0;
// 结束时间
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byHour = 23;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byMinute = 59;
struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.bySecond = 59;
}
struWeekPlanCfg.write();
// 设置卡权限周计划参数
if (false == hCNetSDK.NET_DVR_SetDeviceConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size())) {
returnMessage.setStatusCode(hCNetSDK.NET_DVR_GetLastError());
returnMessage.setStatusCodeMsg("卡权限模板设置失败" + CommonUtil.getLastErrorMsg(hCNetSDK.NET_DVR_GetLastError()));
return returnMessage;
}
returnMessage.setStatusCode(0);
returnMessage.setStatusCodeMsg("卡权限模板设置成功");
return returnMessage;
}
#4, 下面展示部分以卡为中心下发数据-删除卡号
/**
* 删除卡号,会关联删除用户的人脸
*
* @param lUserId 登录语句柄
* @param cardNo 卡号
* @return
*/
public static LongConnectionResult deleteByCardNo(int lUserId, String cardNo) throws Exception {
LongConnectionResult connectionResult = new LongConnectionResult();
connectionResult.setUserId(lUserId);
HCNetSDK.NET_DVR_CARD_COND struCardCond = new HCNetSDK.NET_DVR_CARD_COND();
struCardCond.read();
struCardCond.dwSize = struCardCond.size();
struCardCond.dwCardNum = 1;
struCardCond.write();
Pointer ptrStruCond = struCardCond.getPointer();
int lHandler = hCNetSDK.NET_DVR_StartRemoteConfig(lUserId, HCNetSDK.NET_DVR_DEL_CARD, ptrStruCond, struCardCond.size(), null, null);
if (lHandler == -1) {
int errorCode = hCNetSDK.NET_DVR_GetLastError();
connectionResult.setLongConnectionState(1);
connectionResult.setLongConnectionMsg(CommonUtil.getLastErrorMsg(errorCode) + "建立长连接失败");
logger.error("建立查询卡参数长连接失败,错误码为:{}::登录语句柄{}", CommonUtil.getLastErrorMsg(errorCode), lUserId);
return connectionResult;
}
HCNetSDK.NET_DVR_CARD_SEND_DATA struCardData = new HCNetSDK.NET_DVR_CARD_SEND_DATA();
struCardData.read();
struCardData.dwSize = struCardData.size();
for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
struCardData.byCardNo[i] = 0;
}
for (int i = 0; i < cardNo.length(); i++) {
struCardData.byCardNo[i] = cardNo.getBytes()[i];
}
struCardData.write();
HCNetSDK.NET_DVR_CARD_STATUS struCardStatus = new HCNetSDK.NET_DVR_CARD_STATUS();
struCardStatus.read();
struCardStatus.dwSize = struCardStatus.size();
struCardStatus.write();
HCNetSDK.BYTE_ARRAY ptrOutuff = new HCNetSDK.BYTE_ARRAY(1024);
IntByReference pInt = new IntByReference(0);
while (true) {
int dwState = hCNetSDK.NET_DVR_SendWithRecvRemoteConfig(lHandler, struCardData.getPointer(), struCardData.size(), struCardStatus.getPointer(), struCardStatus.size(), pInt);
struCardStatus.read();
if (dwState == -1) {
int errorCode = hCNetSDK.NET_DVR_GetLastError();
connectionResult.setLastError(hCNetSDK.NET_DVR_GetLastError());
connectionResult.setLastErrorMsg(CommonUtil.getLastErrorMsg(hCNetSDK.NET_DVR_GetLastError()));
logger.error("接口调用失败, cardNo:{}, errorCode:{}, msg :{}", cardNo, errorCode, CommonUtil.getLastErrorMsg(hCNetSDK.NET_DVR_GetLastError()));
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEED_WAIT) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
int errorCode = hCNetSDK.NET_DVR_GetLastError();
connectionResult.setLastError(dwState);
connectionResult.setLastErrorMsg("删除卡失败");
logger.error("删除卡失败, cardNo:{}, errorCode:{}", cardNo, errorCode);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
connectionResult.setLastError(dwState);
connectionResult.setLastErrorMsg("删除卡异常");
logger.error("删除卡异常 , cardNo:{}, errorCode:{}", cardNo, struCardStatus.dwErrorCode);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
if (struCardStatus.dwErrorCode != 0) {
connectionResult.setLastError(struCardStatus.dwErrorCode);
connectionResult.setLastErrorMsg("删除卡成功,但是有错误");
logger.error("删除卡成功,但是有错误 , cardNo:{}, errorCode:{}", cardNo, struCardStatus.dwErrorCode);
} else {
connectionResult.setLastError(0);
connectionResult.setLastErrorMsg("删除卡成功");
logger.info("删除卡成功 cardNo:{}", cardNo);
}
continue;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
connectionResult.setLastError(0);
connectionResult.setLastErrorMsg("删除卡完成");
logger.info("删除卡完成 cardNo:{}", cardNo);
break;
}
}
if (!hCNetSDK.NET_DVR_StopRemoteConfig(lHandler)) {
logger.error("NET_DVR_StopRemoteConfig接口调用失败,错误码:{}>>关闭长链接失败不计入失败", hCNetSDK.NET_DVR_GetLastError());
}
lHandler = -1;
return connectionResult;
}
#5, 时间有限只能粘贴部分代码,后续逐渐补齐完整的代码,如果有小伙伴感兴趣,欢迎在评论区交流或留言!