|  |  | 
 |  |  | 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.config.MinioClientSingleton; | 
 |  |  | import com.ruoyi.device.hiksdk.service.IHikClientService; | 
 |  |  | import com.ruoyi.inspect.domain.ArdVideoInspectRecord; | 
 |  |  | import com.ruoyi.inspect.domain.ArdVideoInspectTask; | 
 |  |  | import com.ruoyi.inspect.domain.ArdVideoInspectTaskStep; | 
 |  |  | import com.ruoyi.inspect.service.IArdVideoInspectRecordService; | 
 |  |  | import com.ruoyi.inspect.service.IArdVideoInspectTaskService; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import javax.annotation.Resource; | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @Description: 巡检任务 | 
 |  |  | 
 |  |  |  * @Version: 1.0 | 
 |  |  |  **/ | 
 |  |  | @Component("PatrolInspectionTask") | 
 |  |  | @Slf4j(topic = "PatrolInspectionTask") | 
 |  |  | public class PatrolInspectionTask { | 
 |  |  |  | 
 |  |  |     @Resource | 
 |  |  |     IArdVideoInspectTaskService ardVideoInspectTaskService; | 
 |  |  |     @Resource | 
 |  |  |     IArdAlarmpointsWellService ardAlarmpointsWellService; | 
 |  |  |     @Resource | 
 |  |  |     IArdVideoInspectRecordService ardVideoInspectRecordService; | 
 |  |  |     @Resource | 
 |  |  |     IArdCamerasService ardCamerasService; | 
 |  |  |     @Resource | 
 |  |  |     IHikClientService hikClientService; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 定时巡检任务 | 
 |  |  |      * 刘苏义 | 
 |  |  |      * 2023/8/9 9:18:42 | 
 |  |  |      */ | 
 |  |  |     public void scanRun() { | 
 |  |  |         /*扫描所有可执行任务1-时间满足2-自动*/ | 
 |  |  |         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) { | 
 |  |  |                 /*获取当前任务的所有巡检步骤*/ | 
 |  |  |                 List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTaskService.selectArdVideoInspectTaskById(videoInspectTask.getId()).getArdVideoInspectTaskStepList(); | 
 |  |  |                 if (ardVideoInspectTaskStepList.size() == 0) { | 
 |  |  |                     continue; | 
 |  |  |                 } | 
 |  |  |                 videoInspectTask.setArdVideoInspectTaskStepList(ardVideoInspectTaskStepList); | 
 |  |  |                 String currentStepId = videoInspectTask.getCurrentStepId(); | 
 |  |  |                 if (StringUtils.isNull(currentStepId)) { | 
 |  |  |                     videoInspectTask.setCurrentStepId(ardVideoInspectTaskStepList.get(0).getId()); | 
 |  |  |                     startRunStep(videoInspectTask);//开始当前任务的第一个步骤 | 
 |  |  |                 } else /*当前任务已经执行,判断是否到期*/ { | 
 |  |  |                     boolean expird = isExpirdStep(videoInspectTask); /*判断当前步骤时间是否过期*/ | 
 |  |  |                     if (expird) { | 
 |  |  |                         /*已过期,停止录像*/ | 
 |  |  |  | 
 |  |  |                         stopRunStep(videoInspectTask); | 
 |  |  |                         /*切换下一步*/ | 
 |  |  |                         String nextStepId = changeNextStep(videoInspectTask); | 
 |  |  |                         /*开始*/ | 
 |  |  |                         videoInspectTask.setCurrentStepId(nextStepId); | 
 |  |  |                         startRunStep(videoInspectTask); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //步骤开始 | 
 |  |  |     private void startRunStep(ArdVideoInspectTask ardVideoInspectTask) { | 
 |  |  |         String currentStepId = ardVideoInspectTask.getCurrentStepId(); | 
 |  |  |         if(StringUtils.isNull(currentStepId)) | 
 |  |  |         { | 
 |  |  |             log.info("当前开始巡检步骤id为空"); | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         log.info("步骤:" + currentStepId + "开始"); | 
 |  |  |         String cameraId = ardVideoInspectTask.getCameraId(); | 
 |  |  |         Integer channel = ardVideoInspectTask.getChannel(); | 
 |  |  |         Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream() | 
 |  |  |                 .filter(obj -> obj.getId().equals(currentStepId)) | 
 |  |  |                 .findFirst(); | 
 |  |  |         if (objectOptional.isPresent()) { | 
 |  |  |             ArdVideoInspectTaskStep step = objectOptional.get(); | 
 |  |  |             String wellId = step.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(step.getRecordingTime()); | 
 |  |  |                 boolean setTargetPosition = hikClientService.setTargetPosition(cmd); | 
 |  |  |                 if (setTargetPosition) { | 
 |  |  |                     /*控制相机巡检成功,开始录像*/ | 
 |  |  |                     cmd.setEnable(true);//启动录像 | 
 |  |  |                     hikClientService.recordToMinio(cmd); | 
 |  |  |                     /*更新任务当前步骤id和步骤启动时间*/ | 
 |  |  |                     ArdVideoInspectTask avit = new ArdVideoInspectTask(); | 
 |  |  |                     avit.setId(ardVideoInspectTask.getId()); | 
 |  |  |                     avit.setCurrentStepId(step.getId()); | 
 |  |  |                     avit.setCurrentStepStartTime(DateUtils.getTime()); | 
 |  |  |                     ardVideoInspectTaskService.updateArdVideoInspectTaskNoUpdater(avit); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //步骤停止 | 
 |  |  |     private void stopRunStep(ArdVideoInspectTask ardVideoInspectTask) { | 
 |  |  |         String currentStepId = ardVideoInspectTask.getCurrentStepId(); | 
 |  |  |         log.info("步骤:" + currentStepId + "停止"); | 
 |  |  |         if(StringUtils.isNull(currentStepId)) | 
 |  |  |         { | 
 |  |  |             log.info("当前停止巡检步骤id为空"); | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         String currentStepStartTime = ardVideoInspectTask.getCurrentStepStartTime(); | 
 |  |  |         String cameraId = ardVideoInspectTask.getCameraId(); | 
 |  |  |         Integer channel = ardVideoInspectTask.getChannel(); | 
 |  |  |         Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream() | 
 |  |  |                 .filter(obj -> obj.getId().equals(currentStepId)) | 
 |  |  |                 .findFirst(); | 
 |  |  |         if (objectOptional.isPresent()) { | 
 |  |  |             ArdVideoInspectTaskStep step = objectOptional.get(); | 
 |  |  |             /*停止录像*/ | 
 |  |  |             CameraCmd cmd = new CameraCmd(); | 
 |  |  |             cmd.setCameraId(cameraId); | 
 |  |  |             cmd.setChannelNum(channel); | 
 |  |  |             cmd.setOperator("sys_patrol_inspect"); | 
 |  |  |             cmd.setEnable(false);//停止录像 | 
 |  |  |             String uuid = UUID.randomUUID().toString().replace("-", ""); | 
 |  |  |             String time = new SimpleDateFormat("yyyyMMdd").format(new Date()); | 
 |  |  |             String recordName = cameraId + "/" + time + "/" + uuid + ".mp4"; | 
 |  |  |             cmd.setRecordBucketName("record"); | 
 |  |  |             cmd.setRecordObjectName(recordName); | 
 |  |  |             hikClientService.recordToMinio(cmd); | 
 |  |  |             /*插入巡检记录*/ | 
 |  |  |             ArdVideoInspectRecord ardVideoInspectRecord = new ArdVideoInspectRecord(); | 
 |  |  |             ardVideoInspectRecord.setStepId(step.getId()); | 
 |  |  |             Date currentStepStartDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStartTime); | 
 |  |  |             Date currentStepStopDate = DateUtils.addMinutes(currentStepStartDate, step.getRecordingTime()); | 
 |  |  |             ardVideoInspectRecord.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStartDate)); | 
 |  |  |             ardVideoInspectRecord.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStopDate)); | 
 |  |  |             String url = MinioClientSingleton.domainUrl + "/" + cmd.getRecordBucketName() + "/" + recordName; | 
 |  |  |             ardVideoInspectRecord.setRecordFilePath(url); | 
 |  |  |             ardVideoInspectRecordService.insertArdVideoInspectRecord(ardVideoInspectRecord); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //步骤判断是否过期 | 
 |  |  |     private boolean isExpirdStep(ArdVideoInspectTask ardVideoInspectTask) { | 
 |  |  |         List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTask.getArdVideoInspectTaskStepList(); | 
 |  |  |         /*获取当前任务正在执行的步骤和当前步骤开始的时间*/ | 
 |  |  |         String currentStepId = ardVideoInspectTask.getCurrentStepId(); | 
 |  |  |         String currentStepStartTime = ardVideoInspectTask.getCurrentStepStartTime(); | 
 |  |  |         Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTaskStepList.stream() | 
 |  |  |                 .filter(ardVideoInspectTaskStep -> ardVideoInspectTaskStep.getId().equals(currentStepId)) | 
 |  |  |                 .findFirst(); | 
 |  |  |         if (objectOptional.isPresent()) { | 
 |  |  |             /*获取当前步骤信息*/ | 
 |  |  |             ArdVideoInspectTaskStep currentStep = objectOptional.get(); | 
 |  |  |             /*获取到当前步骤的开始和结束时间*/ | 
 |  |  |             Date currentStepStartDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStartTime); | 
 |  |  |             Date currentStepStopDate = DateUtils.addMinutes(currentStepStartDate, currentStep.getRecordingTime()); | 
 |  |  |             /*判断当前步骤时间是否过期*/ | 
 |  |  |             if (!DateUtils.TimeCompare(currentStepStartDate, currentStepStopDate)) { | 
 |  |  |                 return true; | 
 |  |  |             } else { | 
 |  |  |                 return false; | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //步骤切换 | 
 |  |  |     private String changeNextStep(ArdVideoInspectTask ardVideoInspectTask) { | 
 |  |  |         String currentStepId = ardVideoInspectTask.getCurrentStepId(); | 
 |  |  |         Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream() | 
 |  |  |                 .filter(obj -> obj.getId().equals(currentStepId)) | 
 |  |  |                 .findFirst(); | 
 |  |  |         if (objectOptional.isPresent()) { | 
 |  |  |             ArdVideoInspectTaskStep step = objectOptional.get(); | 
 |  |  |             Integer currentStepOrderNumber = step.getOrderNumber(); | 
 |  |  |             /*判断当前步骤序号是否小于步骤总数*/ | 
 |  |  |             if (currentStepOrderNumber < ardVideoInspectTask.getArdVideoInspectTaskStepList().size()) { | 
 |  |  |                 /*小于则执行下一步骤*/ | 
 |  |  |                 currentStepOrderNumber++; | 
 |  |  |             } else { | 
 |  |  |                 /*否则从1开始执行*/ | 
 |  |  |                 currentStepOrderNumber = 1; | 
 |  |  |             } | 
 |  |  |             Integer nextStepOrderNumber = currentStepOrderNumber; | 
 |  |  |             /*更新当前任务切换新步骤*/ | 
 |  |  |             ArdVideoInspectTask avit = new ArdVideoInspectTask(); | 
 |  |  |             avit.setId(ardVideoInspectTask.getId()); | 
 |  |  |             String nextStepId = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream() | 
 |  |  |                     .filter(obj -> obj.getOrderNumber() == nextStepOrderNumber) | 
 |  |  |                     .map(ArdVideoInspectTaskStep::getId) | 
 |  |  |                     .findFirst() | 
 |  |  |                     .orElse(null); | 
 |  |  |             avit.setCurrentStepId(nextStepId); | 
 |  |  |             ardVideoInspectTaskService.updateArdVideoInspectTaskNoUpdater(avit); | 
 |  |  |             log.info("步骤:" + currentStepId + "切换为"+nextStepId); | 
 |  |  |             return nextStepId; | 
 |  |  |         } | 
 |  |  |         return ""; | 
 |  |  |       ardVideoInspectTaskService.autoTaskRun(); | 
 |  |  |     } | 
 |  |  | } |