增加海康摄像头上报事件

This commit is contained in:
lxj 2025-07-18 15:13:18 +08:00
parent 9c0591dfaa
commit 305705abc0
3 changed files with 329 additions and 109 deletions

View File

@ -40,9 +40,9 @@ public class HikDeviceApplicationRunner implements ApplicationRunner {
// 现在查询全部后面要更具需要查询指定厂商的设备
HikDeviceApplicationRunner runner = SpringUtils.getAopProxy(this);
// 梯控登录
runner.hikElevatorInfoLogin();
// runner.hikElevatorInfoLogin();
// 网络摄像头登录
hikNetCameraLogin();
runner.hikNetCameraLogin();
}
@Async
@ -62,37 +62,21 @@ public class HikDeviceApplicationRunner implements ApplicationRunner {
}
});
}
// String ip = "192.168.24.188";
// short port = 8000;
// String account = "admin";
// String pwd = "qweasd123";
// HikApiService.getInstance().login(ip, port, account, pwd);
// List<Integer> arrs = Arrays.asList(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2);
// for (int i = 0; i < arrs.size(); i++) {
// HikApiService.getInstance().controlGateway(ip, (i + 1), arrs.get(i));
// }
}
@Async
public void hikNetCameraLogin() {
List<SisDeviceManageVo> deviceList = deviceManageService.queryList(new SisDeviceManageBo());
if (CollUtil.isEmpty(deviceList)) {
log.info("没有查询到摄像设备信息,不执行登录");
return;
}
deviceList.forEach(item -> {
int handler = HikApiService.getInstance().login(item.getDeviceIp(), item.getDevicePort().shortValue(), item.getDeviceAccount(), item.getDevicePwd());
log.info("设备[{}]执行登录完成result={}", item.getDeviceIp(), handler);
if (handler != -1) {
HikApiService.getInstance().setAlarmChan(handler);
}
});
// String ip = "192.168.24.19";
// short port = 8000;
// String account = "admin";
// String pwd = "xzf13579";
// int handler = HikApiService.getInstance().login(ip, port, account, pwd);
}
}

View File

@ -17,10 +17,17 @@ import org.dromara.sis.sdk.huawei.HuaWeiBoxApi;
import org.dromara.sis.service.*;
import org.springframework.stereotype.Component;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import static org.dromara.sis.sdk.hik.HCNetSDK.*;
@Slf4j
@Component
@RequiredArgsConstructor
@ -32,111 +39,341 @@ public class HikAlarmCallBack implements HCNetSDK.FMSGCallBack_V31 {
private final ISisDeviceBindRefService deviceBindRefService;
private final ISisAccessControlService accessControlService;
private final ISisElevatorFloorRefService elevatorFloorRefService;
private final E8PlatformApi e8PlatformApi;
@DubboReference
private RemoteFloorService remoteFloorService;
private static final int COMM_UPLOAD_FACESNAP_RESULT = 0x1112;
@Override
public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
if (lCommand == COMM_UPLOAD_FACESNAP_RESULT) {// 读取抓拍人脸数据
long s = System.currentTimeMillis();
log.info("hik 抓拍信息上报完成,lCommand={}", lCommand);
HCNetSDK.NET_VCA_FACESNAP_RESULT result = new HCNetSDK.NET_VCA_FACESNAP_RESULT();
result.write();
Pointer pItsPlateInfo = result.getPointer();
pItsPlateInfo.write(0, pAlarmInfo.getByteArray(0, result.size()), 0, result.size());
result.read();
// 读取人脸小图
ByteBuffer buffers = result.pBuffer1.getByteBuffer(0, result.dwFacePicLen);
byte[] smallImg = new byte[result.dwFacePicLen];
buffers.rewind();
buffers.get(smallImg);
switch (lCommand) {
//实时特征抓拍上传
case COMM_UPLOAD_FACESNAP_RESULT: {
log.info("hik 抓拍信息上报完成,lCommand={}", lCommand);
HCNetSDK.NET_VCA_FACESNAP_RESULT strFaceSnapInfo = new HCNetSDK.NET_VCA_FACESNAP_RESULT();
strFaceSnapInfo.write();
Pointer pFaceSnapInfo = strFaceSnapInfo.getPointer();
pFaceSnapInfo.write(0, pAlarmInfo.getByteArray(0, strFaceSnapInfo.size()), 0, strFaceSnapInfo.size());
strFaceSnapInfo.read();
handleFaceSnap(strFaceSnapInfo, pAlarmer);
break;
}
// 异常行为检测
case COMM_ALARM_RULE: {
// 读数据
HCNetSDK.NET_VCA_RULE_ALARM strVcaAlarm = new HCNetSDK.NET_VCA_RULE_ALARM();
strVcaAlarm.write();
Pointer pVCAInfo = strVcaAlarm.getPointer();
pVCAInfo.write(0, pAlarmInfo.getByteArray(0, strVcaAlarm.size()), 0, strVcaAlarm.size());
strVcaAlarm.read();
// 读取人脸大图
switch (strVcaAlarm.struRuleInfo.wEventTypeEx) {
case 1: //穿越警戒面 (越界侦测)
System.out.println("越界侦测报警发生");
strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_TRAVERSE_PLANE.class);
System.out.println("检测目标:" + strVcaAlarm.struRuleInfo.uEventParam.struTraversePlane.byDetectionTarget); //检测目标0表示所有目标表示不锁定检测目标所有目标都将进行检测其他取值按位表示不同的检测目标0x01-0x02-
//图片保存
if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
String filename = "./pic/" + newName + "VCA_TRAVERSE_PLANE" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 2: //目标进入区域
System.out.println("目标进入区域报警发生");
strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_AREA.class);
System.out.println("检测目标:" + strVcaAlarm.struRuleInfo.uEventParam.struArea.byDetectionTarget);
//图片保存
if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
String filename = "./pic/" + newName + "_TargetEnter" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 3: //目标离开区域
System.out.println("目标离开区域报警触发");
strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_AREA.class);
System.out.println("检测目标:" + strVcaAlarm.struRuleInfo.uEventParam.struArea.byDetectionTarget);
//图片保存
if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
String filename = "./pic/" + newName + "_TargetLeave" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 4: //周界入侵
System.out.println("周界入侵报警发生");
strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_INTRUSION.class);
System.out.println("检测目标:" + strVcaAlarm.struRuleInfo.uEventParam.struIntrusion.byDetectionTarget);
//图片保存
if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
String filename = "./pic/" + newName + "VCA_INTRUSION" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 5: //徘徊
System.out.println("徘徊事件触发");
break;
case 8: //快速移动(奔跑)
System.out.println("快速移动(奔跑)事件触发");
break;
case 15:
System.out.println("事件触发");
strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_LEAVE_POSITION.class);
System.out.println(strVcaAlarm.struRuleInfo.uEventParam.struLeavePos.byOnPosition);
//图片保存
if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
String filename = "./pic/" + newName + "VCA_LEAVE_POSITION_" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
case 20: //倒地检测
System.out.println("倒地事件触发");
break;
case 44: //玩手机
System.out.println("玩手机报警发生");
//图片保存
if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
String filename = "./pic/" + newName + "PLAY_CELLPHONE_" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 45: //持续检测
System.out.println("持续检测事件触发");
default:
System.out.println("行为事件类型:" + strVcaAlarm.struRuleInfo.wEventTypeEx);
break;
}
break;
}
// 人脸比对结果上报
case COMM_SNAP_MATCH_ALARM: {
HCNetSDK.NET_VCA_FACESNAP_MATCH_ALARM strFaceSnapMatch = new HCNetSDK.NET_VCA_FACESNAP_MATCH_ALARM();
strFaceSnapMatch.write();
Pointer pFaceSnapMatch = strFaceSnapMatch.getPointer();
pFaceSnapMatch.write(0, pAlarmInfo.getByteArray(0, strFaceSnapMatch.size()), 0, strFaceSnapMatch.size());
strFaceSnapMatch.read();
//比对结果0-保留1-比对成功2-比对失败
String sFaceSnapMatchInfo = "比对结果:" + strFaceSnapMatch.byContrastStatus + ",相似度:" + strFaceSnapMatch.fSimilarity;
}
default: {
}
}
return true;
}
/**
* 处理人脸抓拍上报
*
* @param result 人脸数据
* @param pAlarmer 设备数据
* @return 返回是否处理成功
*/
private boolean handleFaceSnap(HCNetSDK.NET_VCA_FACESNAP_RESULT result, HCNetSDK.NET_DVR_ALARMER pAlarmer) {
long s = System.currentTimeMillis();
// 读取人脸小图
ByteBuffer buffers = result.pBuffer1.getByteBuffer(0, result.dwFacePicLen);
byte[] smallImg = new byte[result.dwFacePicLen];
buffers.rewind();
buffers.get(smallImg);
// 读取人脸大图
/*ByteBuffer buffers1 = result.pBuffer2.getByteBuffer(0, result.dwBackgroundPicLen);
byte[] bigImg = new byte[result.dwBackgroundPicLen];
buffers1.rewind();
buffers1.get(bigImg);*/
//设备ip
String sAlarmInfo = new String(pAlarmer.sDeviceIP).trim();
// 设备绑定关系
List<SisDeviceBindRefVo> bindRefList = deviceBindRefService.queryByDeviceIp(sAlarmInfo);
if (CollUtil.isEmpty(bindRefList)) {
log.info("当前报警设备未绑定门禁/梯控,不做处理!");
return true;
}
//设备ip
String sAlarmInfo = new String(pAlarmer.sDeviceIP).trim();
// 设备绑定关系
List<SisDeviceBindRefVo> bindRefList = deviceBindRefService.queryByDeviceIp(sAlarmInfo);
if (CollUtil.isEmpty(bindRefList)) {
log.info("当前报警设备未绑定门禁/梯控,不做处理!");
return true;
}
// todo 进行人脸比对
String smallImgBase64Str = Base64Encoder.encode(smallImg);
long s1 = System.currentTimeMillis();
Long person = huaWeiBoxApi.findPerson(smallImgBase64Str);
log.info("人脸比对执行完成,耗时:{}", System.currentTimeMillis() - s1);
if (person == null) {
log.info("未命中人脸数据,暂不处理。");
return true;
}
log.info("人脸比对完成,personId={}", person);
// todo 进行人脸比对
String smallImgBase64Str = Base64Encoder.encode(smallImg);
long s1 = System.currentTimeMillis();
Long person = huaWeiBoxApi.findPerson(smallImgBase64Str);
log.info("人脸比对执行完成,耗时:{}", System.currentTimeMillis() - s1);
if (person == null) {
log.info("未命中人脸数据,暂不处理。");
return true;
}
log.info("人脸比对完成,personId={}", person);
// 授权记录
List<SisAuthRecordVo> authVoList = authRecordService.checkAuth(person);
// 获取门禁id
Collection<Long> acIds = authVoList.stream().filter(vo -> vo.getDeviceType() == 1).map(SisAuthRecordVo::getDeviceId).toList();
if (CollUtil.isNotEmpty(acIds)) {
acIds.forEach(id -> {
Long deviceId = bindRefList.stream().filter(vo -> vo.getBindId().equals(id)).findFirst().map(SisDeviceBindRefVo::getDeviceId).orElse(null);
SisAccessControlVo ac = accessControlService.queryById(deviceId);
if (ac != null) {
log.info("调用门禁服务远程开门doorName:{}", ac.getAccessName());
RemoteOpenDoorReq req = new RemoteOpenDoorReq();
req.setType(0);
RemoteOpenDoorReq.ControlData data = new RemoteOpenDoorReq.ControlData();
data.setDeviceId(Long.parseLong(ac.getOutCode()));
data.setDoorId(Long.parseLong(ac.getOutCode()));
req.setControlList(List.of(data));
Boolean flag = e8PlatformApi.remoteOpenDoor(req);
log.info("远程开门结果,result={}", flag);
}
});
}
// 授权记录
List<SisAuthRecordVo> authVoList = authRecordService.checkAuth(person);
// 获取门禁id
Collection<Long> acIds = authVoList.stream().filter(vo -> vo.getDeviceType() == 1).map(SisAuthRecordVo::getDeviceId).toList();
if (CollUtil.isNotEmpty(acIds)) {
acIds.forEach(id -> {
Long deviceId = bindRefList.stream().filter(vo -> vo.getBindId().equals(id)).findFirst().map(SisDeviceBindRefVo::getDeviceId).orElse(null);
SisAccessControlVo ac = accessControlService.queryById(deviceId);
if (ac != null) {
log.info("调用门禁服务远程开门doorName:{}", ac.getAccessName());
RemoteOpenDoorReq req = new RemoteOpenDoorReq();
req.setType(0);
RemoteOpenDoorReq.ControlData data = new RemoteOpenDoorReq.ControlData();
data.setDeviceId(Long.parseLong(ac.getOutCode()));
data.setDoorId(Long.parseLong(ac.getOutCode()));
req.setControlList(List.of(data));
Boolean flag = e8PlatformApi.remoteOpenDoor(req);
log.info("远程开门结果,result={}", flag);
}
});
}
// 获取电梯ids
Collection<Long> eleIds = authVoList.stream().filter(vo -> vo.getDeviceType() == 2).map(SisAuthRecordVo::getDeviceId).toList();
if (CollUtil.isNotEmpty(eleIds)) {
eleIds.forEach(id -> {
Long deviceId = bindRefList.stream().filter(vo -> vo.getBindId().equals(id)).findFirst().map(SisDeviceBindRefVo::getDeviceId).orElse(null);
SisElevatorInfoVo ele = elevatorInfoService.queryById(deviceId);
if (ele != null) {
log.info("下发电梯权限");
// 根据单元ID获取楼层信息
List<RemoteFloorVo> floorInfo = remoteFloorService.queryByUnitId(ele.getUnitId());
// 获取电梯楼层关联信息
List<SisElevatorFloorRefVo> floorRefList = elevatorFloorRefService.queryByElevatorId(deviceId);
// 获取楼层数组
List<Long> layerArray = floorInfo.stream().map(RemoteFloorVo::getFloorNumber).sorted().toList();
// 获取电梯ids
Collection<Long> eleIds = authVoList.stream().filter(vo -> vo.getDeviceType() == 2).map(SisAuthRecordVo::getDeviceId).toList();
if (CollUtil.isNotEmpty(eleIds)) {
eleIds.forEach(id -> {
Long deviceId = bindRefList.stream().filter(vo -> vo.getBindId().equals(id)).findFirst().map(SisDeviceBindRefVo::getDeviceId).orElse(null);
SisElevatorInfoVo ele = elevatorInfoService.queryById(deviceId);
if (ele != null) {
log.info("下发电梯权限");
// 根据单元ID获取楼层信息
List<RemoteFloorVo> floorInfo = remoteFloorService.queryByUnitId(ele.getUnitId());
// 获取电梯楼层关联信息
List<SisElevatorFloorRefVo> floorRefList = elevatorFloorRefService.queryByElevatorId(deviceId);
// 获取楼层数组
List<Long> layerArray = floorInfo.stream().map(RemoteFloorVo::getFloorNumber).sorted().toList();
layerArray.forEach(layer -> {
SisElevatorFloorRefVo floorRef = floorRefList.stream()
.filter(vo -> vo.getFloorNum().intValue() == layer) // 直接使用 layer
.findFirst()
.orElse(null);
if (floorRef == null) {
HikApiService.getInstance().controlGateway(ele.getControlIp(), layer.intValue(), 3);
} else {
HikApiService.getInstance().controlGateway(ele.getControlIp(), layer.intValue(), 2);
}
});
}
});
}
log.info("权限下发执行完成,耗时:{}", System.currentTimeMillis() - s);
layerArray.forEach(layer -> {
SisElevatorFloorRefVo floorRef = floorRefList.stream()
.filter(vo -> vo.getFloorNum().intValue() == layer) // 直接使用 layer
.findFirst()
.orElse(null);
if (floorRef == null) {
HikApiService.getInstance().controlGateway(ele.getControlIp(), layer.intValue(), 3);
} else {
HikApiService.getInstance().controlGateway(ele.getControlIp(), layer.intValue(), 2);
}
});
}
});
}
log.info("权限下发执行完成,耗时:{}", System.currentTimeMillis() - s);
// todo 做延时队列关闭梯控授权
// try {
@ -148,9 +385,6 @@ public class HikAlarmCallBack implements HCNetSDK.FMSGCallBack_V31 {
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
} else {
log.info("未知报警类型,lCommand={}", lCommand);
}
return true;
return false;
}
}

View File

@ -1,9 +1,11 @@
package org.dromara.sis.sdk.hik.service;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sis.sdk.hik.HCNetSDK;
import org.dromara.sis.sdk.hik.SDK_Structure;
import org.springframework.util.Assert;
import static org.dromara.sis.sdk.hik.common.osSelect.isWindows;
@ -98,7 +100,7 @@ public class SdkBaseServer {
return b;
}
public static boolean setAlarmChan(int lUserID){
public static boolean setAlarmChan(int lUserID) {
//报警布防参数设置
HCNetSDK.NET_DVR_SETUPALARM_PARAM_V50 m_strAlarmInfoV50 = new HCNetSDK.NET_DVR_SETUPALARM_PARAM_V50();
m_strAlarmInfoV50.dwSize = m_strAlarmInfoV50.size();