cpT
2025-06-30 5aa831dffdfb0ca524362e8d649c28babc681ece
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -50,6 +50,9 @@
        add(1008);add(1009);add(1010);add(1011);add(1012);add(1013);add(1014);add(1015);
        add(1016);add(1017);add(1018);add(1019);add(1020);add(1021);add(1022);add(1023);
    }};
    private boolean connectDev = false;
    public Long currentTimeMilliConnectDev= 0L;
    /**
     * 条码数量
@@ -82,36 +85,59 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        connect();
        while (true) {
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Devp, slave.getId());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
                        read();
                        break;
                    // 写数据 ID+目标站
                    case 2:
                        write((StaProtocol)task.getData());
                        break;
                    default:
                        break;
                }
                // 心跳
//                heartbeat();
                Thread.sleep(400);
            } catch (Exception e) {
                e.printStackTrace();
            }
        initSite();
        try{
            Thread.sleep(2000);
        } catch (Exception e){
        }
        connectDev = connect();
        while(!connectDev){
            try {
                connectDev = this.connect();
                Thread.sleep(100);
            } catch (Exception e){
            }
        }
        // 启动线程自动重连
        new Thread(this::devConnect).start();
        new Thread(this::readStatusDev).start();
        new Thread(this::writeStatusDev).start();
    }
    private void devConnect() {
        while (true) {
            try {
                Thread.sleep(1000);
                if(!connectDev){
                    try {
                        connectDev = this.connect();
                        Thread.sleep(100);
                    } catch (Exception e){
                    }
                }
            } catch (Exception e) {
                log.info("dev连接失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
//                log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("devErr", slave.getId(), "rgv连接失败"+e.getMessage());
                } catch (Exception e2){
//                    log.error("e2:"+e2.getMessage());
                }
                initSite();
//                e.printStackTrace();
            }
        }
    }
    /**
     * 初始化站点状态
     */
@@ -153,12 +179,99 @@
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】输送线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("输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
//            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
//            deviceErrorService.addDeviceError("devp", slave.getId(), "输送线plc连接失败");
            if (System.currentTimeMillis()-currentTimeMilliConnectDev>1000*60*10){
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("devErr", slave.getId(), "输送线plc连接失败");
                } catch (Exception e2){
//                log.error("e2:"+e2.getMessage());
                }
                if (currentTimeMilliConnectDev == 0){
                    currentTimeMilliConnectDev = System.currentTimeMillis()-1000*60*10-1;
                } else {
                    currentTimeMilliConnectDev = System.currentTimeMillis();
                }
            }
            initSite();
        }
        siemensS7Net.ConnectClose();
        return result;
    }
    private void writeStatusDev() {
        while (true) {
            try {
                if(!connectDev){
                    try {
                        Thread.sleep(1000L);
                    } catch (Exception e){
                    }
                    continue;
                }
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Devp, slave.getId());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
//                        read();
                        break;
                    // 写数据 ID+目标站
                    case 2:
                        write((StaProtocol)task.getData());
                        break;
                    default:
                        break;
                }
                // 心跳
//                heartbeat();
                Thread.sleep(200);
            } catch (Exception e) {
//                e.printStackTrace();
            }
        }
    }
    private void readStatusDev() {
        while (true) {
            try {
                if(!connectDev){
                    try {
                        Thread.sleep(1000L);
                    } catch (Exception e){
                    }
                    initSite();
                    continue;
                }
                Thread.sleep(50);
//                System.out.println("读线程"+ slave.getId());
                read();
            } catch (Exception e) {
                connectDev = false;
                log.error("dev数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("devErr", slave.getId(), "dev数据读取线程异常"+e.getMessage());
                } catch (Exception e2){
//                    log.error("e2:"+e2.getMessage());
                }
                initSite();
//                e.printStackTrace();
            }
        }
    }
    /**
@@ -191,7 +304,7 @@
                staProtocol.setInEnable(status[2]); // 可入
//                staProtocol.setInEnable(true); // 可入
                staProtocol.setOutEnable(status[3]);// 可出
                staProtocol.setOutEnable(true);// 可出
//                staProtocol.setOutEnable(true);// 可出
                staProtocol.setEmptyMk(status[4]);  // 空板信号
                staProtocol.setFullPlt(status[5]);  // 满托盘
                staProtocol.setHigh(status[6]);     // 高库位
@@ -212,7 +325,7 @@
        }
        //条码
        Thread.sleep(200);
//        Thread.sleep(50);
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB400.100.0",(short) (barcodeSize*8));
        if (result2.IsSuccess) {
            for (int i = 0; i < barcodeSize; i++) {
@@ -225,7 +338,7 @@
        }
        //条码
        Thread.sleep(200);
//        Thread.sleep(50);
        int[] staW = new int[]{1004,1014,1020};
        OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB400.148.0",(short) (staW.length*4));
@@ -273,17 +386,17 @@
                log.error("更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            }
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.deleteDeviceError("devp", slave.getId());
            } catch (Exception e){
//                log.error("e:"+e.getMessage());
            }
//            try{
//                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
//                deviceErrorService.deleteDeviceError("devp", slave.getId());
//            } catch (Exception e){
////                log.error("e:"+e.getMessage());
//            }
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】读取输送线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("读取输送线plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
            deviceErrorService.addDeviceError("devp", slave.getId(), "读取输送线plc状态信息失败");
            deviceErrorService.addDeviceError("devErr", slave.getId(), "读取输送线plc状态信息失败");
        }
    }
@@ -297,6 +410,12 @@
        ArrayList<Integer> staNos = getStaNo();
        int index = staNos.indexOf(staProtocol.getSiteId());
        try{
            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
            deviceErrorService.addDeviceError("devWrite", staProtocol.getSiteId(), "单站点写入:任务号:"+staProtocol.getWorkNo()+";目标站:"+staProtocol.getStaNo());
        } catch (Exception e2){
        }
//        //更新命令日志
//        CommandInfoLogService commandInfoLogService = SpringUtils.getBean(CommandInfoLogService.class);
@@ -348,19 +467,33 @@
                        OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线命令后返回成功,但是读取任务值不一致。输送线plc编号={1},站点数据={2},写入次数={3}",
                                slave.getId(), JSON.toJSON(staProtocol),writeCount));
                        log.error("写入输送线命令后返回成功,但是读取任务值不一致。输送线plc编号={},{},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("devErr", staProtocol.getSiteId(), "写入次数"+writeCount+"写入后读取任务值不一致:"+"单站点写入:任务号:"+staProtocol.getWorkNo()+";目标站:"+staProtocol.getStaNo());
                        } catch (Exception e2){
                        }
                    }
                } else {
                    writeCount++;
                    OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线命令后读取失败。输送线plc编号={1},站点数据={2},写入次数={3}",
                            slave.getId(), JSON.toJSON(staProtocol), writeCount));
                    log.error("写入输送线命令后读取失败。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                    try{
                        DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                        deviceErrorService.addDeviceError("devErr", staProtocol.getSiteId(), "写入次数"+writeCount+"写入输送线命令后读取失败:"+"单站点写入:任务号:"+staProtocol.getWorkNo()+";目标站:"+staProtocol.getStaNo());
                    } catch (Exception e2){
                    }
                }
            } else {
                writeCount++;
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线命令失败。输送线plc编号={1},站点数据={2},写入次数={3}",
                        slave.getId(), JSON.toJSON(staProtocol),writeCount));
                log.error("写入输送线命令失败。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("devErr", staProtocol.getSiteId(), "写入次数"+writeCount+"写入输送线命令失败:"+"单站点写入:任务号:"+staProtocol.getWorkNo()+";目标站:"+staProtocol.getStaNo());
                } catch (Exception e2){
                }
                //更新指令日志
//                commandInfoLog.setDeviceLog("指令下发失败");
//                commandInfoLogService.insert(commandInfoLog);
@@ -376,7 +509,11 @@
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线命令尝试5次失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线命令尝试5次失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol));
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("devErr", staProtocol.getSiteId(), "写入次数"+writeCount+"写入输送线命令尝试5次失败:"+"单站点写入:任务号:"+staProtocol.getWorkNo()+";目标站:"+staProtocol.getStaNo());
            } catch (Exception e2){
            }
            //重新添加数据到任务队列
            boolean result = MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol));
            read();//读取1次设备状态
@@ -511,4 +648,12 @@
//
//    }
    /**
     * 清除作业启动中
     */
    @Override
    public void setWrkSign() {
    }
}