package com.zy.asrs.wcs.core.timer; import com.zy.asrs.framework.exception.CoolException; import com.zy.asrs.wcs.core.entity.Task; import com.zy.asrs.wcs.core.kernel.command.*; import com.zy.asrs.wcs.core.model.enums.DeviceCtgType; import com.zy.asrs.wcs.core.model.enums.MotionCtgType; import com.zy.asrs.wcs.core.model.enums.MotionStsType; import com.zy.asrs.wcs.core.model.enums.TaskStsType; import com.zy.asrs.wcs.core.service.TaskService; import com.zy.asrs.wcs.core.entity.Motion; import com.zy.asrs.wcs.core.service.MotionService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Objects; /** * Created by vincent on 2023/10/10 */ @Slf4j @Component public class MotionTimer { @Autowired private TaskService taskService; @Autowired private MotionService motionService; @Autowired private AgvCommandService agvCommandService; @Autowired private ConveyorCommandService conveyorCommandService; @Autowired private CraneCommandService craneCommandService; @Autowired private LiftCommandService liftCommandService; @Autowired private ShuttleCommandService shuttleCommandService; // @Scheduled(cron = "0/1 * * * * ? ") public synchronized void executeTask() { Date now = new Date(); // ANALYZE_INBOUND for (Task task : taskService.selectByAnalyzeSts()) { Motion executingMotion = motionService.selectOfTop1(task.getUuid(), MotionStsType.EXECUTING.val(), task.getHostId()); if (executingMotion != null) {//存在正在执行的motion continue; } Motion motion = motionService.selectOfTop1(task.getUuid(), MotionStsType.INIT.val(), task.getHostId()); if (null != motion) { boolean result = this.executeMotion(motion); if (!result) { continue; } // 更新Task switch (TaskStsType.query(task.getTaskSts())) { case ANALYZE_INBOUND: task.setTaskSts(TaskStsType.EXECUTE_INBOUND.sts); break; case ANALYZE_OUTBOUND: task.setTaskSts(TaskStsType.EXECUTE_OUTBOUND.sts); break; } task.setUpdateTime(now); if (!taskService.updateById(task)) { log.error("{}工作档更新状态失败!", task.getTaskNo()); } } } // EXECUTE_INBOUND for (Task task : taskService.selectByExecuteSts()) { if (!motionService.hasRunningMotion(task.getUuid(), task.getHostId())) { Motion motion = motionService.selectOfTop1(task.getUuid(), MotionStsType.WAITING.val(), task.getHostId()); if (null != motion) { boolean result = this.executeMotion(motion); if (!result) { continue; } } else { if (motionService.selectOfTop1(task.getUuid(), MotionStsType.EXECUTING.val(), task.getHostId()) != null) { continue; } // 更新Task switch (TaskStsType.query(task.getTaskSts())) { case EXECUTE_INBOUND: task.setTaskSts(TaskStsType.COMPLETE_INBOUND.sts); break; case EXECUTE_OUTBOUND: task.setTaskSts(TaskStsType.COMPLETE_OUTBOUND.sts); break; } task.setUpdateTime(now); if (!taskService.updateById(task)) { log.error("{}工作档更新状态失败!", task.getTaskNo()); } } } } } @Scheduled(cron = "0/1 * * * * ? ") public synchronized void executeChargeTask() { Date now = new Date(); // ANALYZE_CHARGE for (Task taskCharge : taskService.selectChargeByAnalyzeSts()) { Motion executingMotion = motionService.selectOfTop1(taskCharge.getUuid(), MotionStsType.EXECUTING.val(), taskCharge.getHostId()); if (executingMotion != null) {//存在正在执行的motion continue; } Motion motion = motionService.selectOfTop1(taskCharge.getUuid(), MotionStsType.INIT.val(), taskCharge.getHostId()); if (null != motion) { boolean result = this.executeMotion(motion); if (!result) { continue; } // 更新Task switch (TaskStsType.query(taskCharge.getTaskSts())) { case ANALYZE_CHARGE: taskCharge.setTaskSts(TaskStsType.EXECUTE_CHARGE.sts); break; case ANALYZE_MOVE: taskCharge.setTaskSts(TaskStsType.EXECUTE_MOVE.sts); break; } taskCharge.setUpdateTime(now); if (!taskService.updateById(taskCharge)) { log.error("{}其他工作档更新状态失败!", taskCharge.getTaskNo()); } } } // EXECUTE_CHARGE for (Task taskCharge : taskService.selectChargeByExecuteSts()) { if (!motionService.hasRunningMotion(taskCharge.getUuid(), taskCharge.getHostId())) { Motion motion = motionService.selectOfTop1(taskCharge.getUuid(), MotionStsType.WAITING.val(), taskCharge.getHostId()); if (null != motion) { boolean result = this.executeMotion(motion); if (!result) { continue; } } else { if (motionService.selectOfTop1(taskCharge.getUuid(), MotionStsType.EXECUTING.val(), taskCharge.getHostId()) != null) { continue; } // 更新Task switch (TaskStsType.query(taskCharge.getTaskSts())) { case EXECUTE_CHARGE: taskCharge.setTaskSts(TaskStsType.CHARGE_WORKING.sts); break; case EXECUTE_MOVE: taskCharge.setTaskSts(TaskStsType.COMPLETE_MOVE.sts); break; } taskCharge.setUpdateTime(now); if (!taskService.updateById(taskCharge)) { log.error("{}他工作档更新状态失败!", taskCharge.getTaskNo()); } } } } } @Scheduled(cron = "0/1 * * * * ? ") public synchronized void executeManualTask() { Date now = new Date(); // ANALYZE_MANUAL for (Task task : taskService.selectManualByAnalyzeSts()) { Motion executingMotion = motionService.selectOfTop1(task.getUuid(), MotionStsType.EXECUTING.val(), task.getHostId()); if (executingMotion != null) {//存在正在执行的motion continue; } Motion motion = motionService.selectOfTop1(task.getUuid(), MotionStsType.INIT.val(), task.getHostId()); if (null != motion) { boolean result = this.executeMotion(motion); if (!result) { continue; } // 更新Task switch (TaskStsType.query(task.getTaskSts())) { case ANALYZE_MANUAL: task.setTaskSts(TaskStsType.EXECUTE_MANUAL.sts); break; } task.setUpdateTime(now); if (!taskService.updateById(task)) { log.error("{}其他工作档更新状态失败!", task.getTaskNo()); } } } // EXECUTE_MANUAL for (Task task : taskService.selectManualByExecuteSts()) { if (!motionService.hasRunningMotion(task.getUuid(), task.getHostId())) { Motion motion = motionService.selectOfTop1(task.getUuid(), MotionStsType.WAITING.val(), task.getHostId()); if (null != motion) { boolean result = this.executeMotion(motion); if (!result) { continue; } } else { if (motionService.selectOfTop1(task.getUuid(), MotionStsType.EXECUTING.val(), task.getHostId()) != null) { continue; } // 更新Task switch (TaskStsType.query(task.getTaskSts())) { case EXECUTE_MANUAL: task.setTaskSts(TaskStsType.COMPLETE_MANUAL.sts); break; } task.setUpdateTime(now); if (!taskService.updateById(task)) { log.error("{}他工作档更新状态失败!", task.getTaskNo()); } } } } } @Scheduled(cron = "0/1 * * * * ? ") public void scanMotionByExecuting() { List motionList = motionService.selectBySts(MotionStsType.EXECUTING.val()); for (Motion motion : motionList) { this.finishMotion(motion); } } private synchronized boolean executeMotion(Motion motion) { Boolean executeRes = Boolean.FALSE; switch (Objects.requireNonNull(MotionCtgType.get(motion.getMotionCtgEl())).deviceCtg) { case CONVEYOR: executeRes = conveyorCommandService.accept(motion); break; case CRANE: executeRes = craneCommandService.accept(motion); break; case SHUTTLE: executeRes = shuttleCommandService.accept(motion); break; case LIFT: executeRes = liftCommandService.accept(motion); break; case AGV: executeRes = agvCommandService.accept(motion); break; default: break; } if (executeRes) { Date now = new Date(); motion.setMotionSts(MotionStsType.EXECUTING.val()); motion.setUpdateTime(now); motion.setIoTime(now); if (!motionService.updateById(motion)) { throw new CoolException(motion.generateFlag() + "动作更新失败状态"); } motionService.theNextBeWaiting(motion.getUuid(), motion); return true; } return false; } private synchronized void finishMotion(Motion motion) { Boolean executeRes = Boolean.FALSE; switch (Objects.requireNonNull(DeviceCtgType.get(motion.getDeviceCtgEl()))){ case AGV: break; case CRANE: break; case LIFT: executeRes = liftCommandService.finish(motion); break; case SHUTTLE: executeRes = shuttleCommandService.finish(motion); break; case CONVEYOR: executeRes = conveyorCommandService.finish(motion); break; default: break; } if (executeRes) { Date now = new Date(); motion.setMotionSts(MotionStsType.COMPLETE.val()); motion.setUpdateTime(now); if (!motionService.updateById(motion)) { throw new CoolException(motion.generateFlag() + "动作更新失败状态"); } } } }