|  |  | 
 |  |  | package com.ruoyi.quartz.task; | 
 |  |  |  | 
 |  |  | import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWell; | 
 |  |  | import com.ruoyi.alarmpoints.well.service.IArdAlarmpointsWellService; | 
 |  |  | import com.ruoyi.common.utils.DateUtils; | 
 |  |  | import com.ruoyi.common.utils.StringUtils; | 
 |  |  | import com.ruoyi.device.camera.domain.ArdCameras; | 
 |  |  | import com.ruoyi.device.camera.domain.CameraCmd; | 
 |  |  | import com.ruoyi.device.camera.service.IArdCamerasService; | 
 |  |  | import com.ruoyi.device.hiksdk.service.IHikClientService; | 
 |  |  | import com.ruoyi.inspect.domain.ArdVideoInspectTask; | 
 |  |  | import com.ruoyi.inspect.domain.ArdVideoInspectTaskStep; | 
 |  |  | import com.ruoyi.inspect.service.IArdVideoInspectTaskService; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  | import javax.annotation.Resource; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Optional; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @Description: 巡检任务 | 
 |  |  | 
 |  |  |  * @Version: 1.0 | 
 |  |  |  **/ | 
 |  |  | @Component("PatrolInspectionTask") | 
 |  |  | @Slf4j(topic = "PatrolInspectionTask") | 
 |  |  | public class PatrolInspectionTask { | 
 |  |  |  | 
 |  |  |     @Resource | 
 |  |  |     IArdVideoInspectTaskService ardVideoInspectTaskService; | 
 |  |  |     @Resource | 
 |  |  |     IArdAlarmpointsWellService ardAlarmpointsWellService; | 
 |  |  |  | 
 |  |  |     @Resource | 
 |  |  |     IArdCamerasService ardCamerasService; | 
 |  |  |     @Resource | 
 |  |  |     IHikClientService hikClientService; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 定时巡检任务 | 
 |  |  |      * 刘苏义 | 
 |  |  |      * 2023/8/9 9:18:42 | 
 |  |  |      */ | 
 |  |  |     public void scanRun() { | 
 |  |  |         /*扫描所有可执行任务1-时间满足2-自动*/ | 
 |  |  |         log.info("开始扫描所有可执行任务"); | 
 |  |  |         ArdVideoInspectTask ardVideoInspectTask = new ArdVideoInspectTask(); | 
 |  |  |         ardVideoInspectTask.setInspectMode("自动"); | 
 |  |  |         List<ArdVideoInspectTask> ardVideoInspectTasks = ardVideoInspectTaskService.selectArdVideoInspectTaskList(ardVideoInspectTask); | 
 |  |  |         for (ArdVideoInspectTask videoInspectTask : ardVideoInspectTasks) { | 
 |  |  |             /*遍历所有时间满足的自动任务*/ | 
 |  |  |             boolean timeCompare = DateUtils.TimeCompare(videoInspectTask.getStartTime(), videoInspectTask.getEndTime()); | 
 |  |  |             if (timeCompare) { | 
 |  |  |                 log.info("满足时间"); | 
 |  |  |                 /*当前任务的巡检步骤*/ | 
 |  |  |                 List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTaskService.selectArdVideoInspectTaskById(videoInspectTask.getId()).getArdVideoInspectTaskStepList(); | 
 |  |  |                 Integer setpNum=ardVideoInspectTaskStepList.size();//步骤数量 | 
 |  |  |                 /*获取当前任务正在执行的步骤和当前步骤开始的时间*/ | 
 |  |  |                 String currentStepId = videoInspectTask.getCurrentStepId(); | 
 |  |  |                 String cameraId = videoInspectTask.getCameraId(); | 
 |  |  |                 Integer channel = videoInspectTask.getChannel(); | 
 |  |  |                 String currentStepStartTime = videoInspectTask.getCurrentStepStartTime(); | 
 |  |  |                 /*如果当前步骤未找到,则从1开始*/ | 
 |  |  |                 if (StringUtils.isNull(currentStepId)) { | 
 |  |  |                     String wellId = ardVideoInspectTaskStepList.get(0).getWellId();//获取第一个步骤的井号 | 
 |  |  |                     if (!StringUtils.isNull(wellId)) { | 
 |  |  |                         /*获取井坐标*/ | 
 |  |  |                         ArdAlarmpointsWell ardAlarmpointsWell = ardAlarmpointsWellService.selectArdAlarmpointsWellById(wellId); | 
 |  |  |                         double[] targetPositon = new double[3]; | 
 |  |  |                         targetPositon[0]=ardAlarmpointsWell.getLongitude(); | 
 |  |  |                         targetPositon[1]=ardAlarmpointsWell.getLatitude(); | 
 |  |  |                         targetPositon[2]=ardAlarmpointsWell.getAltitude(); | 
 |  |  |                         /*获取相机坐标*/ | 
 |  |  |                         ArdCameras cameras = ardCamerasService.selectArdCamerasById(cameraId); | 
 |  |  |                         double[] cameraPositon = new double[3]; | 
 |  |  |                         cameraPositon[0]=cameras.getLongitude(); | 
 |  |  |                         cameraPositon[1]=cameras.getLatitude(); | 
 |  |  |                         cameraPositon[2]=cameras.getAltitude(); | 
 |  |  |                         /*控制相机巡检*/ | 
 |  |  |                         CameraCmd cmd = new CameraCmd(); | 
 |  |  |                         cmd.setCameraId(cameraId); | 
 |  |  |                         cmd.setChannelNum(channel); | 
 |  |  |                         cmd.setCamPosition(cameraPositon); | 
 |  |  |                         cmd.setTargetPosition(targetPositon); | 
 |  |  |                         cmd.setOperator("sys_patrol_inspect"); | 
 |  |  |                         cmd.setExpired(ardVideoInspectTaskStepList.get(0).getRecordingTime()); | 
 |  |  |                         boolean setTargetPosition = hikClientService.setTargetPosition(cmd); | 
 |  |  |                         if(setTargetPosition) | 
 |  |  |                         { | 
 |  |  |                             /*控制相机巡检成功,开始录像*/ | 
 |  |  |                             cmd.setEnable(true);//启动录像 | 
 |  |  |                             hikClientService.record(cmd); | 
 |  |  |                             /*更新任务当前步骤id和步骤启动时间*/ | 
 |  |  |                             ArdVideoInspectTask avit=new ArdVideoInspectTask(); | 
 |  |  |                             avit.setId(videoInspectTask.getId()); | 
 |  |  |                             avit.setCurrentStepId(ardVideoInspectTaskStepList.get(0).getId()); | 
 |  |  |                             avit.setCurrentStepStartTime(DateUtils.getTime()); | 
 |  |  |                             //TODO with int i = ardVideoInspectTaskService.updateArdVideoInspectTaskWithCurrentStepInfo(avit); | 
 |  |  |                            // log.info("更新结果"+i); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 else /*当前任务已经执行,判断是否到期*/ | 
 |  |  |                 { | 
 |  |  |                     Optional<Integer> recordingTime = ardVideoInspectTaskStepList.stream() | 
 |  |  |                             .filter(ardVideoInspectTaskStep -> ardVideoInspectTaskStep.getId().equals(currentStepId)) | 
 |  |  |                             .map(ArdVideoInspectTaskStep::getRecordingTime) | 
 |  |  |                             .findFirst(); | 
 |  |  |                     if (recordingTime.isPresent()) { | 
 |  |  |                         System.out.println("找到了匹配的对象,名称为: " + recordingTime.get()); | 
 |  |  |                         /*获取到当前步骤的持续时间*/ | 
 |  |  |                         Date currentStepStartDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStartTime); | 
 |  |  |                         Date currentStepStopDate = DateUtils.addMinutes(currentStepStartDate,recordingTime.get()); | 
 |  |  |                         /*判断时间是否过期*/ | 
 |  |  |                         if(!DateUtils.TimeCompare(currentStepStartDate,currentStepStopDate)) | 
 |  |  |                         { | 
 |  |  |                             /*已过期,停止录像,执行下一步骤*/ | 
 |  |  |                             CameraCmd cmd = new CameraCmd(); | 
 |  |  |                             cmd.setCameraId(cameraId); | 
 |  |  |                             cmd.setChannelNum(channel); | 
 |  |  |                             cmd.setOperator("sys_patrol_inspect"); | 
 |  |  |                             cmd.setEnable(false);//停止录像 | 
 |  |  |                             String record = hikClientService.record(cmd); | 
 |  |  |                             /*更新巡检记录*/ | 
 |  |  |  | 
 |  |  |                             log.info(record); | 
 |  |  |                         } | 
 |  |  |                     } else { | 
 |  |  |                         System.out.println("未找到匹配的对象"); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                 } | 
 |  |  |                 /*否则判断当前步骤开始时间,判断是否到期*/ | 
 |  |  |                 /*未到期*/ | 
 |  |  |                 /*忽略*/ | 
 |  |  |                 /*已到期*/ | 
 |  |  |                         /*结束录像,转到下一个步骤, | 
 |  |  |                         控制云台指向关联井坐标,并开始录像,更新当前步骤和当前步骤开始时间/ | 
 |  |  |                          */ | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |       ardVideoInspectTaskService.autoTaskRun(); | 
 |  |  |     } | 
 |  |  | } |