#
1
1 天以前 72ce003daa99f4e36466e7fc146e6c14bfc6438f
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -9,14 +9,8 @@
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.BasDevpOpt;
import com.zy.asrs.entity.BasRgvMap;
import com.zy.asrs.entity.BasRgvOpt;
import com.zy.asrs.service.BasDevpOptService;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.BasRgvMapService;
import com.zy.asrs.service.BasRgvOptService;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.common.utils.News;
import com.zy.core.DevpThread;
import com.zy.core.cache.MessageQueue;
@@ -62,7 +56,7 @@
        add(1022);add(1024);
        add(1025);add(1026);add(1028);
        add(1029);add(1031);
        add(1032);add(1033);add(1035);
        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(1050);add(1051);add(1052);add(1053);add(1054);add(1055);add(1056);add(1057);
@@ -89,7 +83,7 @@
//        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(4001);add(4003);add(4004);add(4006);
        add(4001);add(4003);add(4004);add(4006);
    }};
    // 站点偏移量映射表
@@ -127,13 +121,13 @@
    }};
    public static final Map<Integer, Integer> siteErrOffsetMap = new HashMap<Integer, Integer>() {{
        put(1036, 512);put(1103, 476);put(1047, 452);put(2037, 270);
        put(1036, 512);put(1103, 476);put(1047, 452);put(2037, 270);put(4001,294);
    }};
    // 特殊站点集合
    private static final Set<Integer> specialSites = new HashSet<>(
            Arrays.asList(1001,1005,1008,1011,1015,1019,1022,1025,1029, 1032,1039,2004,2001,2007,2010,2016,2013,2019,2022,2025,4001,4004));
            Arrays.asList(1001,1005,1008,1011,1015,1019,1022,1025,1029, 1032,1039,2004,2001,2007,2010,2016,2013,2019,2022,2025,2028,4001,4004));
    // 特殊站点集合
    private static final Set<Integer> specialSites2 = new HashSet<>(
@@ -150,7 +144,7 @@
        add(3); add(4); add(5);
    }};
    public static final ArrayList<Integer> staNosErrList = new ArrayList<Integer>() {{
        add(1103);add(1036);add(1047);
        add(1103);add(1036);add(1047);add(2037);
    }};
    private Integer count=0;
@@ -169,9 +163,9 @@
    public static int getOffsetBySiteId2(int siteId) {
        int offset = 0;
        if (specialSites.contains(siteId)) {
            offset = 72; // 特殊站点占 96
            offset = 72; // 特殊站点占 72
        } else {
            offset = 60; // 普通站点占 72
            offset = 60; // 普通站点占 60
        }
        return offset;
    }
@@ -219,11 +213,61 @@
                throw new CoolException("服务器异常");
        }
    }
    private boolean connectDev = false;
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        connect();
        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 readStatusDev() {
        while (true) {
            try {
                Thread.sleep(50);
                read();
            } catch (Exception e) {
                log.error("Dev数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                initSite();
            }
        }
    }
    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());
                initSite();
//                e.printStackTrace();
            }
        }
    }
    private void writeStatusDev() {
        while (true) {
            try {
                int step = 1;
@@ -234,7 +278,7 @@
                switch (step) {
                    // 读数据
                    case 1:
                        read();
//                        read();
                        break;
                    // 写数据 ID+目标站
                    case 2:
@@ -246,13 +290,55 @@
                }
                // 心跳
//                heartbeat();
                Thread.sleep(100);
                Thread.sleep(400);
            } catch (Exception e) {
                e.printStackTrace();
//                e.printStackTrace();
                try{
                    log.error("dev写线程异常"+e.getMessage());
                } catch (Exception e1){
                }
                log.error("DEV数据写入线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            }
        }
    }
//    @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());
//                        log.error("输送线下发命令:"+((StaProtocol) task.getData()).getWorkNo()+","+((StaProtocol) task.getData()).getStaNo());
//                        break;
//                    default:
//                        break;
//                }
//                // 心跳
////                heartbeat();
//                Thread.sleep(100);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        }
//    }
    /**
     * 初始化站点状态
     */
@@ -319,7 +405,7 @@
        OperateResultExOne<byte[]> result4 = null;//1054-1079
        OperateResultExOne<byte[]> result5 = null;//1080-1110
        OperateResultExOne<byte[]> resultErr = null;
        if(staNoSize == 99){
        if(staNoSize == 100){
            result1 = siemensS7Net.Read("DB100.0", (short) (2768));//1001-1028
            result2 = siemensS7Net.Read("DB101.0", (short) (1268));//1029-1037
            result3 = siemensS7Net.Read("DB104.0", (short) (1268));//1042-1053
@@ -347,7 +433,7 @@
                    station.put(siteId, staProtocol);
                }
                staProtocol.setSiteId(siteId);
                if (staNoSize == 99) {
                if (staNoSize == 100) {
                    if (siteId < 1029) {
                        result = result1;
                    } else if (siteId < 1042) {
@@ -406,18 +492,82 @@
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, offset + offset3, 1);
                staProtocol.setAutoing(!status[0]);  // 自动
                boolean[] status1 = siemensS7Net.getByteTransform().TransBool(result.Content, offset + offset3 - 4, 3);
                staProtocol.setLoading(!status1[0]);  // 有物
                boolean[] statusErr = siemensS7Net.getByteTransform().TransBool(result.Content, offset + offset3 - 8, 3);
                boolean loading = false;
                if(!status1[0])
                {
                    loading = true;
                }
                staProtocol.setLoading(loading);  // 有物
                staProtocol.setInEnable(status1[2]); // 可入
                staProtocol.setOutEnable(status1[2]);// 可出
                staProtocol.setErr(status1[6]);
//                staProtocol.setInEnable(status1[1]); // 可入
//                staProtocol.setOutEnable(status1[3]);// 可出
//                staProtocol.setEmptyMk(status[4]);  // 空板信号
//                staProtocol.setFullPlt(status[5]);  // 满托盘
//                staProtocol.setHigh(status[6]);     // 高库位
//                staProtocol.setLow(status[7]);      // 低库位
                staProtocol.setPassTimeOut(statusErr[0]);
                staProtocol.setObjectNoIdError(statusErr[1]);
                staProtocol.setIdNoObjectError(statusErr[2]);
                staProtocol.setLmfrError(statusErr[3]);
                staProtocol.setLiftError(statusErr[4]);
                staProtocol.setDlfrError(statusErr[5]);
                staProtocol.setDmfrError(statusErr[6]);
                staProtocol.setVfdError(statusErr[7]);
                staProtocol.setSrvError(statusErr[8]);
                staProtocol.setRes1(statusErr[9]);
                staProtocol.setRes2(statusErr[10]);
                staProtocol.setRes3(statusErr[11]);
                staProtocol.setCommunicationTimeOut(statusErr[12]);
                staProtocol.setParameterIncompleteError(statusErr[13]);
                staProtocol.setDataNotClear(statusErr[14]);
                staProtocol.setSetParameterError(statusErr[15]);
                staProtocol.setRes4(statusErr[16]);
                staProtocol.setRes5(statusErr[17]);
                staProtocol.setDataError(statusErr[18]);
                if( (staProtocol.getPassTimeOut() ||
                    staProtocol.getObjectNoIdError() ||
                        staProtocol.getIdNoObjectError() ||
                        staProtocol.getLmfrError() ||
                        staProtocol.getLiftError() ||
                        staProtocol.getDlfrError() ||
                        staProtocol.getDmfrError() ||
                        staProtocol.getVfdError() ||
                        staProtocol.getSrvError() ||
                        staProtocol.getRes1() ||
                        staProtocol.getRes2() ||
                        staProtocol.getRes3() ||
                        staProtocol.getCommunicationTimeOut() ||
                        staProtocol.getParameterIncompleteError() ||
                        staProtocol.getDataNotClear() ||
                        staProtocol.getSetParameterError() ||
                        staProtocol.getDataError()) && staProtocol.isWriteMk()
                ){
                    staProtocol.setErrorMk(true);//若存在异常触发写入
                }
                if(     !staProtocol.getPassTimeOut() &&
                        !staProtocol.getObjectNoIdError() &&
                        !staProtocol.getIdNoObjectError() &&
                        !staProtocol.getLmfrError() &&
                        !staProtocol.getLiftError() &&
                        !staProtocol.getDlfrError() &&
                        !staProtocol.getDmfrError() &&
                        !staProtocol.getVfdError() &&
                        !staProtocol.getSrvError() &&
                        !staProtocol.getRes1() &&
                        !staProtocol.getRes2() &&
                        !staProtocol.getRes3() &&
                        !staProtocol.getCommunicationTimeOut() &&
                        !staProtocol.getParameterIncompleteError() &&
                        !staProtocol.getDataNotClear() &&
                        !staProtocol.getSetParameterError() &&
                        !staProtocol.getDataError()
                ){
                    staProtocol.setWriteMk(true);//无异常后才能继续写入
                    staProtocol.setErrorMk(false);//若存在异常触发写入
                }
                if (!staProtocol.isPakMk() && (!staProtocol.isLoading() || staProtocol.getWorkNo()==0)) {
                    staProtocol.setPakMk(true);
@@ -427,10 +577,12 @@
        //条码扫描器
        ArrayList<Integer> barcodeList = getBarcodeList();
        OperateResultExOne<byte[]> reasultBarcode1 = siemensS7Net.Read("DB101.780", (short) 8);//1036
        OperateResultExOne<byte[]> reasultBarcode2 = siemensS7Net.Read("DB104.548", (short) 8);//1047
        OperateResultExOne<byte[]> reasultBarcode3 = siemensS7Net.Read("DB102.2780", (short) 8);//1103
        OperateResultExOne<byte[]> reasultBarcode4 = siemensS7Net.Read("DB201.344", (short) 8);
        OperateResultExOne<byte[]> reasultBarcode1 = siemensS7Net.Read("DB101.780", (short) 4);//1036
        OperateResultExOne<byte[]> reasultBarcode2 = siemensS7Net.Read("DB104.548", (short) 4);//1047
        OperateResultExOne<byte[]> reasultBarcode3 = siemensS7Net.Read("DB102.2780", (short) 4);//1103
        OperateResultExOne<byte[]> reasultBarcode4 = siemensS7Net.Read("DB201.344", (short) 4);//2037
        OperateResultExOne<byte[]> reasultBarcode5 = siemensS7Net.Read("DB400.112", (short) 4);//4001
        if(barcodeList.get(0) == 0){
            if(reasultBarcode1.IsSuccess){
                Integer barcodeId = barcodeList.get(0);
@@ -459,15 +611,22 @@
                }
            }
        }else{
        }else if(barcodeList.get(0) == 3){
            if(reasultBarcode4.IsSuccess){
                Integer barcodeId = barcodeList.get(0);
                String barcode = String.valueOf(siemensS7Net.getByteTransform().TransInt32(reasultBarcode3.Content, 0));
                String barcode = String.valueOf(siemensS7Net.getByteTransform().TransInt32(reasultBarcode4.Content, 0));
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId + +1);
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
                }
            }
            if(reasultBarcode5.IsSuccess){
                Integer barcodeId = barcodeList.get(1);
                String barcode = String.valueOf(siemensS7Net.getByteTransform().TransInt32(reasultBarcode5.Content, 0));
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId + +1);
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
                }
            }
        }
@@ -482,12 +641,27 @@
                for (Integer siteId : staNos) {
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                }
                    if(staProtocol.isWriteMk() && staProtocol.isErrorMk()){
                        try {
                            // 日志记录异常情况
                            BasDevpErrorLog basDevpErrorLogs = new BasDevpErrorLog();
                            basDevpErrorLogs = staProtocol.toSqlModelError();
                            BasDevpErrorLogService basDevpErrorLogService = SpringUtils.getBean(BasDevpErrorLogService.class);
                            basDevpErrorLogService.insert(basDevpErrorLogs);
                            staProtocol.setWriteMk(false);
                        } catch (Exception ignore) {
                            log.error(ignore.getMessage());
                        }
                    }
                }
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                    throw new Exception("更新数据库数据失败");
                }
            } catch (Exception e) {
                initSite();
                e.printStackTrace();