| package com.ruoyi.inspect.service.impl;  | 
|   | 
| import com.ruoyi.device.camera.domain.CameraCmd;  | 
| import com.ruoyi.device.camera.service.ICameraSdkService;  | 
| import com.ruoyi.utils.sdk.common.GlobalVariable;  | 
| import com.ruoyi.inspect.domain.ArdVideoInspectTask;  | 
| import com.ruoyi.inspect.mapper.ArdVideoInspectTaskMapper;  | 
| import lombok.extern.slf4j.Slf4j;  | 
| import org.springframework.stereotype.Component;  | 
|   | 
| import javax.annotation.Resource;  | 
| import java.util.HashMap;  | 
| import java.util.List;  | 
| import java.util.Map;  | 
|   | 
| /**  | 
|  * @Description: 巡检任务管理  | 
|  * @ClassName: InspectionTaskManager  | 
|  * @Author: 刘苏义  | 
|  * @Date: 2023年06月01日10:40  | 
|  * @Version: 1.0  | 
|  **/  | 
|   | 
| @Component  | 
| @Slf4j(topic = "patrolInspectionTask")  | 
| public class InspectionTaskManager {  | 
|   | 
|     @Resource  | 
|     ArdVideoInspectTaskMapper ardVideoInspectTaskMapper;  | 
|     @Resource  | 
|     private ICameraSdkService cameraSdkService;  | 
|   | 
|     public static Map<String, InspectionTask> taskMap;//已启动任务map  | 
|   | 
|     public InspectionTaskManager() {  | 
|         taskMap = new HashMap<>();  | 
|     }  | 
|   | 
|     public Map<String, InspectionTask> getTaskMap() {  | 
|         return taskMap;  | 
|     }  | 
|   | 
|     /*启动任务*/  | 
|     public void startInspectionTask(String taskId) {  | 
|         if(taskMap.containsKey(taskId))  | 
|         {  | 
|             log.info("手动巡检任务已启动:" + taskId);  | 
|             return;  | 
|         }  | 
|         /*每次启动任务清空任务当前步骤信息*/  | 
|         ArdVideoInspectTask ardVideoInspectTask = ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(taskId);  | 
|         String cameraId = ardVideoInspectTask.getCameraId();  | 
|         /*将当前相机的所有任务当前时间置空*/  | 
|         ardVideoInspectTask=new ArdVideoInspectTask();  | 
|         ardVideoInspectTask.setCameraId(cameraId);  | 
|         List<ArdVideoInspectTask> ardVideoInspectTasks = ardVideoInspectTaskMapper.selectArdVideoInspectTaskList(ardVideoInspectTask);  | 
|         if(ardVideoInspectTasks.size()>0) {  | 
|             for (ArdVideoInspectTask VideoInspectTask : ardVideoInspectTasks) {  | 
|                 VideoInspectTask.setCurrentStepStartTime("");  | 
|                 ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(VideoInspectTask);  | 
|             }  | 
|         }  | 
|         /*开启任务*/  | 
|         InspectionTask task = new InspectionTask(taskId);  | 
|         task.start();  | 
|         taskMap.put(taskId, task);  | 
|         log.info("手动巡检任务启动:" + taskId);  | 
|     }  | 
|     /*停止任务*/  | 
|     public void stopInspectionTask(String taskId) {  | 
|   | 
|         ArdVideoInspectTask ardVideoInspectTask = ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(taskId);  | 
|         /*将关联的相机录像关闭*/  | 
|         String cameraId = ardVideoInspectTask.getCameraId();  | 
|         if( GlobalVariable.previewMap.containsKey(cameraId))  | 
|         {   //当前相机正在录像,则停止录像  | 
|             CameraCmd cmd = new CameraCmd();  | 
|             cmd.setCameraId(ardVideoInspectTask.getCameraId());  | 
|             cmd.setChanNo(ardVideoInspectTask.getChannel());  | 
|             cameraSdkService.recordStopNotToMinio(cmd);  | 
|             GlobalVariable.previewMap.remove(cameraId);  | 
|         }  | 
|         /*停止任务*/  | 
|         InspectionTask task = taskMap.get(taskId);  | 
|         if (task != null) {  | 
|             task.stop();  | 
|             taskMap.remove(taskId);  | 
|             log.info("手动巡检任务已停止:" + taskId);  | 
|             /*将任务步骤信息清空*/  | 
|             ardVideoInspectTask.setCurrentStepId("");  | 
|             ardVideoInspectTask.setCurrentStepStartTime("");  | 
|             ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask);  | 
|         } else {  | 
|             log.info("找不到指定的手动巡检任务:" + taskId);  | 
|         }  | 
|     }  | 
| }  |