野心家
2025-05-23 d75bdc98e2bc456eecfa4003bf700cb701d52a9b
src/main/java/com/zy/core/thread/RgvThread.java
@@ -12,6 +12,7 @@
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;
@@ -114,7 +115,13 @@
                    }
                }
            } 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();
            }
@@ -129,7 +136,13 @@
                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();
            }
@@ -156,7 +169,13 @@
                    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();
            }
@@ -216,7 +235,13 @@
                    }
                }
            } 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();
            }
        }
@@ -380,7 +405,13 @@
                    }
                }
            } 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();
            }
        }
@@ -437,12 +468,18 @@
            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());
@@ -452,17 +489,23 @@
                    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());
@@ -472,18 +515,24 @@
                    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()-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());
@@ -500,7 +549,7 @@
            } else {
                if ((rgvProtocolOther.getRgvPosDestinationOrPos(true) - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance - 50) {//无需避让
                    return true;
                }
            }
@@ -508,12 +557,18 @@
            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());
@@ -523,17 +578,23 @@
                    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());
@@ -543,19 +604,25 @@
                    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() + 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());
@@ -572,7 +639,7 @@
            } else {
                if ((rgvProtocolOther.getRgvPosDestinationOrPos(false) - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance) {//无需避让
                        > avoidDistance - 50) {//无需避让
                    return true;
                }
            }
@@ -608,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);
        }
@@ -641,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();
@@ -652,7 +731,7 @@
     */
    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());
@@ -679,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复位信号
@@ -709,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) {
@@ -723,11 +815,25 @@
                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();
        }
    }
@@ -737,7 +843,14 @@
     */
    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);
@@ -789,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 {