DvrController.java 2.58 KB
package com.bsth.controller;

import com.bsth.common.ResponseCode;
import com.bsth.data.SystemParamCache;
import com.bsth.util.HttpClientUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("dvr")
public class DvrController {

    private final static Logger log = LoggerFactory.getLogger(DvrController.class);

    @Autowired
    private ObjectMapper mapper;

    @RequestMapping(value = "/jt1078")
    public Map<String, Object> jt1078(@RequestParam("sim") String sim, @RequestParam("channel") int channel) {
        Map<String, Object> result = new HashMap<>();
        result.put("status", ResponseCode.ERROR);
        result.put("msg", "未获取到视频播放地址");
        try {
            if (StringUtils.isEmpty(sim)) {
                result.put("msg", "sim为空");
                throw new IllegalArgumentException("无效的sim");
            }
            if (channel < 1 || channel > 10) {
                result.put("msg", "无效的通道号");
                throw new IllegalArgumentException("无效的通道号");
            }
            sim = sim.replaceAll("^0*", "");
            String url = SystemParamCache.getUrlHttpWvpPlay();
            url = String.format(url, sim, channel);
            Map<String, Object> header = new HashMap<>();
            header.put("access-token", SystemParamCache.getTokenHttpWvp());
            StringBuilder sb = HttpClientUtils.post(url, "{}", header);
            if (sb != null) {
                Map<String, Object> response = mapper.readValue(sb.toString(), Map.class);
                if ((Integer) response.get("code") == 0) {
                    Map<String, Object> data = mapper.readValue(mapper.writeValueAsString(response.get("data")), Map.class);
                    result.put("status", ResponseCode.SUCCESS);
                    result.put("data", data);
                }
                result.put("msg", response.get("msg"));
            }
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            if (result.get("msg") == null) {
                result.put("msg", "捕获到异常,检查日志信息");
            }
            log.error(e.getMessage(), e);
        }

        return result;
    }
}