VideoServiceImpl.java
22.1 KB
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package com.bsth.service.impl.videoimpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.data.BasicData;
import com.bsth.entity.Business;
import com.bsth.entity.Line;
import com.bsth.entity.video.VideoChannel;
import com.bsth.entity.video.VideoTree;
import com.bsth.service.BusinessService;
import com.bsth.service.LineService;
import com.bsth.service.video.VideoService;
import com.bsth.util.HttpClientUtils;
import com.bsth.util.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.geolatte.geom.M;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author liujun
* @date 2024年07月01日 12:57
*/
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {
@Autowired
private BusinessService businessService;
@Autowired
private LineService lineService;
@Autowired
private WvpConfig wvpConfig;
@Autowired
private BasicData basicData;
@Override
public List<VideoTree> combinationTree() {
List<VideoTree> returnList = new ArrayList<>();
List<VideoTree> bussessTree = queryBusiness();
if (CollectionUtils.isNotEmpty(bussessTree)) {
returnList.addAll(bussessTree);
}
// List<VideoTree> lineTree = queryLine();
// if (CollectionUtils.isNotEmpty(lineTree)) {
// returnList.addAll(lineTree);
// }
return returnList;
}
@Override
public List<VideoTree> combinationTreeOfCar(String code,boolean spread,boolean asyn) {
if (Objects.nonNull(code)) {
List<VideoTree> trees = new ArrayList<>();
Set<String> carNos = groupCarNosByComputeCode(code);
Set<Line> lineSet = groupLingByCompany(code);
if (CollectionUtils.isNotEmpty(lineSet)) {
for (Line line : lineSet) {
VideoTree videoTree = combinationOfLine(code, line,spread,asyn);
trees.add(videoTree);
Set<String> carNoSets = groupCarNoByLine(line.getId());
if (CollectionUtils.isNotEmpty(carNoSets)) {
List<VideoTree> children = new ArrayList<>();
for (String carNo : carNoSets) {
VideoTree carTree = combnationOfCar(carNo, line.getId().toString(),spread,asyn);
children.add(carTree);
if (CollectionUtils.isNotEmpty(carNos)) {
carNos.remove(carNo);
}
}
videoTree.setChildren(children);
}
}
}
if (CollectionUtils.isNotEmpty(carNos)) {
for (String carNo : carNos) {
VideoTree carTree = combnationOfCar(carNo, code,spread,asyn);
trees.add(carTree);
}
}
return trees;
}
return null;
}
private VideoTree combnationOfCar(String carNo, String line,boolean spread,boolean asyn) {
String name = Objects.isNull(basicData) || MapUtils.isEmpty(basicData.nbbmCompanyPlateMap) ? null : basicData.nbbmCompanyPlateMap.get(carNo);
VideoTree tree = combinationVideoTree(carNo, carNo, line, carNo, 301, "bus1", carNo,spread,asyn);
tree.setWvpLiveBroadcast(wvpConfig.getWvpLiveBroadcast());
return tree;
}
private VideoTree combinationOfLine(String code, Line line,boolean spread,boolean asyn) {
return combinationVideoTree(line.getId(), line.getName(), code, line.getId(), 201, "line", line.getLineCode(),spread,asyn);
}
@Override
public List<VideoTree> searchTreeDataByCarNo(String carNo,boolean spread,boolean asyn) throws Exception {
Line line = findLineByCode(carNo);
Iterable<Business> businessIterable = businessService.list(new HashMap<>());
if (Objects.isNull(businessIterable)) {
return Collections.emptyList();
}
if (Objects.nonNull(line)) {
Set<Business> businesses = new LinkedHashSet<>();
findBussinessByUpCode(line.getCompany(), businessIterable, businesses);
if (CollectionUtils.isNotEmpty(businesses)) {
List<VideoTree> group = combitionBussiness(businesses,spread,asyn);
VideoTree sourceTree = findVideoTreeByBusinessCode(line.getCompany(), group);
if (Objects.isNull(sourceTree.getChildren())) {
sourceTree.setChildren(new ArrayList<>());
}
VideoTree lineOfTreeNode = combinationOfLine(sourceTree.getId().toString(), line,spread,asyn);
lineOfTreeNode.setSpread(Boolean.TRUE.toString());
lineOfTreeNode.setChildren(new ArrayList<>());
sourceTree.getChildren().add(lineOfTreeNode);
VideoTree carOfTreeNode = combnationOfCar(carNo, line.getLineCode(),spread,asyn);
lineOfTreeNode.getChildren().add(carOfTreeNode);
carOfTreeNode.setSpread(Boolean.TRUE.toString());
List<VideoTree> children = queryChannelsTree(carNo,spread,asyn);
carOfTreeNode.setChildren(children);
return group;
}
} else {
Set<Business> businesses = new LinkedHashSet<>();
String company = basicData.nbbm2CompanyCodeMap.get(carNo);
if (StringUtils.isNotEmpty(company)) {
findBussinessByUpCode(company, businessIterable, businesses);
if (CollectionUtils.isNotEmpty(businesses)) {
List<VideoTree> group = combitionBussiness(businesses,spread,asyn);
VideoTree sourceTree = findVideoTreeByBusinessCode(company, group);
if (Objects.nonNull(sourceTree) && Objects.isNull(sourceTree.getChildren())) {
sourceTree.setChildren(new ArrayList<>());
}
VideoTree carOfTreeNode = combnationOfCar(carNo, company,spread,asyn);
carOfTreeNode.setSpread(Boolean.TRUE.toString());
sourceTree.getChildren().add(carOfTreeNode);
sourceTree.setSpread("true");
List<VideoTree> children = queryChannelsTree(carNo,spread,asyn);
carOfTreeNode.setChildren(children);
return group;
}
}
}
return Collections.emptyList();
}
@Override
public List<VideoTree> queryChannelsTree(String carNo, boolean spread,boolean asyn) throws Exception {
List<Map<String, Object>> videoChannels = getVideoChannel(carNo);
List<VideoTree> trees = new ArrayList<>();
if (CollectionUtils.isNotEmpty(videoChannels)) {
for (Map<String, Object> map : videoChannels) {
VideoTree tree = new VideoTree();
tree.setId(map.get("channelId"));
tree.setCode(map.get("channelId"));
tree.setSourceId(map.get("deviceId"));
tree.setType(401);
tree.setIcon("layui-icon-video");
String name = Objects.isNull(map.get("name")) ? map.get("channelId") + "_" + map.get("deviceId") : map.get("name").toString();
tree.setText("<span><img src='/metronic_v4.5.4/layui/icon/video1.ico' class ='imageIcon' /></span><span>"+name+"</span>");
tree.setNodePValue(carNo);
tree.setWvpLiveBroadcast(wvpConfig.getWvpLiveBroadcast());
trees.add(tree);
}
}
return trees;
}
@Override
public List<Map<String, Object>> getVideoChannel(String carNo) throws Exception {
return getWvpVideoChannelByCarNo(carNo);
}
@Override
public List<Map<String, Object>> getVideoChannelHistoryList(String device, String channel, String dateStr, String token) throws Exception {
return getVideoChannelHistoryListReq(device, channel, dateStr, token);
}
@Override
public String getToken() throws Exception {
return getWvpLoginToken();
}
/***
*查询公交公司的数据。
* @author liujun
* @date 2024/6/27 11:29
* @return java.util.List<com.bsth.entity.video.VideoTree>
*/
private List<VideoTree> queryBusiness() {
Iterable<Business> businessIterable = businessService.list(new HashMap<>());
if (Objects.isNull(businessIterable)) {
return Collections.emptyList();
}
return combitionBussiness(businessIterable,false,Boolean.FALSE);
}
private List<VideoTree> combitionBussiness(Iterable<Business> businessIterable,boolean spread,boolean asyn) {
List<VideoTree> trees = new ArrayList<>();
Iterator<Business> iterator = businessIterable.iterator();
while (iterator.hasNext()) {
Business business = iterator.next();
VideoTree videoTree = combinationVideoTree(business.getBusinessCode(), business.getBusinessName(), business.getUpCode(), business.getId(), null, "company1", business.getBusinessCode(),spread,asyn);
trees.add(videoTree);
}
List<VideoTree> groupTree = new ArrayList<>();
for (VideoTree tree : trees) {
if (Objects.equals(tree.getNodePValue(), "0")) {
VideoTree target = new VideoTree();
BeanUtils.copyProperties(tree, target);
target.setSpread(String.valueOf(tree.getSpread()));
// target.setParent("#");
target.setType(0);
groupTree.add(target);
}
}
if (CollectionUtils.isNotEmpty(groupTree)) {
for (VideoTree tree : groupTree) {
groupTree(trees, tree);
}
}
return groupTree;
}
/***
* 组合树
* @author liujun
* @date 2024/6/27 13:12
* @param id id
* @param text 显示的文本
* @param sourceId 数据的ID
* @param parent 数据的父节点
* @param type 类型
* @param icon 图标
* @return com.bsth.entity.video.VideoTree
*/
private VideoTree combinationVideoTree(Object id, String text, String parent, Object sourceId, Integer type, String icon, Object code,boolean spread,boolean asyn) {
VideoTree videoTree = new VideoTree();
videoTree.setId(id);
videoTree.setText("<span><img src='/metronic_v4.5.4/layui/icon/"+icon+".png' class ='imageIcon' /></span><span>"+text+"</span>");
videoTree.setSourceId(sourceId);
videoTree.setNodePValue(parent);
videoTree.setType(type);
videoTree.setIcon(icon);
videoTree.setCode(code);
videoTree.setInitChild(asyn);
videoTree.setSpread(String.valueOf(spread));
return videoTree;
}
private void groupTree(List<VideoTree> trees, VideoTree tree) {
if (Objects.isNull(trees) || CollectionUtils.isEmpty(trees)) {
return;
}
for (VideoTree videoTree : trees) {
if (Objects.equals(videoTree.getNodePValue(), tree.getId())) {
if (Objects.isNull(tree.getChildren())) {
tree.setChildren(new ArrayList<>());
}
VideoTree target = new VideoTree();
BeanUtils.copyProperties(videoTree, target);
target.setType(tree.getType() + 1);
target.setSpread(String.valueOf(tree.getSpread()));
tree.getChildren().add(target);
groupTree(trees, target);
}
}
}
/***
* 获取wvp 系统的登陆token
* @author liujun
* @date 2024/7/2 16:16
* @return java.lang.String
*/
private String getWvpLoginToken() throws Exception {
StringBuilder url = new StringBuilder();
url.append(wvpConfig.getLoginURL());
if (StringUtils.indexOf(wvpConfig.getLoginURL(), "?") == -1) {
url.append("?");
}
url.append("username=");
url.append(wvpConfig.getUserName());
url.append("&password=");
url.append(wvpConfig.getPwd());
try {
StringBuilder result = HttpClientUtils.get(url.toString());
Map<String, Object> resultMap = (Map<String, Object>) switchWVPResult(result, url);
if (MapUtils.isEmpty(resultMap)) {
return null;
}
return Objects.isNull(resultMap.get("accessToken")) ? null : resultMap.get("accessToken").toString();
} catch (Exception e) {
log.error("[{}]请求异常:", url, e);
throw e;
}
}
private List<Map<String, Object>> getWvpVideoChannelByCarNo(String carNo) throws Exception {
String token = getWvpLoginToken();
if (StringUtils.isEmpty(token)) {
log.error("wvp login token is null");
return Collections.emptyList();
}
Map<String, Object> headers = new HashMap<>();
headers.put("Access-Token", token);
String url = StringUtils.replace(wvpConfig.getChannelListOfCarNoURL(), "{carNo}", carNo);
try {
StringBuilder result = get(url, headers);
return (List<Map<String, Object>>) switchWVPResult(result, url);
} catch (Exception e) {
log.error("get wvp of channel list error[{}];", url, e);
throw e;
}
}
private List<Map<String, Object>> getVideoChannelHistoryListReq(String device, String channel, String dateStr, String token) throws Exception {
if (StringUtils.isEmpty(token)) {
log.error("wvp login token is null");
return Collections.emptyList();
}
Map<String, Object> headers = new HashMap<>();
headers.put("Access-Token", token);
String url = StringUtils.replace(wvpConfig.getWvpChannelHistoryUrl(), "{device}", device);
url = StringUtils.replace(url, "{channel}", channel);
url = StringUtils.replace(url, "{startTimeStr}", dateStr + "%2000:00:00");
url = StringUtils.replace(url, "{endTimeStr}", dateStr + "%2023:59:59");
try {
StringBuilder result = get(url, headers);
Map<String, Object> resulMap = (Map<String, Object>) switchWVPResult(result, url);
if (MapUtils.isEmpty(resulMap)) {
return Collections.emptyList();
}
List<Map<String, Object>> recordList = (List<Map<String, Object>>) resulMap.get("recordList");
return CollectionUtils.isEmpty(recordList) ? Collections.emptyList() : recordList;
} catch (Exception e) {
log.error("get wvp of channel history list error[{}];", url, e);
throw e;
}
}
public static StringBuilder get(String url, Map<String, Object> headers) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
StringBuilder stringBuffer = null;
try {
httpClient = HttpClientUtils.defaultHttpClient(url);
HttpGet get = new HttpGet(url);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, Object> header : headers.entrySet()) {
get.setHeader(header.getKey(), String.valueOf(header.getValue()));
}
}
//超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(12500).setConnectionRequestTimeout(12000)
.setSocketTimeout(13500).build();
get.setConfig(requestConfig);
response = httpClient.execute(get);
stringBuffer = getResult(response.getEntity());
} catch (Exception e) {
log.error("", e);
throw e;
} finally {
if (null != httpClient)
httpClient.close();
if (null != response)
response.close();
}
return stringBuffer;
}
private static StringBuilder getResult(HttpEntity entity) throws IOException {
StringBuilder stringBuffer = null;
if (null != entity) {
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
stringBuffer = new StringBuilder();
String str = "";
while ((str = br.readLine()) != null)
stringBuffer.append(str);
}
return stringBuffer;
}
/***
* 解析wvp系统返回的数据
* @author liujun
* @date 2024/7/2 14:43
* @param result
* @param url
* @return java.lang.Object
*/
private Object switchWVPResult(Object result, Object url) {
log.info("[{}]请求返回的数据:[{}]", url, result);
if (Objects.nonNull(result)) {
String resultStr = result.toString();
if (StringUtils.isNotEmpty(resultStr)) {
Map<String, Object> resultMap = (Map<String, Object>) JSON.parse(resultStr);
Integer code = (Integer) resultMap.get("code");
if (Objects.equals(0, code)) {
return resultMap.get("data");
}
}
}
return null;
}
private Set<String> groupCarNosByComputeCode(String code) {
if (Objects.isNull(basicData) || MapUtils.isEmpty(basicData.nbbm2CompanyCodeMap)) {
return Collections.emptySet();
}
Set<String> carNos = new LinkedHashSet<>();
for (Map.Entry<String, String> entry : basicData.nbbm2CompanyCodeMap.entrySet()) {
if (Objects.equals(code, entry.getValue())) {
carNos.add(entry.getKey());
}
}
return carNos;
}
private Set<String> groupCarNoByLine(Integer id) {
if (Objects.isNull(basicData) || MapUtils.isEmpty(basicData.nbbm2LineMap)) {
return Collections.emptySet();
}
Set<String> carNos = new LinkedHashSet<>();
for (Map.Entry<String, Line> entry : basicData.nbbm2LineMap.entrySet()) {
if (Objects.nonNull(entry.getValue()) && Objects.equals(entry.getValue().getId(), id)) {
carNos.add(entry.getKey());
}
}
return carNos;
}
private Set<Line> groupLine(Collection<String> carNos) {
if (Objects.isNull(basicData) || MapUtils.isEmpty(basicData.nbbm2LineMap)) {
return Collections.emptySet();
}
Set<Line> lineSet = new LinkedHashSet<>();
for (String carNo : carNos) {
Line line = basicData.nbbm2LineMap.get(carNo);
if (Objects.nonNull(line)) {
lineSet.add(line);
}
}
return lineSet;
}
private Set<Line> groupLingByCompany(String companyCode) {
if (Objects.isNull(basicData) || MapUtils.isEmpty(basicData.nbbm2LineMap)) {
return Collections.emptySet();
}
Set<Line> lineSet = new LinkedHashSet<>();
for (Map.Entry<String, Line> entry : basicData.nbbm2LineMap.entrySet()) {
if (Objects.nonNull(entry.getValue()) && Objects.equals(entry.getValue().getCompany(), companyCode)) {
lineSet.add(entry.getValue());
}
}
return lineSet;
}
private void findBussinessByUpCode(String upCode, Iterable<Business> businessIterable, Collection<Business> results) {
for (Business business : businessIterable) {
if (Objects.equals(business.getBusinessCode(), upCode)) {
results.add(business);
findBussinessByUpCode(business.getUpCode(), businessIterable, results);
}
}
}
private VideoTree findVideoTreeByBusinessCode(String bussinessCode, List<VideoTree> videoTrees) {
if (CollectionUtils.isEmpty(videoTrees)) {
return null;
}
for (VideoTree videoTree : videoTrees) {
if (Objects.nonNull(videoTree.getId()) && Objects.equals(videoTree.getId().toString(), bussinessCode)) {
return videoTree;
} else if (CollectionUtils.isNotEmpty(videoTree.getChildren())) {
VideoTree source = findVideoTreeByBusinessCode(bussinessCode, videoTree.getChildren());
if (Objects.nonNull(source)) {
return source;
}
}
}
return null;
}
private Line findLineByCode(String carNo) {
Line line = basicData.nbbm2LineMap.get(carNo);
if (Objects.nonNull(line)) {
return line;
}
for (Map.Entry<String, Line> entry : basicData.nbbm2LineMap.entrySet()) {
if (StringUtils.equalsIgnoreCase(carNo, entry.getKey())) {
return entry.getValue();
}
}
String lineId = null;
for (Map.Entry<String, List<String>> entry : basicData.lineCodeHistoryNbbmMap.entrySet()) {
if (CollectionUtils.isNotEmpty(entry.getValue())) {
for (String sourceCarNo : entry.getValue()) {
if (StringUtils.equalsIgnoreCase(carNo, sourceCarNo)) {
lineId = entry.getKey();
break;
}
}
}
if (StringUtils.isNotEmpty(lineId)) {
break;
}
}
if (StringUtils.isEmpty(lineId)) {
return null;
}
for (Map.Entry<String, Line> entry : basicData.nbbm2LineMap.entrySet()) {
if (StringUtils.equalsIgnoreCase(lineId, String.valueOf(entry.getValue().getId()))) {
return entry.getValue();
}
}
return null;
}
}