‘liusuyi’
2023-11-29 2574db703fa175765394dba9d4e0d623a1bcd16a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package com.ard.utils.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.hiksdk.domain.*;
import com.ard.utils.minio.MinioUtil;
import com.ard.utils.other.ByteUtils;
import com.ard.utils.hiksdk.common.GlobalVariable;
import com.ard.utils.hiksdk.util.hikSdkUtil.HCNetSDK;
import com.ard.utils.other.DateUtils;
import com.ard.utils.mqtt.MqttProducer;
import com.ard.utils.uuid.IdUtils;
import com.sun.jna.Pointer;
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.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<ArdCameras> foundCamera = findArdCameraByIpAndPort(sDeviceIP, wLinkPort);
        Optional<ArdEquipExternal> 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 info;
        String alarmTime;//事件时间
        String alarmType = "";//报警类型
        String picUrl = "";//事件图片
        String facePicUrl = "";//人脸图片
        //lCommand是传的报警类型
        log.debug("【" + sDeviceIP + ":" + wLinkPort + "】报警事件类型:lCommand:" + Integer.toHexString(lCommand));
        switch (lCommand) {
            case COMM_ALARM_V30:
                log.debug("移动侦测报警信息上报");
                if (camera == null) {
                    return false;
                }
                //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:
                        info = new CameraEventInfo();
                        info.setAlarmName("移动侦测");
                        info.setAlarmTime(fmt.format(new Date()));
                        info.setCameraName(camera.getName());
                        info.setCameraId(camera.getId());
                        info.setCameraIp(camera.getIp());
                        info.setCameraPort(camera.getPort());
                        info.setCameraType(camera.getGdType());
                        info.setCameraChannel(Integer.valueOf(netDvrAlarminfoV30.byChannel[0]));
                        info.setLongitude(camera.getLongitude());
                        info.setLatitude(camera.getLatitude());
                        info.setAlarmType("移动侦测");
                        //图片存入minio
                        picUrl = snapPicture(info);
                        info.setPicUrl(picUrl);
                        publishMqtt(info, "camera");
                        break;
                }
                //endregion
                break;
            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);//通道
                info = new CameraEventInfo();
                info.setAlarmName("人员行为分析");
                info.setAlarmTime(alarmTime);
                info.setCameraName(camera.getName());
                info.setCameraId(camera.getId());
                info.setCameraIp(camera.getIp());
                info.setCameraPort(camera.getPort());
                info.setCameraType(camera.getGdType());
                info.setCameraChannel(channel);
                info.setLongitude(camera.getLongitude());
                info.setLatitude(camera.getLatitude());
                info.setRuleId(ruleID);
                switch (strVcaAlarm.struRuleInfo.wEventTypeEx) {
                    case 1: //region穿越警戒面 (越界侦测)
                        info.setAlarmType("越界侦测报警");
                        strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_TRAVERSE_PLANE.class);
                        //图片存入minio
                        picUrl = snapPicture(info);
                        info.setPicUrl(picUrl);
                        publishMqtt(info, "camera");
                        //endregion
                        break;
//                    case 2: //region 目标进入区域
//                        info.setAlarmType("目标进入区域报警");
//                        strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_AREA.class);
//                        //endregion
//                        break;
//                    case 3: //region 目标离开区域
//                        info.setAlarmType("目标离开区域报警");
//                        strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_AREA.class);
//                        //endregion
//                        break;
                    case 4: //region 周界入侵
                        info.setAlarmType("周界入侵报警");
                        strVcaAlarm.struRuleInfo.uEventParam.setType(HCNetSDK.NET_VCA_INTRUSION.class);
                        //图片存入minio
                        picUrl = snapPicture(info);
                        info.setPicUrl(picUrl);
                        publishMqtt(info, "camera");
                        //endregion
                        break;
//                    case 5: //region 徘徊
//                        info.setAlarmType("徘徊事件报警");
//                        //endregion
//                        break;
//                    case 8: //region 快速移动(奔跑)
//                        info.setAlarmType("快速移动(奔跑)事件报警");
//                        //endregion
//                        break;
//                    case 13: //region 物品遗留事件
//                        info.setAlarmType("物品遗留事件报警");
//                        //endregion
//                        break;
//                    case 14: //region 物品拿取事件
//                        info.setAlarmType("物品拿取事件事件报警");
//                        //endregion
//                        break;
//                    case 20: //region 倒地检测
//                        info.setAlarmType("倒地事件触发");
//                        //endregion
//                        break;
//                    case 44: //region 玩手机
//                        info.setAlarmType("玩手机报警事件");
//                        //endregion
//                        break;
                    default:
                        log.debug("未知异常行为事件类型:" + strVcaAlarm.struRuleInfo.wEventTypeEx);
                        break;
                }
                //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.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 picTransType = "";
                switch (struFaceMatchAlarm.byPicTransType) {
                    case 0:
                        picTransType = "二进制图片";
                        break;
                    case 1:
                        picTransType = "URL路径图片";
                        break;
                }
                String contrastStatus = "";
                switch (struFaceMatchAlarm.byContrastStatus) {
                    case 1:
                        contrastStatus = "比对成功";
 
                        break;
                    case 2:
                        contrastStatus = "比对失败";
                        break;
                    default:
                        contrastStatus = "未知";
                        break;
                }
                switch (struFaceMatchAlarm.struBlockListInfo.struBlockListInfo.byType) {
                    case 1:
                        alarmType = "陌生人报警";
                        break;
                    case 2:
                        alarmType = "人脸比对报警";
                        break;
                    default:
                        alarmType = "未知";
                        break;
                }
                if (struFaceMatchAlarm.dwSnapPicLen > 0 && struFaceMatchAlarm.pSnapPicBuffer != null && struFaceMatchAlarm.byPicTransType == 0) {
                    //将字节写入文件
                    long offset = 0;
                    ByteBuffer buffers = struFaceMatchAlarm.pSnapPicBuffer.getByteBuffer(offset, struFaceMatchAlarm.dwSnapPicLen);
                    byte[] bytes = new byte[struFaceMatchAlarm.dwSnapPicLen];
                    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());
                    }
                }
                if (struFaceMatchAlarm.struBlockListInfo.dwBlockListPicLen > 0 && struFaceMatchAlarm.struBlockListInfo.pBuffer1 != null) {
                    //将字节写入文件
                    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);
                    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());
                    }
                }
                String sAlarmType = "";
                try {
                    sAlarmType = "人脸黑名单比对报警,相识度:" + struFaceMatchAlarm.fSimilarity + ",黑名单姓名:" +
                            new String(struFaceMatchAlarm.struBlockListInfo.struBlockListInfo.struAttribute.byName, "GBK").trim() + ",黑名单证件信息:" +
                            new String(struFaceMatchAlarm.struBlockListInfo.struBlockListInfo.struAttribute.byCertificateNumber).trim();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                log.debug(sAlarmType);
                log.debug("【人脸比对报警信息】:" + " 比对结果:" + contrastStatus + " 比对类型:" + alarmType + " 报警时间:" + alarmTime + " 匹配图片数量:" + byMatchPicNum + " 图片类型:" + picTransType + " 比对图片:" + picUrl);
                FaceMatchEventInfo faceMatchEventInfo = new FaceMatchEventInfo();
                faceMatchEventInfo.setAlarmTime(alarmTime);
                faceMatchEventInfo.setStatus(contrastStatus);
                faceMatchEventInfo.setAlarmType(alarmType);
                publishMqtt(faceMatchEventInfo, "faceMatch");
                //endregion
                break;
            case COMM_UPLOAD_AIOP_VIDEO:
                log.debug("设备支持AI开放平台接入,上传视频检测数据(暂不解析)");
                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;
 
                //处理安全帽检测上传的照片数据
                HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA struPicData = new HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA();
                struPicData.write();
                Pointer pPicData = struPicData.getPointer();
                pPicData.write(0, struEventISAPI.pPicPackData.getByteArray(0, struPicData.size()), 0,
                        struPicData.size());
                struPicData.read();
                log.debug("检测到安全帽事件");
                //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");
 
                //事件图片处理
                int 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;
            default:
                log.debug("未知报警事件类型:lCommand:" + Integer.toHexString(lCommand));
                break;
 
        }
        return true;
    }
 
    /**
     * 根据给定的IP地址和端口查找ArdEquipExternal对象
     *
     * @param ip   IP地址
     * @param port 端口
     * @return ArdEquipExternal对象的Optional,如果找到则有值,否则为空
     */
    public static Optional<ArdEquipExternal> 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<ArdCameras> 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 <T> 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;
    }
}