Pu Zhibing
2025-04-10 0f9ecda1919beb1e8fbbca0f669ac86badf70806
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CarController.java
@@ -1,5 +1,6 @@
package com.ruoyi.system.controller;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
@@ -20,9 +21,11 @@
import com.ruoyi.system.service.ICarTypeService;
import com.ruoyi.system.service.IDriverService;
import com.ruoyi.system.service.IEnterpriseService;
import com.ruoyi.system.util.JavaCVStreamUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -30,10 +33,16 @@
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
 * @author zhibing.pu
@@ -43,30 +52,30 @@
@RequestMapping("/car")
public class CarController {
   
   @Value("${live.output.path}")
   private String outputPath;
   @Value("${live.ip}")
   private String liveIp;
   @Value("${live.port}")
   private Integer livePort;
   @Resource
   private ICarService carService;
   @Resource
   private IDriverService driverService;
   @Resource
   private IEnterpriseService enterpriseService;
   @Resource
   private RealVideoMsgClient realVideoMsgClient;
   @Resource
   private PlaybackMsgClient playbackMsgClient;
   @Resource
   private UPExgMsgRealLocationClient upExgMsgRealLocationClient;
   @Resource
   private ICarTypeService carTypeService;
   @Resource
   private RedisTemplate redisTemplate;
   
   @GetMapping("/getCarList")
   @ApiOperation(value = "获取车辆列表", tags = {"车辆管理"})
@@ -108,16 +117,68 @@
      if (null == car) {
         return R.fail("失败");
      }
      //手动加一次状态数据,避免定时任务结束任务线程
      redisTemplate.opsForValue().set("live:" + id, true, 1, TimeUnit.MINUTES);
      Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
      R<UPRealvideoMsgStartupAckVo> msgStartupAckVoR = realVideoMsgClient.startupRealVideo(Integer.valueOf(enterprise.getCode()), car.getVehicleNumber());
      if (200 == msgStartupAckVoR.getCode()) {
         String path = outputPath + "hls\\" + car.getVehicleNumber() + "\\live.m3u8";
         String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
         FileUtil.mkParentDirs(path);
         File file = new File(path);
         if (!file.exists()) {
            try {
               file.createNewFile();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }
         UPRealvideoMsgStartupAckVo data = msgStartupAckVoR.getData();
         RealVideoResp resp = new RealVideoResp();
         resp.setServerIp(data.getServerIP());
         resp.setServerPort(data.getServerPort());
         //执行拉流和推流
         ExecutorService executorService = new ThreadPoolExecutor(1, 1,
               0L, TimeUnit.MILLISECONDS,
               new LinkedBlockingQueue<Runnable>());
         executorService.execute(new Runnable() {
            @Override
            public void run() {
               JavaCVStreamUtil.push_hls(data.getUrl(), path, id, folderPath);
            }
         });
         resp.setServerIp(liveIp);
         resp.setServerPort(livePort);
         return R.ok(resp);
      }
      return R.fail(msgStartupAckVoR.getMsg());
   }
   @GetMapping("/closeRealVideo/{id}")
   @ApiOperation(value = "关闭实时音视频", tags = {"车辆管理"})
   @ApiImplicitParams({
         @ApiImplicitParam(value = "车辆id", name = "id", required = true)
   })
   public R closeRealVideo(@PathVariable("id") Integer id) {
      Car car = carService.getById(id);
      if (null == car) {
         return R.fail("失败");
      }
      String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
      JavaCVStreamUtil.close(id, folderPath);
      return R.ok();
   }
   @GetMapping("/playDetection/{id}")
   @ApiOperation(value = "播放检测", tags = {"车辆管理"})
   @ApiImplicitParams({
         @ApiImplicitParam(value = "车辆id", name = "id", required = true)
   })
   public R playDetection(@PathVariable("id") Integer id) {
      redisTemplate.opsForValue().set("live:" + id, true, 1, TimeUnit.MINUTES);
      return R.ok();
   }
   
   
@@ -128,14 +189,38 @@
      if (null == car) {
         return R.fail("失败");
      }
      //手动加一次状态数据,避免定时任务结束任务线程
      redisTemplate.opsForValue().set("live:" + req.getId(), true, 1, TimeUnit.MINUTES);
      Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
      R<UPPlaybackMsgStartupAckVo> startupAckVoR = playbackMsgClient.playbackMsgStartup(Integer.valueOf(enterprise.getCode()), car.getVehicleNumber(),
            req.getStartTime(), req.getEndTime());
      if (200 == startupAckVoR.getCode()) {
         UPPlaybackMsgStartupAckVo data = startupAckVoR.getData();
         RealVideoResp resp = new RealVideoResp();
         resp.setServerIp(data.getServerIP());
         resp.setServerPort(data.getServerPort());
         String path = outputPath + "hls\\" + car.getVehicleNumber() + "\\live.m3u8";
         String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
         FileUtil.mkParentDirs(path);
         File file = new File(path);
         if (!file.exists()) {
            try {
               file.createNewFile();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }
         //执行拉流和推流
         ExecutorService executorService = new ThreadPoolExecutor(1, 1,
               0L, TimeUnit.MILLISECONDS,
               new LinkedBlockingQueue<Runnable>());
         executorService.execute(new Runnable() {
            @Override
            public void run() {
               JavaCVStreamUtil.push_hls(data.getUrl(), path, req.getId(), folderPath);
            }
         });
         resp.setServerIp(liveIp);
         resp.setServerPort(livePort);
         return R.ok(resp);
      }
      return R.fail(startupAckVoR.getMsg());
@@ -223,6 +308,7 @@
         if (null != gnssDataVo) {
            car.setLongitude(new BigDecimal(gnssDataVo.getLon()).divide(new BigDecimal(1000000)).toString());
            car.setLatitude(new BigDecimal(gnssDataVo.getLat()).divide(new BigDecimal(1000000)).toString());
            car.setSpeed(gnssDataVo.getVec1());
         }
      }
      return R.ok(list);