xiangpei
2024-12-26 7082826bf8f817f4484eda8555599610243ff6a5
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
package com.ycl.service;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.ycl.config.HKProperties;
import com.ycl.domain.form.CameraCommandForm;
import com.ycl.domain.query.CameraPlayQuery;
import com.ycl.domain.query.CameraQuery;
import com.ycl.domain.result.HKResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.util.*;
 
/**
 * @author:xp
 * @date:2024/12/20 10:57
 */
@Service
public class HKService {
 
    private final HKProperties hkProperties;
 
    public HKService(HKProperties hkProperties) {
        this.hkProperties = hkProperties;
    }
 
    /**
     * 获取监控资源点-摄像头
     * @see <a href="https://open.hikvision.com/docs/docId?productId=5c67f1e2f05948198c909700&version=%2Ff95e951cefc54578b523d1738f65f0a1&tagPath=API%E5%88%97%E8%A1%A8-%E8%A7%86%E9%A2%91%E5%BA%94%E7%94%A8%E6%9C%8D%E5%8A%A1-%E8%A7%86%E9%A2%91%E8%B5%84%E6%BA%90#a11a856d">...</a>
     *
     */
    public Map<String,Object> getCamerasPage(CameraQuery query) {
        /**
         * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = hkProperties.getHost(); // 平台的ip端口
        ArtemisConfig.appKey = hkProperties.getAk();  // 密钥appkey
        ArtemisConfig.appSecret = hkProperties.getSk();// 密钥appSecret
 
        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";
 
        /**
         * STEP3:设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/resource/v2/camera/search";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
 
        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";
 
        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", query.getPageNo());
        jsonBody.put("pageSize", query.getPageSize());
        jsonBody.put("orderBy", "name");
        jsonBody.put("orderType", "desc");
 
        if (StringUtils.hasText(query.getName())) {
            // 查询条件
            List<Map> expressions = new ArrayList<>(1);
            Map<String, Object> nameSearch = new HashMap<>(3);
            nameSearch.put("key", "name");
            nameSearch.put("operator", 6);
            nameSearch.put("values", Arrays.asList(query.getName()));
            expressions.add(nameSearch);
 
            jsonBody.put("expressions", expressions);
        }
 
        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        HKResult hkResult = JSON.parseObject(result, HKResult.class);
        return hkResult.getData();
    }
 
 
    /**
     * 获取视频流地址
     *
     * @see <a href="https://open.hikvision.com/docs/docId?productId=5c67f1e2f05948198c909700&version=%2Ff95e951cefc54578b523d1738f65f0a1&tagPath=API%E5%88%97%E8%A1%A8-%E8%A7%86%E9%A2%91%E5%BA%94%E7%94%A8%E6%9C%8D%E5%8A%A1-%E8%A7%86%E9%A2%91%E8%83%BD%E5%8A%9B#b5bd6fd9">...</a>
     * @param query
     * @return
     */
    public Map<String, Object> previewURLs(CameraPlayQuery query) {
        /**
         * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = hkProperties.getHost(); // 平台的ip端口
        ArtemisConfig.appKey = hkProperties.getAk();  // 密钥appkey
        ArtemisConfig.appSecret = hkProperties.getSk();// 密钥appSecret
 
        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";
 
        /**
         * STEP3:设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
 
        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";
 
        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", query.getIndexCode());
        jsonBody.put("protocol", query.getProtocol());
//        jsonBody.put("netZoneCode", 53);
 
 
        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        HKResult hkResult = JSON.parseObject(result, HKResult.class);
        return hkResult.getData();
    }
 
 
    /**
     * 控制摄像头
     *
     * @see <a href="https://open.hikvision.com/docs/docId?productId=5c67f1e2f05948198c909700&version=%2Ff95e951cefc54578b523d1738f65f0a1&tagPath=API%E5%88%97%E8%A1%A8-%E8%A7%86%E9%A2%91%E5%BA%94%E7%94%A8%E6%9C%8D%E5%8A%A1-%E8%A7%86%E9%A2%91%E8%83%BD%E5%8A%9B#e6643a97">...</a>
     * @param form
     * @return
     */
    public Boolean controlling(CameraCommandForm form) {
        /**
         * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = hkProperties.getHost(); // 平台的ip端口
        ArtemisConfig.appKey = hkProperties.getAk();  // 密钥appkey
        ArtemisConfig.appSecret = hkProperties.getSk();// 密钥appSecret
 
        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";
 
        /**
         * STEP3:设置接口的URI地址
         */
        final String controllerApi = ARTEMIS_PATH + "/api/video/v1/ptzs/controlling";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", controllerApi);//根据现场环境部署确认是http还是https
            }
        };
 
        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";
 
        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", form.getIndexCode());
        jsonBody.put("action", form.getAction());
        jsonBody.put("command", form.getCommand());
 
 
        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        HKResult hkResult = JSON.parseObject(result, HKResult.class);
        return "success".equals(hkResult.getMsg());
    }
}