野心家
2025-05-23 d75bdc98e2bc456eecfa4003bf700cb701d52a9b
src/main/java/com/zy/core/thread/RgvThread.java
@@ -12,8 +12,10 @@
import com.zy.asrs.entity.BasRgvOpt;
import com.zy.asrs.service.BasRgvOptService;
import com.zy.asrs.service.BasRgvService;
import com.zy.asrs.service.DeviceErrorService;
import com.zy.asrs.utils.RouteUtils;
import com.zy.asrs.utils.TrackRangeUtils;
import com.zy.core.DevpThread;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.*;
import com.zy.core.enums.RgvModeType;
@@ -25,6 +27,7 @@
import com.zy.core.model.command.RgvCommand;
import com.zy.core.model.protocol.RgvProtocol;
import com.zy.core.model.protocol.RgvTaskProtocol;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.model.protocol.TaskProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -48,11 +51,11 @@
    //    private RgvProtocol rgvProtocol;
    private TaskProtocolCache taskProtocolCache = new TaskProtocolCache();
    // # 轨道总长
    private Long trackEntireLength = 100L;
    private Long trackEntireLength = 224000L;
    //# 轨道基准点
    private Long trackBenchmark = 100L;
    private Long trackBenchmark = 1L;
    //  # 避让距离
    private Long avoidDistance = 100L;
    private Long avoidDistance = 5000L;
    /**
     * 工位1复位信号
@@ -63,6 +66,9 @@
     */
    private boolean resetFlag2 = false;
    private boolean connectRgv = false;
    private boolean delRgvTask = false;
    public RgvThread(RgvSlave slave) {
        this.slave = slave;
    }
@@ -70,32 +76,73 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        boolean connect = this.connect();
        if (connect) {
        connectRgv = this.connect();
        while(!connectRgv){
            try {
                connectRgv = this.connect();
                Thread.sleep(100);
            } catch (Exception e){
            // 启动读数据线程
            new Thread(this::readStatusRgv).start();
            }
        }
            // 启动任务下发线程
            new Thread(this::taskIssued).start();
        // 启动线程自动重连
        new Thread(this::rgvConnect).start();
            // 启动漫游线程
            new Thread(this::taskWalkIssued).start();
        // 启动读数据线程
        new Thread(this::readStatusRgv).start();
            // 启动任务完成线程
            new Thread(this::taskComplete).start();
        // 启动任务下发线程
        new Thread(this::taskIssued).start();
        // 启动漫游线程
//        new Thread(this::taskWalkIssued).start();
//        new Thread(this::taskWalkIssued2).start();
        // 启动任务完成线程
        new Thread(this::taskComplete).start();
    }
    private void rgvConnect() {
        while (true) {
            try {
                if(!connectRgv){
                    try {
                        connectRgv = this.connect();
                        Thread.sleep(100);
                    } catch (Exception e){
                    }
                }
            } catch (Exception e) {
                log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "rgv连接失败"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
                }
                initRgv();
//                e.printStackTrace();
            }
        }
    }
    private void readStatusRgv() {
        while (true) {
            try {
                Thread.sleep(100);
                Thread.sleep(20);
                readStatus();
            } catch (Exception e) {
                log.error("RGV数据读取线程异常!!!" + e.getMessage());
                log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV数据读取线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
                }
                initRgv();
//                e.printStackTrace();
            }
@@ -110,20 +157,94 @@
    private void taskComplete() {
        while (true) {
            try {
                Thread.sleep(100);
                if (delRgvTask){
                    writeDelRgvTask();
                    delRgvTask = false;
                    continue;
                }
                Thread.sleep(40);
                OperateResultExOne<byte[]> result = siemensNet.Read("DB100.12", (short) 1);
                boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, 0, 1);
                if (status[0]){
                    OperateResult result4 = siemensNet.Write("DB100.12.0", true);
                    OperateResult result4 = siemensNet.Write("DB100.12.0", false);
                }
            } catch (Exception e) {
                log.error("RGV数据读取线程异常!!!" + e.getMessage());
                log.error("RGV数据任务下发复位线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV数据任务下发复位线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
                }
                initRgv();
//                e.printStackTrace();
            }
        }
    }
    /**
     * 任务下发
     */
    private void taskWalkIssued2() {
        while (true) {
            try {
                // 休眠 1 秒
                Thread.sleep(100);
                if (!deviceDetection()) {
                    continue;
                }
                RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                if (rgvProtocol == null || rgvTaskProtocol == null) {
                    initRgv();
                    rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                }
                if (rgvTaskProtocol.getAvoid() != 0) {
                    continue;
                }
                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
                    continue;
                }
                if (rgvProtocol.getLoaded() == -1){
                    continue;
                }
                List<TaskProtocol> allTakeTaskProtocol = taskProtocolCache.getAllWalkTaskProtocol();
                for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
                    if (taskProtocol.getIsRunning() == 1) {//准备下发
                        // 双车
                        if (rgvOtherStatusEnable()) {
                            //另一台车是否允许此台车执行
                            if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
                                continue;
                            }
                        }
                        if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
                                && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                            break;
                        } else {
                            TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                            write(issued);
                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                log.error("RGV任务下发线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV任务下发线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
                }
//                e.printStackTrace();
            }
        }
    }
    /**
@@ -149,6 +270,8 @@
                TaskProtocol issued = new TaskProtocol();
                issued.setTaskNo(32222L);
                issued.setTargetPosition(rgvTaskProtocol.getAvoidingTheDestination());
                issued.setTaskStatus(1);
                issued.setDirection(true);
                write(issued);
                rgvTaskProtocol.setAvoid(0);
@@ -180,44 +303,115 @@
                    rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                }
                if (rgvTaskProtocol.getAvoid() != 0) {
                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
                    continue;
                }
                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
                if (rgvProtocol.getLoaded() == -1){
                    continue;
                }
                if (rgvTaskProtocol.getAvoid() != 0) {
                    if (rgvTaskProtocol.getAvoid() == 1) {
                        TaskProtocol issued = new TaskProtocol();
                        issued.setTaskNo(32222L);
                        issued.setTargetPosition(rgvTaskProtocol.getAvoidingTheDestination());
                        issued.setTaskStatus(1);
                        issued.setDirection(true);
                        write(issued);
                        rgvTaskProtocol.setAvoid(0);
                        Thread.sleep(200);
                        RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
                    }
                    continue;
                }
                List<TaskProtocol> allTakeTaskProtocol = taskProtocolCache.getTakeOrPutTaskProtocol(rgvProtocol.getLoaded());
                for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
                    if (taskProtocol.getIsRunning() == 1) {//准备下发
                        RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
                        //双车
                        if (rgvOtherStatusEnable()) {
                            //另一台车是否允许此台车执行
                            if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
                                continue;
                if (allTakeTaskProtocol.isEmpty()){
                    allTakeTaskProtocol = taskProtocolCache.getAllWalkTaskProtocol();
                    for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
                        if (taskProtocol.getIsRunning() == 1) {//准备下发
                            // 双车
                            if (rgvOtherStatusEnable()) {
                                //另一台车是否允许此台车执行
                                if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
                                    continue;
                                }
                            }
                            if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
                                    && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
                                taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                                break;
                            } else {
                                TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                                write(issued);
                                taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                                break;
                            }
                        }
                        if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
                                && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
                            Thread.sleep(100);
                            TaskProtocol issued = new TaskProtocol(taskProtocol);
                            write(issued);
                            taskProtocol.setIsRunning(4);
                            taskProtocolCache.updateTaskProtocol(taskProtocol);
                            break;
                        } else {
                            Thread.sleep(100);
                            TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                            write(issued);
//                            taskProtocol.setIsRunning(4);
//                            taskProtocolCache.updateTaskProtocol(taskProtocol);
                            break;
                    }
                } else {
                    for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
                        if (taskProtocol.getIsRunning() == 1) {//准备下发
//                        RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
                            if (taskProtocol.getTaskStatus()==3){
                                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, taskProtocol.getTargetPositionStaNoPlcId());
                                StaProtocol staProtocol = devpThread.getStation().get(taskProtocol.getTargetPositionStaNo());
                                if (staProtocol == null ) {
                                    continue;
                                }
                                // 判断是否满足入库条件
                                if (!staProtocol.isAutoing() || staProtocol.isLoading()
                                        || staProtocol.getWorkNo() != 0
                                ){
                                    Thread.sleep(500);
//                                    System.out.println("<放货目标站>" + taskProtocol.getTargetPositionStaNo() + "<非自动或者有物或者存在工作号!!!>");
//                                continue;
                                }
                            }
                            //双车
                            if (rgvOtherStatusEnable()) {
                                //另一台车是否允许此台车执行
                                if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
                                    continue;
                                }
                            }
                            if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
                                    && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
                                if (taskProtocol.getTaskStatus()==3){
                                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, taskProtocol.getTargetPositionStaNoPlcId());
                                    StaProtocol staProtocol = devpThread.getStation().get(taskProtocol.getTargetPositionStaNo());
                                    if (staProtocol == null ) {
                                        continue;
                                    }
                                    // 判断是否满足入库条件
                                    if (!staProtocol.isAutoing() || staProtocol.isLoading()
                                            || staProtocol.getWorkNo() != 0
                                    ){
                                        Thread.sleep(500);
//                                    System.out.println("<放货目标站>" + taskProtocol.getTargetPositionStaNo() + "<非自动或者有物或者存在工作号!!!>");
                                        continue;
                                    }
                                }
                                TaskProtocol issued = new TaskProtocol(taskProtocol);
                                write(issued);
                                taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                                break;
                            } else {
                                TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                                write(issued);
                                break;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                log.error("RGV取货任务下发线程异常!!!" + e.getMessage());
                log.error("RGV任务下发线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV任务下发线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
                }
//                e.printStackTrace();
            }
        }
@@ -274,45 +468,75 @@
            if (rgvProtocolOther.getStatusType().equals(RgvStatusType.IDLE)) {
                if ((rgvProtocolOther.getRgvPos() - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance - 50) {//无需避让
                    return true;
                } else {
                    long avoid = targetPosition + rgvProtocol.getCarBodyKunPeng() + avoidDistance + rgvProtocolOther.getCarBodyJiaoMing();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                        log.error("行走超出范围!!!任务异常  联系管理员!!!");
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
                    if (avoidAbs<51){
                        return true;
                    }
                    rgvTaskProtocolOther.setAvoid(1);
                    rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
                    RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
                    return true;
                    return false;
                }
            } else if (rgvProtocolOther.getStatusType().equals(RgvStatusType.ROAM)) {
                if ((rgvProtocolOther.getRgvPosDestination() - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance - 50) {//无需避让
                    return true;
                } else {
                    long avoid = targetPosition + rgvProtocol.getCarBodyKunPeng() + avoidDistance + rgvProtocolOther.getCarBodyJiaoMing();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                        log.error("行走超出范围!!!任务异常  联系管理员!!!");
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
                    if (avoidAbs<51){
                        return true;
                    }
                    rgvTaskProtocolOther.setAvoid(1);
                    rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
                    RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
                    return true;
                    return false;
                }
            } else if (rgvProtocolOther.getStatusType().equals(RgvStatusType.WORKING)) {
                if ((rgvProtocolOther.getRgvPosDestination() - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance - 50) {//无需避让
                    return true;
                } else {
                    if (rgvProtocolOther.getRgvPosDestination() >= rgvProtocolOther.getRgvPos()) {
                    if (rgvProtocolOther.getRgvPosDestination() >= rgvProtocolOther.getRgvPos()-50) {
                        long avoid = rgvProtocolOther.getRgvPos() - rgvProtocolOther.getCarBodyJiaoMing() - avoidDistance - rgvProtocol.getCarBodyKunPeng();
                        if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                            log.error("行走超出范围!!!任务异常  联系管理员!!!");
                            log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                            try{
                                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                            } catch (Exception e2){
                                log.error("e2:"+e2.getMessage());
                            }
                            return false;
                        }
                        long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
                        if (avoidAbs<51){
                            return false;
                        }
                        rgvTaskProtocol.setAvoid(1);
@@ -321,52 +545,88 @@
                        return false;
                    }
                    return false;
                }
            } else {
                if ((rgvProtocolOther.getRgvPosDestinationOrPos(true) - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance - 50) {//无需避让
                    return true;
                }
            }
        } else {
            if (rgvProtocolOther.getStatusType().equals(RgvStatusType.IDLE)) {
                if ((targetPosition - rgvProtocol.getCarBodyJiaoMing())
                        - (rgvProtocolOther.getRgvPos() + rgvProtocolOther.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance - 50) {//无需避让
                    return true;
                } else {
                    long avoid = targetPosition - rgvProtocol.getCarBodyJiaoMing() - avoidDistance - rgvProtocolOther.getCarBodyKunPeng();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                        log.error("行走超出范围!!!任务异常  联系管理员!!!");
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
                    if (avoidAbs<51){
                        return true;
                    }
                    rgvTaskProtocolOther.setAvoid(1);
                    rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
                    RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
                    return true;
                    return false;
                }
            } else if (rgvProtocolOther.getStatusType().equals(RgvStatusType.ROAM)) {
                if ((targetPosition - rgvProtocol.getCarBodyJiaoMing())
                        - (rgvProtocolOther.getRgvPosDestination() + rgvProtocolOther.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance - 50) {//无需避让
                    return true;
                } else {
                    long avoid = targetPosition - rgvProtocol.getCarBodyJiaoMing() - avoidDistance - rgvProtocolOther.getCarBodyKunPeng();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                        log.error("行走超出范围!!!任务异常  联系管理员!!!");
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
                    if (avoidAbs<51){
                        return true;
                    }
                    rgvTaskProtocolOther.setAvoid(1);
                    rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
                    RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
                    return true;
                    return false;
                }
            } else if (rgvProtocolOther.getStatusType().equals(RgvStatusType.WORKING)) {
                if ((targetPosition - rgvProtocol.getCarBodyJiaoMing())
                        - (rgvProtocolOther.getRgvPosDestination() + rgvProtocolOther.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance-50) {//无需避让
                    return true;
                } else {
                    if (rgvProtocolOther.getRgvPosDestination() <= rgvProtocolOther.getRgvPos()) {
                    if (rgvProtocolOther.getRgvPosDestination() <= rgvProtocolOther.getRgvPos() + 50) {
                        long avoid = rgvProtocolOther.getRgvPos() + rgvProtocolOther.getCarBodyKunPeng() + avoidDistance + rgvProtocol.getCarBodyJiaoMing();
                        if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                            log.error("行走超出范围!!!任务异常  联系管理员!!!");
                            log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                            try{
                                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                            } catch (Exception e2){
                                log.error("e2:"+e2.getMessage());
                            }
                            return false;
                        }
                        long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
                        if (avoidAbs<51){
                            return false;
                        }
                        rgvTaskProtocol.setAvoid(1);
@@ -375,6 +635,12 @@
                        return false;
                    }
                    return false;
                }
            } else {
                if ((rgvProtocolOther.getRgvPosDestinationOrPos(false) - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance - 50) {//无需避让
                    return true;
                }
            }
        }
@@ -409,7 +675,13 @@
                rgvProtocol.setStatusEnable(false);
            }
        } catch (Exception e) {
            log.error("RGV异常!!!" + e.getMessage());
            log.error("RGV异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV异常"+e.getMessage());
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
            }
            rgvProtocol.setStatusEnable(true);
        }
@@ -442,6 +714,12 @@
//            OutputQueue.RGV.offer(MessageFormat.format("【{0}】RGV plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.error("RGV plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGVplc连接失败");
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
            }
        }
        initRgv();
//        siemensNet.ConnectClose();
@@ -453,14 +731,16 @@
     */
    private void readStatus() {
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 19);
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 20);
            if (result.IsSuccess) {
                // 构建设备状态对象
                RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                if (rgvProtocol == null) {
                    rgvProtocol = new RgvProtocol();
                    rgvProtocol.setRgvNo(slave.getId());
                }
                rgvProtocol.setRgvNo(slave.getId());
                rgvProtocol.setCarBodyJiaoMing(slave.getCarBodyJiaoMing());
                rgvProtocol.setCarBodyKunPeng(slave.getCarBodyKunPeng());
                rgvProtocol.setRgvPos((long)siemensNet.getByteTransform().TransInt32(result.Content, 0));
                rgvProtocol.setRgvPosDestination((long)siemensNet.getByteTransform().TransInt32(result.Content, 4));
@@ -478,8 +758,14 @@
//                rgvProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 48));
//                OperateResultExOne<byte[]> result11 = siemensNet.Read("DB100.12", (short) 1);
                boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, 18, 1);
                boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, 18, 2);
                rgvProtocol.setLoaded(status[0]? (short)1:(short)0);
                rgvProtocol.setErr1(status[8]);
                rgvProtocol.setErr2(status[9]);
                rgvProtocol.setErr3(status[10]);
                rgvProtocol.setErr4(status[11]);
                rgvProtocol.setErr5(status[12]);
                rgvProtocol.setErr6(status[13]);
                OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId()));
//                // 工位1复位信号
@@ -508,6 +794,13 @@
                    basRgv.setRgvSts((int) rgvProtocol.getMode());
                    if (!basRgvService.updateById(rgvProtocol.toSqlModel(basRgv))) {
                        log.error("RGV plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV plc数据库更新失败");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
                        }
                    }
                } catch (Exception ignore) {
@@ -519,13 +812,28 @@
            } else {
                initRgv();
                connectRgv = false;
//                OutputQueue.RGV.offer(MessageFormat.format("【{0}】读取RGV plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
                log.error("读取RGV plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "读取RGV plc状态信息失败");
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
//            OutputQueue.RGV.offer(MessageFormat.format("【{0}】读取RGV plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("读取RGV plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "读取RGV plc状态信息失败");
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
            }
            initRgv();
        }
    }
@@ -535,12 +843,21 @@
     */
    private boolean write(TaskProtocol taskProtocol) throws InterruptedException {
        if (null == taskProtocol) {
            log.error("RGV写入命令为空");
            log.error("RGV写入命令为空 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV写入命令为空");
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
            }
            return false;
        }
//        convertRow(command);
//        taskProtocol.setRgvNo(slave.getId());
        Long[] array = new Long[11];
        OperateResult result41 = siemensNet.Write("DB100.12.0", false);
//        array[0] = taskProtocol.getAckFinish1();
//        array[1] = taskProtocol.getTaskNo();
//        array[2] = taskProtocol.getTaskMode();
@@ -585,6 +902,14 @@
            Thread.sleep(200);
            this.readStatus();
            log.info("RGV 命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(taskProtocol));
            log.error("RGV 命令下发 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV 命令下发"+JSON.toJSON(taskProtocol));
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
            }
            OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(taskProtocol)));
            return true;
        } else {
@@ -594,6 +919,53 @@
        }
    }
    private boolean writeDelRgvTask() throws InterruptedException {
//
        OperateResult result = siemensNet.Write("DB100.12.0", false);
        OperateResult result0 = siemensNet.Write("DB100.0", (int) 0);
        OperateResult result1 = siemensNet.Write("DB100.4", (short) 0);
        OperateResult result2 = siemensNet.Write("DB100.6", (int) 0);
        OperateResult result3 = siemensNet.Write("DB100.10", (short) 0); // 执行方向(面朝轨道 定位值左小右大)    true:左   false:右
        try {
            // 日志记录
            BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class);
            BasRgvOpt basRgvOpt = new BasRgvOpt(
                    0,
                    0,
                    slave.getId(),
                    new Date(),
                    String.valueOf(0),
                    null,
                    null,
                    null,
                    result.IsSuccess ? 1 : 0,
                    null,
                    new Date(),
                    null
            );
            bean.insert(basRgvOpt);
        } catch (Exception ignore) {
        }
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
            log.info("RGV 命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON("null"));
            OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON("null")));
            return true;
        } else {
            OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("写入RGV plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
    public void setDelRgvTask() {
        delRgvTask = true;
    }
    @Override
    public void close() {
        siemensNet.ConnectClose();