MainController.java 3.9 KB
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();
    }
}