自动化立体仓库 - WCS系统
#
Junjie
2023-09-28 3909c3583e8fc2057715ca20ac16b14072f32b14
src/main/java/com/zy/core/thread/NyShuttleThread.java
@@ -11,6 +11,7 @@
import com.zy.asrs.utils.Utils;
import com.zy.common.model.NavigateNode;
import com.zy.common.model.NyShuttleOperaResult;
import com.zy.common.service.CommonService;
import com.zy.common.utils.*;
import com.zy.core.News;
import com.zy.core.ThreadHandler;
@@ -28,6 +29,7 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
import java.io.IOException;
import java.net.Socket;
import java.text.MessageFormat;
@@ -67,10 +69,10 @@
                    case 1:
                        read();
                        break;
                    // 写入数据
                    case 2:
                        write((NyShuttleHttpCommand) task.getData());
                        break;
//                    // 写入数据
//                    case 2:
//                        write((NyShuttleHttpCommand) task.getData());
//                        break;
                    //下发任务
                    case 3:
                        assignWork((ShuttleAssignCommand) task.getData());
@@ -98,6 +100,11 @@
                    && !shuttleProtocol.getPakMk()) {
                //执行下一步指令
                executeWork(shuttleProtocol.getTaskNo().shortValue());
            }
            //小车空闲且有跑库程序
            if (shuttleProtocol.isIdle() && shuttleProtocol.getMoveLoc()) {
                moveLoc();
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -275,7 +282,7 @@
    }
    private boolean write(NyShuttleHttpCommand command){
    private boolean write(NyShuttleHttpCommand command, ShuttleAssignCommand assignCommand) {
        if (null == command) {
            News.error("四向穿梭车写入命令为空");
            return false;
@@ -308,10 +315,33 @@
                throw new RuntimeException(ex);
            }
        }
        //保存数据到数据库做流水
        BasShuttleOptService shuttleOptService = SpringUtils.getBean(BasShuttleOptService.class);
        BasShuttleOpt opt = null;
        if (shuttleOptService != null) {
            opt = new BasShuttleOpt(
                    assignCommand.getTaskNo().intValue(),
                    assignCommand.getShuttleNo().intValue(),
                    new Date(),
                    ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc,
                    assignCommand.getSourceLocNo(),
                    assignCommand.getLocNo(),
                    null,
                    null,
                    null,
                    JSON.toJSONString(command),
                    null,
                    JSON.toJSONString(shuttleProtocol)
            );
            opt.setSend(1);//已下发
            opt.setResponse(JSON.toJSONString(result));//请求响应
            shuttleOptService.insert(opt);
        }
        if (result == null) {
            return false;//请求失败
        }
        return true;
    }
@@ -373,10 +403,12 @@
                    lastCommand.setComplete(true);
                    //解锁锁定路径,上一条路径和当前路径
                    List<NavigateNode> nodes = lastCommand.getNodes();
                    if (command != null) {
                    if (command != null && command.getNodes() != null) {
                        nodes.addAll(command.getNodes());
                    }
                    navigateMapData.writeNavigateNodeToRedisMap(nodes, false);//解锁路径
                    if (nodes != null) {
                        navigateMapData.writeNavigateNodeToRedisMap(nodes, false);//解锁路径
                    }
                }
            }else {
                lastCommand.setComplete(true);//其他命令默认认为完成
@@ -414,7 +446,10 @@
        }
        if (shuttleProtocol.getFree() == ShuttleStatusType.BUSY.id) {
            return false;//小车状态忙,禁止执行命令
            //停止充电
            if(!(command.getRequest().getBody().get("requestType").equals("stopCharge") && shuttleProtocol.getChargState() == 1)){
                return false;//小车状态忙,禁止执行命令
            }
        }
//        //检测小车是否要进提升机,如需要进提升机则调度提升机
@@ -427,20 +462,24 @@
            return false;
        }
        //检测路径是否可行走
        if (!checkPath(command.getNodes(), nextNodes, redisCommand)) {
            return false;
        }
        if (command.getRequest().getBody().get("requestType").equals("move")) {
            //检测路径是否可行走
            if (!checkPath(command.getNodes(), nextNodes, redisCommand)) {
                return false;
            }
        //锁定路径,锁定当前路径和下一步路径
        List<NavigateNode> nodes = command.getNodes();
        if (nextNodes != null) {
            nodes.addAll(nextNodes);
            //锁定路径,锁定当前路径和下一步路径
            List<NavigateNode> nodes = command.getNodes();
            if (nextNodes != null) {
                nodes.addAll(nextNodes);
            }
            if (nodes != null) {
                navigateMapData.writeNavigateNodeToRedisMap(nodes, true);//所使用的路径进行锁定禁用
            }
        }
        navigateMapData.writeNavigateNodeToRedisMap(nodes, true);//所使用的路径进行锁定禁用
        //可执行命令
        if (!write(command)) {
        if (!write(command, assignCommand)) {
            News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            return false;
        }
@@ -449,24 +488,10 @@
        //将标记置为false(防止重发)
        shuttleProtocol.setPakMk(true);
        //保存数据到数据库做流水
        BasShuttleOptService shuttleOptService = SpringUtils.getBean(BasShuttleOptService.class);
        if (shuttleOptService != null) {
            BasShuttleOpt opt = new BasShuttleOpt(
                    assignCommand.getTaskNo().intValue(),
                    assignCommand.getShuttleNo().intValue(),
                    new Date(),
                    ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc,
                    assignCommand.getSourceLocNo(),
                    assignCommand.getLocNo(),
                    null,
                    null,
                    null,
                    JSON.toJSONString(command),
                    null,
                    JSON.toJSONString(shuttleProtocol)
            );
            shuttleOptService.insert(opt);
        String requestType = command.getRequest().getBody().get("requestType").toString();
        if (requestType.equals("updateFloor")) {
            //更新楼层命令
            shuttleProtocol.setPakMk(false);//恢复标记
        }
        commandStep++;
@@ -474,37 +499,6 @@
        redisCommand.setCommandStep(commandStep);
        //任务数据保存到redis
        redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
//        //判断数据是否执行完成
//        if (commandStep < commands.size() - 1) {
//            commandStep++;
//            //更新redis数据
//            redisCommand.setCommandStep(commandStep);
//            //任务数据保存到redis
//            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
//        }else {
//            //已执行完成
//
//            commandStep = commands.size();
//            //更新redis数据
//            redisCommand.setCommandStep(commandStep);
//            //任务数据保存到redis
//            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
////            //最后一段命令为移动命令,则暂缓删除redis等待清除路径时一次性删除
////            //最后一段命令为不是移动命令,则删除redis
////            if (!command.getRequest().getBody().get("requestType").equals("move")) {
////                //删除redis
////                redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo());
////            }
////
////            if (!assignCommand.getCharge()) {
////                //对主线程抛出等待确认状态waiting
////                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WAITING);
////            }else {
////                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING_WAITING);
////            }
////            News.info("四向穿梭车任务执行下发完成等待执行结束,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
//        }
        return true;
    }
@@ -656,41 +650,93 @@
            }
        }
        ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand();
        NavigateNode currentTarget = currentNodes.get(currentNodes.size() - 1);
        String currentLocNo = NavigatePositionConvert.nodeToLocNo(currentTarget);
        NavigateNode nextTarget = nextNodes.get(nextNodes.size() - 1);
        String nextLocNo = NavigatePositionConvert.nodeToLocNo(nextTarget);
        if (assignCommand.getLocNo().equals(currentLocNo) || assignCommand.getLocNo().equals(nextLocNo)) {
            //当前路径最后一个节点是目标库位,进行路径检测,如果不可行走,重新计算路径
            //不可行走,重新计算路径
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleProtocol.getShuttleNo().intValue(), shuttleProtocol.getTaskNo(), shuttleProtocol.getCurrentLocNo(), assignCommand.getLocNo());
            if (result == null) {
                return false;//路径计算失败,不可行走
            }
            List<NyShuttleHttpCommand> newCommands = result.getCommands();//新路径
            //当前步序
            int commandStep = redisCommand.getCommandStep();
            List<NyShuttleHttpCommand> commands = assignCommand.getCommands();
            commands.remove(commandStep);//移除当前步序指令
            if (assignCommand.getLocNo().equals(currentLocNo)) {
                //当前路径,需要再多移除下一步指令
                commands.remove(commandStep + 1);
            }
            //将新路径添加进指令集合
            commands.addAll(commandStep, newCommands);
            assignCommand.setCommands(commands);
            redisCommand.setAssignCommand(assignCommand);
            //任务数据保存到redis
            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            return false;//当前不可行走,等待下一次执行走新路径
        }
//        ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand();
//        NavigateNode currentTarget = currentNodes.get(currentNodes.size() - 1);
//        String currentLocNo = NavigatePositionConvert.nodeToLocNo(currentTarget);
//        NavigateNode nextTarget = nextNodes.get(nextNodes.size() - 1);
//        String nextLocNo = NavigatePositionConvert.nodeToLocNo(nextTarget);
//        if (assignCommand.getLocNo().equals(currentLocNo) || assignCommand.getLocNo().equals(nextLocNo)) {
//            //当前路径最后一个节点是目标库位,进行路径检测,如果不可行走,重新计算路径
//            //不可行走,重新计算路径
//            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleProtocol.getShuttleNo().intValue(), shuttleProtocol.getTaskNo(), shuttleProtocol.getCurrentLocNo(), assignCommand.getLocNo());
//            if (result == null) {
//                return false;//路径计算失败,不可行走
//            }
//
//            List<NyShuttleHttpCommand> newCommands = result.getCommands();//新路径
//
//            //当前步序
//            int commandStep = redisCommand.getCommandStep();
//            List<NyShuttleHttpCommand> commands = assignCommand.getCommands();
//
//            commands.remove(commandStep);//移除当前步序指令
//            if (assignCommand.getLocNo().equals(currentLocNo)) {
//                //当前路径,需要再多移除下一步指令
//                commands.remove(commandStep + 1);
//            }
//
//            //将新路径添加进指令集合
//            commands.addAll(commandStep, newCommands);
//            assignCommand.setCommands(commands);
//            redisCommand.setAssignCommand(assignCommand);
//            //任务数据保存到redis
//            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
//            return false;//当前不可行走,等待下一次执行走新路径
//        }
        return false;//不可行走
    }
    /**
     * 跑库程序
     */
    private void moveLoc() {
        LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
        ShuttleDispatchUtils shuttleDispatchUtils = SpringUtils.getBean(ShuttleDispatchUtils.class);
        CommonService commonService = SpringUtils.getBean(CommonService.class);
        WrkMastMapper wrkMastMapper = SpringUtils.getBean(WrkMastMapper.class);
        int lev = Utils.getLev(shuttleProtocol.getCurrentLocNo());//小车当前楼层
        if (!shuttleProtocol.isIdle()) {
            return;
        }
        WrkMast wrkMast = wrkMastMapper.selectShuttleHasMoveWorking(shuttleProtocol.getShuttleNo().intValue());
        if (wrkMast != null) {
            return;
        }
        if (shuttleProtocol.getMoveType() == 0) {//跑轨道
            ArrayList<String> locs = new ArrayList<>();
            for (int i = shuttleProtocol.getXCurrent(); i <= shuttleProtocol.getXTarget(); i++) {
                String locNo = Utils.getLocNo(i, shuttleProtocol.getYCurrent(), lev);
                locs.add(locNo);
            }
            List<LocMast> locMasts = locMastService.selectEmptyLocNos(locs);
            if (locMasts.isEmpty()) {
                //空库位
                shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
                return;
            }
            LocMast start = locMasts.get(0);
            LocMast target = locMasts.get(locMasts.size() - 1);
            //判断小车是否在起点位置
            if (!shuttleProtocol.getCurrentLocNo().equals(start.getLocNo())) {//不在起点位置,调度去起点位置
                shuttleDispatchUtils.dispatchShuttle(commonService.getWorkNo(3), start.getLocNo());
            }else {
                //在起点位置,调度去目标位置
                if (shuttleProtocol.getCurrentLocNo().equals(target.getLocNo())) {
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);//小车和目标位置一致,跳过
                }else {
                    boolean result = shuttleDispatchUtils.dispatchShuttle(commonService.getWorkNo(3), target.getLocNo());
                    if (result) {//调度成功
                        shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
                    }
                }
            }
        }else {//跑库位
        }
    }
}