MainService.java 6.07 KB
package com.viontech.integration.jiaoguansuo.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.viontech.fanxing.commons.constant.ChannelType;
import com.viontech.fanxing.commons.model.Channel;
import com.viontech.fanxing.commons.model.Task;
import com.viontech.fanxing.commons.vo.TaskVo;
import com.viontech.integration.jiaoguansuo.entity.Code;
import com.viontech.integration.jiaoguansuo.entity.VideoEntity;
import com.viontech.integration.jiaoguansuo.feign.OpsClient;
import com.viontech.integration.jiaoguansuo.feign.TaskClient;
import com.viontech.keliu.util.JsonMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;
import java.util.List;

/**
 * .
 *
 * @author 谢明辉
 * @date 2022/2/21
 */

@Service
@Slf4j
public class MainService {

    @Resource
    private TaskClient taskClient;
    @Resource
    private OpsClient opsClient;
    @Value("${vion.storage-config-id}")
    private Long storageConfigId;
    @Resource
    private XingheDeviceService xingheDeviceService;

    public List<Channel> getChannels(Channel channel) {
        JsonMessageUtil.JsonMessage<List<Channel>> channels = opsClient.getChannels(channel);
        if (channels.isSuccess()) {
            return channels.getData();
        } else {
            throw new RuntimeException(channels.getMsg());
        }
    }

    public Channel addChannel(VideoEntity addVideo) {

        Channel channel = new Channel();
        channel.setChannelUnid(addVideo.getSxjbh());
        channel.setDeviceUnid(addVideo.getDwxh());
        JsonMessageUtil.JsonMessage<List<Channel>> channels = opsClient.getChannels(channel);
        if (channels.isSuccess() && channels.getData().size() > 0) {
            return channels.getData().get(0);
        }

        channel.setName(addVideo.getSxjbh());
        channel.setStreamType(ChannelType.STREAM_RTSP.value);
        channel.setStreamPath(addVideo.getJkdz());
        channel.setIp("0.0.0.0");
        channel.setPort(0);
        channel.setUsername("username");
        channel.setPassword("password");
        channel.setType(ChannelType.MANUALLY.value);
        JsonMessageUtil.JsonMessage<Channel> add = opsClient.add(channel);
        if (add.isSuccess()) {
            //星河平台同步新增设备
            xingheDeviceService.addDevice(addVideo);
            return add.getData();
        } else {
            throw new RuntimeException(add.getMsg());
        }
    }

    public Task addTask(VideoEntity videoEntity, Channel channel, Code code) {
        TaskVo taskVo = new TaskVo();
        taskVo.setResourceNeed(1F);
        taskVo.setStoreConfigId(storageConfigId);
        taskVo.setName(videoEntity.getSxjbh());
        taskVo.setAlgType(String.valueOf(0));
        taskVo.setDeviceUnid(channel.getDeviceUnid());
        taskVo.setChannelUnid(channel.getChannelUnid());
        taskVo.setStreamPath(channel.getStreamPath());
        taskVo.setStreamType(channel.getStreamType());
        taskVo.setAlgEnabled("[\"" + code.paramName + "\"]");
        // 视频检测运行配置
        Integer jcfx = videoEntity.getJcfx();
        if (jcfx == 1) {
            long l = Duration.ofMinutes(videoEntity.getJcsj()).toMillis();
            if (l == 0) {
                taskVo.setRuntimeType(4);
                taskVo.setRuntimeConf("{\"type\":4}");
            } else {
                taskVo.setRuntimeType(1);
                JSONObject config = new JSONObject();
                config.set("start", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                config.set("end", DateUtil.format(new Date(System.currentTimeMillis() + l + 1000), "yyyy-MM-dd HH:mm:ss"));
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("type", 1);
                jsonObject.set("config", config);
                taskVo.setRuntimeConf(jsonObject.toString());
            }
        } else {
            String[] split = videoEntity.getJcsd().split("#");
            if (split.length == 0) {
                taskVo.setRuntimeType(4);
                taskVo.setRuntimeConf("{\"type\":4}");
            } else {
                taskVo.setRuntimeType(0);


                JSONArray configs = new JSONArray();
                for (String dd : split) {
                    String[] ddd = dd.split("-");
                    JSONObject config = new JSONObject();
                    config.set("start", ddd[0] + ":00");
                    config.set("end", ddd[1] + ":00");
                    configs.add(config);
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("type", 0);
                jsonObject.set("config", configs);
                taskVo.setRuntimeConf(jsonObject.toString());
            }
        }
        JsonMessageUtil.JsonMessage<TaskVo> res = taskClient.addTask(taskVo);
        if (res.isSuccess()) {
            return res.getData();
        } else {
            throw new RuntimeException(res.getMsg());
        }
    }

    public void removeChannel(Long id) {
        JsonMessageUtil.JsonMessage remove = opsClient.remove(id);
        log.info("删除视频资源结果:{}", remove.getMsg());
    }

    public void startTask(Long id) {
        JsonMessageUtil.JsonMessage<TaskVo> task = taskClient.startTask(id);
    }

    public void removeTask(Long id) {
        JsonMessageUtil.JsonMessage jsonMessage = taskClient.removeTask(id);
        log.info("删除任务结果:{}", jsonMessage.getMsg());
    }

    public List<Task> getTask(Task task) {
        JsonMessageUtil.JsonMessage<List<Task>> res = taskClient.getTask(task);
        if (res.isSuccess()) {
            return res.getData();
        }
        throw new RuntimeException(res.getMsg());

    }

    public void updateTask(Long id, Task task) {
        JsonMessageUtil.JsonMessage<TaskVo> update = taskClient.update(id, task);
        if (!update.isSuccess()) {
            throw new RuntimeException(update.getMsg());
        }
    }

}