MainController.java
3.9 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
package com.viontech.integration.jiaoguansuo.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.viontech.fanxing.commons.constant.TaskStatus;
import com.viontech.fanxing.commons.model.Channel;
import com.viontech.fanxing.commons.model.Task;
import com.viontech.integration.jiaoguansuo.entity.Code;
import com.viontech.integration.jiaoguansuo.entity.Message;
import com.viontech.integration.jiaoguansuo.entity.VideoEntity;
import com.viontech.integration.jiaoguansuo.service.MainService;
import com.viontech.integration.jiaoguansuo.service.XingheDeviceService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* .
*
* @author 谢明辉
* @date 2022/2/21
*/
@RestController
public class MainController {
@Resource
private MainService mainService;
@Resource
private XingheDeviceService xingheDeviceService;
/**
* 1. 添加对应视频信息
* 2. 创建任务
* 3. 启动任务
*/
@PostMapping("/rmevent/addvideo")
public Message addVideoAndTask(@RequestBody VideoEntity videoEntity) {
Code code;
try {
code = Code.valueOf("C_" + videoEntity.getJclx());
} catch (Exception e) {
return Message.error("不支持的检测类型:" + videoEntity.getJclx());
}
Channel channel = mainService.addChannel(videoEntity);
Task task = null;
try {
task = mainService.addTask(videoEntity, channel,code);
// 修改scene
JSONArray scenes = JSON.parseArray(task.getScene());
JSONObject config = scenes.getJSONObject(0).getJSONObject("config");
String xml = config.getString("xml");
xml = code.modifyConfig(xml);
config.put("xml", xml);
Task temp = new Task();
temp.setScene(scenes.toString());
mainService.updateTask(task.getId(), temp);
mainService.startTask(task.getId());
} catch (Exception e) {
if (task != null) {
mainService.removeTask(task.getId());
}
mainService.removeChannel(channel.getId());
xingheDeviceService.deleteDevice(videoEntity);
throw e;
}
return Message.success();
}
/**
* 查询运行中的任务
*
* @return
*/
@PostMapping("/rmevent/queryvideo")
public Message queryVideoAndTask() {
Task task = new Task();
task.setStatus(TaskStatus.RUNNING.val);
List<Task> tasks = mainService.getTask(task);
List<VideoEntity> videos = tasks.stream().map(x -> new VideoEntity().setDwxh(x.getDeviceUnid()).setSxjbh(x.getChannelUnid())).collect(Collectors.toList());
Message success = Message.success();
success.setVideos(videos);
return success;
}
@PostMapping("/rmevent/removevideo")
public Message removeVideoAndTask(@RequestBody VideoEntity videoEntity) {
Channel channel = new Channel();
channel.setChannelUnid(videoEntity.getSxjbh());
channel.setDeviceUnid(videoEntity.getDwxh());
List<Channel> channels = mainService.getChannels(channel);
if (channels.size() > 0) {
channel = channels.get(0);
Task task = new Task();
task.setChannelUnid(videoEntity.getSxjbh());
List<Task> tasks = mainService.getTask(task);
if (tasks.size() > 0) {
task = tasks.get(0);
mainService.removeTask(task.getId());
}
mainService.removeChannel(channel.getId());
xingheDeviceService.deleteDevice(videoEntity);
}
return Message.success();
}
}