| | |
| | | import com.dji.sample.component.mqtt.service.IMessageSenderService; |
| | | import com.dji.sample.component.redis.RedisConst; |
| | | import com.dji.sample.component.redis.RedisOpsUtils; |
| | | import com.dji.sample.control.model.param.DrcModeParam; |
| | | import com.dji.sample.control.service.IDrcService; |
| | | import com.dji.sample.manage.model.dto.DeviceDTO; |
| | | import com.dji.sample.manage.model.enums.DeviceModeCodeEnum; |
| | | import com.dji.sample.manage.model.enums.DockModeCodeEnum; |
| | | import com.dji.sample.manage.model.receiver.OsdDockReceiver; |
| | | import com.dji.sample.manage.model.receiver.OsdSubDeviceReceiver; |
| | | import com.dji.sample.manage.service.IDeviceRedisService; |
| | | import com.dji.sample.manage.service.IDeviceService; |
| | | import com.dji.sample.media.model.MediaFileCountDTO; |
| | | import com.dji.sample.media.model.MediaMethodEnum; |
| | |
| | | import com.dji.sample.wayline.dao.IWaylineJobMapper; |
| | | import com.dji.sample.wayline.model.dto.*; |
| | | import com.dji.sample.wayline.model.entity.WaylineJobEntity; |
| | | import com.dji.sample.wayline.model.enums.WaylineJobStatusEnum; |
| | | import com.dji.sample.wayline.model.enums.WaylineMethodEnum; |
| | | import com.dji.sample.wayline.model.enums.WaylineTaskTypeEnum; |
| | | import com.dji.sample.wayline.model.enums.*; |
| | | import com.dji.sample.wayline.model.param.CreateJobParam; |
| | | import com.dji.sample.wayline.model.param.UpdateJobParam; |
| | | import com.dji.sample.wayline.model.param.WaylineJobQueryParam; |
| | | import com.dji.sample.wayline.service.IWaylineFileService; |
| | | import com.dji.sample.wayline.service.IWaylineJobService; |
| | | import com.dji.sample.wayline.service.IWaylineRedisService; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.springframework.transaction.annotation.Isolation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.net.URL; |
| | | import java.sql.SQLException; |
| | |
| | | @Autowired |
| | | private IFileService fileService; |
| | | |
| | | @Autowired |
| | | private IDrcService drcService; |
| | | |
| | | @Autowired |
| | | private IDeviceRedisService deviceRedisService; |
| | | |
| | | @Autowired |
| | | private IWaylineRedisService waylineRedisService; |
| | | |
| | | private Optional<WaylineJobDTO> insertWaylineJob(WaylineJobEntity jobEntity) { |
| | | int id = mapper.insert(jobEntity); |
| | | if (id <= 0) { |
| | |
| | | .beginTime(beginTime) |
| | | .endTime(endTime) |
| | | .status(WaylineJobStatusEnum.PENDING.getVal()) |
| | | .taskType(param.getTaskType()) |
| | | .waylineType(param.getWaylineType()) |
| | | .taskType(param.getTaskType().getVal()) |
| | | .waylineType(param.getWaylineType().getVal()) |
| | | .outOfControlAction(param.getOutOfControlAction()) |
| | | .rthAltitude(param.getRthAltitude()) |
| | | .mediaCount(0) |
| | |
| | | return this.insertWaylineJob(jobEntity); |
| | | } |
| | | |
| | | /** |
| | | * 对于即时任务,以服务器时间为准。 |
| | | * @param param |
| | | */ |
| | | private void fillImmediateTime(CreateJobParam param) { |
| | | if (WaylineTaskTypeEnum.IMMEDIATE != param.getTaskType()) { |
| | | return; |
| | | } |
| | | long now = System.currentTimeMillis() / 1000 - 10; |
| | | // param.setTaskDays(Collections.singletonList(now)); |
| | | // param.setTaskPeriods(Collections.singletonList(Collections.singletonList(now))); |
| | | param.setTaskDays(List.of(now)); |
| | | param.setTaskPeriods(List.of(List.of(now))); |
| | | } |
| | | |
| | | @Override |
| | | public ResponseResult publishFlightTask(CreateJobParam param, CustomClaim customClaim) throws SQLException { |
| | | if (WaylineTaskTypeEnum.IMMEDIATE.getVal() == param.getTaskType()) { |
| | | param.setExecuteTime(System.currentTimeMillis()); |
| | | } |
| | | Optional<WaylineJobDTO> waylineJobOpt = this.createWaylineJob(param, |
| | | customClaim.getWorkspaceId(), customClaim.getUsername(), |
| | | param.getExecuteTime(), param.getExecuteTime()); |
| | | if (waylineJobOpt.isEmpty()) { |
| | | throw new SQLException("Failed to create wayline job."); |
| | | } |
| | | WaylineJobDTO waylineJob = waylineJobOpt.get(); |
| | | |
| | | boolean isOnline = deviceService.checkDeviceOnline(waylineJob.getDockSn()); |
| | | //当重复定时和连续执行时走这个逻辑 |
| | | if (param.getTaskType().getVal() == WaylineTaskTypeEnum.CONDITION.getVal()){ |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | //保存数据 |
| | | WaylineJobEntity waylineJobEntity = WaylineJobEntity.builder() |
| | | .jobId(UUID.randomUUID().toString()) |
| | | .name(param.getName()) |
| | | .dockSn(param.getDockSn()) |
| | | .fileId(param.getFileId()) |
| | | .username(customClaim.getUsername()) |
| | | .workspaceId(customClaim.getWorkspaceId()) |
| | | |
| | | |
| | | .beginTime(param.getTaskDays().get(0)*1000) |
| | | .endTime(param.getTaskDays().get(1)*1000) |
| | | .status(WaylineJobStatusEnum.PENDING.getVal()) |
| | | .taskType(param.getTaskType().getVal()) |
| | | .waylineType(param.getWaylineType().getVal()) |
| | | .outOfControlAction(param.getOutOfControlAction()) |
| | | .batteryCapacity(param.getMinBatteryCapacity()) |
| | | .rthAltitude(param.getRthAltitude()) |
| | | .mediaCount(0) |
| | | .repFreVal(param.getRepFreVal()) |
| | | .repFreType(param.getRepFreType()) |
| | | .repRuleType(param.getRepRuleType()) |
| | | .repRuleVal(param.getRepRuleVal()) |
| | | .executeTimeArr(param.getTaskPeriods()) |
| | | .executeStartTimeArr(param.getExecuteStartTimeArr()) |
| | | .build(); |
| | | |
| | | Boolean insert = mapper.insert(waylineJobEntity)>0; |
| | | |
| | | return ResponseResult.success(insert); |
| | | } |
| | | |
| | | fillImmediateTime(param); |
| | | |
| | | // param.getTaskDays().sort((a, b) -> (int) (a - b)); |
| | | // param.getTaskPeriods().sort((a, b) -> (int) (a.get(0) - b.get(0))); |
| | | for (Long taskDay : param.getTaskDays()) { |
| | | LocalDate date = LocalDate.ofInstant(Instant.ofEpochSecond(taskDay), ZoneId.systemDefault()); |
| | | for (List<Long> taskPeriod : param.getTaskPeriods()) { |
| | | long beginTime = LocalDateTime.of(date, LocalTime.ofInstant(Instant.ofEpochSecond(taskPeriod.get(0)), ZoneId.systemDefault())) |
| | | .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| | | long endTime = taskPeriod.size() > 1 && Objects.nonNull(taskPeriod.get(1)) ? |
| | | LocalDateTime.of(date, LocalTime.ofInstant(Instant.ofEpochSecond(taskPeriod.get(1)), ZoneId.systemDefault())) |
| | | .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() : beginTime; |
| | | if (WaylineTaskTypeEnum.IMMEDIATE != param.getTaskType() && endTime < System.currentTimeMillis()) { |
| | | return ResponseResult.error("任务已过期"); |
| | | } |
| | | Optional<WaylineJobDTO> waylineJobOpt = this.createWaylineJob(param, customClaim.getWorkspaceId(), customClaim.getUsername(), beginTime, endTime); |
| | | if (waylineJobOpt.isEmpty()) { |
| | | throw new SQLException("任务创建失败"); |
| | | } |
| | | WaylineJobDTO waylineJob = waylineJobOpt.get(); |
| | | // If it is a conditional task type, add conditions to the job parameters. |
| | | //如果是条件任务类型,需要在任务参数中添加条件。 |
| | | addPreparedJob(waylineJob, param, beginTime, endTime); |
| | | |
| | | ResponseResult response = this.publishOneFlightTask(waylineJob); |
| | | if (ResponseResult.CODE_SUCCESS != response.getCode()) { |
| | | return response; |
| | | } |
| | | } |
| | | } |
| | | return ResponseResult.success(); |
| | | } |
| | | |
| | | private void addPreparedJob(WaylineJobDTO waylineJob, CreateJobParam param, Long beginTime, Long endTime) { |
| | | if (WaylineTaskTypeEnum.CONDITION == param.getTaskType()) { |
| | | waylineJob.setConditions( |
| | | WaylineTaskConditionDTO.builder() |
| | | .executableConditions(Objects.nonNull(param.getMinStorageCapacity()) ? WaylineTaskExecutableConditionDTO.builder().storageCapacity(param.getMinStorageCapacity()).build() : null) |
| | | .readyConditions(WaylineTaskReadyConditionDTO.builder() |
| | | .batteryCapacity(param.getMinBatteryCapacity()) |
| | | .beginTime(beginTime) |
| | | .endTime(endTime) |
| | | .build()) |
| | | .build()); |
| | | |
| | | waylineRedisService.setConditionalWaylineJob(waylineJob); |
| | | } |
| | | // value: {workspace_id}:{dock_sn}:{job_id} |
| | | boolean isAdd = waylineRedisService.addPreparedWaylineJob(waylineJob); |
| | | if (!isAdd) { |
| | | throw new RuntimeException("创建任务失败。"); |
| | | } |
| | | } |
| | | |
| | | public ResponseResult publishOneFlightTask(WaylineJobDTO waylineJob) throws SQLException { |
| | | |
| | | boolean isSuccess = this.prepareFlightTask(waylineJob); |
| | | if (!isSuccess) { |
| | | return ResponseResult.error("任务准备失败"); |
| | | } |
| | | |
| | | // Issue an immediate task execution command. |
| | | //发出立即任务执行命令 |
| | | if (WaylineTaskTypeEnum.IMMEDIATE == waylineJob.getTaskType()) { |
| | | boolean isExecuted = executeFlightTask(waylineJob.getWorkspaceId(), waylineJob.getJobId()); |
| | | if (!isExecuted) { |
| | | return ResponseResult.error("执行任务失败"); |
| | | } |
| | | } |
| | | |
| | | return ResponseResult.success(); |
| | | } |
| | | |
| | | @Override |
| | | public Boolean publishFlightTask(WaylineJobEntity waylineJobEntity) { |
| | | WaylineJobDTO waylineJob = new WaylineJobDTO(); |
| | | |
| | | |
| | | waylineJob.setConditions( |
| | | WaylineTaskConditionDTO.builder() |
| | | .executableConditions(null) |
| | | .readyConditions(WaylineTaskReadyConditionDTO.builder() |
| | | .batteryCapacity(waylineJobEntity.getBatteryCapacity()) |
| | | |
| | | .build()) |
| | | .build()); |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | return null; |
| | | } |
| | | |
| | | private Boolean prepareFlightTask(WaylineJobDTO waylineJob) throws SQLException { |
| | | |
| | | boolean isOnline = deviceRedisService.checkDeviceOnline(waylineJob.getDockSn()); |
| | | if (!isOnline) { |
| | | throw new RuntimeException("Dock is offline."); |
| | | throw new RuntimeException("设备离线。"); |
| | | } |
| | | |
| | | // get wayline file |
| | | Optional<WaylineFileDTO> waylineFile = waylineFileService.getWaylineByWaylineId(waylineJob.getWorkspaceId(), waylineJob.getFileId()); |
| | | if (waylineFile.isEmpty()) { |
| | | throw new SQLException("Wayline file doesn't exist."); |
| | | throw new SQLException("航线文件不存在。"); |
| | | } |
| | | |
| | | // get file url |
| | | //获取航线文件地址 |
| | | URL url = waylineFileService.getObjectUrl(waylineJob.getWorkspaceId(), waylineFile.get().getWaylineId()); |
| | | |
| | | WaylineTaskCreateDTO flightTask = WaylineTaskCreateDTO.builder() |
| | |
| | | .build()) |
| | | .build(); |
| | | |
| | | String topic = TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + |
| | | waylineJob.getDockSn() + TopicConst.SERVICES_SUF; |
| | | CommonTopicResponse<Object> response = CommonTopicResponse.builder() |
| | | .tid(UUID.randomUUID().toString()) |
| | | .bid(waylineJob.getJobId()) |
| | | .timestamp(System.currentTimeMillis()) |
| | | .data(flightTask) |
| | | .method(WaylineMethodEnum.FLIGHT_TASK_PREPARE.getMethod()) |
| | | .build(); |
| | | //当任务类型为条件时 |
| | | if (WaylineTaskTypeEnum.CONDITION == waylineJob.getTaskType()) { |
| | | if (Objects.isNull(waylineJob.getConditions())) { |
| | | throw new IllegalArgumentException(); |
| | | } |
| | | flightTask.setReadyConditions(waylineJob.getConditions().getReadyConditions()); |
| | | flightTask.setExecutableConditions(waylineJob.getConditions().getExecutableConditions()); |
| | | } |
| | | |
| | | ServiceReply serviceReply = messageSender.publishWithReply(topic, response); |
| | | //发布飞行指令 |
| | | ServiceReply serviceReply = messageSender.publishServicesTopic( |
| | | waylineJob.getDockSn(), WaylineMethodEnum.FLIGHT_TASK_PREPARE.getMethod(), flightTask, waylineJob.getJobId()); |
| | | if (ResponseResult.CODE_SUCCESS != serviceReply.getResult()) { |
| | | log.info("Prepare task ====> Error code: {}", serviceReply.getResult()); |
| | | //飞行失败,更新数据库信息 |
| | | this.updateJob(WaylineJobDTO.builder() |
| | | .workspaceId(waylineJob.getWorkspaceId()) |
| | | .jobId(waylineJob.getJobId()) |
| | |
| | | .status(WaylineJobStatusEnum.FAILED.getVal()) |
| | | .completedTime(LocalDateTime.now()) |
| | | .code(serviceReply.getResult()).build()); |
| | | return ResponseResult.error("Prepare task ====> Error code: " + serviceReply.getResult()); |
| | | return false; |
| | | } |
| | | |
| | | // Issue an immediate task execution command. |
| | | if (WaylineTaskTypeEnum.IMMEDIATE.getVal() == waylineJob.getTaskType()) { |
| | | if (!executeFlightTask(waylineJob.getWorkspaceId(), waylineJob.getJobId())) { |
| | | return ResponseResult.error("Failed to execute job."); |
| | | } |
| | | } |
| | | |
| | | if (WaylineTaskTypeEnum.TIMED.getVal() == waylineJob.getTaskType()) { |
| | | boolean isAdd = RedisOpsUtils.zAdd(RedisConst.WAYLINE_JOB_TIMED_EXECUTE, |
| | | waylineJob.getWorkspaceId() + RedisConst.DELIMITER + waylineJob.getDockSn() + RedisConst.DELIMITER + waylineJob.getJobId(), |
| | | waylineJob.getBeginTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); |
| | | if (!isAdd) { |
| | | return ResponseResult.error("Failed to create scheduled job."); |
| | | } |
| | | } |
| | | |
| | | return ResponseResult.success(); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | |
| | | // get job |
| | | Optional<WaylineJobDTO> waylineJob = this.getJobByJobId(workspaceId, jobId); |
| | | if (waylineJob.isEmpty()) { |
| | | throw new IllegalArgumentException("Job doesn't exist."); |
| | | throw new IllegalArgumentException("任务不存在"); |
| | | } |
| | | |
| | | boolean isOnline = deviceService.checkDeviceOnline(waylineJob.get().getDockSn()); |
| | | boolean isOnline = deviceRedisService.checkDeviceOnline(waylineJob.get().getDockSn()); |
| | | if (!isOnline) { |
| | | throw new RuntimeException("Dock is offline."); |
| | | throw new RuntimeException("设备离线"); |
| | | } |
| | | |
| | | WaylineJobDTO job = waylineJob.get(); |
| | | WaylineTaskCreateDTO flightTask = WaylineTaskCreateDTO.builder().flightId(jobId).build(); |
| | | |
| | | String topic = TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + |
| | | job.getDockSn() + TopicConst.SERVICES_SUF; |
| | | CommonTopicResponse<Object> response = CommonTopicResponse.builder() |
| | | .tid(UUID.randomUUID().toString()) |
| | | .bid(jobId) |
| | | .timestamp(System.currentTimeMillis()) |
| | | .data(flightTask) |
| | | .method(WaylineMethodEnum.FLIGHT_TASK_EXECUTE.getMethod()) |
| | | .build(); |
| | | |
| | | ServiceReply serviceReply = messageSender.publishWithReply(topic, response); |
| | | ServiceReply serviceReply = messageSender.publishServicesTopic( |
| | | job.getDockSn(), WaylineMethodEnum.FLIGHT_TASK_EXECUTE.getMethod(), flightTask, jobId); |
| | | if (ResponseResult.CODE_SUCCESS != serviceReply.getResult()) { |
| | | log.info("Execute job ====> Error code: {}", serviceReply.getResult()); |
| | | this.updateJob(WaylineJobDTO.builder() |
| | |
| | | .status(WaylineJobStatusEnum.FAILED.getVal()) |
| | | .completedTime(LocalDateTime.now()) |
| | | .code(serviceReply.getResult()).build()); |
| | | // The conditional task fails and enters the blocking status. |
| | | if (WaylineTaskTypeEnum.CONDITION == job.getTaskType() |
| | | && WaylineErrorCodeEnum.find(serviceReply.getResult()).isBlock()) { |
| | | waylineRedisService.setBlockedWaylineJob(job.getDockSn(), jobId); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | |
| | | .executeTime(LocalDateTime.now()) |
| | | .status(WaylineJobStatusEnum.IN_PROGRESS.getVal()) |
| | | .build()); |
| | | RedisOpsUtils.setWithExpire(RedisConst.WAYLINE_JOB_RUNNING_PREFIX + job.getDockSn(), |
| | | EventsReceiver.<WaylineTaskProgressReceiver>builder().bid(jobId).sn(job.getDockSn()).build(), |
| | | RedisConst.DEVICE_ALIVE_SECOND * RedisConst.DEVICE_ALIVE_SECOND); |
| | | waylineRedisService.setRunningWaylineJob(job.getDockSn(), EventsReceiver.<WaylineTaskProgressReceiver>builder().bid(jobId).sn(job.getDockSn()).build()); |
| | | return true; |
| | | } |
| | | |
| | |
| | | // Check if the task status is correct. |
| | | boolean isErr = !jobIds.removeAll(waylineJobIds) || !jobIds.isEmpty() ; |
| | | if (isErr) { |
| | | throw new IllegalArgumentException("These tasks have an incorrect status and cannot be canceled. " |
| | | + Arrays.toString(jobIds.toArray())); |
| | | throw new IllegalArgumentException("以下任务状态不正确,不能取消" + Arrays.toString(jobIds.toArray())); |
| | | } |
| | | |
| | | // Group job id by dock sn. |
| | |
| | | } |
| | | |
| | | public void publishCancelTask(String workspaceId, String dockSn, List<String> jobIds) { |
| | | boolean isOnline = deviceService.checkDeviceOnline(dockSn); |
| | | boolean isOnline = deviceRedisService.checkDeviceOnline(dockSn); |
| | | if (!isOnline) { |
| | | throw new RuntimeException("Dock is offline."); |
| | | throw new RuntimeException("设备离线"); |
| | | } |
| | | String topic = TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + dockSn + TopicConst.SERVICES_SUF; |
| | | |
| | | CommonTopicResponse<Object> response = CommonTopicResponse.builder() |
| | | .tid(UUID.randomUUID().toString()) |
| | | .bid(UUID.randomUUID().toString()) |
| | | .timestamp(System.currentTimeMillis()) |
| | | .data(Map.of(MapKeyConst.FLIGHT_IDS, jobIds)) |
| | | .method(WaylineMethodEnum.FLIGHT_TASK_CANCEL.getMethod()) |
| | | .build(); |
| | | |
| | | ServiceReply serviceReply = messageSender.publishWithReply(topic, response); |
| | | ServiceReply serviceReply = messageSender.publishServicesTopic( |
| | | dockSn, WaylineMethodEnum.FLIGHT_TASK_CANCEL.getMethod(), Map.of(MapKeyConst.FLIGHT_IDS, jobIds)); |
| | | if (ResponseResult.CODE_SUCCESS != serviceReply.getResult()) { |
| | | log.info("Cancel job ====> Error code: {}", serviceReply.getResult()); |
| | | throw new RuntimeException("Failed to cancel the wayline job of " + dockSn); |
| | | throw new RuntimeException("航路作业取消失败 " + dockSn); |
| | | } |
| | | |
| | | for (String jobId : jobIds) { |
| | |
| | | .status(WaylineJobStatusEnum.CANCEL.getVal()) |
| | | .completedTime(LocalDateTime.now()) |
| | | .build()); |
| | | RedisOpsUtils.zRemove(RedisConst.WAYLINE_JOB_TIMED_EXECUTE, workspaceId + RedisConst.DELIMITER + dockSn + RedisConst.DELIMITER + jobId); |
| | | } |
| | | |
| | | } |
| | |
| | | new LambdaQueryWrapper<WaylineJobEntity>() |
| | | .eq(WaylineJobEntity::getWorkspaceId, workspaceId) |
| | | .eq(Objects.nonNull(status), WaylineJobEntity::getStatus, status.getVal()) |
| | | .and(!CollectionUtils.isEmpty(jobIds), |
| | | wrapper -> jobIds.forEach(id -> wrapper.eq(WaylineJobEntity::getJobId, id).or()))) |
| | | .in(!CollectionUtils.isEmpty(jobIds), WaylineJobEntity::getJobId, jobIds)) |
| | | .stream() |
| | | .map(this::entity2Dto) |
| | | .collect(Collectors.toList()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public PaginationData<WaylineJobDTO> getJobsByWorkspaceId(String workspaceId, long page, long pageSize) { |
| | | Page<WaylineJobEntity> pageData = mapper.selectPage( |
| | | new Page<WaylineJobEntity>(page, pageSize), |
| | | new LambdaQueryWrapper<WaylineJobEntity>() |
| | | .eq(WaylineJobEntity::getWorkspaceId, workspaceId) |
| | | .orderByDesc(WaylineJobEntity::getId)); |
| | | public PaginationData<WaylineJobDTO> getJobsByWorkspaceId(String workspaceId, long page, long pageSize, WaylineJobQueryParam waylineJobQueryParam) { |
| | | // Page<WaylineJobEntity> pageData = mapper.selectPage( |
| | | // new Page<WaylineJobEntity>(page, pageSize), |
| | | // new LambdaQueryWrapper<WaylineJobEntity>() |
| | | // .eq(WaylineJobEntity::getWorkspaceId, workspaceId) |
| | | // .eq(waylineJobDTO.getTaskType() !=null, WaylineJobEntity::getTaskType, waylineJobDTO.getTaskType()) |
| | | // .eq(waylineJobDTO.getStatus()!=null, WaylineJobEntity::getStatus, waylineJobDTO.getStatus()) |
| | | // .like(StringUtils.hasText(waylineJobDTO.getJobName()),WaylineJobEntity::getName,waylineJobDTO.getJobName()) |
| | | // |
| | | // |
| | | // .orderByDesc(WaylineJobEntity::getId)); |
| | | |
| | | Page<WaylineJobEntity> pageData = mapper.getPage(new Page<WaylineJobEntity>(page, pageSize),waylineJobQueryParam,workspaceId); |
| | | |
| | | |
| | | |
| | | List<WaylineJobDTO> records = pageData.getRecords() |
| | | .stream() |
| | | .map(this::entity2Dto) |
| | |
| | | |
| | | String topic = headers.get(MqttHeaders.RECEIVED_TOPIC).toString() + TopicConst._REPLY_SUF; |
| | | |
| | | DeviceDTO device = (DeviceDTO) RedisOpsUtils.get(RedisConst.DEVICE_ONLINE_PREFIX + receiver.getGateway()); |
| | | Optional<WaylineJobDTO> waylineJobOpt = this.getJobByJobId(device.getWorkspaceId(), jobId); |
| | | Optional<DeviceDTO> deviceOpt = deviceRedisService.getDeviceOnline(receiver.getGateway()); |
| | | if (deviceOpt.isEmpty()) { |
| | | return; |
| | | } |
| | | Optional<WaylineJobDTO> waylineJobOpt = this.getJobByJobId(deviceOpt.get().getWorkspaceId(), jobId); |
| | | if (waylineJobOpt.isEmpty()) { |
| | | builder.data(RequestsReply.error(CommonErrorEnum.ILLEGAL_ARGUMENT)); |
| | | messageSender.publish(topic, builder.build()); |
| | |
| | | |
| | | String dockSn = jobOpt.get().getDockSn(); |
| | | String key = RedisConst.MEDIA_HIGHEST_PRIORITY_PREFIX + dockSn; |
| | | if (RedisOpsUtils.checkExist(key) && |
| | | jobId.equals(((MediaFileCountDTO) RedisOpsUtils.get(key)).getJobId())) { |
| | | if (RedisOpsUtils.checkExist(key) && jobId.equals(((MediaFileCountDTO) RedisOpsUtils.get(key)).getJobId())) { |
| | | return; |
| | | } |
| | | |
| | | ServiceReply reply = messageSender.publishWithReply(TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + dockSn + TopicConst.SERVICES_SUF, |
| | | CommonTopicResponse.builder() |
| | | .tid(UUID.randomUUID().toString()) |
| | | .bid(UUID.randomUUID().toString()) |
| | | .timestamp(System.currentTimeMillis()) |
| | | .method(MediaMethodEnum.UPLOAD_FLIGHT_TASK_MEDIA_PRIORITIZE.getMethod()) |
| | | .data(Map.of(MapKeyConst.FLIGHT_ID, jobId)) |
| | | .build()); |
| | | ServiceReply reply = messageSender.publishServicesTopic( |
| | | dockSn, MediaMethodEnum.UPLOAD_FLIGHT_TASK_MEDIA_PRIORITIZE.getMethod(), Map.of(MapKeyConst.FLIGHT_ID, jobId)); |
| | | if (ResponseResult.CODE_SUCCESS != reply.getResult()) { |
| | | throw new RuntimeException("Failed to set media job upload priority. Error Code: " + reply.getResult()); |
| | | throw new RuntimeException("设置媒体作业上传优先级失败. 错误码: " + reply.getResult()); |
| | | } |
| | | } |
| | | |
| | |
| | | .fileId(dto.getFileId()) |
| | | .dockSn(dto.getDockSn()) |
| | | .workspaceId(dto.getWorkspaceId()) |
| | | .taskType(dto.getTaskType()) |
| | | .waylineType(dto.getWaylineType()) |
| | | .taskType(Optional.ofNullable(dto.getTaskType()).map(WaylineTaskTypeEnum::getVal).orElse(null)) |
| | | .waylineType(Optional.ofNullable(dto.getWaylineType()).map(WaylineTemplateTypeEnum::getVal).orElse(null)) |
| | | .username(dto.getUsername()) |
| | | .rthAltitude(dto.getRthAltitude()) |
| | | .outOfControlAction(dto.getOutOfControlAction()) |
| | | .parentId(dto.getParentId()) |
| | | .build(); |
| | | } |
| | | |
| | | @Override |
| | | public void updateJobStatus(String workspaceId, String jobId, UpdateJobParam param) { |
| | | Optional<WaylineJobDTO> waylineJobOpt = this.getJobByJobId(workspaceId, jobId); |
| | | if (waylineJobOpt.isEmpty()) { |
| | | throw new RuntimeException("任务不存在"); |
| | | } |
| | | WaylineJobDTO waylineJob = waylineJobOpt.get(); |
| | | WaylineJobStatusEnum statusEnum = this.getWaylineState(waylineJob.getDockSn()); |
| | | if (statusEnum.getEnd() || WaylineJobStatusEnum.PENDING == statusEnum) { |
| | | throw new RuntimeException("航路线作业状态不匹配,无法执行操作."); |
| | | } |
| | | |
| | | switch (param.getStatus()) { |
| | | case PAUSE: |
| | | pauseJob(workspaceId, waylineJob.getDockSn(), jobId, statusEnum); |
| | | break; |
| | | case RESUME: |
| | | resumeJob(workspaceId, waylineJob.getDockSn(), jobId, statusEnum); |
| | | break; |
| | | } |
| | | |
| | | } |
| | | |
| | | public WaylineJobStatusEnum getWaylineState(String dockSn) { |
| | | Optional<DeviceDTO> dockOpt = deviceRedisService.getDeviceOnline(dockSn); |
| | | if (dockOpt.isEmpty() || !StringUtils.hasText(dockOpt.get().getChildDeviceSn())) { |
| | | return WaylineJobStatusEnum.UNKNOWN; |
| | | } |
| | | Optional<OsdDockReceiver> dockOsdOpt = deviceRedisService.getDeviceOsd(dockSn, OsdDockReceiver.class); |
| | | Optional<OsdSubDeviceReceiver> deviceOsdOpt = deviceRedisService.getDeviceOsd(dockOpt.get().getChildDeviceSn(), OsdSubDeviceReceiver.class); |
| | | if (dockOsdOpt.isEmpty() || deviceOsdOpt.isEmpty() || DockModeCodeEnum.WORKING != dockOsdOpt.get().getModeCode()) { |
| | | return WaylineJobStatusEnum.UNKNOWN; |
| | | } |
| | | |
| | | OsdSubDeviceReceiver osdDevice = deviceOsdOpt.get(); |
| | | if (DeviceModeCodeEnum.WAYLINE == osdDevice.getModeCode() |
| | | || DeviceModeCodeEnum.MANUAL == osdDevice.getModeCode() |
| | | || DeviceModeCodeEnum.TAKEOFF_AUTO == osdDevice.getModeCode()) { |
| | | if (StringUtils.hasText(waylineRedisService.getPausedWaylineJobId(dockSn))) { |
| | | return WaylineJobStatusEnum.PAUSED; |
| | | } |
| | | if (waylineRedisService.getRunningWaylineJob(dockSn).isPresent()) { |
| | | return WaylineJobStatusEnum.IN_PROGRESS; |
| | | } |
| | | } |
| | | return WaylineJobStatusEnum.UNKNOWN; |
| | | } |
| | | |
| | | private void pauseJob(String workspaceId, String dockSn, String jobId, WaylineJobStatusEnum statusEnum) { |
| | | if (WaylineJobStatusEnum.PAUSED == statusEnum && jobId.equals(waylineRedisService.getPausedWaylineJobId(dockSn))) { |
| | | waylineRedisService.setPausedWaylineJob(dockSn, jobId); |
| | | return; |
| | | } |
| | | |
| | | ServiceReply reply = messageSender.publishServicesTopic( |
| | | dockSn, WaylineMethodEnum.FLIGHT_TASK_PAUSE.getMethod(), "", jobId); |
| | | if (ResponseResult.CODE_SUCCESS != reply.getResult()) { |
| | | throw new RuntimeException("未能恢复航路作业。错误码: " + reply.getResult()); |
| | | } |
| | | waylineRedisService.delRunningWaylineJob(dockSn); |
| | | waylineRedisService.setPausedWaylineJob(dockSn, jobId); |
| | | } |
| | | |
| | | private void resumeJob(String workspaceId, String dockSn, String jobId, WaylineJobStatusEnum statusEnum) { |
| | | Optional<EventsReceiver<WaylineTaskProgressReceiver>> runningDataOpt = waylineRedisService.getRunningWaylineJob(dockSn); |
| | | if (WaylineJobStatusEnum.IN_PROGRESS == statusEnum && jobId.equals(runningDataOpt.map(EventsReceiver::getSn).get())) { |
| | | waylineRedisService.setRunningWaylineJob(dockSn, runningDataOpt.get()); |
| | | return; |
| | | } |
| | | ServiceReply reply = messageSender.publishServicesTopic( |
| | | dockSn, WaylineMethodEnum.FLIGHT_TASK_RESUME.getMethod(), "", jobId); |
| | | if (ResponseResult.CODE_SUCCESS != reply.getResult()) { |
| | | throw new RuntimeException("未能恢复航路作业。错误码:: " + reply.getResult()); |
| | | } |
| | | |
| | | runningDataOpt.ifPresent(runningData -> waylineRedisService.setRunningWaylineJob(dockSn, runningData)); |
| | | waylineRedisService.delPausedWaylineJob(dockSn); |
| | | |
| | | if (deviceService.checkDockDrcMode(dockSn)) { |
| | | drcService.deviceDrcExit(workspaceId, DrcModeParam.builder().dockSn(dockSn) |
| | | .clientId(drcService.getDrcModeInRedis(dockSn)).build()); |
| | | } |
| | | |
| | | } |
| | | |
| | | private WaylineJobDTO entity2Dto(WaylineJobEntity entity) { |
| | |
| | | .username(entity.getUsername()) |
| | | .workspaceId(entity.getWorkspaceId()) |
| | | .status(WaylineJobStatusEnum.IN_PROGRESS.getVal() == entity.getStatus() && |
| | | RedisOpsUtils.checkExist(RedisConst.WAYLINE_JOB_PAUSED_PREFIX + entity.getJobId()) ? |
| | | WaylineJobStatusEnum.PAUSED.getVal() : entity.getStatus()) |
| | | entity.getJobId().equals(waylineRedisService.getPausedWaylineJobId(entity.getDockSn())) ? |
| | | WaylineJobStatusEnum.PAUSED.getVal() : entity.getStatus()) |
| | | .code(entity.getErrorCode()) |
| | | .beginTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(entity.getBeginTime()), ZoneId.systemDefault())) |
| | | .endTime(Objects.nonNull(entity.getEndTime()) ? |
| | |
| | | LocalDateTime.ofInstant(Instant.ofEpochMilli(entity.getExecuteTime()), ZoneId.systemDefault()) : null) |
| | | .completedTime(WaylineJobStatusEnum.find(entity.getStatus()).getEnd() ? |
| | | LocalDateTime.ofInstant(Instant.ofEpochMilli(entity.getUpdateTime()), ZoneId.systemDefault()) : null) |
| | | .taskType(entity.getTaskType()) |
| | | .waylineType(entity.getWaylineType()) |
| | | .taskType(WaylineTaskTypeEnum.find(entity.getTaskType())) |
| | | .waylineType(WaylineTemplateTypeEnum.find(entity.getWaylineType())) |
| | | .rthAltitude(entity.getRthAltitude()) |
| | | .outOfControlAction(entity.getOutOfControlAction()) |
| | | .mediaCount(entity.getMediaCount()); |
| | |
| | | if (Objects.nonNull(entity.getEndTime())) { |
| | | builder.endTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(entity.getEndTime()), ZoneId.systemDefault())); |
| | | } |
| | | if (WaylineJobStatusEnum.IN_PROGRESS.getVal() == entity.getStatus() && RedisOpsUtils.getExpire(entity.getJobId()) > 0) { |
| | | EventsReceiver<WaylineTaskProgressReceiver> taskProgress = (EventsReceiver<WaylineTaskProgressReceiver>) RedisOpsUtils.get(RedisConst.WAYLINE_JOB_RUNNING_PREFIX + entity.getDockSn()); |
| | | if (Objects.nonNull(taskProgress.getOutput()) && Objects.nonNull(taskProgress.getOutput().getProgress())) { |
| | | builder.progress(taskProgress.getOutput().getProgress().getPercent()); |
| | | } |
| | | if (WaylineJobStatusEnum.IN_PROGRESS.getVal() == entity.getStatus()) { |
| | | builder.progress(waylineRedisService.getRunningWaylineJob(entity.getDockSn()) |
| | | .map(EventsReceiver::getOutput) |
| | | .map(WaylineTaskProgressReceiver::getProgress) |
| | | .map(WaylineTaskProgress::getPercent) |
| | | .orElse(null)); |
| | | } |
| | | |
| | | if (entity.getMediaCount() == 0) { |