#
Junjie
2024-09-26 a032e83b86f182f2939454949129adfece45ebed
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/timer/MotionTimer.java
@@ -1,17 +1,16 @@
package com.zy.asrs.wcs.core.timer;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.entity.Task;
import com.zy.asrs.wcs.core.kernel.command.AgvCommandService;
import com.zy.asrs.wcs.core.kernel.command.CraneCommandService;
import com.zy.asrs.wcs.core.kernel.command.ShuttleCommandService;
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.rcs.entity.Motion;
import com.zy.asrs.wcs.rcs.service.MotionService;
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;
@@ -34,23 +33,35 @@
    private MotionService motionService;
    @Autowired
    private AgvCommandService agvCommandService;
//    @Autowired
//    private ConveyorCommandService conveyorCommandService;
    @Autowired
    private ConveyorCommandService conveyorCommandService;
    @Autowired
    private CraneCommandService craneCommandService;
//    @Autowired
//    private LiftCommandService liftCommandService;
    @Autowired
    private LiftCommandService liftCommandService;
    @Autowired
    private ShuttleCommandService shuttleCommandService;
    @Autowired
    private MapCommandService mapCommandService;
    @Scheduled(cron = "0/1 * * * * ? ")
    public synchronized void executeWrkMast() {
    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;
                if (executingMotion.getSync() == 1) {//当前执行中的动作是同步动作,等待执行完成后再执行下一个动作
                    continue;
                }
                //异步动作,只允许下一条动作被执行
                Motion nextMotion = motionService.getOne(new LambdaQueryWrapper<Motion>().eq(Motion::getUuid, executingMotion.getUuid()).eq(Motion::getPriority, executingMotion.getPriority() - 1));
                if (nextMotion != null) {
                    if (!(nextMotion.getMotionSts() == MotionStsType.INIT.val() || nextMotion.getMotionSts() == MotionStsType.WAITING.val())) {
                        continue;//下一条动作已经被执行,不允许再执行后续动作
                    }
                }
            }
            Motion motion = motionService.selectOfTop1(task.getUuid(), MotionStsType.INIT.val(), task.getHostId());
@@ -60,7 +71,7 @@
                    continue;
                }
                // 更新wrkMast
                // 更新Task
                switch (TaskStsType.query(task.getTaskSts())) {
                    case ANALYZE_INBOUND:
                        task.setTaskSts(TaskStsType.EXECUTE_INBOUND.sts);
@@ -89,7 +100,7 @@
                        continue;
                    }
                    // 更新wrkMast
                    // 更新Task
                    switch (TaskStsType.query(task.getTaskSts())) {
                        case EXECUTE_INBOUND:
                            task.setTaskSts(TaskStsType.COMPLETE_INBOUND.sts);
@@ -107,68 +118,279 @@
        }
    }
//    @Scheduled(cron = "0/1 * * * * ? ")
//    public synchronized void executeWrkCharge() {
//        Date now = new Date();
//        // ANALYZE_INBOUND
//        for (WrkCharge wrkCharge : wrkChargeMapper.selectByAnalyzeSts()) {
//            Motion executingMotion = motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.EXECUTING.val());
//            if (executingMotion != null) {//存在正在执行的motion
//                continue;
//            }
//
//            Motion motion = motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.INIT.val());
//            if (null != motion) {
//                boolean result = this.executeMotion(motion);
//                if (!result) {
//                    continue;
//                }
//                // 更新wrkMast
//                switch (WrkMastStsType.query(wrkCharge.getWrkSts())) {
//                    case ANALYZE_CHARGE:
//                        wrkCharge.setWrkSts(WrkMastStsType.EXECUTE_CHARGE.sts);
//                        break;
//                    case ANALYZE_MOVE:
//                        wrkCharge.setWrkSts(WrkMastStsType.EXECUTE_MOVE.sts);
//                        break;
//                }
//                wrkCharge.setModiTime(now);
//                if (wrkChargeMapper.updateById(wrkCharge) == 0) {
//                    log.error("{}其他工作档更新状态失败!", wrkCharge.getWrkNo());
//                }
//            }
//        }
//        // EXECUTE_INBOUND
//        for (WrkCharge wrkCharge : wrkChargeMapper.selectByExecuteSts()) {
//            if (!motionService.hasRunningMotion(wrkCharge.getUuid())) {
//                Motion motion = motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.WAITING.val());
//                if (null != motion) {
//                    boolean result = this.executeMotion(motion);
//                    if (!result) {
//                        continue;
//                    }
//                } else {
//                    if (motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.EXECUTING.val()) != null) {
//                        continue;
//                    }
//
//                    // 更新wrkMast
//                    switch (WrkMastStsType.query(wrkCharge.getWrkSts())) {
//                        case EXECUTE_CHARGE:
//                            wrkCharge.setWrkSts(WrkMastStsType.CHARGE_WORKING.sts);
//                            break;
//                        case EXECUTE_MOVE:
//                            wrkCharge.setWrkSts(WrkMastStsType.COMPLETE_MOVE.sts);
//                            break;
//                    }
//                    wrkCharge.setModiTime(now);
//                    if (wrkChargeMapper.updateById(wrkCharge) == 0) {
//                        log.error("{}他工作档更新状态失败!", wrkCharge.getWrkNo());
//                    }
//                }
//            }
//        }
//    }
    @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
                if (executingMotion.getSync() == 1) {//当前执行中的动作是同步动作,等待执行完成后再执行下一个动作
                    continue;
                }
                //异步动作,只允许下一条动作被执行
                Motion nextMotion = motionService.getOne(new LambdaQueryWrapper<Motion>().eq(Motion::getUuid, executingMotion.getUuid()).eq(Motion::getPriority, executingMotion.getPriority() - 1));
                if (nextMotion != null) {
                    if (!(nextMotion.getMotionSts() == MotionStsType.INIT.val() || nextMotion.getMotionSts() == MotionStsType.WAITING.val())) {
                        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 executeLadenMoveTask() {
        Date now = new Date();
        // ANALYZE_CHARGE
        for (Task taskCharge : taskService.selectLadenMoveByAnalyzeSts()) {
            Motion executingMotion = motionService.selectOfTop1(taskCharge.getUuid(), MotionStsType.EXECUTING.val(), taskCharge.getHostId());
            if (executingMotion != null) {//存在正在执行的motion
                if (executingMotion.getSync() == 1) {//当前执行中的动作是同步动作,等待执行完成后再执行下一个动作
                    continue;
                }
                //异步动作,只允许下一条动作被执行
                Motion nextMotion = motionService.getOne(new LambdaQueryWrapper<Motion>().eq(Motion::getUuid, executingMotion.getUuid()).eq(Motion::getPriority, executingMotion.getPriority() - 1));
                if (nextMotion != null) {
                    if (!(nextMotion.getMotionSts() == MotionStsType.INIT.val() || nextMotion.getMotionSts() == MotionStsType.WAITING.val())) {
                        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_LADEN_MOVE:
                        taskCharge.setTaskSts(TaskStsType.EXECUTE_LADEN_MOVE.sts);
                        break;
                }
                taskCharge.setUpdateTime(now);
                if (!taskService.updateById(taskCharge)) {
                    log.error("{}其他工作档更新状态失败!", taskCharge.getTaskNo());
                }
            }
        }
        // EXECUTE_CHARGE
        for (Task taskCharge : taskService.selectLadenMoveByExecuteSts()) {
            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_LADEN_MOVE:
                            taskCharge.setTaskSts(TaskStsType.COMPLETE_LADEN_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
                if (executingMotion.getSync() == 1) {//当前执行中的动作是同步动作,等待执行完成后再执行下一个动作
                    continue;
                }
                //异步动作,只允许下一条动作被执行
                Motion nextMotion = motionService.getOne(new LambdaQueryWrapper<Motion>().eq(Motion::getUuid, executingMotion.getUuid()).eq(Motion::getPriority, executingMotion.getPriority() - 1));
                if (nextMotion != null) {
                    if (!(nextMotion.getMotionSts() == MotionStsType.INIT.val() || nextMotion.getMotionSts() == MotionStsType.WAITING.val())) {
                        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 synchronized void executeMoveTask() {
        Date now = new Date();
        // ANALYZE_MOVE
        for (Task task : taskService.selectMoveByAnalyzeSts()) {
            Motion executingMotion = motionService.selectOfTop1(task.getUuid(), MotionStsType.EXECUTING.val(), task.getHostId());
            if (executingMotion != null) {//存在正在执行的motion
                if (executingMotion.getSync() == 1) {//当前执行中的动作是同步动作,等待执行完成后再执行下一个动作
                    continue;
                }
                //异步动作,只允许下一条动作被执行
                Motion nextMotion = motionService.getOne(new LambdaQueryWrapper<Motion>().eq(Motion::getUuid, executingMotion.getUuid()).eq(Motion::getPriority, executingMotion.getPriority() - 1));
                if (nextMotion != null) {
                    if (!(nextMotion.getMotionSts() == MotionStsType.INIT.val() || nextMotion.getMotionSts() == MotionStsType.WAITING.val())) {
                        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_MOVE:
                        task.setTaskSts(TaskStsType.EXECUTE_MOVE.sts);
                        break;
                }
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    log.error("{}其他工作档更新状态失败!", task.getTaskNo());
                }
            }
        }
        // EXECUTE_MOVE
        for (Task task : taskService.selectMoveByExecuteSts()) {
            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_MOVE:
                            task.setTaskSts(TaskStsType.COMPLETE_MOVE.sts);
                            break;
                    }
                    task.setUpdateTime(now);
                    if (!taskService.updateById(task)) {
                        log.error("{}他工作档更新状态失败!", task.getTaskNo());
                    }
                }
            }
        }
    }
    @Scheduled(cron = "0/1 * * * * ? ")
    public void scanMotionByExecuting() {
@@ -182,7 +404,7 @@
        Boolean executeRes = Boolean.FALSE;
        switch (Objects.requireNonNull(MotionCtgType.get(motion.getMotionCtgEl())).deviceCtg) {
            case CONVEYOR:
//                executeRes = conveyorCommandService.accept(motion);
                executeRes = conveyorCommandService.accept(motion);
                break;
            case CRANE:
                executeRes = craneCommandService.accept(motion);
@@ -191,10 +413,13 @@
                executeRes = shuttleCommandService.accept(motion);
                break;
            case LIFT:
//                executeRes = liftCommandService.accept(motion);
                executeRes = liftCommandService.accept(motion);
                break;
            case AGV:
                executeRes = agvCommandService.accept(motion);
                break;
            case MAP:
                executeRes = mapCommandService.accept(motion);
                break;
            default:
                break;
@@ -223,13 +448,16 @@
            case CRANE:
                break;
            case LIFT:
//                executeRes = liftCommandService.finish(motion);
                executeRes = liftCommandService.finish(motion);
                break;
            case SHUTTLE:
                executeRes = shuttleCommandService.finish(motion);
                break;
            case CONVEYOR:
//                executeRes = conveyorCommandService.finish(motion);
                executeRes = conveyorCommandService.finish(motion);
                break;
            case MAP:
                executeRes = mapCommandService.finish(motion);
                break;
            default:
                break;