zjj
2025-02-17 4146cd7916dc94b2336e56267824ad14904435f2
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/action/LiftAction.java
@@ -1,20 +1,27 @@
package com.zy.asrs.wcs.core.action;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.wcs.core.entity.Task;
import com.zy.asrs.wcs.core.model.command.*;
import com.zy.asrs.wcs.core.model.enums.LiftCommandModeType;
import com.zy.asrs.wcs.core.service.TaskService;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.constant.DeviceRedisConstant;
import com.zy.asrs.wcs.rcs.entity.BasLiftOpt;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.enums.ShuttleProtocolStatusType;
import com.zy.asrs.wcs.rcs.model.CommandResponse;
import com.zy.asrs.wcs.rcs.model.enums.LiftProtocolStatusType;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.LiftProtocol;
import com.zy.asrs.wcs.rcs.service.BasLiftOptService;
import com.zy.asrs.wcs.rcs.thread.LiftThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
@Component
@@ -22,6 +29,10 @@
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TaskService taskService;
    @Autowired
    private BasLiftOptService basLiftOptService;
    public synchronized boolean assignWork(Device device, LiftAssignCommand assignCommand) {
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
@@ -53,6 +64,11 @@
            return false;
        }
        Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskNo, taskNo));
        if (task == null) {
            return false;
        }
        LiftRedisCommand redisCommand = JSON.parseObject(obj.toString(), LiftRedisCommand.class);
        if (redisCommand == null) {
            return false;
@@ -69,7 +85,7 @@
        }
        //判断提升机是否空闲
        if (!liftThread.isIdle()) {
        if (!liftThread.isDeviceIdle()) {
            return false;
        }
@@ -83,22 +99,53 @@
        if (commandStep != 0) {
            LiftCommand command = commands.get(commandStep - 1);
            if (command.getMode() == LiftCommandModeType.MOVE.id) {
                //提升机升降
                if (liftProtocol.getLev() == command.getTargetLev()) {
                    command.setComplete(true);
                }
            } else if (command.getMode() == LiftCommandModeType.MOVE_CAR.id) {
                //提升机升降小车
                if (liftProtocol.getLev() == command.getTargetLev()) {
                    command.setComplete(true);
                }
            } else if (command.getMode() == LiftCommandModeType.PALLET_INOUT.id) {
                //托盘出入
                if (liftProtocol.getLev() == command.getTargetLev()) {
                    command.setComplete(true);
                }
            //目前没有判断,直接判定上一条指令完成
            command.setComplete(true);
                //判断提升机托盘是否存在
                if (liftProtocol.getHasTray()) {
                    return false;
                }
            } else if (command.getMode() == LiftCommandModeType.RESET.id) {
                //复位
                command.setComplete(true);
            } else if (command.getMode() == LiftCommandModeType.LOCK.id) {
                //锁定
                command.setComplete(true);
            } else if (command.getMode() == LiftCommandModeType.UNLOCK.id) {
                //解锁
                command.setComplete(true);
            }
            // 更新redis数据
            redisUtil.set(DeviceRedisConstant.LIFT_WORK_FLAG + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            redisUtil.set(DeviceRedisConstant.LIFT_WORK_FLAG + taskNo, JSON.toJSONString(redisCommand));
            if (!command.getComplete()) {
                return false;
            }
            //判断是否为最后一条命令且命令执行完成,抛出等待确认状态
            LiftCommand endCommand = commands.get(commands.size() - 1);
            if (endCommand.getComplete()) {
                News.info("提升机任务执行下发完成等待执行结束,提升机号={},任务数据={}", liftProtocol.getLiftNo(), JSON.toJSON(commands));
                //对主线程抛出等待确认状态waiting
                liftThread.setProtocolStatus(LiftProtocolStatusType.WAITING);
                redisUtil.del(DeviceRedisConstant.LIFT_WORK_FLAG + taskNo);
                return false;//禁止再下发命令
            }
        }
@@ -106,42 +153,66 @@
        //取出命令
        LiftCommand command = commands.get(commandStep);
        boolean result = write(command, device);
        if (!result) {
        // 下发命令
        CommandResponse response = write(command, device);
        //保存命令日志
        BasLiftOpt basLiftOpt = new BasLiftOpt();
        basLiftOpt.setTaskNo(taskNo);
        basLiftOpt.setLiftNo(liftProtocol.getLiftNo());
        basLiftOpt.setMode(LiftCommandModeType.get(command.getMode()).desc);
        basLiftOpt.setCommand(JSON.toJSONString(command));
        basLiftOpt.setSystemStatus(JSON.toJSONString(liftProtocol));
        basLiftOpt.setDeviceId(device.getId());
        basLiftOpt.setDeviceWrk(String.valueOf(command.getTaskNo()));
        basLiftOpt.setResponse(response.getMessage());//获取响应
        basLiftOpt.setSendTime(new Date());//指令下发时间
        basLiftOpt.setSend(response.getResult() ? 1 : 0);
        basLiftOpt.setHostId(device.getHostId());
        //保存命令流水
        basLiftOptService.save(basLiftOpt);
        if (!response.getResult()) {
            News.error("提升机命令下发失败,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command));
            return false;
        } else {
            News.info("提升机命令下发成功,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command));
        }
        redisUtil.del(DeviceRedisConstant.LIFT_WORK_FLAG + command.getTaskNo());
        liftThread.setProtocolStatus(LiftProtocolStatusType.WORKING);
        commandStep++;
        //更新redis数据
        redisCommand.setCommandStep(commandStep);
        // 更新redis数据
        redisUtil.set(DeviceRedisConstant.LIFT_WORK_FLAG + taskNo, JSON.toJSONString(redisCommand));
        return true;
    }
    private synchronized boolean write(LiftCommand command, Device device) {
    private synchronized CommandResponse write(LiftCommand command, Device device) {
        CommandResponse response = new CommandResponse(false);
        if (null == command) {
            News.error("提升机写入命令为空");
            return false;
            response.setMessage("提升机写入命令为空");
            return response;
        }
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
        if (liftThread == null) {
            return false;
            return response;
        }
        boolean result = false;
        if (command.getMode() == LiftCommandModeType.MOVE.id) {
            result = liftThread.move(command);
            response = liftThread.move(command);
        } else if (command.getMode() == LiftCommandModeType.PALLET_INOUT.id) {
            result = liftThread.palletInOut(command);
            response = liftThread.palletInOut(command);
        } else if (command.getMode() == LiftCommandModeType.LOCK.id) {
            result = liftThread.lock(command);
            response = liftThread.lock(command);
        } else if (command.getMode() == LiftCommandModeType.UNLOCK.id) {
            result = liftThread.unlock(command);
            response = liftThread.unlock(command);
        } else if (command.getMode() == LiftCommandModeType.RESET.id) {
            result = liftThread.reset(command);
            response = liftThread.reset(command);
        }
        return result;
        return response;
    }
}