czt
2026-01-05 c25d369c352b959887999da035e019b840d0b75c
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
package com.fzzy.igds.camera.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.fzzy.igds.camera.AbstractApiCameraService;
import com.fzzy.igds.camera.data.*;
import com.fzzy.igds.camera.data.ydqly.YdQlyCamera;
import com.fzzy.igds.camera.data.ydqly.YdQlyDto;
import com.fzzy.igds.camera.data.ydqly.YdQlyResp;
import com.fzzy.igds.constant.CameraPlayType;
import com.fzzy.igds.constant.Constant;
import com.fzzy.igds.constant.RedisConst;
import com.fzzy.igds.domain.Camera;
import com.fzzy.igds.domain.CameraMedia;
import com.fzzy.igds.service.CameraMediaService;
import com.fzzy.igds.service.SecCameraService;
import com.fzzy.igds.utils.ContextUtil;
import com.fzzy.igds.utils.HttpUtils;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
 
/**
 * @Desc 移动千里眼播放
 * @Author CZT
 * @Date 2026/01/04 16:09
 */
@Slf4j
@Component
public class ApiPlayYdQlyService extends AbstractApiCameraService {
 
    /**
     * 获取accessToken接口路径
     */
    private static final String URL_ACCESS_TOKEN = "https://open.qly.cmviot.cn/v3/open/api/token";
    private static final String URL_PLAY_LIVE = "https://open.qly.cmviot.cn/v3/open/api/websdk/live";
 
    private static final String URL_CAMERA_LIST = "https://open.qly.cmviot.cn/v3/open/api/node/tree";
 
    @Resource
    private CameraMediaService cameraMediaService;
    @Resource
    private SecCameraService cameraService;
    @Resource
    private RedisCache redisCache;
 
    @Override
    public String getType() {
        return CameraPlayType.PLAY_TYPE_YD_QLY.getCode();
    }
 
    @Override
    public ApiCameraResp getPlayAddr(ApiCameraData apiCameraDto) {
 
        if (StringUtils.isBlank(apiCameraDto.getCameraId())) {
            log.error("获取摄像头信息失败,监控ID为空!");
            return null;
        }
 
        if (StringUtils.isBlank(apiCameraDto.getDeptId())) {
            log.error("获取摄像头信息失败,库区编码为空!");
            return null;
        }
 
        //查询库区流媒体配置,获取库区节点
        CameraMedia cameraMedia = cameraMediaService.getByDeptId(apiCameraDto.getDeptId(), apiCameraDto.getCompanyId());
        if(null == cameraMedia || StringUtils.isBlank(cameraMedia.getYdStoreId())){
            log.error("获取摄像头信息失败,库区节点未配置!");
            return null;
        }
 
        //接口获取播放地址
        String str = getPlayUrl(cameraMedia, apiCameraDto.getCameraId());
        YdQlyDto dto = JSONObject.parseObject(str, YdQlyDto.class);
        if (null == dto || StringUtils.isBlank(dto.getUrl())) {
            return new ApiCameraResp(ApiCameraResp.CODE_ERROR, "未获取到摄像头播放地址!");
        }
 
        return new ApiCameraResp(dto.getUrl());
    }
 
    @Override
    public List<Camera> searchCamera(ApiCameraData apiCameraDto) {
 
        if (StringUtils.isEmpty(apiCameraDto.getDeptId())) {
            log.error("获取摄像头信息失败,库区编码为空!");
            return null;
        }
 
        //查询库区流媒体配置,获取库区节点
        CameraMedia cameraMedia = cameraMediaService.getByDeptId(apiCameraDto.getDeptId(), apiCameraDto.getCompanyId());
        if(null == cameraMedia || StringUtils.isEmpty(cameraMedia.getYdStoreId())){
            log.error("获取摄像头信息失败,库区节点未配置!");
            return null;
        }
 
        String str = getCameraList(cameraMedia);
        if(null== str){
            log.error("获取摄像头信息失败,库区节点未配置!");
            return null;
        }
 
        YdQlyDto ydQlyDto = JSONObject.parseObject(str, YdQlyDto.class);
 
        if(null== ydQlyDto || null == ydQlyDto.getDevice()){
            log.error("获取摄像头信息失败,库区节点未配置!");
            return null;
        }
 
        //持久化
        List<Camera> listCamera = new ArrayList<>();
        Camera camera;
        for (YdQlyCamera dto : ydQlyDto.getDevice()) {
            camera = new Camera();
            camera.setId(dto.getDeviceId());
            camera.setCompanyId(cameraMedia.getCompanyId());
            camera.setDeptId(cameraMedia.getDeptId());
            camera.setSn(dto.getDeviceId());
            camera.setName(dto.getDeviceName());
            camera.setType("01");
            camera.setPlayType(CameraPlayType.PLAY_TYPE_YD_QLY.getCode());
            cameraService.pullUpdateCamera(camera);
        }
        return listCamera;
    }
 
 
    /**
     * 获取accessToken
     *
     * @param cameraMedia
     * @return
     */
    private String getAccessToken(CameraMedia cameraMedia) {
        //默认先从缓存获取
        String key = RedisConst.buildKey(ContextUtil.getCompanyId(), Constant.KEY_ACCESS_TOKEN_YD_QLY);
        String accessToken = (String) redisCache.getCacheObject(key);
 
        //若缓存获取为空,则通过接口请求获取
        if (null == accessToken) {
            String result = getAccessTokenApi(cameraMedia);
 
            YdQlyDto dto = JSONObject.parseObject(result, YdQlyDto.class);
            if (null == dto) {
                return null;
            }
            accessToken = dto.getToken();
            Integer expiresIn = dto.getExpires_in();
 
            //将token存入缓存,默认7天
            redisCache.setCacheObject(key, accessToken, 7*60*24, TimeUnit.MINUTES);
        }
 
        return accessToken;
    }
 
    /**
     * 接口获取授权accessToken
     * @param cameraMedia
     * @return
     */
    private String getAccessTokenApi(CameraMedia cameraMedia) {
        try {
            String timestamp = String.valueOf(System.currentTimeMillis());
            String sig =  Hex.encodeHexString(MessageDigest.getInstance("MD5").digest(StringUtils.getBytes(cameraMedia.getYdAppKey()+cameraMedia.getYdAppSecret(),"UTF-8")));
            JSONObject requestBody = new JSONObject();
            requestBody.put("operatorType", 1);
            requestBody.put("sig", sig);
            String requestBodyStr = requestBody.toString();
 
            // 2. 计算请求体MD5
            String md5 = DigestUtils.md5Hex(String.valueOf(requestBody));
 
            // 3. 构建签名字符串
            LinkedHashMap<String, String> herderParams = new LinkedHashMap<>();
            herderParams.put("appid", cameraMedia.getYdAppKey());
            herderParams.put("md5", md5);
            herderParams.put("timestamp", timestamp);
            herderParams.put("version", "1.0.0");
            String jsonString = JSONObject.toJSONString(herderParams);
 
            // 4. RSA签名
            byte[] keyBytes = Base64.getDecoder().decode(cameraMedia.getYdRsa());
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
 
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes("UTF-8"));
            String signatureString = Base64.getEncoder().encodeToString(signature.sign());
 
            herderParams.put("signature", signatureString);
            herderParams.put("content-type", "application/json");
 
            // 5. 发送请求
            String response = HttpUtils.doPost(URL_ACCESS_TOKEN, herderParams, requestBodyStr);
 
            YdQlyResp ydQlyResp = JSONObject.parseObject(response, YdQlyResp.class);
            if(!"000000".equals(ydQlyResp.getResultCode())){
                return null;
            }
            return ydQlyResp.getData();
        } catch (Exception e) {
            log.error("-----获取身份认证异常-----,原因={}", e.toString());
            return null;
        }
    }
 
    /**
     * 接口获取播放地址
     * @param cameraMedia
     * @param cameraId
     * @return
     */
    private String getPlayUrl(CameraMedia cameraMedia, String cameraId) {
        try {
 
            String timestamp = String.valueOf(System.currentTimeMillis());
 
            JSONObject requestBody = new JSONObject();
            requestBody.put("deviceId", cameraId);
 
            String requestBodyStr = requestBody.toString();
 
            // 2. 计算请求体MD5
            String md5 = DigestUtils.md5Hex(String.valueOf(requestBody));
 
            // 3. 构建签名字符串
            LinkedHashMap<String, String> herderParams = new LinkedHashMap<>();
 
            herderParams.put("appid", cameraMedia.getYdAppKey());
            herderParams.put("md5", md5);
            herderParams.put("timestamp", timestamp);
            herderParams.put("token", getAccessToken(cameraMedia));
            herderParams.put("version", "1.0.0");
            String jsonString = JSONObject.toJSONString(herderParams);
 
            // 4. RSA签名
            byte[] keyBytes = Base64.getDecoder().decode(PRIVATE_KEY);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
 
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes("UTF-8"));
            String signatureString = Base64.getEncoder().encodeToString(signature.sign());
 
            herderParams.put("signature", signatureString);
            herderParams.put("content-type", "application/json");
 
            // 5. 发送请求
            String response = HttpUtils.doPost(URL_PLAY_LIVE, herderParams, requestBodyStr);
 
            YdQlyResp ydQlyResp = JSONObject.parseObject(response, YdQlyResp.class);
            if(!"000000".equals(ydQlyResp.getResultCode())){
                return null;
            }
            return ydQlyResp.getData();
        } catch (Exception e) {
            return null;
        }
    }
 
    /**
     * 接口获取播放地址
     * @param cameraMedia
     * @return
     */
    private String getCameraList(CameraMedia cameraMedia) {
        try {
 
            String timestamp = String.valueOf(System.currentTimeMillis());
 
            JSONObject requestBody = new JSONObject();
            requestBody.put("queryType", 0);
            requestBody.put("page", 1);
            requestBody.put("pageSize", 100);
            requestBody.put("nodeId", cameraMedia.getYdStoreId());
 
            String requestBodyStr = requestBody.toString();
 
            // 2. 计算请求体MD5
            String md5 = DigestUtils.md5Hex(String.valueOf(requestBody));
 
            // 3. 构建签名字符串
            LinkedHashMap<String, String> herderParams = new LinkedHashMap<>();
 
            herderParams.put("appid", cameraMedia.getYdAppKey());
            herderParams.put("md5", md5);
            herderParams.put("timestamp", timestamp);
            herderParams.put("token", getAccessToken(cameraMedia));
            herderParams.put("version", "1.0.0");
            String jsonString = JSONObject.toJSONString(herderParams);
 
            // 4. RSA签名
            byte[] keyBytes = Base64.getDecoder().decode(PRIVATE_KEY);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
 
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes("UTF-8"));
            String signatureString = Base64.getEncoder().encodeToString(signature.sign());
 
            herderParams.put("signature", signatureString);
            herderParams.put("content-type", "application/json");
 
            // 5. 发送请求
            String response = HttpUtils.doPost(URL_CAMERA_LIST, herderParams, requestBodyStr);
 
            YdQlyResp ydQlyResp = JSONObject.parseObject(response, YdQlyResp.class);
            if(!"000000".equals(ydQlyResp.getResultCode())){
                return null;
            }
            return ydQlyResp.getData();
        } catch (Exception e) {
            return null;
        }
    }
 
 
    private static final String TOKEN = "eyJhbGciOiJIUzI1NiJ9.eyJwcm9mZXNzaW9uIjoxLCJhcHBpZCI6ImUwYTFhNDIwZjU1YTRmOWRiYTBkNjM4MmRmNTlkYjY2Iiwib3BlcmF0b3JUeXBlIjoxLCJvcGVyYXRvciI6ImUwYTFhNDIwZjU1YTRmOWRiYTBkNjM4MmRmNTlkYjY2IiwianRpIjoiMjE4NzE3MDI2MDQyNDk2MjA3NCIsImlhdCI6MTc2NzUxODIxNiwic3ViIjoiZTBhMWE0MjBmNTVhNGY5ZGJhMGQ2MzgyZGY1OWRiNjYiLCJleHAiOjE3NjgxMjMwMTZ9.FGYq1TzTUHBpv3xEk6DpMfo_SrEZEK_zPkBYsmrTly4";
    private static final String APP_ID = "e0a1a420f55a4f9dba0d6382df59db66";
    private static final String SECRET = "OuLsvFL9nqxeeltu";
    private static final String URL = "https://open.qly.cmviot.cn/v3/open/api/token";
    private static final String URL_PLAY = "https://open.qly.cmviot.cn/v3/open/api/websdk/live";
    private static final String URL_LIST = "https://open.qly.cmviot.cn/v3/open/api/node/tree";
    private static final String PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAI7iA0WASV8pekjHVcsYV3ycZR2/+XK7O6BZpybAw/cauEzHRKsrDknErFxdNFca1OlVbPljBOfXjIrhS1CpAWez3Ff2+XaAaFE3fFer9LTxQ5GMXJSIAGQ4ntq/7y57ucwdVjL9+4B4WT8Yfea1DLa/fv+I0Gm7haVQj75JVYhBAgMBAAECgYAfvBs16JRjSncaiwuHHFTyHFppDSZ+UQ+hQchaVx4vWAPvKaMa0WlPARu3OAxT/GWlnY+ipFeGlt1kSz3LIH5rERYd5sNPOFmK7Nzih1VK6r/y1XobM4cLqLrP2UW2c3V36A77xz1xpaa9CP5l6gFjYAdZ1vnGS7HyjolWvzjUAQJBAMB6OTwrHcxOUJiLRJSK697LMIziBi1p5cR4NdSoQjQ4/enUqzAalwi6ZODuGSYai5VKhH9xkxFASWMT8waTOxECQQC+CbWK4Igx8Uh5qPpBIiFuA6ReAuFoRDMvKlzPUi8FGFkvF/CDx5IUFvYUd7FFX4l6Wtu5vf+VF3OydmzFr5oxAkB96Xa3nZ0IodKnd7ONWLOkM1e3UcSowLXGKL1OOCPW9dFkvfKerUZQN172pctd2c+mAfFbTXjfuCZJ4isBCh+BAkA8+ZiKaujIPYbnolRJuc3IItsGXx47+kYCCDGnQ9NaaymSdrfxUIOKdqOWhaEXhUPzQlvmCRSMne29gUn9VA7BAkBLvhtfSuANFSDt942GAY0Q25bJBM01yrRjtTEVCnGDxztexksPKaWGnJxk0Zk0xp4PkJCZvVuxFZ1Dw7F9yg6X";
 
    public static void main(String[] args) {
//        String accessToken = getAccessTokenTest();
//        System.out.println("accessToken: " + accessToken);
 
//        String playUrl = getPlayUrlTest();
//        System.out.println("playUrl: " + playUrl);
 
        String cameraList = getCameraListTest();
        System.out.println("cameraList: " + cameraList);
    }
 
    private static String getCameraListTest() {
        try {
 
            String timestamp = String.valueOf(System.currentTimeMillis());
 
            JSONObject requestBody = new JSONObject();
            requestBody.put("queryType", 0);
            requestBody.put("nodeId", "1346024619493187584");
 
            String requestBodyStr = requestBody.toString();
 
            // 2. 计算请求体MD5
            String md5 = DigestUtils.md5Hex(String.valueOf(requestBody));
 
            // 3. 构建签名字符串
            Map signParams = new LinkedHashMap();
            signParams.put("appid", APP_ID);
            signParams.put("md5", md5);
            signParams.put("timestamp", timestamp);
            signParams.put("token", TOKEN);
            signParams.put("version", "1.0.0");
            String jsonString = JSONObject.toJSONString(signParams);
 
            // 4. RSA签名
            byte[] keyBytes = Base64.getDecoder().decode(PRIVATE_KEY);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
 
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes("UTF-8"));
            String signatureString = Base64.getEncoder().encodeToString(signature.sign());
 
            LinkedHashMap<String, String> herderParams = new LinkedHashMap<>();
 
            herderParams.put("content-type", "application/json");
            herderParams.put("appid", APP_ID);
            herderParams.put("md5", md5);
            herderParams.put("timestamp", timestamp);
            herderParams.put("token", TOKEN);
            herderParams.put("version", "1.0.0");
            herderParams.put("signature", signatureString);
 
            // 5. 发送请求
            String response = HttpUtils.doPost(URL_LIST, herderParams, requestBodyStr);
 
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    private static String getPlayUrlTest() {
        try {
 
            String timestamp = String.valueOf(System.currentTimeMillis());
 
            JSONObject requestBody = new JSONObject();
            requestBody.put("deviceId", "gb_26236371771319266195");
 
            String requestBodyStr = requestBody.toString();
 
            // 2. 计算请求体MD5
            String md5 = DigestUtils.md5Hex(String.valueOf(requestBody));
 
            // 3. 构建签名字符串
            Map signParams = new LinkedHashMap();
            signParams.put("appid", APP_ID);
            signParams.put("md5", md5);
            signParams.put("timestamp", timestamp);
            signParams.put("token", TOKEN);
            signParams.put("version", "1.0.0");
            String jsonString = JSONObject.toJSONString(signParams);
 
            // 4. RSA签名
            byte[] keyBytes = Base64.getDecoder().decode(PRIVATE_KEY);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
 
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes("UTF-8"));
            String signatureString = Base64.getEncoder().encodeToString(signature.sign());
 
            LinkedHashMap<String, String> herderParams = new LinkedHashMap<>();
 
            herderParams.put("content-type", "application/json");
            herderParams.put("appid", APP_ID);
            herderParams.put("md5", md5);
            herderParams.put("timestamp", timestamp);
            herderParams.put("token", TOKEN);
            herderParams.put("version", "1.0.0");
            herderParams.put("signature", signatureString);
 
            // 5. 发送请求
            String response = HttpUtils.doPost(URL_PLAY, herderParams, requestBodyStr);
 
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    private static String getAccessTokenTest() {
        try {
 
            String timestamp = String.valueOf(System.currentTimeMillis());
 
            String sig =  Hex.encodeHexString(MessageDigest.getInstance("MD5").digest(StringUtils.getBytes(APP_ID+SECRET,"UTF-8")));
 
            JSONObject requestBody = new JSONObject();
            requestBody.put("operatorType", 1);
            requestBody.put("sig", sig);
            String requestBodyStr = requestBody.toString();
 
            // 2. 计算请求体MD5
            String md5 = DigestUtils.md5Hex(String.valueOf(requestBody));
 
            // 3. 构建签名字符串
            Map signParams = new LinkedHashMap();
            signParams.put("appid", APP_ID);
            signParams.put("md5", md5);
            signParams.put("timestamp", timestamp);
            signParams.put("version", "1.0.0");
            String jsonString = JSONObject.toJSONString(signParams);
 
            // 4. RSA签名
            byte[] keyBytes = Base64.getDecoder().decode(PRIVATE_KEY);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
 
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes("UTF-8"));
            String signatureString = Base64.getEncoder().encodeToString(signature.sign());
 
            LinkedHashMap<String, String> herderParams = new LinkedHashMap<>();
 
            herderParams.put("content-type", "application/json");
            herderParams.put("appid", APP_ID);
            herderParams.put("md5", md5);
            herderParams.put("timestamp", timestamp);
            herderParams.put("version", "1.0.0");
 
            herderParams.put("signature", signatureString);
 
            // 5. 发送请求
            String response = HttpUtils.doPost(URL, herderParams, requestBodyStr);
 
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}