cpT
2025-07-01 3c46e80f67fabd8afcc088720a88e0e02cee2049
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -51,6 +51,7 @@
        add(1016);add(1017);add(1018);add(1019);add(1020);add(1021);add(1022);add(1023);
    }};
    private boolean connectDev = false;
    public Long currentTimeMilliConnectDev= 0L;
    /**
@@ -84,6 +85,12 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        initSite();
        try{
            Thread.sleep(2000);
        } catch (Exception e){
        }
        connectDev = connect();
        while(!connectDev){
            try {
@@ -122,7 +129,7 @@
//                log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("dev", slave.getId(), "rgv连接失败"+e.getMessage());
                    deviceErrorService.addDeviceError("devErr", slave.getId(), "rgv连接失败"+e.getMessage());
                } catch (Exception e2){
//                    log.error("e2:"+e2.getMessage());
                }
@@ -172,8 +179,22 @@
        } 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();
@@ -185,6 +206,14 @@
    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) {
@@ -214,16 +243,26 @@
    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) {
                log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                connectDev = false;
                log.error("dev数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV数据读取线程异常"+e.getMessage());
                    deviceErrorService.addDeviceError("devErr", slave.getId(), "dev数据读取线程异常"+e.getMessage());
                } catch (Exception e2){
//                    log.error("e2:"+e2.getMessage());
                }
@@ -282,25 +321,41 @@
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
                if (!staProtocol.isPakMkRun() && !staProtocol.isLoading()) {
                    staProtocol.setPakMkRun(true);
                }
            }
        }
        int[] staW = new int[]{1004,1014,1020};
        //条码
//        Thread.sleep(50);
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB400.100.0",(short) (barcodeSize*8));
        if (result2.IsSuccess) {
            for (int i = 0; i < barcodeSize; i++) {
                Integer siteId = staW[i]; // 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    station.put(siteId, staProtocol);
                }
                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,6, "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1);
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
                    if (!Cools.isEmpty(barcode) && !barcode.equals("000000")){
                        staProtocol.setBarcodeNow(barcode);
                    } else if (Cools.isEmpty(barcode)){
                        staProtocol.setBarcodeNow("-");
                    }
                }
            }
        }
        //条码
//        Thread.sleep(50);
        int[] staW = new int[]{1004,1014,1020};
        OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB400.148.0",(short) (staW.length*4));
        if (result3.IsSuccess) {
@@ -318,7 +373,36 @@
                staProtocol.setHighErr(status[2]);  // 高超限
                staProtocol.setLeftErr(status[3]);  // 左超限
                staProtocol.setRightErr(status[4]); // 右超限
                staProtocol.setWeightErr(status[5]); // 超重
                staProtocol.setBarcodeErr(status[6]); // 扫码失败
            }
        }
        try{
            OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.922.0",(short) (staNoSize*4));
            if (resultErr.IsSuccess){
                for (int i = 0; i < staNoSize; i++) {
                    Integer siteId = staNos.get(i); // 站点编号
                    StaProtocol staProtocol = station.get(siteId);
                    if (null == staProtocol) {
                        staProtocol = new StaProtocol();
                        staProtocol.setSiteId(siteId);
                        station.put(siteId, staProtocol);
                    }
                    staProtocol.setSiteId(siteId);
                    boolean[] statusErr = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*4, 1);
                    staProtocol.setErrOther1(statusErr[0]);  // 电路保护器断开
                    staProtocol.setErrOther2(statusErr[1]);  // 光电异常
                    staProtocol.setErrOther3(statusErr[2]);  // 运行超时
                    staProtocol.setErrOther4(statusErr[3]);  // 占位超时
                    staProtocol.setErrOther5(statusErr[4]);  // 有任务无货故障
                    staProtocol.setErrOther6(statusErr[5]);  // 变频器故障
                    staProtocol.setErrOther7(statusErr[6]);  // 接触器故障
                    staProtocol.setErrOther8(statusErr[7]);  // 顶升电机接触器故障
                }
            }
        } catch (Exception e){
        }
        if (result.IsSuccess) {
@@ -347,17 +431,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状态信息失败");
        }
    }
@@ -371,6 +455,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);
@@ -422,19 +512,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);
@@ -450,7 +554,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次设备状态
@@ -512,6 +620,17 @@
        }
    }
    /**
     * 设置入库标记
     */
    @Override
    public void setPakMkRun(Integer siteId, boolean pakMk) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setPakMkRun(pakMk);
        }
    }
    /**
     * 设置入库标记
     */
@@ -585,4 +704,12 @@
//
//    }
    /**
     * 清除作业启动中
     */
    @Override
    public void setWrkSign() {
    }
}