13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
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
package com.ruoyi.fuzhou.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.fuzhou.config.HikConfig;
import com.ruoyi.fuzhou.domain.CameraPTZ;
import com.ruoyi.fuzhou.domain.HikEvent;
import com.ruoyi.fuzhou.domain.HikEventObj;
import com.ruoyi.fuzhou.mapper.HikEventMapper;
import com.ruoyi.fuzhou.service.HikService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class HikServiceImpl extends ServiceImpl<HikEventMapper, HikEvent>
        implements HikService {
    @Autowired
    private HikConfig hikConfig;
    @Autowired
    private HikEventMapper hikEventMapper;
 
    @Override
    public Map<String,Integer> getCameraOnline(String[] indexCodes){
        final String ARTEMIS_PATH = "/artemis";
        /**
         * STEP3:设置接口的URI地址
         */
        final String onlineApi = ARTEMIS_PATH +
                "/api/nms/v1/online/camera/get";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", onlineApi);//根据现场环境部署确认是http还是https
            }
        };
 
        Map<String,Integer> onlineMap = new LinkedHashMap<>();
 
        //组装参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("indexCodes", indexCodes);
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 999);
        String body = jsonBody.toJSONString();
 
        try {
            String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
 
            JSONObject res = JSON.parseObject(result);
 
            JSONObject data = res.getJSONObject("data");
            JSONArray jsonArray = data.getJSONArray("list");
            for (Object object : jsonArray){
                String objJson = object.toString();
                JSONObject jsonObject = JSON.parseObject(objJson);
                if(jsonObject.get("indexCode")!=null && jsonObject.get("online")!=null){
                    onlineMap.put(jsonObject.getString("indexCode"),jsonObject.getIntValue("online"));
                }
            }
            return onlineMap;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return onlineMap;
    }
 
    @Override
    public JSONObject subByEventTypes(HikEventObj hikEventObj) throws Exception{
        final String ARTEMIS_PATH = "/artemis";
        /**
         * STEP3:设置接口的URI地址
         */
        final String subEventApi = ARTEMIS_PATH +
                "/api/eventService/v1/eventSubscriptionByEventTypes";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", subEventApi);//根据现场环境部署确认是http还是https
            }
        };
 
        JSONObject res = new JSONObject();
 
        //组装参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("eventTypes",hikEventObj.getEventTypes());
        jsonBody.put("eventDest", hikEventObj.getEventDest());
        jsonBody.put("subType", hikEventObj.getSubType());
        jsonBody.put("eventLvl", hikEventObj.getEventLvl());
        String body = jsonBody.toJSONString();
 
        try {
            String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
 
            res = JSON.parseObject(result);
            return res;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return res;
    }
 
    @Override
    public JSONObject unSubByEventTypes(HikEventObj hikEventObj) throws Exception{
        final String ARTEMIS_PATH = "/artemis";
        /**
         * STEP3:设置接口的URI地址
         */
        final String unSubEventApi = ARTEMIS_PATH +
                "/api/eventService/v1/eventUnSubscriptionByEventTypes";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", unSubEventApi);//根据现场环境部署确认是http还是https
            }
        };
 
        JSONObject res = new JSONObject();
 
        //组装参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("eventTypes",hikEventObj.getEventTypes());
 
        String body = jsonBody.toJSONString();
 
        try {
            String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
 
            res = JSON.parseObject(result);
            return res;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return res;
    }
 
    @Override
    public JSONObject eventView() throws Exception{
        final String ARTEMIS_PATH = "/artemis";
        /**
         * STEP3:设置接口的URI地址
         */
        final String eventApi = ARTEMIS_PATH +
                "/api/eventService/v1/eventSubscriptionView";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", eventApi);//根据现场环境部署确认是http还是https
            }
        };
 
        JSONObject res = new JSONObject();
 
        //组装参数
        JSONObject jsonBody = new JSONObject();
 
        String body = jsonBody.toJSONString();
 
        try {
            String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
 
            res = JSON.parseObject(result);
            return res;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return res;
    }
 
    @Override
    public boolean saveInsert(HikEvent hikEvent){
        int res =  hikEventMapper.insert(hikEvent);
        if(res != 0){
            return true;
        }else {
            return false;
        }
    }
 
    @Override
    public TableDataInfo getList(HikEvent hikEvent) {
        Integer offset = (hikEvent.getPageNum()-1)*hikEvent.getPageSize();
        hikEvent.setOffset(offset);
        List<HikEvent> list = hikEventMapper.getPageList(hikEvent);
        Integer total = hikEventMapper.getTotal(hikEvent);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(total);
        return tableDataInfo;
    }
 
    @Override
    public JSONObject cameraCommand(CameraPTZ cameraPTZ) throws Exception{
        final String ARTEMIS_PATH = "/artemis";
        /**
         * STEP3:设置接口的URI地址
         */
        final String urlApi = ARTEMIS_PATH +
                "/api/video/v1/ptzs/controlling";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", urlApi);//根据现场环境部署确认是http还是https
            }
        };
 
        //组装参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraPTZ.getCameraIndexCode());
        jsonBody.put("action", cameraPTZ.getAction());
        jsonBody.put("command", cameraPTZ.getCommand());
        jsonBody.put("speed", cameraPTZ.getSpeed());
        jsonBody.put("presetIndex", cameraPTZ.getPresetIndex());
 
        String body = jsonBody.toJSONString();
        String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
 
        JSONObject res = JSON.parseObject(result);
 
        return res;
    }
 
    @Override
    public String getCameraPreviewURL(String cameraName) throws Exception{
        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
            }
        };
        String cameraIndexCode = getCameraIndexCode(cameraName);
 
        //组装参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", "ws");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "transcode=0");
        String body = jsonBody.toJSONString();
        String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
 
        JSONObject res = JSON.parseObject(result);
 
        JSONObject data = res.getJSONObject("data");
        String url = data.getString("url");
 
        return url;
    }
 
    private String getCameraIndexCode(String cameraName) throws Exception {
        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
            }
        };
        //组装参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("name", cameraName);
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 10);
        String body = jsonBody.toJSONString();
        String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
        JSONObject res = JSON.parseObject(result);
        JSONArray array = res.getJSONObject("data").getJSONArray("list");
        JSONObject data = array.getJSONObject(0);
        String cameraIndexCode = data.getString("indexCode");
 
        return cameraIndexCode;
    }
 
    @Override
    public String getCameraURL(String cameraIndexCode) throws Exception{
        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
            }
        };
        //组装参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", "ws");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "transcode=0");
        String body = jsonBody.toJSONString();
        String result = hikConfig.hkpost(path, body);// post请求application/json类型参数
 
        JSONObject res = JSON.parseObject(result);
 
        JSONObject data = res.getJSONObject("data");
        String url = data.getString("url");
 
        return url;
    }
 
//    public static void main(String[] args) {
//        String ipAddr = "10.3.10.66";
//        try {
//            InetAddress address = InetAddress.getByName(ipAddr);
//            boolean res = address.isReachable(5000);
//            if(res){
//                System.out.println(ipAddr+"联通");
//            }else {
//                System.out.println(ipAddr+"不通");
//            }
//        }catch (Exception e){
//            System.out.println(e.getMessage());
//        }
//
//    }
 
}