| | |
| | | package com.ruoyi.inspect.service.impl; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWell; |
| | | import com.ruoyi.alarmpoints.well.mapper.ArdAlarmpointsWellMapper; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.constant.Global; |
| | | import com.ruoyi.common.utils.uuid.IdUtils; |
| | | import com.ruoyi.device.camera.domain.ArdCameras; |
| | | import com.ruoyi.device.camera.domain.CameraCmd; |
| | | import com.ruoyi.device.camera.mapper.ArdCamerasMapper; |
| | | import com.ruoyi.device.hiksdk.config.MinioClientSingleton; |
| | | import com.ruoyi.device.camera.service.ICameraSdkService; |
| | | import com.ruoyi.device.hiksdk.service.IHikClientService; |
| | | import com.ruoyi.inspect.domain.ArdVideoInspectRecord; |
| | | import com.ruoyi.inspect.mapper.ArdVideoInspectRecordMapper; |
| | | import com.ruoyi.inspect.mapper.ArdVideoInspectTaskStepMapper; |
| | | import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | |
| | | import com.ruoyi.inspect.mapper.ArdVideoInspectTaskMapper; |
| | | import com.ruoyi.inspect.domain.ArdVideoInspectTask; |
| | | import com.ruoyi.inspect.service.IArdVideoInspectTaskService; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.Resource; |
| | | |
| | |
| | | * @date 2023-05-30 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @Slf4j(topic = "patrolInspectionTask") |
| | | public class ArdVideoInspectTaskServiceImpl implements IArdVideoInspectTaskService { |
| | | @Resource |
| | | private ArdVideoInspectTaskMapper ardVideoInspectTaskMapper; |
| | |
| | | @Resource |
| | | private ArdCamerasMapper ardCamerasMapper; |
| | | @Resource |
| | | private IHikClientService hikClientService; |
| | | private ICameraSdkService cameraSdkService; |
| | | @Resource |
| | | private ArdCamerasMapper camerasMapper; |
| | | |
| | | @PostConstruct |
| | | public void initTask() { |
| | |
| | | } |
| | | ardVideoInspectTask.setCurrentStepId(""); |
| | | ardVideoInspectTask.setCurrentStepStartTime(""); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTask(ardVideoInspectTask); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask); |
| | | } |
| | | } |
| | | |
| | |
| | | @Transactional |
| | | @Override |
| | | public int insertArdVideoInspectTask(ArdVideoInspectTask ardVideoInspectTask) { |
| | | ardVideoInspectTask.setId(IdUtils.simpleUUID()); |
| | | ardVideoInspectTask.setCreateBy(SecurityUtils.getUsername()); |
| | | ardVideoInspectTask.setCreateTime(DateUtils.getNowDate()); |
| | | ardVideoInspectTask.setUserId(SecurityUtils.getUserId()); |
| | |
| | | */ |
| | | public void insertArdVideoInspectTaskStep(ArdVideoInspectTask ardVideoInspectTask) { |
| | | List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTask.getArdVideoInspectTaskStepList(); |
| | | String id = ardVideoInspectTask.getId(); |
| | | //获取相机 |
| | | ArdCameras camera = camerasMapper.selectArdCamerasById(ardVideoInspectTask.getCameraId()); |
| | | Collections.sort(ardVideoInspectTaskStepList, new ArdVideoInspectTaskStepComparator(this.ardAlarmpointsWellMapper, camera.getLongitude(), camera.getLatitude())); |
| | | String taskId = ardVideoInspectTask.getId(); |
| | | for (int i = 0; i < ardVideoInspectTaskStepList.size(); i++) { |
| | | ArdVideoInspectTaskStep step = ardVideoInspectTaskStepList.get(i); |
| | | step.setTaskId(taskId); |
| | | step.setOrderNumber(new Integer(i + 1)); |
| | | } |
| | | if (StringUtils.isNotNull(ardVideoInspectTaskStepList)) { |
| | | List<ArdVideoInspectTaskStep> list = new ArrayList<ArdVideoInspectTaskStep>(); |
| | | for (ArdVideoInspectTaskStep ardVideoInspectTaskStep : ardVideoInspectTaskStepList) { |
| | | if (ardVideoInspectTaskStep.getId() == null) { |
| | | //新的步骤,添加 |
| | | ardVideoInspectTaskStep.setId(IdUtils.simpleUUID()); |
| | | ardVideoInspectTaskStepMapper.insertArdVideoInspectTaskStep(ardVideoInspectTaskStep); |
| | | } else { |
| | | //已有步骤,更新 |
| | |
| | | public void manualTaskRun(String TaskId) { |
| | | try { |
| | | ArdVideoInspectTask videoInspectTask = ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(TaskId); |
| | | if (!videoInspectTask.getMenualSwitch().equals("开")) { |
| | | return; |
| | | } |
| | | if (videoInspectTask.getArdVideoInspectTaskStepList().size() == 0) { |
| | | return; |
| | | } |
| | |
| | | //开始下一步骤 |
| | | videoInspectTask.setCurrentStepId(nextStepId); |
| | | startRunStep(videoInspectTask); |
| | | } else { |
| | | //未过期尝试引导,引导失败清空当前步骤开始时间,标记中断 |
| | | tryGuide(videoInspectTask); |
| | | } |
| | | } |
| | | } |
| | |
| | | continue; |
| | | } |
| | | videoInspectTask.setArdVideoInspectTaskStepList(ardVideoInspectTaskStepList); |
| | | |
| | | String currentStepId = videoInspectTask.getCurrentStepId(); |
| | | String currentStepStartTime = videoInspectTask.getCurrentStepStartTime(); |
| | | if (StringUtils.isNull(currentStepId) || StringUtils.isEmpty(currentStepId)) { |
| | |
| | | //开始下一步骤 |
| | | videoInspectTask.setCurrentStepId(nextStepId); |
| | | startRunStep(videoInspectTask); |
| | | } else { |
| | | tryGuide(videoInspectTask); |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | if(Global.task_record.contains(videoInspectTask.getId())) |
| | | { |
| | | /*停止录像*/ |
| | | CameraCmd cmd = new CameraCmd(); |
| | | cmd.setCameraId(videoInspectTask.getCameraId()); |
| | | cmd.setChannelNum(videoInspectTask.getChannel()); |
| | | cmd.setOperator("sys_patrol_inspect"); |
| | | cmd.setEnable(false);//停止录像 |
| | | String uuid = UUID.randomUUID().toString().replace("-", ""); |
| | | String time = new SimpleDateFormat("yyyyMMdd").format(new Date()); |
| | | String recordName = videoInspectTask.getCameraId() + "/" + time + "/" + uuid + ".mp4"; |
| | | cmd.setRecordBucketName("record"); |
| | | cmd.setRecordObjectName(recordName); |
| | | cmd.setUploadMinio(true); |
| | | hikClientService.recordToMinio(cmd); |
| | | if (StringUtils.isNotEmpty(videoInspectTask.getCurrentStepId())) { |
| | | videoInspectTask.setCurrentStepId(""); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(videoInspectTask); |
| | | } |
| | | if (StringUtils.isEmpty(videoInspectTask.getCurrentStepId()) || StringUtils.isEmpty(videoInspectTask.getCurrentStepStartTime())) { |
| | | continue; |
| | | if (StringUtils.isNotEmpty(videoInspectTask.getCurrentStepStartTime())) { |
| | | videoInspectTask.setCurrentStepId(""); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(videoInspectTask); |
| | | } |
| | | /*过期的任务清空当前步骤信息*/ |
| | | videoInspectTask.setCurrentStepId(""); |
| | | videoInspectTask.setCurrentStepStartTime(""); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTask(videoInspectTask); |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | |
| | | try { |
| | | String currentStepId = ardVideoInspectTask.getCurrentStepId(); |
| | | if (StringUtils.isNull(currentStepId)) { |
| | | log.info("当前开始巡检步骤id为空"); |
| | | log.debug("当前开始巡检步骤id为空"); |
| | | return; |
| | | } |
| | | log.info("步骤:" + currentStepId + "开始"); |
| | | log.debug("巡检步骤:" + 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(); |
| | | /*更新任务当前步骤id和步骤启动时间*/ |
| | | ardVideoInspectTask.setCurrentStepId(step.getId()); |
| | | ardVideoInspectTask.setCurrentStepStartTime(DateUtils.getTime()); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask); |
| | | String wellId = step.getWellId(); |
| | | if (!StringUtils.isNull(wellId)) { |
| | | /*获取井坐标*/ |
| | | ArdAlarmpointsWell ardAlarmpointsWell = ardAlarmpointsWellMapper.selectArdAlarmpointsWellById(wellId); |
| | | double[] targetPositon = new double[3]; |
| | | targetPositon[0] = ardAlarmpointsWell.getLongitude(); |
| | | targetPositon[1] = ardAlarmpointsWell.getLatitude(); |
| | | targetPositon[2] = ardAlarmpointsWell.getAltitude(); |
| | | /*获取相机坐标*/ |
| | | ArdCameras cameras = ardCamerasMapper.selectArdCamerasById(cameraId); |
| | | if(StringUtils.isNull(cameras)) |
| | | { |
| | | log.debug("找不到相机:"+cameraId); |
| | | return; |
| | | } |
| | | 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.setChanNo(channel); |
| | | cmd.setTargetPosition(targetPositon); |
| | | cmd.setOperator("sys_patrol_inspect"); |
| | | cmd.setExpired(step.getRecordingTime() * 60); |
| | | boolean setTargetPosition = cameraSdkService.guideTargetPosition(cmd); |
| | | if (setTargetPosition) { |
| | | /*控制相机巡检成功,开始录像*/ |
| | | cameraSdkService.recordStart(cmd); |
| | | } else { |
| | | /*控制失败,当前步骤启动时间置null*/ |
| | | ardVideoInspectTask.setCurrentStepStartTime(""); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error("巡检开始异常:" + ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | //尝试控制引导 |
| | | private void tryGuide(ArdVideoInspectTask ardVideoInspectTask) { |
| | | try { |
| | | String currentStepId = ardVideoInspectTask.getCurrentStepId(); |
| | | if (StringUtils.isNull(currentStepId)) { |
| | | log.debug("当前开始巡检步骤id为空"); |
| | | return; |
| | | } |
| | | log.debug("巡检正常,尝试引导:" + currentStepId); |
| | | String cameraId = ardVideoInspectTask.getCameraId(); |
| | | Integer channel = ardVideoInspectTask.getChannel(); |
| | | Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream() |
| | |
| | | targetPositon[2] = ardAlarmpointsWell.getAltitude(); |
| | | /*获取相机坐标*/ |
| | | ArdCameras cameras = ardCamerasMapper.selectArdCamerasById(cameraId); |
| | | if(StringUtils.isNull(cameras)) |
| | | { |
| | | log.debug("找不到相机:"+cameraId); |
| | | return; |
| | | } |
| | | double[] cameraPositon = new double[3]; |
| | | cameraPositon[0] = cameras.getLongitude(); |
| | | cameraPositon[1] = cameras.getLatitude(); |
| | |
| | | /*控制相机巡检*/ |
| | | CameraCmd cmd = new CameraCmd(); |
| | | cmd.setCameraId(cameraId); |
| | | cmd.setChannelNum(channel); |
| | | cmd.setCamPosition(cameraPositon); |
| | | cmd.setChanNo(channel); |
| | | cmd.setTargetPosition(targetPositon); |
| | | cmd.setOperator("sys_patrol_inspect"); |
| | | cmd.setExpired(step.getRecordingTime()); |
| | | boolean setTargetPosition = hikClientService.setTargetPosition(cmd); |
| | | if (setTargetPosition) { |
| | | if(!Global.task_record.contains(ardVideoInspectTask.getId())) |
| | | { |
| | | /*控制相机巡检成功,开始录像*/ |
| | | cmd.setEnable(true);//启动录像 |
| | | hikClientService.recordToMinio(cmd); |
| | | Global.task_record.add(ardVideoInspectTask.getId()); |
| | | } |
| | | /*更新任务当前步骤id和步骤启动时间*/ |
| | | ardVideoInspectTask.setCurrentStepId(step.getId()); |
| | | ardVideoInspectTask.setCurrentStepStartTime(DateUtils.getTime()); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTask(ardVideoInspectTask); |
| | | } else { |
| | | cmd.setEnable(false);//停止录像 |
| | | cmd.setUploadMinio(true); |
| | | hikClientService.recordToMinio(cmd); |
| | | cmd.setExpired(step.getRecordingTime() * 60); |
| | | boolean setTargetPosition = cameraSdkService.guideTargetPosition(cmd); |
| | | if (!setTargetPosition) { |
| | | /*控制失败,当前步骤启动时间置null*/ |
| | | ardVideoInspectTask.setCurrentStepStartTime(""); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTask(ardVideoInspectTask); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask); |
| | | } else { |
| | | log.debug("引导成功!"); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error("巡检开始异常:" + ex.getMessage()); |
| | | log.error("巡检尝试引导异常:" + ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | //步骤停止 |
| | | private void stopRunStep(ArdVideoInspectTask ardVideoInspectTask) { |
| | | try { |
| | | String currentStepId = ardVideoInspectTask.getCurrentStepId(); |
| | | log.info("步骤:" + currentStepId + "停止"); |
| | | log.debug("巡检步骤:" + currentStepId + "停止"); |
| | | if (StringUtils.isNull(currentStepId)) { |
| | | log.info("当前停止巡检步骤id为空"); |
| | | return; |
| | |
| | | /*停止录像*/ |
| | | CameraCmd cmd = new CameraCmd(); |
| | | cmd.setCameraId(cameraId); |
| | | cmd.setChannelNum(channel); |
| | | cmd.setChanNo(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); |
| | | cmd.setUploadMinio(true); |
| | | hikClientService.recordToMinio(cmd); |
| | | Global.task_record.remove(ardVideoInspectTask.getId()); |
| | | cmd.setRecordObjectName("inspect_" + IdUtils.fastSimpleUUID()); |
| | | String url = cameraSdkService.recordStopToMinio(cmd); |
| | | /*插入巡检记录*/ |
| | | ArdVideoInspectRecord ardVideoInspectRecord = new ArdVideoInspectRecord(); |
| | | ardVideoInspectRecord.setStepId(step.getId()); |
| | | ArdAlarmpointsWell ardAlarmpointsWell = ardAlarmpointsWellMapper.selectArdAlarmpointsWellById(step.getWellId()); |
| | | if (StringUtils.isNotNull(ardAlarmpointsWell)) { |
| | | ardVideoInspectRecord.setWellName(ardAlarmpointsWell.getWellId()); |
| | | } |
| | | 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); |
| | | ardVideoInspectRecordMapper.insertArdVideoInspectRecord(ardVideoInspectRecord); |
| | | } |
| | |
| | | .findFirst() |
| | | .orElse(null); |
| | | avit.setCurrentStepId(nextStepId); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTask(avit); |
| | | ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(avit); |
| | | log.info("步骤:" + currentStepId + "切换为" + nextStepId); |
| | | return nextStepId; |
| | | } |
| | |
| | | return ardVideoInspectTaskMapper.getTaskUsedCameraPeriods(cameraId); |
| | | } |
| | | |
| | | /** |
| | | * 获取相机的空闲时段 |
| | | * @param cameraId |
| | | * @return |
| | | */ |
| | | public List getCameraIdleTimeList(String cameraId) { |
| | | LinkedList<String> timeList = new LinkedList(); |
| | | List<Map> usedPeriods = this.getTaskUsedCameraPeriods(cameraId); |
| | |
| | | timeList.add((String) p.get("end_time")); |
| | | } |
| | | //判断第一个起始点 |
| | | if (timeList.getFirst().equals("00:00:00")) { |
| | | timeList.removeFirst(); |
| | | } else { |
| | | timeList.addFirst("00:00:00"); |
| | | } |
| | | //判断最后一个结束时间 |
| | | if (timeList.getLast().equals("23:59:59")) { |
| | | timeList.removeLast(); |
| | | } else { |
| | | timeList.addLast("23:59:59"); |
| | | } |
| | | //事件段为空,则全天作为一个时间段 |
| | | if (timeList.size() == 0) { |
| | | timeList.add("00:00:00"); |
| | | timeList.add("23:59:59"); |
| | | if (timeList.size() > 0) { |
| | | if ("00:00:00".equals(timeList.getFirst())) { |
| | | timeList.removeFirst(); |
| | | } else { |
| | | timeList.addFirst("00:00:00"); |
| | | } |
| | | //判断最后一个结束时间 |
| | | if ("23:59:59".equals(timeList.getLast())) { |
| | | timeList.removeLast(); |
| | | } else { |
| | | timeList.addLast("23:59:59"); |
| | | } |
| | | //事件段为空,则全天作为一个时间段 |
| | | if (timeList.size() == 0) { |
| | | timeList.add("00:00:00"); |
| | | timeList.add("23:59:59"); |
| | | } |
| | | |
| | | } |
| | | |
| | | return timeList; |
| | | } |
| | | } |
| | | } |