自动化立体仓库 - WCS系统
野心家
2025-04-01 66942458f7753fe4ddee7c5ed3386d61cff28b24
src/main/java/com/zy/asrs/utils/CommandUtils.java
@@ -3,96 +3,110 @@
import com.alibaba.fastjson.JSON;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.CommandInfo;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.entity.TaskWrk;
import com.zy.asrs.service.CommandInfoService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.service.TaskWrkService;
import com.zy.core.cache.MessageQueue;
import com.zy.core.enums.CommandStatusType;
import com.zy.core.enums.CommandType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.Task;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.model.command.LedCommand;
import com.zy.core.model.command.CommandPackage;
import com.zy.core.model.protocol.StaProtocol;
import java.util.Date;
import java.util.List;
public class CommandUtils {
    public static void offer(SlaveType type, Integer id, Task task) {
    public static boolean offer(SlaveType type, Integer id, Task task) {
        return offer(type, id, task, true);
    }
    public static boolean offer(SlaveType type, Integer id, Task task, Boolean auto) {
        CommandInfoService commandInfoService = SpringUtils.getBean(CommandInfoService.class);
        if (commandInfoService == null) {
            return;
            return false;
        }
        WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class);
        if (wrkMastService == null) {
            return;
        TaskWrkService taskWrkService = SpringUtils.getBean(TaskWrkService.class);
        if (taskWrkService == null) {
            return false;
        }
        CommandInfo commandInfo = null;
        WrkMast wrkMast = null;
        int wrkNo = 0;
        String taskNo = null;
        switch (type) {
            case Crn:
                CrnCommand command = (CrnCommand) task.getData();
                wrkNo = command.getTaskNo();
                if (wrkNo != 0) {
                    wrkMast = wrkMastService.selectById(wrkNo);
                    taskNo = wrkMast.getTaskNo();
        TaskWrk taskWrk = null;
        int wrkNo = 0;//工作号
        String taskNo = null;//任务号
        if (type == SlaveType.Crn) {
            CommandPackage<CrnCommand> commandPackage = new CommandPackage<>();
            CrnCommand command = (CrnCommand) task.getData();
            wrkNo = command.getTaskNo();
            if (auto && wrkNo != 0) {
                taskWrk = taskWrkService.selectByWrkNo(wrkNo);
                taskNo = taskWrk.getTaskNo();
            }
            commandInfo = new CommandInfo();
            commandInfo.setWrkNo(wrkNo);
            commandInfo.setTaskNo(taskNo);
            commandInfo.setCommandStatus(CommandStatusType.CREATE.id);
            commandInfo.setStartTime(new Date());
            commandInfo.setDevice(SlaveType.Crn.toString());
            commandInfo.setCommandType(auto? CommandType.AUTO.id : CommandType.MANUAL.id);
            commandInfoService.insert(commandInfo);//插入指令
            command.setCommandId(commandInfo.getId());
            commandPackage.setWrkNo(wrkNo);
            commandPackage.setDevice("堆垛机");
            commandPackage.setCommand(command);//保存命令报文
            commandInfo.setCommand(JSON.toJSONString(commandPackage));
            commandInfoService.updateById(commandInfo);
            if (!auto) {
                //将指令进行投递
                if (!MessageQueue.offer(type, id, task)) {
                    return false;
                }
            }
            return true;
        } else if (type == SlaveType.Devp) {
            CommandPackage<StaProtocol> commandPackage = new CommandPackage<>();
            StaProtocol staProtocol = (StaProtocol) task.getData();
            wrkNo = staProtocol.getWorkNo();
            if (auto && wrkNo != 0) {
                taskWrk = taskWrkService.selectByWrkNo(wrkNo);
                taskNo = taskWrk.getTaskNo();
            }
                commandInfo = new CommandInfo();
                commandInfo.setWrkNo(wrkNo);
                commandInfo.setTaskNo(taskNo);
                commandInfo.setCommandStatus(1);
                commandInfo.setStartTime(new Date());
                commandInfo.setDevice("crn");
                commandInfo.setCommand(JSON.toJSONString(command));
                commandInfoService.insert(commandInfo);
            commandInfo = new CommandInfo();
            commandInfo.setWrkNo(wrkNo);
            commandInfo.setTaskNo(taskNo);
            commandInfo.setCommandStatus(CommandStatusType.CREATE.id);
            commandInfo.setStartTime(new Date());
            commandInfo.setDevice(SlaveType.Devp.toString());
            commandInfo.setCommandType(auto? CommandType.AUTO.id : CommandType.MANUAL.id);
                command.setCommandInfo(commandInfo);
                break;
            case Devp:
                StaProtocol staProtocol = (StaProtocol) task.getData();
                wrkNo = staProtocol.getWorkNo();
                if (wrkNo != 0) {
                    wrkMast = wrkMastService.selectById(wrkNo);
                    taskNo = wrkMast.getTaskNo();
            commandInfoService.insert(commandInfo);//插入指令
            staProtocol.setCommandId(commandInfo.getId());
            commandPackage.setCommand(staProtocol);
            commandPackage.setWrkNo(wrkNo);
            commandPackage.setDevice("输送线");
            commandInfo.setCommand(JSON.toJSONString(commandPackage));//保存命令报文
            commandInfoService.updateById(commandInfo);
            if (!auto) {
                //将指令进行投递
                if (!MessageQueue.offer(type, id, task)) {
                    return false;
                }
                commandInfo = new CommandInfo();
                commandInfo.setWrkNo(wrkNo);
                commandInfo.setTaskNo(taskNo);
                commandInfo.setCommandStatus(1);
                commandInfo.setStartTime(new Date());
                commandInfo.setDevice("devp");
                commandInfo.setCommand(JSON.toJSONString(staProtocol));
                commandInfoService.insert(commandInfo);
                staProtocol.setCommandInfo(commandInfo);
                break;
            case Led:
                List<LedCommand> data = (List<LedCommand>) task.getData();
                for (LedCommand ledCommand : data) {
                    wrkNo = ledCommand.getWorkNo();
                    if (wrkNo != 0) {
                        wrkMast = wrkMastService.selectById(wrkNo);
                        taskNo = wrkMast.getTaskNo();
                    }
                    commandInfo = new CommandInfo();
                    commandInfo.setWrkNo(ledCommand.getWorkNo());
                    commandInfo.setTaskNo(taskNo);
                    commandInfo.setCommandStatus(1);
                    commandInfo.setStartTime(new Date());
                    commandInfo.setDevice("led");
                    commandInfo.setCommand(JSON.toJSONString(ledCommand));
                    commandInfoService.insert(commandInfo);
                    ledCommand.setCommandInfo(commandInfo);
                }
                break;
            }
            return true;
        }
        return false;
    }
}