本文主要是介绍JAVA对接海康门禁实现事件查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
此功能开发是基于基础环境搭建完之后的功能代码,如果没有搭建基础环境还需看下网上的教程自己搭建一下,如有疑问或者逻辑问题可私信博主!
实战代码
1、实体类
package com.sansint.avideo.param;import lombok.Data;import java.util.Calendar;
import java.util.Date;/*** @Description 查询事件请求实体类* @Author gzj* @Date 2023/6/29 15:02* @Version 1.0**/@Data
public class SearchEventRequest extends BaseRequest {static Calendar calendar = Calendar.getInstance();//查询全部主次类型的报警//主次事件类型设为0,代表查询所有事件private int dwMajor = 5; //private int dwMinor = 0; ////开始时间private int startdwYear = calendar.get(Calendar.YEAR);private int startdwMonth = calendar.get(Calendar.MONTH) + 1;private int startdwDay = calendar.get(Calendar.DATE);private int startdwHour = calendar.get(Calendar.HOUR_OF_DAY) - 1;private int startdwMinute = 0;private int startdwSecond = 0;//结束时间private int enddwYear = calendar.get(Calendar.YEAR);private int enddwMonth = calendar.get(Calendar.MONTH) + 1;private int enddwDay = calendar.get(Calendar.DATE);private int enddwHour = calendar.get(Calendar.HOUR_OF_DAY) + 1;private int enddwMinute = 59;private int enddwSecond = 0;private int wInductiveEventType = 1;private int byPicEnable = 1; //是否带图片,0-不带图片,1-带图片public int dwBeginSerialNo; //起始流水号(为0时默认全部)public int dwEndSerialNo; //结束流水号(为0时默认全部)public String byName;public String byCardNo;public String byEmployeeNo;private int current;private int size;static {calendar.setTime(new Date()); // 放入Date类型数据calendar.get(Calendar.YEAR); // 获取年份calendar.get(Calendar.MONTH); // 获取月份calendar.get(Calendar.DATE); // 获取日calendar.get(Calendar.HOUR_OF_DAY); // 时(24进制)calendar.get(Calendar.MINUTE); // 分}}
2、Controller代码
在redis做了60s的缓存,提高查询速度
/*** 获取事件列表** @return*/@PostMapping("/searchAllEvent")public R<PageInfo<SearchAllDTO>> searchAllEvent(@RequestBody SearchEventRequest request, SanSintUser sanSintUser) {System.out.println("事件查询入参:" + request.toString());PageHelper.startPage(request.getCurrent(), request.getSize()).setReasonable(true);List<SearchAllDTO> searchAllDTOS = new ArrayList<>();String mdRequest = DigestUtils.md5Hex(request.toString());if (null != redisUtil.get(mdRequest)) {searchAllDTOS = JSONObject.parseArray(redisUtil.get(mdRequest).toString(), SearchAllDTO.class);} else {searchAllDTOS = DoorAccessService.searchAllEvent(request, sanSintUser);redisUtil.set(mdRequest, JSON.toJSONString(searchAllDTOS), 60);}List<SearchAllDTO> collect = searchAllDTOS.stream().skip((long) (request.getCurrent() - 1) * request.getSize()).limit(request.getSize()).collect(Collectors.toList());PageInfo<SearchAllDTO> pageInfo = new PageInfo<>(searchAllDTOS);pageInfo.setList(collect);return R.data(pageInfo);}
3、Service代码
我是在项目启动的时候将所有的设备默认登录后放入Map中,每次访问有ip获取登录手柄,如果为空,则重新登录
/*** 获取全部事件** @param request* @return*/@Overridepublic List<SearchAllDTO> searchAllEvent(SearchEventRequest request, SanSintUser sanSintUser) {Integer lUserID = lUserIDMap.get(request.getIpadress());if (Objects.isNull(lUserID) || lUserID < 0) {UserManageService.login_V40(request.getIpadress(), request.getUser(), request.getPsw(), request.getPort());}lUserID = lUserIDMap.get(request.getIpadress());//登陆设备List<SearchAllDTO> netDvrAcsEventCfgs = EventSearchService.searchAllEvent(lUserID, request);if (!CollectionUtil.isEmpty(netDvrAcsEventCfgs)) {for (SearchAllDTO netDvrAcsEventCfg : netDvrAcsEventCfgs) {netDvrAcsEventCfg.setSNetUser((String) UserManageService.userMap.get(netDvrAcsEventCfg.getDwEmployeeNo()));}}//第一个元素将成为最后一个Collections.reverse(netDvrAcsEventCfgs);return netDvrAcsEventCfgs;}
4、事件工具代码
目前是查询所有的事件,也可以查询特定事件,具体可看sdk使用手册
package com.sansint.avideo.service.impl;import com.sansint.avideo.domain.SearchAllDTO;
import com.sansint.avideo.param.SearchEventRequest;
import com.sansint.avideo.utils.MinioStaticUtils;
import com.sun.jna.Pointer;
import io.netty.util.internal.StringUtil;import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;/*** 事件查询模块 1*/
public final class EventSearchService {public static List<SearchAllDTO> searchAllEvent(int lUserID, SearchEventRequest request) {int i = 0; //事件条数HCNetSDK.NET_DVR_ACS_EVENT_COND struAcsEventCond = new HCNetSDK.NET_DVR_ACS_EVENT_COND();struAcsEventCond.read();struAcsEventCond.dwSize = struAcsEventCond.size();//查询全部主次类型的报警struAcsEventCond.dwMajor = request.getDwMajor(); // 主次事件类型设为0,代表查询所有事件struAcsEventCond.dwMinor = request.getDwMinor(); ////开始时间struAcsEventCond.struStartTime.dwYear = request.getStartdwYear();struAcsEventCond.struStartTime.dwMonth = request.getStartdwMonth();struAcsEventCond.struStartTime.dwDay = request.getStartdwDay();struAcsEventCond.struStartTime.dwHour = request.getStartdwHour();struAcsEventCond.struStartTime.dwMinute = request.getStartdwMinute();struAcsEventCond.struStartTime.dwSecond = request.getStartdwSecond();//结束时间struAcsEventCond.struEndTime.dwYear = request.getEnddwYear();struAcsEventCond.struEndTime.dwMonth = request.getEnddwMonth();struAcsEventCond.struEndTime.dwDay = request.getEnddwDay();struAcsEventCond.struEndTime.dwHour = request.getEnddwHour();struAcsEventCond.struEndTime.dwMinute = request.getEnddwMinute();struAcsEventCond.struEndTime.dwSecond = request.getEnddwSecond();struAcsEventCond.dwBeginSerialNo = request.dwBeginSerialNo;struAcsEventCond.dwEndSerialNo = request.dwEndSerialNo;if (!StringUtil.isNullOrEmpty(request.getByEmployeeNo())) {struAcsEventCond.byEmployeeNo = request.getByEmployeeNo().getBytes();}if (!StringUtil.isNullOrEmpty(request.getByName())) {struAcsEventCond.byName = request.getByName().getBytes();}if (!StringUtil.isNullOrEmpty(request.getByCardNo())) {struAcsEventCond.byCardNo = request.getByCardNo().getBytes();}struAcsEventCond.wInductiveEventType = 1;struAcsEventCond.byPicEnable = 1; //是否带图片,0-不带图片,1-带图片struAcsEventCond.write();Pointer ptrStruEventCond = struAcsEventCond.getPointer();int m_lSearchEventHandle = ClientUtil.hCNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCNetSDK.NET_DVR_GET_ACS_EVENT, ptrStruEventCond, struAcsEventCond.size(), null, null);if (m_lSearchEventHandle <= -1) {System.out.println("NET_DVR_StartRemoteConfig调用失败,错误码:" + ClientUtil.hCNetSDK.NET_DVR_GetLastError());}List<SearchAllDTO> struAcsEventCfgList = new ArrayList<>();//创建返回数据的数组HCNetSDK.NET_DVR_ACS_EVENT_CFG struAcsEventCfg = new HCNetSDK.NET_DVR_ACS_EVENT_CFG();struAcsEventCfg.read();struAcsEventCfg.dwSize = struAcsEventCfg.size();struAcsEventCfg.write();Pointer ptrStruEventCfg = struAcsEventCfg.getPointer();String hostAddress = null;try {hostAddress = InetAddress.getLocalHost().getHostAddress();} catch (UnknownHostException e) {throw new RuntimeException(e);}System.out.println(hostAddress);while (true) {System.out.println("i=" + i);int dwEventSearch = ClientUtil.hCNetSDK.NET_DVR_GetNextRemoteConfig(m_lSearchEventHandle, ptrStruEventCfg, struAcsEventCfg.size());if (dwEventSearch <= -1) {System.out.println("NET_DVR_GetNextRemoteConfig接口调用失败,错误码:" + ClientUtil.hCNetSDK.NET_DVR_GetLastError());}if (dwEventSearch == HCNetSDK.NET_SDK_GET_NEXT_STATUS_NEED_WAIT) {System.out.println("配置等待....");try {Thread.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}continue;} else if (dwEventSearch == HCNetSDK.NET_SDK_NEXT_STATUS__FINISH) {System.out.println("获取事件完成");break;} else if (dwEventSearch == HCNetSDK.NET_SDK_GET_NEXT_STATUS_FAILED) {System.out.println("获取事件出现异常");break;} else if (dwEventSearch == HCNetSDK.NET_SDK_GET_NEXT_STATUS_SUCCESS) {struAcsEventCfg.read();SearchAllDTO searchAllDTO = new SearchAllDTO();searchAllDTO.setCollectTime(struAcsEventCfg.struTime.dwYear + "-" + struAcsEventCfg.struTime.dwMonth + "-" + struAcsEventCfg.struTime.dwDay + " " + struAcsEventCfg.struTime.dwHour + ":" + struAcsEventCfg.struTime.dwMinute + ":" + struAcsEventCfg.struTime.dwSecond);searchAllDTO.setDwMajor(String.valueOf(struAcsEventCfg.dwMajor));searchAllDTO.setDwMinor(String.valueOf(struAcsEventCfg.dwMinor));searchAllDTO.setByCardNo(new String(struAcsEventCfg.struAcsEventInfo.byCardNo).trim());searchAllDTO.setDwEmployeeNo(String.valueOf(struAcsEventCfg.struAcsEventInfo.dwEmployeeNo));searchAllDTO.setSNetUser(new String(struAcsEventCfg.sNetUser).trim());searchAllDTO.setByMask(String.valueOf(struAcsEventCfg.struAcsEventInfo.byMask));//人脸图片保存if (struAcsEventCfg.dwPicDataLen > 0 || struAcsEventCfg.pPicData != null) {long time = System.currentTimeMillis();//将字节写入文件long offset = 0;ByteBuffer buffers = struAcsEventCfg.pPicData.getByteBuffer(offset, struAcsEventCfg.dwPicDataLen);byte[] bytes = new byte[struAcsEventCfg.dwPicDataLen];buffers.rewind();buffers.get(bytes);InputStream inputStream = new ByteArrayInputStream(bytes);String url = MinioStaticUtils.uploadStatic("", searchAllDTO.getDwEmployeeNo() + time + "_event.jpg", inputStream);searchAllDTO.setPic(url);}i++;struAcsEventCfgList.add(searchAllDTO);continue;} else {System.out.println("走了这里");break;}}i = 0;if (!ClientUtil.hCNetSDK.NET_DVR_StopRemoteConfig(m_lSearchEventHandle)) {System.out.println("NET_DVR_StopRemoteConfig接口调用失败,错误码:" + ClientUtil.hCNetSDK.NET_DVR_GetLastError());} else {System.out.println("NET_DVR_StopRemoteConfig接口成功");}return struAcsEventCfgList;}}
这篇关于JAVA对接海康门禁实现事件查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!