pjb
2026-04-27 4b5dc415f417db6cdf1f232228122fc9b4667a0d
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -8,7 +8,6 @@
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.core.DevpThread;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
@@ -26,10 +25,7 @@
import lombok.extern.slf4j.Slf4j;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
@@ -45,21 +41,79 @@
    private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>();
    private short heartBeatVal = 1;
    private StaError1 staError1;
    public static final ArrayList<Integer> staNos = new ArrayList<Integer>() {{
        add(101);add(102);add(103);add(104);add(105);add(106);add(107);add(108);
    }};
    public static final ArrayList<Integer> BarcodeList = new ArrayList<Integer>() {{
        add(1); add(2);
    // 站点
    public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{
        for(int i = 1 ;i <= 21; i ++) {add(1100+i);}
    }};
    public static final ArrayList<Integer> staNosErrList = new ArrayList<Integer>() {{
        add(101); add(103);
    public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{
        for(int i = 1 ;i <= 21; i ++) {add(1200+i);}
    }};
    public static final ArrayList<Integer> staNos3 = new ArrayList<Integer>() {{
        for(int i = 1 ;i <= 21; i ++) {add(1300+i);}
    }};
    public static final ArrayList<Integer> staNos4 = new ArrayList<Integer>() {{
        for(int i = 1 ;i <= 21; i ++) {add(1400+i);}
    }};
    private ArrayList<Integer> getStaNo() {
        switch (this.slave.getId()) {
            case 1: return staNos1;
            case 2: return staNos2;
            case 3: return staNos3;
            case 4: return staNos4;
            default:
        }
        return new ArrayList<>();
    }
    /**
     * 条码数量
     */
    private int barcodeSize = 2;
    // 条码扫描器
    public static final ArrayList<Integer> barcodeList1 = new ArrayList<Integer>(){{
        add(2);add(1);
    }};
    public static final ArrayList<Integer> barcodeList2 = new ArrayList<Integer>(){{
        add(3);
    }};
    public static final ArrayList<Integer> barcodeList3 = new ArrayList<Integer>(){{
        add(4);
    }};
    public static final ArrayList<Integer> barcodeList4 = new ArrayList<Integer>(){{
        add(6);add(5);
    }};
    private ArrayList<Integer> getBarcodeList() {
        switch (this.slave.getId()) {
            case 1: return barcodeList1;
            case 2: return barcodeList2;
            case 3: return barcodeList3;
            case 4: return barcodeList4;
            default:
        }
        return new ArrayList<>();
    }
    // 外形检测
    public static final ArrayList<Integer> shapeDetectionList1 = new ArrayList<Integer>(){{
        add(1112);add(1109);
    }};
    public static final ArrayList<Integer> shapeDetectionList2 = new ArrayList<Integer>(){{
        add(1209);
    }};
    public static final ArrayList<Integer> shapeDetectionList3 = new ArrayList<Integer>(){{
        add(1309);
    }};
    public static final ArrayList<Integer> shapeDetectionList4 = new ArrayList<Integer>(){{
        add(1407);add(1402);
    }};
    private ArrayList<Integer> getShapeDetectionList() {
        switch (this.slave.getId()) {
            case 1: return shapeDetectionList1;
            case 2: return shapeDetectionList2;
            case 3: return shapeDetectionList3;
            case 4: return shapeDetectionList4;
            default:
        }
        return new ArrayList<>();
    }
    /**
     * 入出库模式
@@ -71,12 +125,10 @@
     */
    public IoModeType ioModeOf101 = IoModeType.NONE;
    private int pathStatus  = 0;
    public SiemensDevpThread(DevpSlave slave) {
        this.slave = slave;
    }
    private ArrayList<Integer> getStaNo() {
        return staNos;
    }
    @Override
@@ -138,6 +190,24 @@
                    staProtocol.setPakMk(true);
                }
            }
        // 根据实时信息更新数据库
        try {
            List<BasDevp> basDevps = new ArrayList<>();
            for (Integer siteId2 : staNos) {
                StaProtocol staProtocol2 = station.get(siteId2);
                basDevps.add(staProtocol2.toSqlModel());
            }
            BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
            if(basDevps.size() > 0) {
                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                    throw new Exception("更新数据库数据失败");
                }
            }
        } catch (Exception e){
        }
    }
    @Override
@@ -165,11 +235,11 @@
     */
    private void read() {
        // 读一楼连续8个站点
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (8*8));
        // 站点信息
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (getStaNo().size()*8));
        if (result.IsSuccess) {
            for (int i = 0; i < 8; i++) {
                Integer siteId = staNos.get(i); // 站点编号
            for (int i = 0; i < getStaNo().size(); i++) {
                Integer siteId = getStaNo().get(i); // 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
@@ -188,20 +258,26 @@
                staProtocol.setEmptyMk(status[4]);  // 空板信号
                staProtocol.setFullPlt(status[5]);  // 满托盘
                staProtocol.setHigh(status[6]);     // 高库位
                staProtocol.setLow(status[7]);      // 低库位
                staProtocol.setLow(status[7]);      // 中库位
                staProtocol.setCenter(status[8]);   // 低库位
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
            }
        } else {
            initSite();
            OutputQueue.CRN.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()));
            News.error("SiemensCrn"+" - 4"+" - 读取输送线plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            return;
        }
        // 条码扫描器
        OperateResultExOne<byte[]> barcodeResult = siemensS7Net.Read("DB101.1672", (short) ((BarcodeList.size()+1) * 8)); // 不连续
        OperateResultExOne<byte[]> barcodeResult = siemensS7Net.Read("DB101.600", (short) (getBarcodeList().size() * 10));
        if (barcodeResult.IsSuccess) {
            for (int i = 0; i < BarcodeList.size(); i++) {
                String barcode = siemensS7Net.getByteTransform().TransString(barcodeResult.Content,i*16,8, "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, BarcodeList.get(i));
            for (int i = 0; i < getBarcodeList().size(); i++) {
                String barcode = siemensS7Net.getByteTransform().TransString(barcodeResult.Content,i*10 + 2,8, "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, getBarcodeList().get(i));
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
                }
@@ -209,27 +285,30 @@
        }
        // 外形检测
        OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.1652", (short) ((staNosErrList.size()+1)*4)); // 不连续,多读中间一个
        OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.700", (short) (getShapeDetectionList().size() *6));
        if (resultErr.IsSuccess){
            for (int i = 0;i<staNosErrList.size();i++){
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*8+2, 1);
                StaProtocol staProtocol = station.get(staNosErrList.get(i));
            for (int i = 0;i<getShapeDetectionList().size();i++){
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*6+2, 1);
                StaProtocol staProtocol = station.get(getShapeDetectionList().get(i));
                staProtocol.setFrontErr(status[0]);
                staProtocol.setBackErr(status[1]);
                staProtocol.setHighErr(status[2]);
                staProtocol.setLeftErr(status[3]);
                staProtocol.setRightErr(status[4]);
                staProtocol.setWeightErr(status[5]);
                staProtocol.setGroupSupportErr(status[5]);
                staProtocol.setBarcodeErr(status[6]);
                staProtocol.setWeightErr(status[7]);
                short goodsHeight = siemensS7Net.getByteTransform().TransInt16(resultErr.Content, i * 6 + 4);
                staProtocol.setGoodsHeight(goodsHeight);
            }
        }
        // 读一楼连续8个站点,故障信息反馈
        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.3312", (short) (8*2));
        // 故障信息反馈
        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.400", (short) (getStaNo().size() * 4));
        if (resultErr2.IsSuccess) {
            for (int i = 0; i < 8; i++) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*2 + 1, 1);
                StaProtocol staProtocol = station.get(staNos.get(i)); // 站点编号
            for (int i = 0; i < getStaNo().size(); i++) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*4 + 2, 1);
                StaProtocol staProtocol = station.get(getStaNo().get(i)); // 站点编号
                staProtocol.setBreakerErr(status[0]);
                staProtocol.setInfraredErr(status[1]);
                staProtocol.setOutTimeErr(status[2]);
@@ -249,7 +328,7 @@
            // 根据实时信息更新数据库
            try {
                List<BasDevp> basDevps = new ArrayList<>();
                for (Integer siteId : staNos) {
                for (Integer siteId : getStaNo()) {
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                }
@@ -271,6 +350,13 @@
        }
    }
    public void updatePathStatus(int status) {
        if (this.pathStatus == status) return;
        this.pathStatus = status;
        siemensS7Net.Write("DB100.300", (short) status);
        log.info("更新输送线路径状态成功 ===>> [id:{}] [status: {}]", slave.getId(), status);
    }
    /**
     * 写入 ID+目标站 =====> 单站点写入
     */
@@ -279,35 +365,22 @@
            return;
        }
        int address = -1;
        switch (staProtocol.getSiteId()) {
            case 100: address = 0; break;
            case 101: address = 4; break;
            case 102: address = 8; break;
            case 103: address = 12; break;
            case 104: address = 16; break;
            case 105: address = 20; break;
            case 106: address = 24; break;
            case 107: address = 28; break;
            default:
        }
        if(address == -1) {
            throw new CoolException("写入站点异常:" + staProtocol.getSiteId());
        }
        int address = getStaNo().indexOf(staProtocol.getSiteId()) * 6;
        OperateResult write;
        OperateResult write1;
        //任务下发次数
        int writeCount = 0;
        do {
            write1 = siemensS7Net.Write("DB100." + (address+2), staProtocol.getStaNo());    // 目标站
            write = siemensS7Net.Write("DB100." + address, staProtocol.getWorkNo().shortValue());    // 工作号
            write = siemensS7Net.Write("DB100." + (address), staProtocol.getWorkNo());    // 工作号
            write1 = siemensS7Net.Write("DB100." + (address + 4), staProtocol.getStaNo());    // 目标站
            Thread.sleep(200);
            if(write.IsSuccess && write1.IsSuccess){
                break;
            }
            else {
                writeCount++;
                log.error("写入输送线命令失败。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                News.error("写入输送线命令失败。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
            }
        }while (writeCount<5);
@@ -322,6 +395,7 @@
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol)));
            News.info("SiemensDevp"+" - 5"+" - 输送线命令下发 [id:{}] >>>>> 命令下发: {}",  slave.getId(), JSON.toJSON(staProtocol));
        }
        read();
    }
    /**