zhongrj
2024-05-10 b63dccee424ee9dfb8944ead15ff084ff62d63fa
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
package cn.gistack.sm.hk.util;
 
import cn.gistack.sm.hk.constant.HkConstant;
import cn.gistack.sm.hk.constant.ProtocolConstant;
import cn.gistack.sm.hk.vo.EventVO;
import cn.gistack.sm.hk.vo.HkVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
 
import java.util.*;
 
import static cn.gistack.sm.hk.constant.HkConstant.ARTEMIS_PATH;
public class GetCameraUtils {
 
    /**
     * 公共方法
     *
     * @param url
     * @param body
     * @return
     */
    public static String common(Integer type,String url, String body) {
        /**
         * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
         */
        // artemis网关服务器ip端口
        if (null==type){
            ArtemisConfig.host = HkConstant.HOST;
        }
        if (type==1) {
            ArtemisConfig.host = HkConstant.HOST;
        }
        if (type==2) {
            ArtemisConfig.host = HkConstant.NEW_HOST;
        }
        // 秘钥appKey
        ArtemisConfig.appKey = HkConstant.AppKey;
        // 秘钥appSecret
        ArtemisConfig.appSecret = HkConstant.AppSecret;
 
        if (type==3) {
            // 秘钥appKey
            ArtemisConfig.appKey = HkConstant.EventAppKey;
            // 秘钥appSecret
            ArtemisConfig.appSecret = HkConstant.EventAppSecret;
            // host
            ArtemisConfig.host = HkConstant.Event_NEW_HOST;
        }
 
 
        /**
         * STEP3:设置接口的URI地址
         */
        final String apiUrl = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", apiUrl);
            }
        };
 
        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";
 
 
        /**
         * STEP6:调用接口
         * // post请求application/json类型参数
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
        // 返回数据
        return result;
    }
 
    /**
     * 获取视频预览数据
     *
     * @param type  类型,新/老
     * @param url 视频预览url
     * @param cameraIndexCode 监控点唯一标识
     * @param protocol
     * @param transmode
     * @param expand
     * @return
     */
    public static String GetCameraPreviewURL(Integer type,
                                             String url,
                                             String cameraIndexCode,
                                             String protocol,
                                             Integer transmode,
                                             String expand,
                                             Integer streamType) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType",streamType);
        jsonBody.put("protocol", protocol);
        jsonBody.put("transmode", transmode);
        if (null!= expand && !expand.equals("")) {
            jsonBody.put("expand", expand);
        }
        String body = jsonBody.toJSONString();
        // 调用公共方法
        return common(type,url,body);
    }
 
    /**
     * 获取视频预览数据
     *
     * @param url        视频预览url
     * @param cameraCode 监控点唯一标识
     * @return
     */
    public static String QueryCameraPreviewURL(Integer type,
                                               String url,
                                               String cameraCode,
                                               String resourceType,
                                               String key) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        List<JSONObject> list = new ArrayList<>();
        List list2 = new ArrayList();
        JSONObject map = new JSONObject();
        list2.add(cameraCode);
        map.put("key", key);
        map.put("operator", 0);
        map.put("values", list2);
        list.add(map);
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 100);
        jsonBody.put("resourceType", resourceType);
        jsonBody.put("expressions", list);
        String body = jsonBody.toJSONString();
        System.out.println(jsonBody);
        return common(type,url,body);
    }
 
 
    /**
     * 语音对讲
     *
     * @param url             语音对讲url
     * @param cameraIndexCode 监控点唯一标识
     * @return
     */
    public static String GetTalkURLs(Integer type, String url, String cameraIndexCode) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", "");
        jsonBody.put("expand", "");
        jsonBody.put("eurlExpand", "");
        jsonBody.put("netZoneCode", 0);
        jsonBody.put("transmode", 1);
        jsonBody.put("talkIndexCode", "ce744c4c67df464f92994185d825b6fa");
        jsonBody.put("protocol", "wss");
        String body = jsonBody.toJSONString();
        return common(type,url,body);
    }
 
    /**
     * 云台控制
     * action 0-开始  1-停止
     * 注:GOTO_PRESET命令下填任意值均可转到预置点,建议填0即可
     * @param url  云台控制url
     * @param cameraIndexCode 监控点唯一标识
     * @param command 命令
     * @return
     */
    public static String controlling(Integer type,
                                     String url,
                                     String cameraIndexCode,
                                     Integer action,
                                     String command,
                                     Integer speed,
                                     Integer presetIndex
    ) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("action", action);
        jsonBody.put("command", command);
        jsonBody.put("speed", speed);
        jsonBody.put("presetIndex", presetIndex);
        String body = jsonBody.toJSONString();
        /**
         * 调用接口
         */
        return common(type,url,body);
    }
 
    /**
     * 查询监控点的预置点信息
     *
     * @param url  查询监控点的预置点信息URL
     * @param cameraIndexCode 监控点唯一标识
     * @return
     */
    public static String presetsSearches(Integer type,String url,String cameraIndexCode) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        String body = jsonBody.toJSONString();
        /**
         * 调用接口
         */
        return common(type,url,body);
    }
 
 
    /**
     * 云台控制
     *
     * @param url  云台控制url
     * @param cameraIndexCode 监控点唯一标识
     * @param startX 命令
     * @param startY 命令
     * @param endX 命令
     * @param endY 命令
     * @return
     */
    public static String selZoomURLs(Integer type,
                                     String url,
                                     String cameraIndexCode,
                                     Integer startX,
                                     Integer startY,
                                     Integer endX,
                                     Integer endY
                                     ) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("startX", startX);
        jsonBody.put("startY", startY);
        jsonBody.put("endX", endX);
        jsonBody.put("endY", endY);
        String body = jsonBody.toJSONString();
        /**
         * 调用接口
         */
        return common(type,url,body);
    }
 
    /**
     * 抓图
     *
     * @param url 监控资源url
     * @return
     */
    public static String manualCaptureUrls(Integer type,String url,String cameraIndexCode) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        String body = jsonBody.toJSONString();
 
        /**
         * 调用接口并返回数据
         * post请求application/json类型参数;
         */
        return common(type,url, body);
    }
 
    /**
     * 获取监控点资源分页数据
     * @param url 监控资源url
     * @return
     */
    public static String GetCameraResourcePage(Integer type,Integer pageNo, Integer pageSize, String url) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        String body = jsonBody.toJSONString();
 
        /**
         * 调用接口并返回数据
         * post请求application/json类型参数;
         */
        return common(type,url, body);
    }
 
    /**
     * 海康视频事件订阅
     * @param eventVO
     * @param url
     * @return
     */
    public static String addEventSubscriptionByEventTypes(EventVO eventVO, String url) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("eventTypes", Arrays.stream(eventVO.getEventTypes().split(",")).mapToLong(Long::parseLong).toArray());
        jsonBody.put("eventDest", eventVO.getEventDest());
        if (null!=eventVO.getSubType()){
            jsonBody.put("subType", eventVO.getSubType());
        }
        if (null!=eventVO.getEventLvl()){
            jsonBody.put("eventLvl", Arrays.stream(eventVO.getEventLvl().split(",")).mapToInt(Integer::parseInt).toArray());
        }
        String body = jsonBody.toJSONString();
 
        /**
         * 调用接口并返回数据
         * post请求application/json类型参数;
         */
        return common(null==eventVO.getType()?1:eventVO.getType(),url, body);
    }
 
    /**
     * 海康视频事件取消订阅
     * @param eventVO
     * @param url
     * @return
     */
    public static String removeEventSubscriptionByEventTypes(EventVO eventVO, String url) {
        long[] eventTypes =  Arrays.stream(eventVO.getEventTypes().split(",")).mapToLong(Long::parseLong).toArray();
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("eventTypes", eventTypes);
        String body = jsonBody.toJSONString();
 
        /**
         * 调用接口并返回数据
         * post请求application/json类型参数;
         */
        return common(null==eventVO.getType()?1:eventVO.getType(),url, body);
    }
 
    /**
     * 查询事件订阅信息
     * @param eventVO
     * @param url
     * @return
     */
    public static String getEventSubscriptionView(EventVO eventVO, String url) {
        /**
         * 组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        String body = jsonBody.toJSONString();
 
        /**
         * 调用接口并返回数据
         * post请求application/json类型参数;
         */
        return common(null==eventVO.getType()?1:eventVO.getType(),url, body);
    }
 
//    public static void main(String[] args) {
//
//        String previewURL = GetCameraPreviewURL(1, HkConstant.previewURLs,
//            "0c4f30948dde4998a676ec6983119cb5",
//            "rtsp",
//            "transcode=1&videotype=h264");
//        System.out.println("previewURL = " + previewURL);
 
//        String result = GetCameraResourcePage(1, 10, HkConstant.cameras);
//
//        JSONObject jsonObject1 = JSONObject.parseObject(result);
//        JSONObject res1 = (JSONObject) jsonObject1.get("data");
//        JSONArray arr1 = JSON.parseArray(res1.getString("list"));
//        JSONObject data1 = (JSONObject) arr1.get(1);
//        String code1 = data1.get("cameraIndexCode").toString();
 
        // 云台控制
//        String right = controlling(HkConstant.controllingURLs,
//            "ff1f050afd1c47829dec1ec14575c9e3",
//            0,
//            "ZOOM_OUT",
//            null,
//            null
//        );
//        System.out.println("right = " + right);
 
//        // 放大
//        String big = selZoomURLs(HkConstant.selZoomURLs,
//            "ff1f050afd1c47829dec1ec14575c9e3",
//            55,
//            58,
//            22,
//            23
//        );
//        System.out.println("big = " + big);
 
        // 抓图
//        String manualCapture = manualCaptureUrls(HkConstant.manualCaptureUrls,
//            "ff1f050afd1c47829dec1ec14575c9e3");
//        System.out.println("manualCapture = " + manualCapture);
 
        //根据设备编号获取子节点编号
//        String result2 = QueryCameraPreviewURL(HkConstant.camerasID, code1, "camera", "parentIndexCode");
//
//        JSONObject jsonObject2 = JSONObject.parseObject(result2);
//        JSONObject res2 = (JSONObject) jsonObject2.get("data");
//        JSONArray arr2 = JSON.parseArray(res2.getString("list"));
//        JSONObject data2 = (JSONObject) arr2.get(0);
//        String code2 = data2.get("indexCode").toString();
        //ff1f050afd1c47829dec1ec14575c9e3
        //e2cb261e14144f53a300769839d54753
        //根据子节点编号获取设备播放地址
//        String result3 = GetCameraPreviewURL(HkConstant.previewURLs,
//            "0113d3498d2e41caaf9e5355d1575da3", ProtocolConstant.WS);
//        JSONObject jsonObject = JSONObject.parseObject(result3);
//        Map map = (Map) jsonObject.get("data");
        //根据子节点编号获取音频对讲地址
//        String result4 = GetTalkURLs(HkConstant.talkURLs, code1);
 
//        System.out.println("result结果示例: " + result);
//        System.out.println("result1 = " + result1);
//        System.out.println("result2 = " + result2);
//        System.out.println("result3 = " + result3);
//        System.out.println("url = " + map.get("url"));
//        System.out.println("result4 = " + result4);
 
        // 查询回放url
//        HkVO hk = new HkVO();
//        hk.setCameraIndexCode("0c4f30948dde4998a676ec6983119cb5");
//        hk.setBeginTime("2023-05-08T11:52:16.000+08:00");
//        hk.setEndTime("2023-05-11T11:52:15.000+08:00");
//        hk.setProtocol("wss");
//
//        /**
//         * 组装请求参数
//         */
//        JSONObject jsonBody = new JSONObject();
//        jsonBody.put("cameraIndexCode", hk.getCameraIndexCode());
//        jsonBody.put("protocol", hk.getProtocol());
//        jsonBody.put("recordLocation", "1");
//        jsonBody.put("beginTime", hk.getBeginTime());
//        jsonBody.put("endTime", hk.getEndTime());
//        String body = jsonBody.toJSONString();
//
//        String common = common(HkConstant.playbackURLs, body);
//
//        System.out.println("common = " + common);
//    }
}