#
1
2 天以前 b3280d431ea42f46afe020782b677b11d504be31
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -28,6 +28,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Console;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.*;
@@ -83,12 +84,13 @@
        add(2022);add(2024);
        add(2028);add(2030);
        add(2025);add(2027);
        add(2031);add(2033);
        add(2031);add(2032);add(2033);
        add(2034);add(2035);add(2037);
//        add(2041);add(2042);add(2043);add(2044);
//        add(2045);add(2046);add(2047);add(2048);
//        add(2101);add(2102);add(2103);add(2104);add(2105);add(2106);add(2107);add(2108);add(2109);add(2110);
//        add(2111);add(2112);add(2113);add(2114);add(2115);add(2116);add(2117);add(2118);add(2119);add(2120);
        add(2101);add(2102);add(2103);add(2104);add(2105);add(2106);add(2107);add(2108);add(2109);add(2110);
        add(2111);add(2112);add(2113);add(2114);add(2115);add(2116);add(2117);add(2118);add(2119);add(2120);
        add(2121);add(2122);
        add(4001);add(4003);add(4004);add(4006);
    }};
@@ -120,6 +122,12 @@
        put(2026, 2516);put(2027, 2608);put(2028, 2792);put(2029, 2700);put(2030, 2908);
        put(2031, 464);put(2032, 348);put(2033, 556);put(2034, 92);put(2035, 0);
        put(2037, 256);
        put(2101, 0);put(2102, 92);put(2103, 184);put(2104, 276);put(2105, 368);
        put(2106, 460);put(2107, 552);put(2108, 644);put(2109, 736);put(2110, 828);
        put(2111, 920);put(2112, 1012);put(2113, 1104);put(2114, 1196);put(2115, 1288);
        put(2116, 1380);put(2117, 1472);put(2118, 1564);put(2119, 1656);put(2120, 1748);
        put(2121, 1840);put(2122, 1932);
        put(4001, 0);put(4002, 116);put(4003, 508);put(4004, 392);put(4005, 300);
        put(4006, 208);
@@ -158,17 +166,27 @@
        put(2031, 120);put(2032, 124);put(2033, 128);put(2034, 132);put(2035, 136);
        put(2037, 144);
        put(4001, 240);put(4002, 244);put(4003, 248);put(4004, 252);put(4005, 256);
        put(4006, 260);
        put(2101, 160);put(2102, 164);put(2103, 168);put(2104, 172);put(2105, 176);
        put(2106, 180);put(2107, 184);put(2108, 188);put(2109, 192);put(2110, 196);
        put(2111, 200);put(2112, 204);put(2113, 208);put(2114, 212);put(2115, 216);
        put(2116, 220);put(2117, 224);put(2118, 228);put(2119, 232);put(2120, 236);
        put(2121, 240);put(2122, 244);
        put(4001, 248);put(4002, 252);put(4003, 256);put(4004, 260);put(4005, 264);
        put(4006, 268);
        // 其他站点同理继续加
    }};
    public static final Map<Integer, Integer> siteErrOffsetMap = new HashMap<Integer, Integer>() {{
        put(1036, 512);put(1103, 476);put(1047, 452);put(2037, 270);put(4001,294);
        put(1036, 512);put(1103, 476);put(1047, 452);put(2037, 278);put(4001,302);
    }};
    public static final Map<Integer, Integer> siteAgvOffsetMap = new HashMap<Integer, Integer>() {{
        put(1037, 0);put(1039, 2);put(1041, 4);put(2033, 0);put(2034,2);put(4003,4);put(4006,6);
    }};
    public static final Map<Integer, Integer> site2lBarcodeOffsetMap = new HashMap<Integer, Integer>() {{//2楼pda写入条码
        put(2102, 2280);put(2105, 3048);put(2112, 4840);put(2115, 5608);
    }};
@@ -457,9 +475,14 @@
        OperateResultExOne<byte[]> result3 = null;//1042-1053
        OperateResultExOne<byte[]> result4 = null;//1054-1079
        OperateResultExOne<byte[]> result5 = null;//1080-1110
        OperateResultExOne<byte[]> result6 = null;//2101-2122
        OperateResultExOne<byte[]> resultErr = null;
        OperateResultExOne<byte[]> resultPlc = null;
        OperateResultExOne<byte[]> resultAgv = null;
        OperateResultExOne<byte[]> resultArm5 = null;//2110
        OperateResultExOne<byte[]> resultArm6 = null;//2120
        if(staNoSize == 102){
            result1 = siemensS7Net.Read("DB100.0", (short) (2768));//1001-1028
@@ -474,9 +497,12 @@
            result1 = siemensS7Net.Read("DB200.0", (short) (3000));//2001-2030
            result2 = siemensS7Net.Read("DB201.0", (short) (720));//2031-2037
            result3 = siemensS7Net.Read("DB400.0", (short) (600));//4001-4006
            resultErr = siemensS7Net.Read("DB3.0", (short) (354));
            result6 = siemensS7Net.Read("DB202.0", (short) (2024));//2101-2122
            resultErr = siemensS7Net.Read("DB3.0", (short) (386));
//            resultAgv = siemensS7Net.Read("DB901.0", (short) (7)); //agv取放货请求
//            resultPlc = siemensS7Net.Read("DB902.0", (short) (7)); //输送线确认
            resultArm5 = siemensS7Net.Read("DB202.4328", (short) (256));//2110
            resultArm6 = siemensS7Net.Read("DB202.6888", (short) (256));//2120
        }
@@ -507,9 +533,25 @@
                }else{
                    if (siteId < 2031) {
                        result = result1;
                    } else if (siteId < 4001) {
                    } else if (siteId < 2040) {
                        result = result2;
                    } else {
                    } else if(siteId<4000){
                        if (siteId == 2110) {
                            if (resultArm5.IsSuccess) {
                                staProtocol.setBarcode(parseS7String(resultArm5));
                            } else {
                                staProtocol.setBarcode("");
                            }
                        } else if (siteId == 2120){
                            if (resultArm6.IsSuccess) {
                                staProtocol.setBarcode(parseS7String(resultArm6));
                            } else {
                                staProtocol.setBarcode("");
                            }
                        }
                        result = result6;
                    }
                    else {
                        result = result3;
                    }
                }
@@ -541,11 +583,14 @@
                    }
                    staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result.Content, offset + offset2));     // 工作号
                    staProtocol.setStaNo((short)siemensS7Net.getByteTransform().TransInt32(result.Content, offset + offset2 + 12));   // 目标站
                    staProtocol.setBarcode(String.valueOf(siemensS7Net.getByteTransform().TransInt32(result.Content, offset + offset2 + 16)));  //条码
                    if (staProtocol.getSiteId()!=2110 && staProtocol.getSiteId()!=2120 ){
                        staProtocol.setBarcode(String.valueOf(siemensS7Net.getByteTransform().TransInt32(result.Content, offset + offset2 + 16)));  //条码
                    }
                    if(specialSites2.contains(siteId)){
                        staProtocol.setStaNo((short)siemensS7Net.getByteTransform().TransInt32(result.Content, offset + offset2 + 48));   // 目标站
                        staProtocol.setBarcode(String.valueOf(siemensS7Net.getByteTransform().TransInt32(result.Content, offset + offset2 + 52)));  //条码
                        if (staProtocol.getSiteId()!=2110 && staProtocol.getSiteId()!=2120 ){
                            staProtocol.setBarcode(String.valueOf(siemensS7Net.getByteTransform().TransInt32(result.Content, offset + offset2 + 52)));  //条码
                        }
                    }
                    if(staNosErrList.contains(siteId)){
                        Integer offsetErr = siteErrOffsetMap.get(siteId);
@@ -574,7 +619,10 @@
                        loading = true;
                    }
                    staProtocol.setLoading(loading);  // 有物
                    if(siteId == 2032){
                        staProtocol.setLoading(loading);  // 有物
                    }
                    staProtocol.setInEnable(status1[2]); // 可入
                    staProtocol.setOutEnable(status1[2]);// 可出
                    staProtocol.setErr(status1[6]);
@@ -798,6 +846,9 @@
                for (Integer siteId : staNos) {
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                    if(siteId == 2110){
                        basDevps.add(staProtocol.toSqlModel());
                    }
                    if(staProtocol.isWriteMk() && staProtocol.isErrorMk()){
                        try {
                            // 日志记录异常情况
@@ -892,7 +943,7 @@
                }else{
                    write1 = siemensS7Net.Write("DB200." + (offset + offset2 + 12), staProtocol.getStaNo().intValue());    // 目标站
                }
            }else if(siteId < 4000){
            }else if(siteId < 2121){
                write = siemensS7Net.Write("DB201." + (offset + offset2), staProtocol.getWorkNo());    // 工作号
                Thread.sleep(200);
                if(specialSites2.contains(siteId)){
@@ -900,6 +951,11 @@
                }else{
                    write1 = siemensS7Net.Write("DB201." + (offset + offset2 + 12), staProtocol.getStaNo().intValue());    // 目标站
                }
            } else if(siteId < 4000){
                Integer offsetBarcode = site2lBarcodeOffsetMap.get(siteId);
                write = siemensS7Net.Write("DB202." + offsetBarcode, staProtocol.getBarcode());    // 条码
                write1 = siemensS7Net.Write("DB202." + offsetBarcode, staProtocol.getBarcode());
                Thread.sleep(200);
            }else{
                write = siemensS7Net.Write("DB400." + (offset + offset2), staProtocol.getWorkNo());    // 工作号
                Thread.sleep(200);
@@ -954,6 +1010,168 @@
            News.info("SiemensDevp"+" - 5"+" - 输送线命令下发 [id:{}] >>>>> 命令下发: {}",  slave.getId(), JSON.toJSON(staProtocol));
        }
    }
//    /**
//     * 写入 ID + 目标站 =====> 单站点写入(带读回校验 + 重试机制)
//     */
//    private void write(StaProtocol staProtocol) throws InterruptedException {
//        if (staProtocol == null) {
//            return;
//        }
//
//        Integer siteId = staProtocol.getSiteId();
//        Integer offset = siteOffsetMap.get(siteId);
//        if (offset == null) {
//            log.warn("站点 {} 没有定义偏移量,跳过写入", siteId);
//            return;
//        }
//
//        Integer offset2 = getOffsetBySiteId(siteId);
//        Integer offset3 = getOffsetBySiteId2(siteId); // 虽然本方法主要用不到,但保留以防后续扩展
//
//        // 准备期望写入的值(用于后续比对)
//        final long expectWorkNo = staProtocol.getWorkNo();
//        final long expectStaNo = staProtocol.getStaNo() != null ? staProtocol.getStaNo().intValue() : 0;
//
//        boolean isSpecial2 = specialSites2.contains(siteId);
//        final long expectStaNoOffset = isSpecial2 ? (offset + offset2 + 48) : (offset + offset2 + 12);
//
//        // 确定要操作的 DB 块
//        String dbName;
//        if (siteId < 1029) {
//            dbName = "DB100.";
//        } else if (siteId < 1042) {
//            dbName = "DB101.";
//        } else if (siteId < 1054) {
//            dbName = "DB104.";
//        } else if (siteId < 1080) {
//            dbName = "DB103.";
//        } else if (siteId < 1111) {
//            dbName = "DB102.";
//        } else if (siteId < 2031) {
//            dbName = "DB200.";
//        } else if (siteId < 2121) {
//            dbName = "DB201.";
//        } else if (siteId < 4000) {
//            // 2楼PDA条码站点特殊处理(目前不做读回重试)
//            Integer offsetBarcode = site2lBarcodeOffsetMap.get(siteId);
//            if (offsetBarcode == null) {
//                log.warn("站点 {} 没有定义2楼条码偏移量,跳过条码写入", siteId);
//                return;
//            }
//            OperateResult writeBarcode = siemensS7Net.Write("DB202." + offsetBarcode, staProtocol.getBarcode());
//            if (writeBarcode.IsSuccess) {
//                log.info("2楼PDA条码写入成功 site={}, barcode={}", siteId, staProtocol.getBarcode());
//            } else {
//                log.error("2楼PDA条码写入失败 site={}, barcode={}", siteId, staProtocol.getBarcode());
//            }
//            return;
//        } else {
//            dbName = "DB400.";
//        }
//
//        String addrWorkNo = dbName + (offset + offset2);
//        String addrStaNo = dbName + expectStaNoOffset;
//
//        int maxRetry = 5;
//        int tryCount = 0;
//        boolean success = false;
//
//        while (tryCount < maxRetry && !success) {
//            tryCount++;
//
//            // 1. 尝试写入
//            OperateResult writeWork = siemensS7Net.Write(addrWorkNo, expectWorkNo);
//            writeWork = siemensS7Net.Write("DB100." + (offset + offset2), staProtocol.getWorkNo());    // 工作号
//
//            Thread.sleep(80);
//
//            OperateResult writeSta = siemensS7Net.Write(addrStaNo, expectStaNo);
//            Thread.sleep(120);
//
//            if (!writeWork.IsSuccess || !writeSta.IsSuccess) {
//                log.warn("第{}次写入失败 site={}, workNo={}, targetSta={}",
//                        tryCount, siteId, expectWorkNo, expectStaNo);
//                Thread.sleep(300);
//                continue;
//            }
//
//            // 2. 读回校验(至少读64字节,确保覆盖所需字段)
//            OperateResultExOne<byte[]> readResult = siemensS7Net.Read(dbName + (offset + offset2), (short) 64);
//            if (!readResult.IsSuccess || readResult.Content == null || readResult.Content.length < 64) {
//                log.warn("第{}次读回失败,无法校验 site={}", tryCount, siteId);
//                Thread.sleep(300);
//                continue;
//            }
//
//            byte[] data = readResult.Content;
//
//            // 计算相对偏移进行读取
//            int relativeWorkNoOffset = 0;
//            int relativeStaNoOffset = (int) (expectStaNoOffset - (offset + offset2));
//
//            int actualWorkNo = siemensS7Net.getByteTransform().TransInt32(data, relativeWorkNoOffset);
//            int actualStaNo = siemensS7Net.getByteTransform().TransInt32(data, relativeStaNoOffset);
//
//            boolean match = (actualWorkNo == expectWorkNo) && (actualStaNo == expectStaNo);
//
//            if (match) {
//                success = true;
//                log.info("写入并校验成功 site={}, workNo={}, targetSta={}, 第{}次尝试",
//                        siteId, expectWorkNo, expectStaNo, tryCount);
//            } else {
//                log.warn("第{}次校验失败 site={}, 期望[workNo={},staNo={}], 实际[workNo={},staNo={}]",
//                        tryCount, siteId, expectWorkNo, expectStaNo, actualWorkNo, actualStaNo);
//                Thread.sleep(300);
//            }
//        }
//
//        // 最终结果处理
//        if (success) {
//            log.info("输送线命令下发并校验成功 [plc:{}] site:{} workNo:{} target:{}",
//                    slave.getId(), siteId, expectWorkNo, expectStaNo);
//
//            OutputQueue.DEVP.offer(MessageFormat.format(
//                    "【{0}】输送线命令下发并校验成功 [id:{1}] >>>>> site:{2} workNo:{3} target:{4}",
//                    DateUtils.convert(new Date()), slave.getId(), siteId, expectWorkNo, expectStaNo));
//
//            News.info("SiemensDevp - 5 - 输送线命令下发并校验成功 [id:{}] >>>>> {}",
//                    slave.getId(), JSON.toJSON(staProtocol));
//        } else {
//            log.error("经过{}次尝试,写入并校验仍然失败!plc={} site={} workNo预期={} target预期={}",
//                    maxRetry, slave.getId(), siteId, expectWorkNo, expectStaNo);
//
//            OutputQueue.DEVP.offer(MessageFormat.format(
//                    "【{0}】经过{1}次尝试,输送线写入校验失败! [id:{2}] site:{3} workNo:{4} target:{5}",
//                    DateUtils.convert(new Date()), maxRetry, slave.getId(), siteId, expectWorkNo, expectStaNo));
//
//            News.error("SiemensDevp - 4 - 多次尝试后写入校验失败!site={} workNo={} target={}",
//                    siteId, expectWorkNo, expectStaNo);
//        }
//
//        // 操作日志记录(无论成功失败都记录)
//        try {
//            BasDevpOptService bean = SpringUtils.getBean(BasDevpOptService.class);
//            BasDevpOpt basDevpOpt = new BasDevpOpt(
//                    staProtocol.getWorkNo(),
//                    new Date(),
//                    staProtocol.getSiteId(),
//                    (int) staProtocol.getStaNo(),
//                    new Date(),
//                    String.valueOf(staProtocol.isLoading()),
//                    staProtocol.getBarcode(),
//                    staProtocol.isFrontErr() ? 1L : 0L,
//                    staProtocol.isBackErr() ? 1L : 0L,
//                    staProtocol.isHighErr() ? 1L : 0L,
//                    staProtocol.isLeftErr() ? 1L : 0L,
//                    staProtocol.isRightErr() ? 1L : 0L,
//                    staProtocol.isBarcodeErr() ? 1L : 0L,
//                    staProtocol.getErrCode()
//            );
//            bean.insert(basDevpOpt);
//        } catch (Exception e) {
//            log.warn("写入操作日志失败 site={}", siteId, e);
//        }
//    }
    // 更新入出库模式
    private void updateIoMode() throws InterruptedException {
@@ -1007,6 +1225,39 @@
        }
    }
    private String parseS7String(OperateResultExOne<byte[]> result) {
        if (result == null || !result.IsSuccess || result.Content == null) {
            return "";
        }
        byte[] buf = result.Content;
        if (buf.length < 2) {
            return "";
        }
        // S7 字符串格式:
        // buf[0] = 最大长度
        // buf[1] = 当前真实长度
        int realLen = buf[1] & 0xFF;
        if (realLen <= 0) {
            return "";
        }
        // 防止 PLC 写错长度导致越界
        if (realLen > buf.length - 2) {
            realLen = buf.length - 2;
        }
        String value = siemensS7Net.getByteTransform()
                .TransString(buf, 2, realLen, "UTF-8");
        return value == null ? "" : value.trim();
    }
    /**
     * 设置入库标记
     */