VideoServiceImpl.java
11.6 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
package com.bsth.service.impl.videoimpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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;
@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<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();
}
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, null, business.getBusinessCode());
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.setParent("#");
target.setType(0);
groupTree.add(target);
}
}
if (CollectionUtils.isNotEmpty(groupTree)) {
for (VideoTree tree : groupTree) {
groupTree(trees, tree);
}
}
return groupTree;
}
private List<VideoTree> queryLine() {
Iterable<Line> lines = lineService.list(new HashMap<>());
if (Objects.isNull(lines)) {
return Collections.emptyList();
}
List<VideoTree> trees = new ArrayList<>();
Iterator<Line> iterator = lines.iterator();
while (iterator.hasNext()) {
Line line = iterator.next();
VideoTree videoTree = combinationVideoTree(line.getId(), line.getName(), line.getCompany(), line.getId(), 2, null, null);
trees.add(videoTree);
}
return trees;
}
private List<VideoTree> queryCars() {
return null;
}
/***
* 组合树
* @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, Integer sourceId, Integer type, String icon, Object code) {
VideoTree videoTree = new VideoTree();
videoTree.setId(id);
videoTree.setText(text);
videoTree.setSourceId(sourceId);
videoTree.setNodePValue(parent);
videoTree.setType(type);
// videoTree.setIcon(icon);
videoTree.setCode(code);
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);
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;
}
}