Pu Zhibing
2025-05-06 1ad5e3d97eb99662b08c0f5acff34875deca04c1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CarServiceImpl.java
@@ -1,6 +1,7 @@
package com.ruoyi.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.dataInterchange.api.feignClient.UPExgMsgRegisterClient;
@@ -18,6 +19,8 @@
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -28,6 +31,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
 * @author zhibing.pu
@@ -35,6 +41,9 @@
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {
   @Value("${live.hls.output-path}")
   private String hlsOutputPath;
   
   @Resource
   private UPExgMsgRegisterClient upExgMsgRegisterClient;
@@ -102,6 +111,22 @@
   
   
   /**
    * 定时保存车辆司机id
    */
   @Override
   public void taskSaveDriverId() {
      List<Driver> driverList = driverService.list(new LambdaQueryWrapper<Driver>().eq(Driver::getStatus, 1));
      List<Car> list = this.list(new LambdaQueryWrapper<Car>().isNull(Car::getDriverId));
      for (Car car : list) {
         Optional<Driver> optional = driverList.stream().filter(s -> s.getVehicleNumber().equals(car.getVehicleNumber())).findFirst();
         if (optional.isPresent()) {
            car.setDriverId(optional.get().getId());
            this.updateById(car);
         }
      }
   }
   /**
    * 获取车辆列表数据
    *
    * @param carListReq
@@ -121,22 +146,60 @@
   public void taskUpdateCarStatus() {
      List<Car> list = this.list();
      for (Car car : list) {
         LambdaUpdateWrapper<Car> updateWrapper = new LambdaUpdateWrapper<>();
         GnssDataVo gnssDataVo = (GnssDataVo) redisTemplate.opsForValue().get("location:" + car.getVehicleNumber());
         if (null == gnssDataVo) {
            car.setStatus(1);
            if (car.getStatus() == 1) {
               updateWrapper.set(Car::getDownlineTime, LocalDateTime.now());
            }
            updateWrapper.set(Car::getStatus, 3);
            updateWrapper.eq(Car::getId, car.getId());
            this.update(updateWrapper);
         } else {
            String dateTime = gnssDataVo.getDate() + " " + gnssDataVo.getTime();
            LocalDateTime localDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            long second = localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
            if (System.currentTimeMillis() / 1000 > second + 60) {
               car.setStatus(4);
               if (car.getStatus() == 1) {
                  updateWrapper.set(Car::getDownlineTime, LocalDateTime.now());
               }
               updateWrapper.set(Car::getStatus, 3);
               updateWrapper.eq(Car::getId, car.getId());
               this.update(updateWrapper);
            } else {
               car.setStatus(1);
               if (car.getStatus() != 1) {
                  updateWrapper.set(Car::getDownlineTime, null);
                  updateWrapper.set(Car::getOnlineTime, LocalDateTime.now());
               }
               updateWrapper.set(Car::getStatus, 1);
               updateWrapper.eq(Car::getId, car.getId());
               this.update(updateWrapper);
            }
         }
      }
      if (list.size() > 0) {
         this.updateBatchById(list);
   }
   /**
    * 检测视频播放,清除没有播放的视频流
    */
   @Override
   public void taskPlayDetection(Integer deviceNumber) {
      Car car = this.getById(deviceNumber);
      try {
         ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
         executorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
               if(!redisTemplate.hasKey("live:" + car.getId())){
                  String folderPath = hlsOutputPath + "hls\\" + car.getVehicleNumber();
                  JavaCVStreamUtil.close(car.getId(), folderPath);
               }
            }}, 1, 1, TimeUnit.MINUTES);
      }catch (Exception e){
         e.printStackTrace();
      }
   }
}