package com.ard.utils.sdk.hiksdk.service.impl; import com.alibaba.fastjson2.JSON; import com.ard.alarm.camera.domain.ArdCameras; import com.ard.alarm.camera.domain.CameraCmd; import com.ard.alarm.camera.service.impl.ArdCamerasServiceImpl; import com.ard.alarm.external.domain.ArdEquipExternal; import com.ard.alarm.external.service.impl.ArdEquipExternalServiceImpl; import com.ard.utils.sdk.hiksdk.domain.*; import com.ard.utils.minio.MinioUtil; import com.ard.utils.util.ByteUtils; import com.ard.utils.sdk.hiksdk.common.GlobalVariable; import com.ard.utils.sdk.hiksdk.util.hikSdkUtil.HCNetSDK; import com.ard.utils.util.DateUtils; import com.ard.utils.mqtt.MqttProducer; import com.ard.utils.util.IdUtils; import com.sun.jna.Pointer; import javafx.scene.Camera; import lombok.extern.slf4j.Slf4j; import java.io.*; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Optional; import static com.ard.utils.sdk.hiksdk.util.hikSdkUtil.HCNetSDK.*; /** * @ClassName: FMSGCallBack * @Description: * @Author: Administrator * @Date: 2023年02月15日 12:16 * @Version: 1.0 **/ @Slf4j(topic = "hikSdk") public class FMSGCallBack implements HCNetSDK.FMSGCallBack_V31 { /** * 报警信息回调函数 * * @param lCommand 上传消息类型 * @param pAlarmer 报警设备信息 * @param pAlarmInfo 报警信息 * @param dwBufLen 报警信息缓存大小 * @param pUser 用户数据 */ @Override public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sDeviceIP = ByteUtils.bytesToStringZh(pAlarmer.sDeviceIP).trim();//设备IP地址 Integer wLinkPort = Integer.valueOf(pAlarmer.wLinkPort);//设备通讯端口 //通过ip和端口查询设备 Optional foundCamera = findArdCameraByIpAndPort(sDeviceIP, wLinkPort); Optional foundExternal = findArdEquipExternalByIpAndPort(sDeviceIP, wLinkPort); ArdCameras camera = null; ArdEquipExternal ardEquipExternal = null; if (foundCamera.isPresent()) { camera = foundCamera.get(); } else { if (foundExternal.isPresent()) { ardEquipExternal = foundExternal.get(); } } //如果没有找到设备,直接返回 if (camera == null && ardEquipExternal == null) { return false; } CameraEventInfo cameraEventInfo; SuperBrainEventInfo superBrainEventInfo; String alarmName = "";//报警名称 String alarmTime;//事件时间 String alarmType = "";//报警类型 String picUrl = "";//事件图片 String facePicUrl = "";//人脸抓拍图片 String faceLibUrl = "";//人脸库图片 int dwPicDataLen;//图片数据长度 //lCommand是传的报警类型 log.debug("【" + sDeviceIP + ":" + wLinkPort + "】报警事件类型:lCommand:" + Integer.toHexString(lCommand)); switch (lCommand) { case COMM_ALARM_V30: log.debug("移动侦测信息上报"); //region 移动侦测、视频丢失、遮挡、IO信号量等报警信息(V3.0以上版本支持的设备) HCNetSDK.NET_DVR_ALARMINFO_V30 netDvrAlarminfoV30 = new HCNetSDK.NET_DVR_ALARMINFO_V30(); netDvrAlarminfoV30.write(); Pointer pNDAInfo = netDvrAlarminfoV30.getPointer(); pNDAInfo.write(0, pAlarmInfo.getByteArray(0, netDvrAlarminfoV30.size()), 0, netDvrAlarminfoV30.size()); netDvrAlarminfoV30.read(); switch (netDvrAlarminfoV30.dwAlarmType) { case 3: if (camera != null) { cameraEventHandler(camera, "移动侦测", "移动侦测报警", fmt.format(new Date()), ""); } break; } //endregion break; case COMM_VCA_ALARM: log.debug("智能检测通用信息上报(暂不解析)"); case COMM_ALARM_RULE: log.debug("异常行为识别信息上报"); //region 异常行为识别信息 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(); Integer ruleID = Integer.valueOf(strVcaAlarm.struRuleInfo.byRuleID); alarmTime = DateUtils.parseTime(strVcaAlarm.dwAbsTime);//事件时间 Integer channel = Integer.valueOf(strVcaAlarm.struDevInfo.byChannel);//通道 switch (strVcaAlarm.struRuleInfo.wEventTypeEx) { case 1: //region穿越警戒面 (越界侦测) alarmType = "越界侦测报警"; //endregion break; case 2: //region 目标进入区域 alarmType = "目标进入区域报警"; //endregion break; case 3: //region 目标离开区域 alarmType = "目标离开区域报警"; //strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_AREA.class); //endregion break; case 4: //region 周界入侵 alarmType = "周界入侵报警"; //endregion break; case 5: //region 徘徊 alarmType = "徘徊事件报警"; //endregion break; case 8: //region 快速移动(奔跑) alarmType = "快速移动报警"; //endregion break; case 9: //region 人员聚集报警 alarmType = "人员聚集报警"; //endregion break; case 13: //region 物品遗留事件 alarmType = "物品遗留事件报警"; //endregion break; case 14: //region 物品拿取事件 alarmType = "物品拿取事件报警"; //endregion break; case 15: //region 离岗事件 strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_LEAVE_POSITION.class); int byMode = strVcaAlarm.struRuleInfo.uEventParam.struLeavePos.byMode; switch (byMode) { case 0: alarmType = "离岗事件报警"; break; case 1: alarmType = "睡岗事件报警"; break; } //endregion break; case 20: //region 倒地检测 alarmType = "倒地检测事件报警"; //endregion break; case 35: //region 倒地检测 alarmType = "人数变化事件报警"; //endregion break; case 41: //region 滞留检测 alarmType = "滞留检测事件报警"; //endregion break; case 44: //region 玩手机 alarmType = "玩手机事件报警"; //endregion break; default: log.debug("未知异常行为事件类型:" + strVcaAlarm.struRuleInfo.wEventTypeEx); break; } //图片接收 int byPicType = strVcaAlarm.byPicType; dwPicDataLen = strVcaAlarm.dwPicDataLen; int byPicTransType = strVcaAlarm.byPicTransType; if (dwPicDataLen > 0 && strVcaAlarm.pImage != null && byPicTransType == 0) { //将字节写入文件 long offset = 0; ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, dwPicDataLen); byte[] bytes = new byte[dwPicDataLen]; buffers.rewind(); buffers.get(bytes); InputStream input = new ByteArrayInputStream(bytes); try { String bucketName = "pic"; String objectName = "alarm/" + IdUtils.simpleUUID() + ".jpeg"; boolean uploadObject = MinioUtil.uploadObject(bucketName, objectName, input, input.available(), "image/JPEG"); if (uploadObject) { picUrl = MinioUtil.getBucketObjectUrl(bucketName, objectName); //log.debug("上传文件成功!" + picUrl); } } catch (Exception e) { log.error("异常行为识别信息报警信息上报图片保存失败" + e.getMessage()); } } if (camera != null) { camera.setChannel(channel); cameraEventHandler(camera, alarmName, alarmType, alarmTime, picUrl); } if (ardEquipExternal != null) { alarmName = "异常行为识别"; superBrainEventHandler(ardEquipExternal, channel, alarmName, alarmType, alarmTime, picUrl, ""); } //endregion break; case COMM_UPLOAD_FACESNAP_RESULT: log.debug("人脸抓拍报警信息(暂不解析)"); //region 人脸抓拍报警信息 HCNetSDK.NET_VCA_FACESNAP_RESULT struFaceSnap = new HCNetSDK.NET_VCA_FACESNAP_RESULT(); struFaceSnap.write(); Pointer vcaInfo = struFaceSnap.getPointer(); vcaInfo.write(0, pAlarmInfo.getByteArray(0, struFaceSnap.size()), 0, struFaceSnap.size()); struFaceSnap.read(); alarmTime = DateUtils.parseTime(struFaceSnap.dwAbsTime); String facePicTyp; switch (struFaceSnap.byUploadEventDataType) { case 0: facePicTyp = "二进制数据"; break; case 1: facePicTyp = "URL"; break; default: facePicTyp = ""; break; } if (struFaceSnap.dwFacePicLen > 0 && struFaceSnap.pBuffer1 != null) { //将字节写入文件 long offset = 0; ByteBuffer buffers = struFaceSnap.pBuffer1.getByteBuffer(offset, struFaceSnap.dwBackgroundPicLen); byte[] bytes = new byte[struFaceSnap.dwBackgroundPicLen]; buffers.rewind(); buffers.get(bytes); InputStream input = new ByteArrayInputStream(bytes); try { String bucketName = "pic"; String objectName = "alarm/" + IdUtils.simpleUUID() + ".jpeg"; boolean uploadObject = MinioUtil.uploadObject(bucketName, objectName, input, input.available(), "image/JPEG"); if (uploadObject) { facePicUrl = MinioUtil.getBucketObjectUrl(bucketName, objectName); //log.debug("上传文件成功!" + url); } } catch (Exception e) { log.error("人脸抓拍报警信息图片保存失败" + e.getMessage()); } } if (struFaceSnap.dwBackgroundPicLen > 0 && struFaceSnap.pBuffer2 != null) { //将字节写入文件 long offset = 0; ByteBuffer buffers = struFaceSnap.pBuffer2.getByteBuffer(offset, struFaceSnap.dwBackgroundPicLen); byte[] bytes = new byte[struFaceSnap.dwBackgroundPicLen]; buffers.rewind(); buffers.get(bytes); InputStream input = new ByteArrayInputStream(bytes); try { String bucketName = "pic"; String objectName = "alarm/" + IdUtils.simpleUUID() + ".jpeg"; boolean uploadObject = MinioUtil.uploadObject(bucketName, objectName, input, input.available(), "image/JPEG"); if (uploadObject) { picUrl = MinioUtil.getBucketObjectUrl(bucketName, objectName); //log.debug("上传文件成功!" + url); } } catch (Exception e) { log.error("人脸抓拍报警信息图片保存失败" + e.getMessage()); } } //log.debug("【人脸抓拍报警信息】: alarmTime:" + alarmTime + " picUrl:" + picUrl + " facePicType:" + facePicTyp + " facePicUrl:" + facePicUrl); FaceSnapEventInfo faceSnapEventInfo = new FaceSnapEventInfo(); faceSnapEventInfo.setAlarmTime(alarmTime); faceSnapEventInfo.setPicUrl(picUrl); publishMqtt(faceSnapEventInfo, "superbrain"); //endregion break; case COMM_SNAP_MATCH_ALARM: log.debug("人脸比对报警信息"); //region 人脸比对报警信息 HCNetSDK.NET_VCA_FACESNAP_MATCH_ALARM struFaceMatchAlarm = new HCNetSDK.NET_VCA_FACESNAP_MATCH_ALARM(); struFaceMatchAlarm.byPicTransType = 0; struFaceMatchAlarm.write(); Pointer fmaInfo = struFaceMatchAlarm.getPointer(); fmaInfo.write(0, pAlarmInfo.getByteArray(0, struFaceMatchAlarm.size()), 0, struFaceMatchAlarm.size()); struFaceMatchAlarm.read(); alarmTime = DateUtils.parseTime(struFaceMatchAlarm.struSnapInfo.dwAbsTime);//报警时间 int byMatchPicNum = struFaceMatchAlarm.byMatchPicNum;//匹配图片数量 String contrastStatus = ""; switch (struFaceMatchAlarm.byContrastStatus) { case 1: contrastStatus = "比对成功"; break; case 2: contrastStatus = "比对失败"; break; default: contrastStatus = "未知"; break; } String BlockType = ""; switch (struFaceMatchAlarm.struBlockListInfo.struBlockListInfo.byType) { case 1: BlockType = "陌生人报警"; break; case 2: BlockType = "人脸比对报警"; break; default: BlockType = "未知"; break; } //人脸比对报警图片保存,图片格式二进制 if ((struFaceMatchAlarm.dwSnapPicLen > 0) && (struFaceMatchAlarm.byPicTransType == 0)) { SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String newName = sf.format(new Date()); FileOutputStream fout; try { String filename = "../pic/" + newName + "_pSnapPicBuffer" + ".jpg"; fout = new FileOutputStream(filename); //将字节写入文件 long offset = 0; ByteBuffer buffers = struFaceMatchAlarm.pSnapPicBuffer.getByteBuffer(offset, struFaceMatchAlarm.dwSnapPicLen); byte[] bytes = new byte[struFaceMatchAlarm.dwSnapPicLen]; 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(); } } if ((struFaceMatchAlarm.struSnapInfo.dwSnapFacePicLen > 0) && (struFaceMatchAlarm.byPicTransType == 0)) { SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String newName = sf.format(new Date()); FileOutputStream fout; try { String filename = "../pic/" + newName + "_struSnapInfo_pBuffer1" + ".jpg"; fout = new FileOutputStream(filename); //将字节写入文件 long offset = 0; ByteBuffer buffers = struFaceMatchAlarm.struSnapInfo.pBuffer1.getByteBuffer(offset, struFaceMatchAlarm.struSnapInfo.dwSnapFacePicLen); byte[] bytes = new byte[struFaceMatchAlarm.struSnapInfo.dwSnapFacePicLen]; 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(); } } if ((struFaceMatchAlarm.struBlockListInfo.dwBlockListPicLen > 0) && (struFaceMatchAlarm.byPicTransType == 0)) { SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String newName = sf.format(new Date()); FileOutputStream fout; try { String filename = "../pic/" + newName + "_fSimilarity_" + struFaceMatchAlarm.fSimilarity + "_struBlackListInfo_pBuffer1" + ".jpg"; fout = new FileOutputStream(filename); //将字节写入文件 long offset = 0; ByteBuffer buffers = struFaceMatchAlarm.struBlockListInfo.pBuffer1.getByteBuffer(offset, struFaceMatchAlarm.struBlockListInfo.dwBlockListPicLen); byte[] bytes = new byte[struFaceMatchAlarm.struBlockListInfo.dwBlockListPicLen]; 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(); } } //人脸比对报警图片保存,图片格式URL格式 if ((struFaceMatchAlarm.dwSnapPicLen > 0) && (struFaceMatchAlarm.byPicTransType == 1)) { long offset = 0; ByteBuffer buffers = struFaceMatchAlarm.pSnapPicBuffer.getByteBuffer(offset, struFaceMatchAlarm.dwSnapPicLen); byte[] bytes = new byte[struFaceMatchAlarm.dwSnapPicLen]; buffers.rewind(); buffers.get(bytes); picUrl = new String(bytes); //log.debug("抓拍图URL:" + picUrl); } if ((struFaceMatchAlarm.struSnapInfo.dwSnapFacePicLen > 0) && (struFaceMatchAlarm.byPicTransType == 1)) { long offset = 0; ByteBuffer buffers = struFaceMatchAlarm.struSnapInfo.pBuffer1.getByteBuffer(offset, struFaceMatchAlarm.struSnapInfo.dwSnapFacePicLen); byte[] bytes = new byte[struFaceMatchAlarm.struSnapInfo.dwSnapFacePicLen]; buffers.rewind(); buffers.get(bytes); facePicUrl = new String(bytes); //System.out.println("抓拍人脸子图URL:" + SnapPicUrl); } if ((struFaceMatchAlarm.struBlockListInfo.dwBlockListPicLen > 0) && (struFaceMatchAlarm.byPicTransType == 1)) { long offset = 0; ByteBuffer buffers = struFaceMatchAlarm.struBlockListInfo.pBuffer1.getByteBuffer(offset, struFaceMatchAlarm.struBlockListInfo.dwBlockListPicLen); byte[] bytes = new byte[struFaceMatchAlarm.struBlockListInfo.dwBlockListPicLen]; buffers.rewind(); buffers.get(bytes); faceLibUrl = new String(bytes); //System.out.println("人脸库人脸图的URL:" + SnapPicUrl); } if (ardEquipExternal != null) { alarmName = "人脸比对检测"; alarmType = contrastStatus + "[" + BlockType + "]"; superBrainEventHandler(ardEquipExternal, 0, alarmName, alarmType, alarmTime, picUrl, facePicUrl); } //endregion break; case COMM_UPLOAD_AIOP_VIDEO: log.debug("AI开放平台接入视频检测报警上传"); //region AI开放平台接入视频检测 HCNetSDK.NET_AIOP_VIDEO_HEAD struAIOPVideo = new HCNetSDK.NET_AIOP_VIDEO_HEAD(); struAIOPVideo.write(); Pointer pAIOPVideo = struAIOPVideo.getPointer(); pAIOPVideo.write(0, pAlarmInfo.getByteArray(0, struAIOPVideo.size()), 0, struAIOPVideo.size()); struAIOPVideo.read(); log.debug("视频任务ID" + new String(struAIOPVideo.szTaskID).trim()); log.debug("通道号:" + struAIOPVideo.dwChannel); String szMPID = new String(struAIOPVideo.szMPID).trim();//检测模型包ID log.debug("检测模型包ID" + szMPID); alarmTime = String.format("%04d", struAIOPVideo.struTime.wYear) + "-" + String.format("%02d", struAIOPVideo.struTime.wMonth) + "-" + String.format("%02d", struAIOPVideo.struTime.wDay) + " " + String.format("%02d", struAIOPVideo.struTime.wHour) + ":" + String.format("%02d", struAIOPVideo.struTime.wMinute) + ":" + String.format("%02d", struAIOPVideo.struTime.wSecond) + "." + String.format("%03d", struAIOPVideo.struTime.wMilliSec); alarmName = "AI开放平台接入视频检测"; switch (szMPID) { case "H930_MODEL_SPD": alarmType = "抽烟打电话"; break; } //图片数据保存 if (struAIOPVideo.dwPictureSize > 0) { //将字节写入文件 long offset = 0; ByteBuffer buffers = struAIOPVideo.pBufferPicture.getByteBuffer(offset, struAIOPVideo.dwPictureSize); byte[] bytes = new byte[struAIOPVideo.dwPictureSize]; buffers.rewind(); buffers.get(bytes); InputStream input = new ByteArrayInputStream(bytes); try { String bucketName = "pic"; String objectName = "alarm/" + IdUtils.simpleUUID() + ".jpeg"; boolean uploadObject = MinioUtil.uploadObject(bucketName, objectName, input, input.available(), "image/JPEG"); if (uploadObject) { picUrl = MinioUtil.getBucketObjectUrl(bucketName, objectName); //log.debug("上传文件成功!" + picUrl); } } catch (Exception e) { log.error("AI开放平台接入视频检测报警上传上报图片保存失败" + e.getMessage()); } } if (ardEquipExternal != null) { superBrainEventHandler(ardEquipExternal, struAIOPVideo.dwChannel, alarmName, alarmType, alarmTime, picUrl, ""); } //endregion break; case COMM_ISAPI_ALARM: log.debug("安全帽检测报警信息上报"); //region 安全帽检测报警信息 HCNetSDK.NET_DVR_ALARM_ISAPI_INFO struEventISAPI = new HCNetSDK.NET_DVR_ALARM_ISAPI_INFO(); struEventISAPI.write(); Pointer pEventISAPI = struEventISAPI.getPointer(); pEventISAPI.write(0, pAlarmInfo.getByteArray(0, struEventISAPI.size()), 0, struEventISAPI.size()); struEventISAPI.read(); int dwAlarmDataLen = struEventISAPI.dwAlarmDataLen; int byDataType = struEventISAPI.byDataType; int byPicturesNumber = struEventISAPI.byPicturesNumber; //处理安全帽检测上传的照片数据 Pointer pPicPackData = struEventISAPI.pPicPackData; if (pPicPackData != null) { HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA struPicData = new HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA(); struPicData.write(); if (struPicData.size() > 0) { Pointer pPicData = struPicData.getPointer(); pPicData.write(0, pPicPackData.getByteArray(0, struPicData.size()), 0, struPicData.size()); struPicData.read(); int dwPicLen = struPicData.dwPicLen; pPicData = struPicData.pPicData; if (dwPicLen > 0 && pPicData != null) { //将字节写入文件 long offset = 0; ByteBuffer buffers = pPicData.getByteBuffer(offset, dwPicLen); byte[] bytes = new byte[dwPicLen]; buffers.rewind(); buffers.get(bytes); InputStream input = new ByteArrayInputStream(bytes); try { String bucketName = "pic"; String objectName = "alarm/" + IdUtils.simpleUUID() + ".jpeg"; boolean uploadObject = MinioUtil.uploadObject(bucketName, objectName, input, input.available(), "image/JPEG"); if (uploadObject) { picUrl = MinioUtil.getBucketObjectUrl(bucketName, objectName); //log.debug("上传文件成功!" + picUrl); } } catch (Exception e) { log.error("异常行为识别信息报警信息上报图片保存失败" + e.getMessage()); } } } } if (ardEquipExternal != null) { alarmName = "安全帽检测"; alarmType = "安全帽事件报警"; alarmTime = fmt.format(new Date()); superBrainEventHandler(ardEquipExternal, 0, alarmName, alarmType, alarmTime, picUrl, ""); } //endregion break; case COMM_GISINFO_UPLOAD: log.debug("GPS报警信息上报(暂不解析)"); break; case COMM_ALARMHOST_CID_ALARM: log.debug("报警主机CID报告信息上报"); //region 报警主机报警处理 HCNetSDK.NET_DVR_CID_ALARM netDvrCidAlarm = new HCNetSDK.NET_DVR_CID_ALARM(); netDvrCidAlarm.write(); Pointer pCIDInfo = netDvrCidAlarm.getPointer(); pCIDInfo.write(0, pAlarmInfo.getByteArray(0, netDvrCidAlarm.size()), 0, netDvrCidAlarm.size()); netDvrCidAlarm.read(); //CID事件名 String sCIDCode = ByteUtils.bytesToStringZh(netDvrCidAlarm.sCIDCode);//CID事件名 //CID事件名 //String sCIDDescribe = ByteUtils.bytesToStringZh(netDvrCidAlarm.sCIDDescribe);//CID事件名 int position = ByteUtils.findIndexOfDoubleZero(netDvrCidAlarm.sCIDDescribe); String sCIDDescribe; if (position != -1) { byte[] result = new byte[position]; System.arraycopy(netDvrCidAlarm.sCIDDescribe, 0, result, 0, position); sCIDDescribe = ByteUtils.bytesToStringZh(result); } else { sCIDDescribe = ByteUtils.bytesToStringZh(netDvrCidAlarm.sCIDDescribe); } //触发报警的时间点 String struTriggerTime = DateUtils.convertDate(parseAlarmTime(netDvrCidAlarm.struTriggerTime), "yyyy-M-d H:m:s"); //上传报警的时间点 String struUploadTime = DateUtils.convertDate(parseAlarmTime(netDvrCidAlarm.struUploadTime), "yyyy-M-d H:m:s"); //报告类型 String byReportType = ""; switch (netDvrCidAlarm.byReportType) { case 1: byReportType = "防区报警"; break; case 2: byReportType = "视频报警"; break; case 3: byReportType = "软防区报警"; break; case 4: byReportType = "挟持报警"; break; case 5: byReportType = "防拆报警 "; break; case 6: byReportType = "操作报告 "; break; case 7: byReportType = "异常报告 "; break; } //子系统号 Integer bySubSysNo = Integer.valueOf(netDvrCidAlarm.bySubSysNo); //防区号 起始0 Integer wDefenceNo = Integer.valueOf(netDvrCidAlarm.wDefenceNo); //防区名称 String wDefenceName = ""; if (!bySubSysNo.equals(-1)) { CameraCmd cmd = new CameraCmd(); cmd.setCameraId(ardEquipExternal.getId()); cmd.setWZoneIndex(wDefenceNo); wDefenceName = HikClientUtil.getDefenseZoneName(cmd); } if (sCIDDescribe.contains("恢复")) { return true; } ExternalAlarmEventInfo externalAlarmEventInfo = new ExternalAlarmEventInfo(); externalAlarmEventInfo.setAlarmId(sCIDCode); externalAlarmEventInfo.setAlarmName(sCIDDescribe); externalAlarmEventInfo.setDefenseId((wDefenceNo + 1)); externalAlarmEventInfo.setDefenseName(wDefenceName); externalAlarmEventInfo.setSubSysNo(bySubSysNo); externalAlarmEventInfo.setAlarmType(byReportType); externalAlarmEventInfo.setAlarmTime(struTriggerTime); externalAlarmEventInfo.setAlarmHostId(ardEquipExternal.getId()); publishMqtt(externalAlarmEventInfo, "external"); //endregion break; case HCNetSDK.COMM_ALARM_ACS: log.debug("门禁主机信息上报"); //region 门禁主机报警处理 HCNetSDK.NET_DVR_ACS_ALARM_INFO strACSInfo = new HCNetSDK.NET_DVR_ACS_ALARM_INFO(); strACSInfo.write(); Pointer acsInfo = strACSInfo.getPointer(); acsInfo.write(0, pAlarmInfo.getByteArray(0, strACSInfo.size()), 0, strACSInfo.size()); strACSInfo.read(); int dwMajor = strACSInfo.dwMajor; if (dwMajor != 5) { //只获取事件 // log.debug("非事件报警数据上报(暂不解析)"); break; } int dwMinor = strACSInfo.dwMinor; if (dwMinor != 80 && dwMinor != 104) { //只解析真人检测失败和人脸抓拍失败的次类型事件 break; } switch (dwMinor) { case 104: alarmType = "真人检测失败"; break; case 80: alarmType = "人脸抓拍失败"; break; } log.debug("门禁主机报警上传:" + alarmType); alarmTime = DateUtils.convertDate(parseAlarmTime(strACSInfo.struTime), "yyyy-M-d H:m:s"); //事件图片处理 dwPicDataLen = strACSInfo.dwPicDataLen; if (dwPicDataLen > 0) { try { //将字节写入文件 long offset = 0; ByteBuffer buffers = strACSInfo.pPicData.getByteBuffer(offset, strACSInfo.dwPicDataLen); byte[] bytes = new byte[strACSInfo.dwPicDataLen]; buffers.rewind(); buffers.get(bytes); InputStream input = new ByteArrayInputStream(bytes); String bucketName = "pic"; String objectName = "alarm/" + IdUtils.simpleUUID() + ".jpeg"; boolean uploadObject = MinioUtil.uploadObject(bucketName, objectName, input, input.available(), "image/JPEG"); if (uploadObject) { picUrl = MinioUtil.getBucketObjectUrl(bucketName, objectName); //log.debug("上传文件成功!" + url); } } catch (Exception e) { log.error("图片保存失败"); } } HCNetSDK.NET_DVR_ACS_EVENT_INFO struAcsEventInfo = strACSInfo.struAcsEventInfo; int byType = struAcsEventInfo.byType; String defenseType = ""; switch (byType) { case 0: defenseType = "即时防区"; break; case 1: defenseType = "24小时防区"; break; case 2: defenseType = "延时防区"; break; case 3: defenseType = "内部防区"; break; case 4: defenseType = "钥匙防区"; break; case 5: defenseType = "火警防区"; break; case 6: defenseType = "周界防区"; break; case 7: defenseType = "24小时无声防区"; break; case 8: defenseType = "24小时辅助防区"; break; case 9: defenseType = "24小时震动防区"; break; case 10: defenseType = "门禁紧急开门防区"; break; case 11: defenseType = "门禁紧急关门防区"; break; default: defenseType = "无"; break; } AccessControlHostEventInfo accessControlHostEventInfo = new AccessControlHostEventInfo(); accessControlHostEventInfo.setSerialNo(struAcsEventInfo.dwSerialNo); accessControlHostEventInfo.setAlarmType(alarmType); accessControlHostEventInfo.setDoorNo(struAcsEventInfo.dwDoorNo); accessControlHostEventInfo.setDefenseType(defenseType); accessControlHostEventInfo.setAlarmTime(alarmTime); accessControlHostEventInfo.setPicUrl(picUrl); accessControlHostEventInfo.setAcsId(ardEquipExternal.getId()); publishMqtt(accessControlHostEventInfo, "accessControl"); //endregion break; case COMM_FIREDETECTION_ALARM: //火点检测报警 log.debug("火点检测信息上报"); //region 火点检测报警 HCNetSDK.NET_DVR_FIREDETECTION_ALARM struFireDecAlarm = new HCNetSDK.NET_DVR_FIREDETECTION_ALARM(); struFireDecAlarm.write(); Pointer pFireDecAlarm = struFireDecAlarm.getPointer(); pFireDecAlarm.write(0, pAlarmInfo.getByteArray(0, struFireDecAlarm.size()), 0, struFireDecAlarm.size()); struFireDecAlarm.read(); String sFireDecAlarmInfo = "绝对时间:" + struFireDecAlarm.dwAbsTime + ",报警子类型:" + struFireDecAlarm.byAlarmSubType + ",火点最高温度 :" + struFireDecAlarm.wFireMaxTemperature + ",火点目标距离:" + struFireDecAlarm.wTargetDistance; System.out.println(sFireDecAlarmInfo); //图片保存 if ((struFireDecAlarm.dwPicDataLen > 0) && (struFireDecAlarm.byPicTransType == 0)) { long offset = 0; ByteBuffer buffers = struFireDecAlarm.pBuffer.getByteBuffer(offset, struFireDecAlarm.dwPicDataLen); byte[] bytes = new byte[struFireDecAlarm.dwPicDataLen]; buffers.rewind(); buffers.get(bytes); InputStream input = new ByteArrayInputStream(bytes); try { String bucketName = "pic"; String objectName = "alarm/" + IdUtils.simpleUUID() + ".jpeg"; boolean uploadObject = MinioUtil.uploadObject(bucketName, objectName, input, input.available(), "image/JPEG"); if (uploadObject) { picUrl = MinioUtil.getBucketObjectUrl(bucketName, objectName); //log.debug("上传文件成功!" + picUrl); } } catch (Exception e) { log.error("火点检测事件报警上报热成像图片保存失败" + e.getMessage()); } } if (ardEquipExternal != null) { alarmName = "火焰检测信息上报"; alarmType = "火焰检测事件报警"; int dwVisibleChannel = struFireDecAlarm.dwVisibleChannel; alarmTime = DateUtils.parseTime(struFireDecAlarm.dwAbsTime);//报警时间 superBrainEventHandler(ardEquipExternal, dwVisibleChannel, alarmName, alarmType, alarmTime, picUrl, ""); } //endregion break; case COMM_ITS_PLATE_RESULT://交通抓拍的终端图片上传 log.debug("交通抓拍的终端图片上报(暂不解析)"); break; default: log.debug("未知报警事件类型:lCommand:" + Integer.toHexString(lCommand)); break; } return true; } /** * 相机事件处理 * 刘苏义 * 2023/12/5 12:56:47 */ public void cameraEventHandler(ArdCameras camera, String alarmName, String alarmType, String alarmTime, String picUrl) { CameraEventInfo cameraEventInfo = new CameraEventInfo(); cameraEventInfo.setAlarmName(alarmName); cameraEventInfo.setAlarmTime(alarmTime); cameraEventInfo.setCameraName(camera.getName()); cameraEventInfo.setCameraId(camera.getId()); cameraEventInfo.setCameraIp(camera.getIp()); cameraEventInfo.setCameraPort(camera.getPort()); cameraEventInfo.setCameraType(camera.getGdType()); cameraEventInfo.setCameraChannel(camera.getChannel()); cameraEventInfo.setLongitude(camera.getLongitude()); cameraEventInfo.setLatitude(camera.getLatitude()); cameraEventInfo.setAlarmType(alarmType); //图片存入minio if (picUrl.equals("")) { picUrl = snapPicture(cameraEventInfo); } cameraEventInfo.setPicUrl(picUrl); log.debug("处理通用光电事件报警:【Name】" + alarmName + "【Type】" + alarmType + "【Time】" + alarmTime + "【Device】" + camera.getName() + "【Url】" + picUrl); publishMqtt(cameraEventInfo, "camera"); } /** * 超脑事件处理 * 刘苏义 * 2023/12/5 12:56:47 */ public void superBrainEventHandler(ArdEquipExternal superBrainDevice, Integer chanNo, String alarmName, String alarmType, String alarmTime, String picUrl, String faceUrl) { SuperBrainEventInfo superBrainEventInfo = new SuperBrainEventInfo(); superBrainEventInfo.setAlarmName(alarmName); superBrainEventInfo.setAlarmType(alarmType); superBrainEventInfo.setAlarmTime(alarmTime); superBrainEventInfo.setDeviceId(superBrainDevice.getId()); superBrainEventInfo.setDeviceName(superBrainDevice.getName()); superBrainEventInfo.setChanNo(chanNo); superBrainEventInfo.setLongitude(superBrainDevice.getLongitude()); superBrainEventInfo.setLatitude(superBrainDevice.getLatitude()); superBrainEventInfo.setPicUrl(picUrl); superBrainEventInfo.setFaceUrl(faceUrl); log.debug("处理通超脑事件报警:【Name】" + alarmName + "【Type】" + alarmType + "【Time】" + alarmTime + "【Device】" + superBrainDevice.getName() + "【Url】" + picUrl + "【faceUrl】" + faceUrl ); publishMqtt(superBrainEventInfo, "superBrain"); } /** * 根据给定的IP地址和端口查找ArdEquipExternal对象 * * @param ip IP地址 * @param port 端口 * @return ArdEquipExternal对象的Optional,如果找到则有值,否则为空 */ public static Optional findArdEquipExternalByIpAndPort(String ip, int port) { return ArdEquipExternalServiceImpl.ardEquipExternalList.stream() .filter(equip -> equip.getIp().equals(ip) && equip.getPort() == port) .findFirst(); } /** * 根据IP地址和端口查找ArdCamera对象。 * * @param ip IP地址 * @param port 端口 * @return ArdCamera对象的Optional,如果找不到则为空 */ public static Optional findArdCameraByIpAndPort(String ip, int port) { return ArdCamerasServiceImpl.ardCameraList.stream() .filter(camera -> camera.getIp().equals(ip) && camera.getPort() == port) .findFirst(); } /** * 相机截图 */ private String snapPicture(CameraEventInfo info) { CameraCmd cmd = new CameraCmd(); String key = info.getCameraIp() + ":" + info.getCameraPort(); ArdCameras Cameras = GlobalVariable.cameraMap.get(key); if (Cameras == null) { return ""; } cmd.setCameraId(Cameras.getId()); cmd.setChannelNum(info.getCameraChannel()); cmd.setBucketName("pic"); String filename = "alarm" + "/" + IdUtils.simpleUUID() + ".jpg"; cmd.setObjectName(filename); String url = HikClientUtil.picCutCate(cmd); return url; } /** * 打印通用光电日志 */ private void printLog(CameraEventInfo info) { log.debug("【规则】" + info.getAlarmName() + "【规则id】" + info.getRuleId() + "【类型】" + info.getAlarmType() + "【时间】" + info.getAlarmTime() + "【相机id】" + info.getCameraId() + "【相机】" + info.getCameraName() + "【IP】" + info.getCameraIp() + "【通道】" + info.getCameraChannel() + "【型号】" + info.getCameraType() + "【图片】" + info.getPicUrl() + "【坐标】" + info.getLongitude() + "," + info.getLatitude()); } /** * 打印报警主机事件日志 */ private void printLog(ExternalAlarmEventInfo info) { log.debug("【报警ID】" + info.getAlarmId() + "【报警名称】" + info.getAlarmName() + "【防区ID】" + info.getDefenseId() + "【防区名称】" + info.getDefenseName() + "【类型】" + info.getAlarmType() + "【时间】" + info.getAlarmTime() + "【报警主机ID】" + info.getAlarmHostId()); } /** * 打印门禁事件日志 */ private void printLog(AccessControlHostEventInfo info) { log.debug("【报警ID】" + info.getSerialNo() + "【设备编号】" + "【门编号】" + info.getDoorNo() + "【事件类型】" + info.getAlarmType() + "【防区类型】" + info.getDefenseType() + "【时间】" + info.getAlarmTime() + "【门禁主机ID】" + info.getAcsId()); } /** * 发布MQTT消息 * * @param info 消息内容 * @param topic 主题 */ private void publishMqtt(T info, String topic) { MqttProducer.publish(2, false, topic, JSON.toJSONString(info)); } /** * 时间格式化 */ private String parseAlarmTime(HCNetSDK.NET_DVR_TIME_EX netDvrTimeEx) { short wYear = netDvrTimeEx.wYear; byte byMonth = netDvrTimeEx.byMonth; byte byDay = netDvrTimeEx.byDay; byte byHour = netDvrTimeEx.byHour; byte byMinute = netDvrTimeEx.byMinute; byte bySecond = netDvrTimeEx.bySecond; return wYear + "-" + byMonth + "-" + byDay + " " + byHour + ":" + byMinute + ":" + bySecond; } /** * 时间格式化 */ private String parseAlarmTime(HCNetSDK.NET_DVR_TIME netDvrTimeEx) { int wYear = netDvrTimeEx.dwYear; int byMonth = netDvrTimeEx.dwMonth; int byDay = netDvrTimeEx.dwDay; int byHour = netDvrTimeEx.dwHour; int byMinute = netDvrTimeEx.dwMinute; int bySecond = netDvrTimeEx.dwSecond; return wYear + "-" + byMonth + "-" + byDay + " " + byHour + ":" + byMinute + ":" + bySecond; } }