puzhibing
2025-05-06 4e87f5f570a84621734035217f08882f52809c48
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CarController.java
@@ -1,33 +1,48 @@
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;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.dataInterchange.api.feignClient.PlaybackMsgClient;
import com.ruoyi.dataInterchange.api.feignClient.RealVideoMsgClient;
import com.ruoyi.dataInterchange.api.feignClient.UPExgMsgRealLocationClient;
import com.ruoyi.dataInterchange.api.vo.GnssDataVo;
import com.ruoyi.dataInterchange.api.vo.OrderTravelVo;
import com.ruoyi.dataInterchange.api.vo.UPPlaybackMsgStartupAckVo;
import com.ruoyi.dataInterchange.api.vo.UPRealvideoMsgStartupAckVo;
import com.ruoyi.system.api.model.Car;
import com.ruoyi.system.api.model.CarType;
import com.ruoyi.system.api.model.Driver;
import com.ruoyi.system.api.model.Enterprise;
import com.ruoyi.system.query.*;
import com.ruoyi.system.service.ICarService;
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;
import org.springframework.web.bind.annotation.RequestMapping;
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
@@ -37,24 +52,39 @@
@RequestMapping("/car")
public class CarController {
   
   @Value("${live.hls.output-path}")
   private String hlsOutputPath;
   @Value("${live.hls.ip}")
   private String hlsIp;
   @Value("${live.hls.port}")
   private Integer hlsPort;
   @Value("${live.flv.ip}")
   private String flvIp;
   @Value("${live.flv.rtmp-port}")
   private Integer flvRtmpPort;
   @Value("${live.flv.http-port}")
   private Integer flvHttpPort;
   @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 = {"车辆管理"})
@@ -69,18 +99,21 @@
   @ApiImplicitParams({
         @ApiImplicitParam(value = "车辆id", name = "id", required = true)
   })
   public R<Car> getCarInfo(@PathVariable("id") Integer id){
   public R<Car> getCarInfo(@PathVariable("id") Integer id) {
      Car car = carService.getById(id);
      if(null == car){
      if (null == car) {
         return R.fail("失败");
      }
      Driver driver = driverService.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getVehicleNumber, car.getVehicleNumber()).eq(Driver::getStatus, 1));
      Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
      car.setEnterpriseName(enterprise.getName());
      car.setDriverName(driver.getName());
      if (null != driver) {
         car.setDriverName(driver.getName());
         car.setDriverPhone(driver.getPhone());
         car.setDrivingLicenseNumber(driver.getDrivingLicenceFile());
      }
      return R.ok(car);
   }
   
   
   @GetMapping("/getRealVideo/{id}")
@@ -88,52 +121,95 @@
   @ApiImplicitParams({
         @ApiImplicitParam(value = "车辆id", name = "id", required = true)
   })
   public R<RealVideoResp> getRealVideo(@PathVariable("id") Integer id){
   public R<RealVideoResp> getRealVideo(@PathVariable("id") Integer id) {
      Car car = carService.getById(id);
      if(null == car){
         return R.fail("失败");
      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()){
      if (200 == msgStartupAckVoR.getCode()) {
         UPRealvideoMsgStartupAckVo data = msgStartupAckVoR.getData();
         RealVideoResp resp = new RealVideoResp();
         resp.setServerIp(data.getServerIP());
         resp.setServerPort(data.getServerPort());
         //执行拉流和推流
//         live_hls(data.getUrl(), car);
//         resp.setServerIp(hlsIp);
//         resp.setServerPort(hlsPort);
         resp.setUrl(data.getUrl());
//         live_flv(data.getUrl(), car.getId());
         resp.setServerIp(flvIp);
         resp.setServerPort(flvHttpPort);
         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 = hlsOutputPath + "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();
   }
   
   
   @GetMapping("/getPlaybackVideo")
   @ApiOperation(value = "获取音视频回放", tags = {"车辆管理"})
   public R<RealVideoResp> getPlaybackVideo(PlaybackVideoReq req){
   public R<RealVideoResp> getPlaybackVideo(PlaybackVideoReq req) {
      Car car = carService.getById(req.getId());
      if(null == car){
      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()){
      if (200 == startupAckVoR.getCode()) {
         UPPlaybackMsgStartupAckVo data = startupAckVoR.getData();
         RealVideoResp resp = new RealVideoResp();
         resp.setServerIp(data.getServerIP());
         resp.setServerPort(data.getServerPort());
         //执行拉流和推流
//         live_hls(data.getUrl(), car);
//         resp.setServerIp(hlsIp);
//         resp.setServerPort(hlsPort);
         resp.setUrl(data.getUrl());
//         live_flv(data.getUrl(), car.getId());
         resp.setServerIp(flvIp);
         resp.setServerPort(flvHttpPort);
         return R.ok(resp);
      }
      return R.fail(startupAckVoR.getMsg());
   }
   
   
   @GetMapping("/playbackMsgControl")
   @ApiOperation(value = "音视频回放远程控制", tags = {"车辆管理"})
   public R playbackMsgControl(PlaybackMsgControlReq req){
   public R playbackMsgControl(PlaybackMsgControlReq req) {
      Car car = carService.getById(req.getId());
      if(null == car){
      if (null == car) {
         return R.fail("失败");
      }
      Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
@@ -142,18 +218,16 @@
   }
   
   
   @GetMapping("/getCarTravel")
   @ApiOperation(value = "获取车辆行程轨迹", tags = {"车辆管理"})
   public R<List<OrderTravelVo>> getCarTravel(CarTravelReq req){
   public R<List<OrderTravelVo>> getCarTravel(CarTravelReq req) {
      Car car = carService.getOne(new LambdaQueryWrapper<Car>().eq(Car::getVehicleNumber, req.getVehicleNumber()));
      Long startTime;
      Long endTime;
      if(null != req.getStartTime() && null != req.getEndTime()){
      if (null != req.getStartTime() && null != req.getEndTime()) {
         startTime = req.getStartTime();
         endTime = req.getEndTime();
      }else{
      } else {
         LocalDateTime now = LocalDateTime.now();
         startTime = now.minusMinutes(1).toEpochSecond(ZoneOffset.ofHours(8));
         endTime = now.toEpochSecond(ZoneOffset.ofHours(8));
@@ -161,4 +235,105 @@
      R<List<OrderTravelVo>> orderTravel = upExgMsgRealLocationClient.getOrderTravel(car.getVehicleNumber(), startTime, endTime);
      return orderTravel;
   }
   @GetMapping("/getCarTypeList")
   @ApiOperation(value = "获取车辆类型列表数据", tags = {"车辆类型"})
   public R<List<CarType>> getCarTypeList(String name) {
      List<CarType> list = carTypeService.list(new LambdaQueryWrapper<CarType>().like(StringUtils.isNotEmpty(name), CarType::getName, name));
      return R.ok(list);
   }
   @GetMapping("/getCarCount")
   @ApiOperation(value = "获取各种车辆类型车辆总数", tags = {"首页"})
   public R<List<CarType>> getCarCount() {
      List<CarType> list = carTypeService.list();
      return R.ok(list);
   }
   @GetMapping("/getCarStatusCount")
   @ApiOperation(value = "获取车辆状态汇总数据和公司总数等", tags = {"首页"})
   public R<CarStatusCount> getCarStatusCount() {
      List<Car> list = carService.list();
      long online = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 1).count();
      long offline = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 3).count();
      long breakdown = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 4).count();
      long abnormal = list.isEmpty() ? 0 : list.stream().filter(s -> s.getStatus() == 2).count();
      CarStatusCount carStatusCount = new CarStatusCount();
      carStatusCount.setOffline(offline);
      carStatusCount.setOnline(online);
      carStatusCount.setBreakdown(breakdown);
      carStatusCount.setAbnormal(abnormal);
      long enterprise = enterpriseService.count();
      carStatusCount.setEnterprise(enterprise);
      carStatusCount.setCar(list.size());
      long driver = driverService.count();
      carStatusCount.setDriver(driver);
      return R.ok(carStatusCount);
   }
   @GetMapping("/getMapCarList")
   @ApiOperation(value = "获取地图司机数据", tags = {"首页"})
   public R<List<Car>> getMapCarList() {
      List<Car> list = carService.list(new LambdaQueryWrapper<Car>().eq(Car::getStatus, 1));
      for (Car car : list) {
         car.setDrivingTime(0L);
         if(null != car.getOnlineTime()){
            Long s = (null == car.getDownlineTime() ? LocalDateTime.now() : car.getDownlineTime()).toEpochSecond(ZoneOffset.ofHours(8)) - car.getOnlineTime().toEpochSecond(ZoneOffset.ofHours(8));
            car.setDrivingTime(s / 60);
         }
         GnssDataVo gnssDataVo = (GnssDataVo) redisTemplate.opsForValue().get("location:" + car.getVehicleNumber());
         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);
   }
   public void live_hls(String input, Car car){
      String path = hlsOutputPath + "hls\\" + car.getVehicleNumber() + "\\live.m3u8";
      String folderPath = hlsOutputPath + "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(input, path, car.getId(), folderPath);
         }
      });
      carService.taskPlayDetection(car.getId());
   }
   public void live_flv(String input, Integer id){
      String url = "rtmp://" + flvIp + ":" + flvRtmpPort + "/flv/" + id;
      //执行拉流和推流
      ExecutorService executorService = new ThreadPoolExecutor(1, 1,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());
      executorService.execute(new Runnable() {
         @Override
         public void run() {
            JavaCVStreamUtil.push_flv(input, url, id);
         }
      });
      carService.taskPlayDetection(id);
   }
}