JAVA对接海康门禁实现事件查询

2024-08-21 20:44

本文主要是介绍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对接海康门禁实现事件查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1094235

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分