Pu Zhibing
2025-02-10 24c1f8a945c6fbcb0968e9d78cf97e74f2677802
guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/PatrolTaskServiceImpl.java
@@ -2,6 +2,7 @@
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.model.PatrolTask;
import com.stylefeng.guns.modular.system.model.TaskDetail;
import com.stylefeng.guns.modular.system.model.TaskDetailVehicles;
@@ -35,10 +36,12 @@
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
 * @author zhibing.pu
@@ -128,6 +131,7 @@
                     
                     //添加任务车船视频通道关系数据
                     List<Integer> videoChannel = vo.getVideoChannel();
                     List<TaskDetailVehiclesChannel> list = new ArrayList<>();
                     for (Integer s : videoChannel) {
                        TaskDetailVehiclesChannel taskDetailVehiclesChannel = new TaskDetailVehiclesChannel();
                        taskDetailVehiclesChannel.setPatrolTaskId(patrolTask.getId());
@@ -135,8 +139,9 @@
                        taskDetailVehiclesChannel.setTaskDetailVehiclesId(taskDetailVehicles.getId());
                        taskDetailVehiclesChannel.setVideoChannel(s);
                        taskDetailVehiclesChannel.setSysStatus(1);
                        taskDetailVehiclesChannelService.insert(taskDetailVehiclesChannel);
                        list.add(taskDetailVehiclesChannel);
                     }
                     taskDetailVehiclesChannelService.insertBatch(list);
                  }
                  
                  //添加定时任务
@@ -168,6 +173,10 @@
   public void execute(Integer id){
      //修改状态为执行中
      TaskDetail taskDetail = taskDetailService.selectById(id);
      if(null == taskDetail){
         log.error("定时任务执行异常,无效的数据ID:{}", id);
         return;
      }
      taskDetail.setStatus(2);
      taskDetailService.updateById(taskDetail);
      
@@ -181,157 +190,131 @@
      
      //获取当前任务中的车船信息
      List<TaskDetailVehicles> taskDetailVehicles = taskDetailVehiclesService.selectList(new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", id));
      int errNum = 0;
      for (TaskDetailVehicles taskDetailVehicle : taskDetailVehicles) {
         try {
            taskDetailVehicle.setStartExecutionTime(LocalDateTime.now());
            Integer taskDetailVehicleId = taskDetailVehicle.getId();
            //车船类型:1 车辆设备 2 船舶设备
            Integer vehicleType = taskDetailVehicle.getVehicleType();
            //车船id
            Integer vehicleId = taskDetailVehicle.getVehicleId();
            VehicleOnline vehicleOnline1 = null;
            for (VehicleOnline vehicleOnline : vehicleOnlines2018) {
               if(vehicleOnline.getVehicleId().equals(vehicleId) && vehicleOnline.getVehicleType().equals(vehicleType)){
                  vehicleOnline1 = vehicleOnline;
                  String vehicleGpsProtocol = vehicleOnline.getVehicleGpsProtocol();
                  vehicleOnline1.setVehicleGpsProtocol("808-guangzhou".equals(vehicleGpsProtocol) ? "1" : "2");
                  break;
               }
            }
            if(null == vehicleOnline1){
               for (VehicleOnline vehicleOnline : vehicleOnlines203) {
                  if(vehicleOnline.getVehicleId().equals(vehicleId) && vehicleOnline.getVehicleType().equals(vehicleType)){
                     vehicleOnline1 = vehicleOnline;
                     vehicleOnline1.setVehicleGpsProtocol(vehicleOnline.getVehicleGpsProtocol());
                     break;
                  }
               }
            }
            //不在线
            if(null == vehicleOnline1){
               taskDetailVehicle.setEndExecutionTime(LocalDateTime.now());
               taskDetailVehicle.setStatus(4);
               taskDetailVehiclesService.updateById(taskDetailVehicle);
               continue;
            }
            taskDetailVehicle.setStatus(2);
            taskDetailVehiclesService.updateById(taskDetailVehicle);
            //成功
            List<TaskDetailVehiclesChannel> taskDetailVehiclesChannels = taskDetailVehiclesChannelService.selectList(new EntityWrapper<TaskDetailVehiclesChannel>().eq("task_detail_vehicles_id", taskDetailVehicleId));
            //开始发送拍照指令,获取照片地址
            String vehicleGpsProtocol = vehicleOnline1.getVehicleGpsProtocol();
            String terminalId = vehicleOnline1.getTerminalId();
            for (TaskDetailVehiclesChannel taskDetailVehiclesChannel : taskDetailVehiclesChannels) {
               Integer videoChannel = taskDetailVehiclesChannel.getVideoChannel();
               if("1".equals(vehicleGpsProtocol)){
                  //2018 协议
                  String fileId = VideoGateway.cameraShot2018(terminalId, videoChannel, 0);
                  if(null == fileId){
                     log.error("2018年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                     throw new RuntimeException();
                  }else{
                     //等待15秒
                     Thread.sleep(15000);
                     String fileUrl = VideoGateway.getCameraShotByFileId2018(terminalId, fileId, 0);
                     if(null == fileUrl){
                        log.error("2018年协议车辆获取拍摄图片失败:terminalId-->{},fileId-->{}", terminalId, fileId);
                        //重新发送拍摄指令,如果还是没有获取到图片,标注失败
                        fileId = VideoGateway.cameraShot2018(terminalId, videoChannel, 0);
                        if(null == fileId){
                           log.error("2018年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                           throw new Exception();
                        }else{
                           //等待15秒
                           Thread.sleep(15000);
                           fileUrl = VideoGateway.getCameraShotByFileId2018(terminalId, fileId, 0);
                           if(null == fileUrl){
                              log.error("2018年协议车辆获取拍摄图片失败:terminalId-->{},fileId-->{}", terminalId, fileId);
                              throw new Exception();
                           }else{
                              taskDetailVehiclesChannel.setImageUrl(fileUrl);
                              taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
                              taskDetailVehiclesChannel.setArtificialStatus(1);
                              //开始调模型接口得出结果
                              boolean b = ImageModelUtil.modelCheck(fileUrl, ImageModelEnum.getImageModelEnum(patrolTask.getImageModel()));
                              taskDetailVehiclesChannel.setSysStatus(b ? 2 : 3);
                              taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
      //根据企业id分组用于定义多线上处理
      Set<String> collect = taskDetailVehicles.stream().map(TaskDetailVehicles::getCompanyId).collect(Collectors.toSet());
      //定义线程池
      ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(collect.size(), collect.size(), 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
      //定义多线程阻塞
      CountDownLatch countDownLatch = new CountDownLatch(collect.size());
      for (String companyId : collect) {
         List<TaskDetailVehicles> vehicles = taskDetailVehicles.stream().filter(s -> s.getCompanyId().equals(companyId)).collect(Collectors.toList());
         threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
               for (TaskDetailVehicles taskDetailVehicle : vehicles) {
                  try {
                     taskDetailVehicle.setStartExecutionTime(LocalDateTime.now());
                     Integer taskDetailVehicleId = taskDetailVehicle.getId();
                     //车船类型:1 车辆设备 2 船舶设备
                     Integer vehicleType = taskDetailVehicle.getVehicleType();
                     //车船id
                     Integer vehicleId = taskDetailVehicle.getVehicleId();
                     VehicleOnline vehicleOnline1 = null;
                     for (VehicleOnline vehicleOnline : vehicleOnlines2018) {
                        if(null != vehicleOnline.getVehicleId() && vehicleOnline.getVehicleId().equals(vehicleId) &&
                              null != vehicleOnline.getVehicleType() && vehicleOnline.getVehicleType().equals(vehicleType)){
                           vehicleOnline1 = vehicleOnline;
                           String vehicleGpsProtocol = vehicleOnline.getVehicleGpsProtocol();
                           vehicleOnline1.setVehicleGpsProtocol("808-guangzhou".equals(vehicleGpsProtocol) ? "1" : "2");
                           break;
                        }
                     }
                     if(null == vehicleOnline1){
                        for (VehicleOnline vehicleOnline : vehicleOnlines203) {
                           if(null != vehicleOnline.getVehicleId() && vehicleOnline.getVehicleId().equals(vehicleId) &&
                                 null != vehicleOnline.getVehicleType() && vehicleOnline.getVehicleType().equals(vehicleType)){
                              vehicleOnline1 = vehicleOnline;
                              vehicleOnline1.setVehicleGpsProtocol(vehicleOnline.getVehicleGpsProtocol());
                              break;
                           }
                        }
                     }else{
                        taskDetailVehiclesChannel.setImageUrl(fileUrl);
                        taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
                        taskDetailVehiclesChannel.setArtificialStatus(1);
                        //开始调模型接口得出结果
                        boolean b = ImageModelUtil.modelCheck(fileUrl, ImageModelEnum.getImageModelEnum(patrolTask.getImageModel()));
                        taskDetailVehiclesChannel.setSysStatus(b ? 2 : 3);
                        taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
                     }
                  }
               }else{
                  //2023 协议
                  String fileId = VideoGateway.cameraShot2023(vehicleId, vehicleType, terminalId, videoChannel, 0);
                  if(null == fileId){
                     log.error("2023年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                     throw new Exception();
                  }else{
                     //等待15秒
                     Thread.sleep(15000);
                     String fileUrl = VideoGateway.getCameraShotByFileId2023(fileId, 0);
                     if(null == fileUrl){
                        log.error("2023年协议车辆获取拍摄图片失败:terminalId-->{},fileId-->{}", terminalId, fileId);
                        //重新发送拍摄指令,如果还是没有获取图片,标注异常
                        fileId = VideoGateway.cameraShot2023(vehicleId, vehicleType, terminalId, videoChannel, 0);
                        if(null == fileId){
                           log.error("2023年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                           throw new Exception();
                        }else{
                           //等待15秒
                           Thread.sleep(15000);
                           fileUrl = VideoGateway.getCameraShotByFileId2023(fileId, 0);
                           if(null == fileUrl){
                              log.error("2023年协议车辆获取拍摄图片失败:terminalId-->{},fileId-->{}", terminalId, fileId);
                              throw new Exception();
                     //不在线
                     if(null == vehicleOnline1){
                        taskDetailVehicle.setEndExecutionTime(LocalDateTime.now());
                        taskDetailVehicle.setStatus(4);
                        taskDetailVehiclesService.updateById(taskDetailVehicle);
                        continue;
                     }
                     taskDetailVehicle.setStatus(2);
                     taskDetailVehiclesService.updateById(taskDetailVehicle);
                     //成功
                     List<TaskDetailVehiclesChannel> taskDetailVehiclesChannels = taskDetailVehiclesChannelService.selectList(new EntityWrapper<TaskDetailVehiclesChannel>().eq("task_detail_vehicles_id", taskDetailVehicleId));
                     //开始发送拍照指令,获取照片地址
                     String vehicleGpsProtocol = vehicleOnline1.getVehicleGpsProtocol();
                     String terminalId = vehicleOnline1.getTerminalId();
                     for (TaskDetailVehiclesChannel taskDetailVehiclesChannel : taskDetailVehiclesChannels) {
                        Integer videoChannel = taskDetailVehiclesChannel.getVideoChannel();
                        try {
                           if("1".equals(vehicleGpsProtocol)){
                              //2018 协议
                              String fileId = VideoGateway.cameraShot2018(terminalId, videoChannel, 0);
                              if(ToolUtil.isNotEmpty(fileId)){
                                 //等待30秒
                                 Thread.sleep(30000);
                                 String fileUrl = VideoGateway.getCameraShotByFileId2018(terminalId, fileId, 0);
                                 if(ToolUtil.isNotEmpty(fileUrl)){
                                    taskDetailVehiclesChannel.setImageUrl(fileUrl);
                                    taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
                                    taskDetailVehiclesChannel.setArtificialStatus(1);
                                    //开始调模型接口得出结果
                                    Map<String, Object> map = ImageModelUtil.modelCheck(fileUrl, ImageModelEnum.getImageModelEnum(patrolTask.getImageModel()));
                                    Boolean b = (Boolean) map.get("b");
                                    String r = map.get("r").toString();
                                    taskDetailVehiclesChannel.setSysStatus(b ? 2 : 3);
                                    taskDetailVehiclesChannel.setResult(r);
                                    taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
                                 }
                              }
                           }else{
                              taskDetailVehiclesChannel.setImageUrl(fileUrl);
                              taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
                              taskDetailVehiclesChannel.setArtificialStatus(1);
                              //开始调模型接口得出结果
                              boolean b = ImageModelUtil.modelCheck(fileUrl, ImageModelEnum.getImageModelEnum(patrolTask.getImageModel()));
                              taskDetailVehiclesChannel.setSysStatus(b ? 2 : 3);
                              taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
                              //2023 协议
                              String fileId = VideoGateway.cameraShot2023(vehicleId, vehicleType, terminalId, videoChannel, 0);
                              if(ToolUtil.isNotEmpty(fileId)){
                                 //等待30秒
                                 Thread.sleep(30000);
                                 String fileUrl = VideoGateway.getCameraShotByFileId2023(fileId, 0);
                                 if(ToolUtil.isNotEmpty(fileUrl)){
                                    taskDetailVehiclesChannel.setImageUrl(fileUrl);
                                    taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
                                    taskDetailVehiclesChannel.setArtificialStatus(1);
                                    //开始调模型接口得出结果
                                    Map<String, Object> map = ImageModelUtil.modelCheck(fileUrl, ImageModelEnum.getImageModelEnum(patrolTask.getImageModel()));
                                    Boolean b = (Boolean) map.get("b");
                                    String r = map.get("r").toString();
                                    taskDetailVehiclesChannel.setSysStatus(b ? 2 : 3);
                                    taskDetailVehiclesChannel.setResult(r);
                                    taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
                                 }
                              }
                           }
                        }catch (Exception e){
                           taskDetailVehiclesChannel.setResult(e.getMessage());
                           taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
                        }
                     }else{
                        taskDetailVehiclesChannel.setImageUrl(fileUrl);
                        taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
                        taskDetailVehiclesChannel.setArtificialStatus(1);
                        //开始调模型接口得出结果
                        boolean b = ImageModelUtil.modelCheck(fileUrl, ImageModelEnum.getImageModelEnum(patrolTask.getImageModel()));
                        taskDetailVehiclesChannel.setSysStatus(b ? 2 : 3);
                        taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
                     }
                     taskDetailVehicle.setEndExecutionTime(LocalDateTime.now());
                     taskDetailVehicle.setStatus(3);
                     taskDetailVehiclesService.updateById(taskDetailVehicle);
                  }catch (Exception e){
                     e.printStackTrace();
                     //失败
                     taskDetailVehicle.setEndExecutionTime(LocalDateTime.now());
                     taskDetailVehicle.setStatus(5);
                     taskDetailVehiclesService.updateById(taskDetailVehicle);
                  }
               }
               //当前线程执行完成后减少等待数
               countDownLatch.countDown();
            }
            taskDetailVehicle.setEndExecutionTime(LocalDateTime.now());
            taskDetailVehicle.setStatus(3);
            taskDetailVehiclesService.updateById(taskDetailVehicle);
         }catch (Exception e){
            e.printStackTrace();
            //失败
            taskDetailVehicle.setEndExecutionTime(LocalDateTime.now());
            taskDetailVehicle.setStatus(5);
            taskDetailVehiclesService.updateById(taskDetailVehicle);
            errNum++;
         }
         });
      }
      taskDetail.setStatus(0 == errNum ? 3 : 4);
      try {
         countDownLatch.await();
      } catch (InterruptedException e) {
         throw new RuntimeException(e);
      }
      int count = taskDetailVehiclesService.selectCount(new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", id).eq("status", 5));
      taskDetail.setStatus(count > 0 ? 4 : 3);
      taskDetailService.updateById(taskDetail);
   }