#
Junjie
2023-11-27 9bbaf1fdf110e78d3daf76f2b8033aae48f322ca
src/main/java/com/zy/asrs/utils/CommandUtils.java
@@ -3,96 +3,98 @@
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.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) {
        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;//任务号
                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);
        if (type == SlaveType.Crn) {
            CommandPackage<CrnCommand> commandPackage = new CommandPackage<>();
            CrnCommand command = (CrnCommand) task.getData();
            wrkNo = command.getTaskNo();
            if (wrkNo != 0) {
                taskWrk = taskWrkService.selectByWrkNo(wrkNo);
                taskNo = taskWrk.getTaskNo();
            }
                command.setCommandInfo(commandInfo);
                break;
            case Devp:
                StaProtocol staProtocol = (StaProtocol) task.getData();
                wrkNo = staProtocol.getWorkNo();
                if (wrkNo != 0) {
                    wrkMast = wrkMastService.selectById(wrkNo);
                    taskNo = wrkMast.getTaskNo();
                }
            commandInfo = new CommandInfo();
            commandInfo.setWrkNo(wrkNo);
            commandInfo.setTaskNo(taskNo);
            commandInfo.setCommandStatus(1);
            commandInfo.setStartTime(new Date());
            commandInfo.setDevice(SlaveType.Crn.toString());
                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);
            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();
                    }
            command.setCommandId(commandInfo.getId());
            commandPackage.setWrkNo(wrkNo);
            commandPackage.setDevice("堆垛机");
            commandPackage.setCommand(command);//保存命令报文
            commandInfo.setCommand(JSON.toJSONString(commandPackage));
            commandInfoService.updateById(commandInfo);
                    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);
            //将指令进行投递
            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 (wrkNo != 0) {
                taskWrk = taskWrkService.selectByWrkNo(wrkNo);
                taskNo = taskWrk.getTaskNo();
            }
                    ledCommand.setCommandInfo(commandInfo);
                }
                break;
            commandInfo = new CommandInfo();
            commandInfo.setWrkNo(wrkNo);
            commandInfo.setTaskNo(taskNo);
            commandInfo.setCommandStatus(1);
            commandInfo.setStartTime(new Date());
            commandInfo.setDevice(SlaveType.Devp.toString());
            commandInfoService.insert(commandInfo);//插入指令
            staProtocol.setCommandId(commandInfo.getId());
            commandPackage.setCommand(staProtocol);
            commandPackage.setWrkNo(wrkNo);
            commandPackage.setDevice("输送线");
            commandInfo.setCommand(JSON.toJSONString(commandPackage));//保存命令报文
            commandInfoService.updateById(commandInfo);
            //将指令进行投递
            if (!MessageQueue.offer(type, id, task)) {
                return false;
            }
            return true;
        }
        return false;
    }
}