chen.llin
3 天以前 f82afda0b3256628f362e30b5470f14f4cca0e7e
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,34 @@
    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> staNos1= new ArrayList<Integer>() {{
        add(1001);add(1002);add(1003);add(1004);add(1005);add(1006);add(1007);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);add(1024);add(1701);
    }};
    public static final ArrayList<Integer> staNos2= new ArrayList<Integer>() {{
        add(1025);add(1026);add(1027);add(1028);add(1029);add(1030);add(1031);add(1032);add(1033);
        add(1034);add(1035);add(1036);add(1037);add(1038);add(1039);add(1040);add(1041);add(1042);add(1043);
        add(1044);add(1045);add(1046);add(1047);add(1048);add(1702);
    }};
    public static final ArrayList<Integer> BarcodeList = new ArrayList<Integer>() {{
        add(1); add(2);
    public static final ArrayList<Integer> BarcodeList1 = new ArrayList<Integer>() {{
        add(5);add(6);add(7);add(8);
    }};
    public static final ArrayList<Integer> staNosErrList = new ArrayList<Integer>() {{
        add(101); add(103);
    public static final ArrayList<Integer> BarcodeList2 = new ArrayList<Integer>() {{
        add(1); add(2); add(3); add(4);
    }};
    /**
     * 条码数量
     */
    private int barcodeSize = 2;
    public static final ArrayList<Integer> ErrList1 = new ArrayList<Integer>() {{
        add(1002); add(1004);add(1006); add(1008);
    }};
    public static final ArrayList<Integer> ErrList2 = new ArrayList<Integer>() {{
        add(1041); add(1043);add(1045); add(1047);
    }};
    /**
     * 入出库模式
@@ -76,7 +85,29 @@
    }
    private ArrayList<Integer> getStaNo() {
        return staNos;
        if (slave.getId() == 1){
            return staNos2;
        }else {
            return staNos1;
        }
    }
    private ArrayList<Integer> getBarcodeList() {
        if (slave.getId() == 1){
            return BarcodeList2;
        }else {
            return BarcodeList1;
        }
    }
    private ArrayList<Integer> getErrList() {
        if (slave.getId() == 1){
            return ErrList2;
        }else {
            return ErrList1;
        }
    }
    @Override
@@ -171,7 +202,7 @@
            News.info("SiemensDevp"+" - 1"+" - 输送线plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        } 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()));
            News.error("SiemensDevp"+" - 2"+" - 输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
//            News.error("SiemensDevp"+" - 2"+" - 输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        }
        siemensS7Net.ConnectClose();
        initSite();
@@ -182,11 +213,11 @@
     * 读取状态 ====> 整块plc
     */
    private void read() {
        // 读一楼连续8个站点
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (8*8));
        ArrayList<Integer> staNos = getStaNo();
        // 站点信息
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNos.size()*8));
        if (result.IsSuccess) {
            for (int i = 0; i < 8; i++) {
            for (int i = 0; i < staNos.size(); i++) {
                Integer siteId = staNos.get(i); // 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
@@ -206,47 +237,58 @@
                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;
        }
        ArrayList<Integer> barcodeList = getBarcodeList();
        // 条码扫描器
        OperateResultExOne<byte[]> barcodeResult = siemensS7Net.Read("DB101.1672", (short) ((BarcodeList.size()+1) * 8)); // 不连续
        OperateResultExOne<byte[]> barcodeResult = siemensS7Net.Read("DB101.860", (short) (barcodeList.size() * 8));
        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 < barcodeList.size(); i++) {
                String barcode = siemensS7Net.getByteTransform().TransString(barcodeResult.Content,i*8 ,8, "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeList.get(i));
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
                }
            }
        }
        ArrayList<Integer> errList = getErrList();
        // 外形检测
        OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.1652", (short) ((staNosErrList.size()+1)*4)); // 不连续,多读中间一个
        OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.800", (short) (errList.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<errList.size();i++){
                StaProtocol staProtocol = station.get(errList.get(i));
                short goodsHeight = siemensS7Net.getByteTransform().TransInt16(resultErr.Content, i * 4);
                staProtocol.setGoodsHeight(goodsHeight);
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*4+2, 1);
                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]);
            }
        }
        // 读一楼连续8个站点,故障信息反馈
        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.3312", (short) (8*2));
        // 故障信息反馈
        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.442", (short) (staNos.size() * 4));
        if (resultErr2.IsSuccess) {
            for (int i = 0; i < 8; i++) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*2 + 1, 1);
            for (int i = 0; i < staNos.size(); i++) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*4 + 2, 1);
                StaProtocol staProtocol = station.get(staNos.get(i)); // 站点编号
                staProtocol.setBreakerErr(status[0]);
                staProtocol.setInfraredErr(status[1]);
@@ -296,36 +338,23 @@
        if (null == staProtocol) {
            return;
        }
        ArrayList<Integer> staNos = getStaNo();
        int address = staNos.indexOf(staProtocol.getSiteId()) * 6;
        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());
        }
        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);
@@ -340,6 +369,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();
    }
    /**