自动化立体仓库 - WCS系统
#
tzsk
2024-02-07 8dc3bcb40363cc6f9281092b4f6ea0ebcb9335b0
src/main/java/com/zy/core/thread/NyShuttleThread.java
@@ -10,7 +10,6 @@
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;
@@ -29,7 +28,6 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
import java.io.IOException;
import java.net.Socket;
import java.text.MessageFormat;
@@ -115,12 +113,6 @@
    private void readStatus() {
        try {
            if (null == shuttleProtocol) {
                shuttleProtocol = new NyShuttleProtocol();
                shuttleProtocol.setShuttleNo(slave.getId().shortValue());
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
            }
            //----------读取四向穿梭车状态-----------
            NyShuttleHttpCommand readStatusCommand = NyHttpUtils.getReadStatusCommand(slave.getId());
            JSONObject jsonObject = NyHttpUtils.requestCommand(socket, readStatusCommand);
@@ -249,6 +241,23 @@
                    OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
//                    log.info(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                }
//                log.warn(JSON.toJSONString(shuttleProtocol));//输出小车状态
                if (System.currentTimeMillis() - shuttleProtocol.getDeviceDataLog() > 1000 * 5) {
                    //采集时间超过5s,保存一次数据记录
                    //保存数据记录
                    DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class);
                    DeviceDataLog deviceDataLog = new DeviceDataLog();
                    deviceDataLog.setOriginData(JSON.toJSONString(jsonObject));
                    deviceDataLog.setWcsData(JSON.toJSONString(shuttleProtocol));
                    deviceDataLog.setType("shuttle");
                    deviceDataLog.setDeviceNo(shuttleProtocol.getShuttleNo().intValue());
                    deviceDataLog.setCreateTime(new Date());
                    deviceDataLogService.insert(deviceDataLog);
                    //更新采集时间
                    shuttleProtocol.setDeviceDataLog(System.currentTimeMillis());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -271,6 +280,11 @@
            socket.setSoTimeout(60000);
            socket.setKeepAlive(true);
            this.socket = socket;
            if (null == shuttleProtocol) {
                shuttleProtocol = new NyShuttleProtocol();
                shuttleProtocol.setShuttleNo(slave.getId().shortValue());
            }
            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
            log.info(MessageFormat.format("【{0}】四向穿梭车Socket链接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
        } catch (IOException e) {
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车Socket链接失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
@@ -344,6 +358,9 @@
        if (result == null) {
            return false;//请求失败
        }
        shuttleProtocol.setSendTime(System.currentTimeMillis());//指令下发时间
        return true;
    }
@@ -358,7 +375,7 @@
        shuttleProtocol.setAssignCommand(assignCommand);
        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WORKING);
        //任务数据保存到redis
        redisUtil.set("shuttle_wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand));
        redisUtil.set(RedisKeyType.SHUTTLE.key + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand));
        //执行下发任务
        executeWork(assignCommand.getTaskNo());
    }
@@ -374,7 +391,7 @@
        WrkMastMapper wrkMastMapper = SpringUtils.getBean(WrkMastMapper.class);
        WrkMast wrkMast = wrkMastMapper.selectByWorkNo(wrkNo.intValue());
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        Object o = redisUtil.get(RedisKeyType.SHUTTLE.key + wrkNo);
        if (o == null) {
            return false;
        }
@@ -407,13 +424,18 @@
                    //上一条指令的目标位置和当前小车位置相同,则认定上一条任务完成
                    lastCommand.setComplete(true);
                    //解锁锁定路径,上一条路径
                    List<NavigateNode> nodes = lastCommand.getNodes();
                    List<NavigateNode> nodes = JSON.parseArray(JSON.toJSONString(lastCommand.getNodes()), NavigateNode.class);//进行深度copy
//                    //解锁当前路径
//                    if (command != null && command.getNodes() != null) {
//                        nodes.addAll(command.getNodes());
//                    }
                    if (nodes != null) {
                        boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(shuttleProtocol.getCurrentLocNo()), nodes, false);//解锁路径
                        NavigateNode targetNode = assignCommand.getNodes().get(assignCommand.getNodes().size() - 1);//最终节点
                        NavigateNode node = nodes.get(nodes.size() - 1);
                        if (!(targetNode.getX() == node.getX() && targetNode.getY() == node.getY())) {
                            nodes.remove(nodes.size() - 1);//剔除尾节点
                        }
                        boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(shuttleProtocol.getCurrentLocNo()), shuttleProtocol.getShuttleNo().intValue(), nodes, false);//解锁路径
                        if (!result) {
                            return false;//解锁失败
                        }
@@ -424,7 +446,7 @@
                lastCommand.setComplete(true);//其他命令默认认为完成
            }
            //任务数据保存到redis
            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            redisUtil.set(RedisKeyType.SHUTTLE.key + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            if (!lastCommand.getComplete()) {
                //上一条任务未完成,禁止下发命令
@@ -435,7 +457,7 @@
            NyShuttleHttpCommand endCommand = commands.get(commands.size() - 1);
            if (endCommand.getComplete()) {
                //删除redis
                redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo());
                redisUtil.del(RedisKeyType.SHUTTLE.key + redisCommand.getWrkNo());
                if (!assignCommand.getCharge()) {
                    //对主线程抛出等待确认状态waiting
@@ -473,41 +495,41 @@
            return false;
        }
        if (command.getRequest().getBody().get("requestType").equals("move")) {
            ArrayList<int[]> whiteList = new ArrayList<>();//设置节点的白名单
            if (wrkMast != null && ((wrkMast.getIoType() > 100 && wrkMast.getIoType() < 200) || wrkMast.getIoType() == 11)) {
                //出库任务,不检测首节点
                int[] startArr = NavigatePositionConvert.positionToXY(wrkMast.getSourceLocNo());//开始节点
                whiteList.add(startArr);
            }
            //解锁过路径,只检测下一段路径是否可走(当前路径已经被锁定无需再检测)
            if (isLock) {
                //只检测下一段路径是否可走(当前路径已经被锁定无需再检测)
                //检测路径是否可行走
                if (!checkPath(nextNodes == null ? command.getNodes() : nextNodes, null, whiteList)) {
                    return false;
                }
            }else {
                //检测当前路径和下一段路径
                //检测路径是否可行走
                if (!checkPath(command.getNodes(), nextNodes, whiteList)) {
                    return false;
                }
            }
            //锁定路径,锁定当前路径和下一步路径
            List<NavigateNode> nodes = command.getNodes();
            if (nextNodes != null) {
                nodes.addAll(nextNodes);
            }
            if (nodes != null) {
                boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(shuttleProtocol.getCurrentLocNo()), nodes, true);//所使用的路径进行锁定禁用
                if (!result) {
                    return false;//路径锁定失败
                }
            }
        }
//        if (command.getRequest().getBody().get("requestType").equals("move")) {
//            ArrayList<int[]> whiteList = new ArrayList<>();//设置节点的白名单
//            if (wrkMast != null && ((wrkMast.getIoType() > 100 && wrkMast.getIoType() < 200) || wrkMast.getIoType() == 11)) {
//                //出库任务,不检测首节点
//                int[] startArr = NavigatePositionConvert.positionToXY(wrkMast.getSourceLocNo());//开始节点
//                whiteList.add(startArr);
//            }
//
//            //解锁过路径,只检测下一段路径是否可走(当前路径已经被锁定无需再检测)
//            if (isLock) {
//                //只检测下一段路径是否可走(当前路径已经被锁定无需再检测)
//                //检测路径是否可行走
//                if (!checkPath(nextNodes == null ? command.getNodes() : nextNodes, null, whiteList)) {
//                    return false;
//                }
//            }else {
//                //检测当前路径和下一段路径
//                //检测路径是否可行走
//                if (!checkPath(command.getNodes(), nextNodes, whiteList)) {
//                    return false;
//                }
//            }
//
//            //锁定路径,锁定当前路径和下一步路径
//            List<NavigateNode> nodes = command.getNodes();
//            if (nextNodes != null) {
//                nodes.addAll(nextNodes);
//            }
//            if (nodes != null) {
//                boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(shuttleProtocol.getCurrentLocNo()), nodes, true);//所使用的路径进行锁定禁用
//                if (!result) {
//                    return false;//路径锁定失败
//                }
//            }
//        }
        //可执行命令
        if (!write(command, assignCommand)) {
@@ -529,7 +551,7 @@
        //更新redis数据
        redisCommand.setCommandStep(commandStep);
        //任务数据保存到redis
        redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
        redisUtil.set(RedisKeyType.SHUTTLE.key + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
        return true;
    }
@@ -541,7 +563,7 @@
            return false;
        }
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        Object o = redisUtil.get(RedisKeyType.SHUTTLE.key + wrkNo);
        if (o == null) {
            return false;
        }
@@ -625,7 +647,7 @@
            return false;
        }
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        Object o = redisUtil.get(RedisKeyType.SHUTTLE.key + wrkNo);
        if (o == null) {
            return false;
        }
@@ -711,7 +733,7 @@
//            assignCommand.setCommands(commands);
//            redisCommand.setAssignCommand(assignCommand);
//            //任务数据保存到redis
//            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
//            redisUtil.set(RedisKeyType.SHUTTLE.key + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
//            return false;//当前不可行走,等待下一次执行走新路径
//        }
@@ -778,7 +800,7 @@
                    }
                }
            }
        }else {//跑库位
        } else if (shuttleProtocol.getMoveType() == 1) {//跑库位
            Integer xCurrent = shuttleProtocol.getXCurrent();
            if (xCurrent > shuttleProtocol.getXTarget()) {//当X值大于X目标值,进行归零且Y方向+1
                shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
@@ -802,12 +824,56 @@
            //调度去目标位置
            if (shuttleProtocol.getCurrentLocNo().equals(target.getLocNo())) {
                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);//小车和目标位置一致,跳过
            }else {
            } else {
                boolean result = shuttleDispatchUtils.dispatchShuttle(commonService.getWorkNo(3), target.getLocNo());
                if (result) {//调度成功
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
                }
            }
        } else if (shuttleProtocol.getMoveType() == 2) {//母轨道循环跑
            Integer xCurrent = shuttleProtocol.getXCurrent();
            Integer yCurrent = shuttleProtocol.getYCurrent();
            String locNo = Utils.getLocNo(xCurrent, yCurrent, lev);
            //调度去目标位置
            if (shuttleProtocol.getCurrentLocNo().equals(locNo)) {
                if (yCurrent.equals(shuttleProtocol.getYStart())) {
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYTarget());//小车和目标位置一致,切换库位
                }else {
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());//小车和目标位置一致,切换库位
                }
            } else {
                boolean result = shuttleDispatchUtils.dispatchShuttle(commonService.getWorkNo(3), locNo);
                if (result) {//调度成功
                    if (yCurrent.equals(shuttleProtocol.getYStart())) {
                        shuttleProtocol.setYCurrent(shuttleProtocol.getYTarget());//切换库位
                    }else {
                        shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());//切换库位
                    }
                }
            }
        } else if (shuttleProtocol.getMoveType() == 3) {//子轨道循环跑
            Integer xCurrent = shuttleProtocol.getXCurrent();
            Integer yCurrent = shuttleProtocol.getYCurrent();
            String locNo = Utils.getLocNo(xCurrent, yCurrent, lev);
            //调度去目标位置
            if (shuttleProtocol.getCurrentLocNo().equals(locNo)) {
                if (xCurrent.equals(shuttleProtocol.getXStart())) {
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXTarget());//小车和目标位置一致,切换库位
                }else {
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());//小车和目标位置一致,切换库位
                }
            } else {
                boolean result = shuttleDispatchUtils.dispatchShuttle(commonService.getWorkNo(3), locNo);
                if (result) {//调度成功
                    if (xCurrent.equals(shuttleProtocol.getXStart())) {
                        shuttleProtocol.setXCurrent(shuttleProtocol.getXTarget());//切换库位
                    }else {
                        shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());//切换库位
                    }
                }
            }
        }
    }