VAServerHttpService.java
10.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
package com.viontech.fanxing.task.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ecwid.consul.v1.ConsulClient;
import com.viontech.fanxing.commons.constant.ChannelType;
import com.viontech.fanxing.commons.exception.FanXingException;
import com.viontech.fanxing.commons.model.Channel;
import com.viontech.fanxing.task.model.TaskData;
import com.viontech.fanxing.task.model.vaserver.VATask;
import com.viontech.fanxing.task.model.vaserver.VaServerInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
import java.time.Duration;
/**
* .
*
* @author 谢明辉
* @date 2021/7/20
*/
@Service
@Slf4j
public class VAServerHttpService {
@Resource
private OpsClientService opsClientService;
@Resource
private ConsulClient consulClient;
/**
* 下发任务
*/
public JSONObject addTask(TaskData taskData, VaServerInfo vaServerInfo) {
VATask vaTask = new VATask(taskData);
if (vaTask.getStream_type().equals(ChannelType.STREAM_VIDEO_CLOUD.value)) {
Channel channel = opsClientService.getChannelByChannelUnid(vaTask.getChannel_unid());
String deviceUnid = channel.getDeviceUnid();
vaTask.setDevice_unid(deviceUnid);
}
String path = "/api/vaserver/v1/task";
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(vaTask)
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono, Duration.ofSeconds(60));
log.info("下发任务结果:{}", response);
return JSON.parseObject(response);
}
/**
* 编辑任务
*/
public JSONObject updateTask(TaskData taskData, VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/task";
VATask vaTask = new VATask(taskData);
if (vaTask.getStream_type().equals(ChannelType.STREAM_VIDEO_CLOUD.value)) {
Channel channel = opsClientService.getChannelByChannelUnid(vaTask.getChannel_unid());
String deviceUnid = channel.getDeviceUnid();
vaTask.setDevice_unid(deviceUnid);
}
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.put()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(vaTask)
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono, Duration.ofSeconds(60));
log.info("更新任务结果:{}", response);
return JSON.parseObject(response);
}
/**
* 删除任务
*/
public JSONObject rmTask(String taskUnid, VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/deleteTask";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_unid", taskUnid);
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(jsonObject.toString())
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono);
log.info("删除任务结果:{}", response);
return JSON.parseObject(response);
}
/**
* 截图
*/
public JSONObject snapshot(String taskUnid, VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/snapshot";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_unid", taskUnid);
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(jsonObject.toString())
.retrieve()
.bodyToMono(String.class);
String block = getResponse(stringMono);
return JSON.parseObject(block);
}
/**
* 获取点播地址
*/
public JSONObject getAnalyzeStream(String taskUnid, VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/get_analyze_stream";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_unid", taskUnid);
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(jsonObject.toString())
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono);
log.info("获取分析流地址结果 : {}", response);
return JSON.parseObject(response);
}
/**
* 开始输出分析流
*/
public JSONObject startAnalyzeStream(String taskUnid, VaServerInfo vaServerInfo, String url) {
String path = "/api/vaserver/v1/start_analyze_stream";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_unid", taskUnid);
jsonObject.put("isDrawRect", 1);
jsonObject.put("mediaServerPushUrl", url);
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(jsonObject.toString())
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono);
log.info("输出分析流结果:{}", response);
return JSON.parseObject(response);
}
/**
* 切换预置位
*/
public JSONObject switchScene(String taskUnid, VaServerInfo vaServerInfo, String sceneId) {
String path = "/api/vaserver/v1/switch_scene";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_unid", taskUnid);
jsonObject.put("sceneID", sceneId);
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(jsonObject.toString())
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono);
log.info("场景切换结果:{}", response);
return JSON.parseObject(response);
}
/**
* 任务轮训状态切换
*/
public JSONObject updateRotationStatus(String taskUnid, Integer rotationStatus, VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/alternate";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_unid", taskUnid);
jsonObject.put("alternateStatus", rotationStatus.toString());
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(jsonObject.toString())
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono);
log.info("轮训状态控制结果:{}", response);
return JSON.parseObject(response);
}
/**
* 任务轮训状态查询
*/
public JSONObject getRotationStatus(String taskUnid, VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/getAlternate";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_unid", taskUnid);
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(uriBuilder -> uriBuilder.path(path).build())
.bodyValue(jsonObject.toString())
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono);
log.info("获取轮训状态:{}", response);
return JSON.parseObject(response);
}
/**
* 运行状态查询
*/
public JSONObject status(VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/status";
Mono<String> stringMono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.get()
.uri(uriBuilder -> uriBuilder
.path(path)
.build())
.retrieve()
.bodyToMono(String.class);
String response = getResponse(stringMono);
log.info("运行状态查询:{}", response);
return JSON.parseObject(response);
}
/**
* 获取默认算法配置
*/
public JSONObject getDefaultAlgorithmConfig(VaServerInfo vaServerInfo, String taskAlgType) {
String path = "/api/vaserver/v1/runtime_config";
JSONObject jsonObject = new JSONObject();
jsonObject.put("task_algo_type", taskAlgType);
Mono<String> mono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(path)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.bodyValue(jsonObject)
.retrieve()
.bodyToMono(String.class);
String block = getResponse(mono);
log.info("默认配置获取:{}", block);
return JSON.parseObject(block);
}
/**
* 获取任务当前预置位
*/
public JSONObject getCurrentScene(String taskUnid, VaServerInfo vaServerInfo) {
String path = "/api/vaserver/v1/get_current_scene";
JSONObject obj = new JSONObject();
obj.put("task_unid", taskUnid);
Mono<String> mono = WebClient.create(vaServerInfo.getServiceBaseUrl())
.post()
.uri(path)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.bodyValue(obj)
.retrieve()
.bodyToMono(String.class);
String block = getResponse(mono);
log.info("获取当前预置位:{}", block);
return JSON.parseObject(block);
}
private <T> T getResponse(Mono<T> mono, Duration duration) {
try {
return mono.block(duration);
} catch (Exception e) {
throw new FanXingException("访问设备失败");
}
}
private <T> T getResponse(Mono<T> mono) {
try {
return mono.block(Duration.ofSeconds(20));
} catch (Exception e) {
throw new FanXingException("访问设备失败");
}
}
}