1
zhanghua
2024-09-28 d385d4d35e20d7b24bc6a00cac20c5fd5374c2d8
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
package com.dahua.netsdk.lib.callback.impl;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Objects;
 
import com.alibaba.fastjson.JSON;
import com.dahua.netsdk.lib.Utils;
import com.dahua.netsdk.lib.enumeration.EM_EVENT_IVS_TYPE;
import com.dahua.netsdk.lib.structure.*;
import com.sun.jna.Pointer;
import com.dahua.netsdk.lib.NetSDKLib;
import com.dahua.netsdk.lib.ToolKits;
import com.ycl.entity.video.VideoAlarmReport;
import com.ycl.service.oss.OssService;
import org.springframework.beans.factory.annotation.Autowired;
 
/**
 * 智能报警事件回调
 */
public class AnalyzerDataCB implements NetSDKLib.fAnalyzerDataCallBack {
 
 
    @Autowired
    private OssService ossService;
 
    private final File picturePath;
    private static AnalyzerDataCB instance;
 
    private AnalyzerDataCB() {
        picturePath = new File("./AnalyzerPicture/");
        if (!picturePath.exists()) {
            picturePath.mkdirs();
        }
    }
 
    public static AnalyzerDataCB getInstance() {
        if (instance == null) {
            instance = new AnalyzerDataCB();
        }
        return instance;
    }
 
    /**
     * @param lAnalyzerHandle 订阅句柄
     * @param dwAlarmType     事件类型枚举
     * @param pAlarmInfo      事件信息,具体结构体视枚举而定,每个事件类型对应不同的事件结构体
     * @param pBuffer         图片信息
     * @param dwBufSize       图片长度
     * @param dwUser          用户自定义数据
     * @param nSequence       表示上传的相同图片情况,为0时表示是第一次出现,为2表示最后一次出现或仅出现一次,为1表示此次之后还有
     * @param reserved        表示当前回调数据的状态,
     *                        为0表示当前数据为实时数据,为1表示当前回调数据是离线数据,为2时表示离线数据传送结束
     * @return
     */
    public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer,
                      int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) {
 
        VideoAlarmReport videoAlarmReport = VideoAlarmReport.builder().ip("172.28.194.186").port(37777).user("admin").password("scdx@123").build();
        if (lAnalyzerHandle == null || lAnalyzerHandle.longValue() == 0) {
            return -1;
        }
 
        NetSDKLib.NET_EVENT_FILE_INFO stuFileInfo = null;
        NetSDKLib.NET_PIC_INFO stPicInfo = null;
        System.out.println("接收到事件报警类型: " + dwAlarmType);
        // 1-机动车违停 2-非机动车违停 3-非法广告 4-群体事件 5-粘贴广告 6-违规撑伞 7-占道经营 8-店外经营 9-垃圾桶满溢 10-打包垃圾 11-暴露垃圾 12-沿街晾晒 13-乱堆物料 14-违规户外广告
        switch (Objects.requireNonNull(EM_EVENT_IVS_TYPE.getEventType(dwAlarmType))) {
            case EVENT_IVS_PARKINGDETECTION: // 非法停车事件
            {
                NetSDKLib.DEV_EVENT_PARKINGDETECTION_INFO msg = new NetSDKLib.DEV_EVENT_PARKINGDETECTION_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                stPicInfo = msg.stuObject.stPicInfo;
                System.out.printf("【非法停车事件】 时间(UTC):%s 通道号:%d 开始时间:%s 结束时间:%s 事件触发累计次数:%d 事件源设备唯一标识:%s \n", msg.UTC,
                        msg.nChannelID, msg.stuObject.stuStartTime, msg.stuObject.stuEndTime, msg.nOccurrenceCount,
                        new String(msg.szSourceDevice));
                break;
            }
            case EVENT_IVS_FIGHTDETECTION: // 斗殴事件
            {
                NetSDKLib.DEV_EVENT_FIGHT_INFO msg = new NetSDKLib.DEV_EVENT_FIGHT_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                String Picture = picturePath + "\\" + System.currentTimeMillis() + ".jpg";
                ToolKits.savePicture(pBuffer, 0, dwBufSize, Picture);
                System.out.println(" 斗殴事件 时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID);
                break;
            }
            case EVENT_IVS_RIOTERDETECTION: // 聚众事件
            {
                NetSDKLib.DEV_EVENT_RIOTERL_INFO msg = new NetSDKLib.DEV_EVENT_RIOTERL_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                stPicInfo = msg.stuObjectIDs[0].stPicInfo; // 取第一个
                System.out.printf("【聚众事件】 时间(UTC):%s 通道号:%d 检测到的物体个数:%d 事件触发累计次数:%d 事件源设备唯一标识:%s \n", msg.UTC,
                        msg.nChannelID, msg.nObjectNum, msg.nOccurrenceCount, new String(msg.szSourceDevice));
                break;
            }
            case EVENT_IVS_MAN_NUM_DETECTION: /// < 立体视觉区域内人数统计事件
            {
                NetSDKLib.DEV_EVENT_MANNUM_DETECTION_INFO msg = new NetSDKLib.DEV_EVENT_MANNUM_DETECTION_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                System.out.println(" 立体视觉区域内人数统计事件 时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + " 区域人员列表数量:"
                        + msg.nManListCount + " 人员身高:" + msg.stuManList[0].nStature);
                break;
            }
            case EVENT_IVS_CROWDDETECTION: /// < 人群密度检测事件
            {
                NetSDKLib.DEV_EVENT_CROWD_DETECTION_INFO msg = new NetSDKLib.DEV_EVENT_CROWD_DETECTION_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                System.out.println(" 人群密度检测事件 时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID);
                break;
            }
            case EVENT_IVS_CITY_MOTORPARKING: /// 1 城市机动车违停事件
            {
                NetSDKLib.DEV_EVENT_CITY_MOTORPARKING_INFO msg = new NetSDKLib.DEV_EVENT_CITY_MOTORPARKING_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                System.out.println(" 城市机动车违停事件 时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + "违停持续时长(单位秒):"
                        + msg.nParkingDuration + "检测到的物体个数:" + msg.nObjectNum);
                System.out.println("stuEventInfoEx = " + msg.stuEventInfoEx.toString());
                System.out.println("byVehicleHeadDirection = " + msg.byVehicleHeadDirection);
                if (stuFileInfo != null) { // 保存图片
                    String bigPicture = picturePath + File.separator
                            + String.format("Big_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType,
                            stuFileInfo.stuFileTime.toStringTitle(), stuFileInfo.bCount, stuFileInfo.bIndex,
                            stuFileInfo.bFileType, stuFileInfo.nGroupId);
                    ToolKits.savePicture(pBuffer, 0, dwBufSize, bigPicture);
                    if (stPicInfo != null && stPicInfo.dwFileLenth > 0) {
                        String smallPicture = picturePath + File.separator
                                + String.format("Small_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType,
                                stuFileInfo.stuFileTime.toStringTitle(), stuFileInfo.bCount, stuFileInfo.bIndex,
                                stuFileInfo.bFileType, stuFileInfo.nGroupId);
                        ToolKits.savePicture(pBuffer, stPicInfo.dwOffSet, stPicInfo.dwFileLenth, smallPicture);
                    }
                }
                if (msg.nImageInfoNum > 0) {
                    System.out.println("Picture Arr got, number +" + msg.nImageInfoNum);
                    NET_IMAGE_INFO_EX2[] pstuImageInfo = new NET_IMAGE_INFO_EX2[msg.nImageInfoNum];
                    for (int i = 0; i < msg.nImageInfoNum; i++) {
                        pstuImageInfo[i] = new NET_IMAGE_INFO_EX2();
                    }
                    ToolKits.GetPointerDataToStructArr(msg.pstuImageInfo, pstuImageInfo);
                    for (int i = 0; i < msg.nImageInfoNum; i++) {
                        System.out.println("pstuImageInfo[" + i + "].nOffset:" + pstuImageInfo[i].nOffset);
                        System.out.println("pstuImageInfo[" + i + "].nLength:" + pstuImageInfo[i].nLength);
                        if (pstuImageInfo != null && pstuImageInfo[i].nLength > 0) {
                            System.out.println("pstuImageInfo[" + i + "] picture received!");
                            String bigPicture = picturePath + "\\" + System.currentTimeMillis()
                                    + "related_MOTORPARKING_pstuImageInfo_" + i + ".jpg";
                            ToolKits.savePicture(pBuffer, pstuImageInfo[i].nOffset, pstuImageInfo[i].nLength, bigPicture);
                        }
                    }
 
                }
 
                break;
            }
            case EVENT_IVS_CITY_NONMOTORPARKING: /// 2 城市非机动车违停事件
            {
                NetSDKLib.DEV_EVENT_CITY_NONMOTORPARKING_INFO msg = new NetSDKLib.DEV_EVENT_CITY_NONMOTORPARKING_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                System.out.println(
                        " 城市非机动车违停事件 时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + "检测到的物体个数:" + msg.nObjectNum);
                break;
            }
            case EVENT_IVS_HOLD_UMBRELLA: /// 6 违规撑伞检测事件
            {
                NetSDKLib.DEV_EVENT_HOLD_UMBRELLA_INFO msg = new NetSDKLib.DEV_EVENT_HOLD_UMBRELLA_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                System.out.println(
                        " 违规撑伞检测事件 时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + "违法持续时长(单位秒)" + msg.nViolationDuration);
                break;
            }
            case EVENT_IVS_SHOPPRESENCE: /// 7 占道经营
            {
                NetSDKLib.DEV_EVENT_SHOPPRESENCE_INFO msg = new NetSDKLib.DEV_EVENT_SHOPPRESENCE_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
 
            }
            case EVENT_IVS_DUSTBIN_OVER_FLOW: /// 9 垃圾桶满溢检测事件
            {
                NetSDKLib.DEV_EVENT_DUSTBIN_OVER_FLOW_INFO msg = new NetSDKLib.DEV_EVENT_DUSTBIN_OVER_FLOW_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                System.out.println(" 垃圾桶满溢检测事件 时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + "违法持续时长(单位秒)"
                        + msg.nViolationDuration + "检测到的物体个数" + msg.nObjectNum);
                break;
            }
            case EVENT_IVS_GARBAGE_PLASTICBAG: /// 10 打包垃圾
            {
 
            }
            case EVENT_IVS_DOOR_FRONT_DIRTY: /// < 门前脏乱检测事件
            {
                NetSDKLib.DEV_EVENT_DOOR_FRONT_DIRTY_INFO msg = new NetSDKLib.DEV_EVENT_DOOR_FRONT_DIRTY_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                System.out.println(" 门前脏乱检测事件  时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + "违法持续时长(单位秒)"
                        + msg.nViolationDuration + "检测到的物体个数" + msg.nObjectNum);
                break;
            }
            case EVENT_IVS_GARBAGE_EXPOSURE: /// 11 垃圾暴露检测事件
            {
                NetSDKLib.DEV_EVENT_GARBAGE_EXPOSURE_INFO msg = new NetSDKLib.DEV_EVENT_GARBAGE_EXPOSURE_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                stuFileInfo = msg.stuFileInfo;
                System.out.println(" 垃圾暴露检测事件  时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + "违法持续时长(单位秒)"
                        + msg.nViolationDuration + "检测到的物体个数" + msg.nObjectNum);
                break;
            }
            case EVENT_IVS_VIOLENT_THROW_DETECTION: /// < 暴力抛物事件事件
            {
                NetSDKLib.DEV_EVENT_VIOLENT_THROW_DETECTION_INFO msg = new NetSDKLib.DEV_EVENT_VIOLENT_THROW_DETECTION_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                if (msg.stuSceneImage != null && msg.stuSceneImage.nLength > 0) {
                    String bigPicture = picturePath + "\\" + System.currentTimeMillis() + "big.jpg";
                    ToolKits.savePicture(pBuffer, msg.stuSceneImage.nOffSet, msg.stuSceneImage.nLength, bigPicture);
                }
                stuFileInfo = msg.stuFileInfo;
                System.out.println(" 暴力抛物事件事件  时间(UTC):" + msg.UTC + " 通道号:" + msg.nChannelID + "暴力抛物检测区域名称"
                        + new String(msg.szRegionName, Charset.forName(Utils.getPlatformEncode())));
                break;
            }
            case EVENT_IVS_SHOP_WINDOW_POST:// 橱窗张贴事件
            {
                DEV_EVENT_SHOP_WINDOW_POST_INFO info = new DEV_EVENT_SHOP_WINDOW_POST_INFO();
                ToolKits.GetPointerData(pAlarmInfo, info);
                stuFileInfo = info.stuFileInfo;
                System.out.println(" 橱窗张贴事件  时间(UTC):" + info.UTC + " 通道号:" + info.nChannelID + " 商铺地址: "
                        + new String(info.szShopAddress, Charset.forName(Utils.getPlatformEncode())).trim() + " 违法持续时间: "
                        + info.nViolationDuration + " 检测到的物体数量: " + info.nObjectNum);
                for (int i = 0; i < info.nObjectNum; i++) {
                    stPicInfo = info.stuObjects[i].stPicInfo;
                    if (stPicInfo != null && stPicInfo.dwFileLenth > 0) {
                        String smallPicture = picturePath + File.separator
                                + String.format("Small_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType,
                                stuFileInfo.stuFileTime.toStringTitle(), stuFileInfo.bCount, stuFileInfo.bIndex,
                                stuFileInfo.bFileType, stuFileInfo.nGroupId);
                        ToolKits.savePicture(pBuffer, stPicInfo.dwOffSet, stPicInfo.dwFileLenth, smallPicture);
                    }
                }
 
                break;
            }
            case EVENT_IVS_STREET_SUNCURE: // 12 沿街晾晒事件
            {
                DEV_EVENT_STREET_SUNCURE_INFO info = new DEV_EVENT_STREET_SUNCURE_INFO();
                ToolKits.GetPointerData(pAlarmInfo, info);
                stuFileInfo = info.stuFileInfo;
                System.out.println(" 沿街晾晒事件  时间(UTC):" + info.UTC + " 通道号:" + info.nChannelID + " 违法持续时间: "
                        + info.nViolationDuration + " 检测到的物体数量: " + info.nObjectNum);
                for (int i = 0; i < info.nObjectNum; i++) {
                    stPicInfo = info.stuObjects[i].stPicInfo;
                    if (stPicInfo != null && stPicInfo.dwFileLenth > 0) {
                        String smallPicture = picturePath + File.separator
                                + String.format("Small_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType,
                                stuFileInfo.stuFileTime.toStringTitle(), stuFileInfo.bCount, stuFileInfo.bIndex,
                                stuFileInfo.bFileType, stuFileInfo.nGroupId);
                        ToolKits.savePicture(pBuffer, stPicInfo.dwOffSet, stPicInfo.dwFileLenth, smallPicture);
                    }
                }
                break;
            }
            case EVENT_IVS_OUTDOOR_ADVERTISEMENT: // 14 户外广告事件
            {
                DEV_EVENT_OUTDOOR_ADVERTISEMENT_INFO info = new DEV_EVENT_OUTDOOR_ADVERTISEMENT_INFO();
                ToolKits.GetPointerData(pAlarmInfo, info);
                stuFileInfo = info.stuFileInfo;
                System.out.println(" 户外广告事件  时间(UTC):" + info.UTC + " 通道号:" + info.nChannelID + " 违法持续时间: "
                        + info.nViolationDuration + " 检测到的物体数量: " + info.nObjectNum);
                for (int i = 0; i < info.nObjectNum; i++) {
                    stPicInfo = info.stuObjects[i].stPicInfo;
                    if (stPicInfo != null && stPicInfo.dwFileLenth > 0) {
                        String smallPicture = picturePath + File.separator
                                + String.format("Small_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType,
                                stuFileInfo.stuFileTime.toStringTitle(), stuFileInfo.bCount, stuFileInfo.bIndex,
                                stuFileInfo.bFileType, stuFileInfo.nGroupId);
                        ToolKits.savePicture(pBuffer, stPicInfo.dwOffSet, stPicInfo.dwFileLenth, smallPicture);
                    }
                }
                break;
            }
            case EVENT_IVS_HUDDLE_MATERIAL:// 13 乱堆物料检测事件   id 648
            {
                DEV_EVENT_HUDDLE_MATERIAL_INFO info = new DEV_EVENT_HUDDLE_MATERIAL_INFO();
                ToolKits.GetPointerData(pAlarmInfo, info);
                stuFileInfo = info.stuFileInfo;
                System.out.println(" 乱堆物料检测事件  时间(UTC):" + info.UTC + " 通道号:" + info.nChannelID + " 违法持续时间: "
                        + info.nViolationDuration + " 检测到的物体数量: " + info.nObjectNum);
                for (int i = 0; i < info.nObjectNum; i++) {
                    stPicInfo = info.stuObjects[i].stPicInfo;
                    if (stPicInfo != null && stPicInfo.dwFileLenth > 0) {
                        String smallPicture = picturePath + File.separator
                                + String.format("Small_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType,
                                stuFileInfo.stuFileTime.toStringTitle(), stuFileInfo.bCount, stuFileInfo.bIndex,
                                stuFileInfo.bFileType, stuFileInfo.nGroupId);
                        ToolKits.savePicture(pBuffer, stPicInfo.dwOffSet, stPicInfo.dwFileLenth, smallPicture);
                    }
                }
                break;
            }
            case EVENT_IVS_TRASH_WITHOUT_COVER_DETECTION: //垃圾桶未盖盖子检测事件
            {
                System.out.println("Trash without cover Event received.");
 
                DEV_EVENT_TRASH_WITHOUT_COVER_DETECTION_INFO trashEvent = new DEV_EVENT_TRASH_WITHOUT_COVER_DETECTION_INFO();
 
                ToolKits.GetPointerDataToStruct(pAlarmInfo, 0, trashEvent);
                String date = trashEvent.UTC.toStringTime();
 
                System.out.println("nChannelID = " + trashEvent.nChannelID);
                System.out.println("szName(GBK) = " + new String(trashEvent.szName, Charset.forName("GBK")));
                System.out.println("szName(UTF-8) = " + new String(trashEvent.szName, Charset.forName("UTF-8")));
                System.out.println("nImageInfoNum = " + trashEvent.nImageInfoNum);
                System.out.println("UTC = " + trashEvent.UTC.toStringTime());
                // save the picture
                String imageName = "trashEvent_" + trashEvent.UTC.toStringTitle() + ".jpg";
                String savePath = picturePath + "/" + imageName;
 
                ToolKits.savePicture(pBuffer, dwBufSize, savePath);
                System.out.println("save picture to " + savePath);
                break;
            }
 
            default: {
                System.out.println("Other Event received: " + dwAlarmType);
                break;
            }
        }
 
        if (stuFileInfo != null) { // 保存图片
//            String bigPicture = picturePath + File.separator
//                    + String.format("Big_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType, stuFileInfo.stuFileTime.toStringTitle(),
//                    stuFileInfo.bCount, stuFileInfo.bIndex, stuFileInfo.bFileType, stuFileInfo.nGroupId);
//            ToolKits.savePicture(pBuffer, 0, dwBufSize, bigPicture);
//            if (stPicInfo != null && stPicInfo.dwFileLenth > 0) {
//                String smallPicture = picturePath + File.separator
//                        + String.format("Small_%d_%s_%d_%d_%d_%d.jpg", dwAlarmType,
//                        stuFileInfo.stuFileTime.toStringTitle(), stuFileInfo.bCount, stuFileInfo.bIndex,
//                        stuFileInfo.bFileType, stuFileInfo.nGroupId);
//                ToolKits.savePicture(pBuffer, stPicInfo.dwOffSet, stPicInfo.dwFileLenth, smallPicture);
//            }
            String extension = "jpg";
            String picData = ossService.uploadImages(new ByteArrayInputStream(pBuffer.getByteArray(stPicInfo.dwOffSet, dwBufSize)), extension);
            videoAlarmReport.setPicData(picData);
        }
 
 
        return 0;
    }
}