lty
2025-05-20 2943c767aaaa9b769ee251133fdbd7ebb84c94b7
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -22,15 +22,13 @@
import com.zy.core.enums.SlaveType;
import com.zy.core.model.DevpSlave;
import com.zy.core.model.Task;
import com.zy.core.model.protocol.StaError1;
import com.zy.core.model.protocol.StaProtocol;
import lombok.Data;
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,35 +43,56 @@
    private SiemensS7Net siemensS7Net;
    private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>();
    private short heartBeatVal = 1;
    private StaError1 staError1;
//    public static final ArrayList<Integer> staNos = new ArrayList<Integer>() {{
//
//    }};
    public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{
        add(100);add(101);add(102);add(103);
        add(104);add(105);add(106);add(107);
        add(108);add(109);add(110);add(111);
        add(112);add(113);add(114);add(115);
        add(116);add(117);add(118);add(119);
        add(120);add(121);add(122);add(123);
        add(124);add(125);add(126);add(127);
        add(128);add(129);add(130);add(131);
        add(132);add(133);add(134);add(135);
        add(136);add(137);add(138);add(139);
        add(140);add(141);add(142);add(143);
        add(144);add(145);add(146);add(147);
        add(148);add(149);add(150);add(151);
        add(152);add(153);add(154);add(155);
        add(156);add(157);add(158);add(159);
        add(160);add(161);
        add(1);
//        add(200);add(201);add(202);
        add(121);add(122);add(123);add(124);add(125);
        add(126);
        add(221);add(222);add(223);add(224);add(225);
        add(226);add(227);add(228);add(229);add(230);
        add(231);
        add(411);add(412);add(413);add(414);
    }};
    public static final ArrayList<Integer> BarcodeList = new ArrayList<Integer>() {{
        add(0); add(1); add(2);
    public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{
        add(101);add(102);add(103);
        add(104);add(105);add(106);add(107);
        add(108);add(109);
        add(201);add(202);add(203);
        add(204);add(205);add(206);add(207);
        add(208);add(209);add(210);add(211);
        add(401);add(402);add(403);add(404);
    }};
    public static final ArrayList<Integer> staNosErrList = new ArrayList<Integer>() {{
        add(106); add(107); add(159);
    public static final ArrayList<Integer> BarcodeList1 = new ArrayList<Integer>() {{
        add(1); add(2);add(3);add(4);add(5);
    }};
    public static final ArrayList<Integer> BarcodeList2 = new ArrayList<Integer>() {{
        add(6);add(7);add(8);add(9);
        add(10); add(11); add(12); add(13);
    }};
    public static final ArrayList<Integer> staNosErrList2 = new ArrayList<Integer>() {{
        add(101); add(104);add(106);add(108);
        add(202);add(204);add(207);add(210);
    }};
    public static final ArrayList<Integer> staNosErrList1 = new ArrayList<Integer>() {{
        add(122);
        add(222);add(224);add(227);add(230);
    }};
    public static final ArrayList<Integer> staNoWeight1 = new ArrayList<Integer>() {{
        add(223);add(225);add(228);add(231);
    }};
    public static final ArrayList<Integer> staNoWeight2 = new ArrayList<Integer>() {{
        add(102);add(105);add(107);add(109);
        add(203);add(205);add(208);add(211);
    }};
    private Integer count=0;
@@ -81,7 +100,7 @@
    /**
     * 条码数量
     */
    private int barcodeSize = 6;
    private int barcodeSize = 13;
    /**
     * 入出库模式
@@ -100,12 +119,46 @@
        this.slave = slave;
    }
    private ArrayList<Integer> getStaNoErr() {
        switch (slave.getId()) {
            case 1:
                return staNosErrList1;
            case 2:
                return staNosErrList2;
            default:
                throw new CoolException("服务器异常");
        }
    }
    private ArrayList<Integer> getStaNoWeight() {
        switch (slave.getId()) {
            case 1:
                return staNoWeight1;
            case 2:
                return staNoWeight2;
            default:
                throw new CoolException("服务器异常");
        }
    }
    private ArrayList<Integer> getStaNo() {
        switch (slave.getId()) {
            case 1:
                return staNos1;
            case 2:
                return staNos2;
            default:
                throw new CoolException("服务器异常");
        }
    }
    private ArrayList<Integer> getBarcodeList() {
        switch (slave.getId()) {
            case 1:
                return BarcodeList1;
            case 2:
                return BarcodeList2;
            default:
                throw new CoolException("获取站点失败!");
        }
    }
@@ -135,7 +188,7 @@
                }
                // 心跳
//                heartbeat();
                Thread.sleep(300);
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
@@ -148,7 +201,7 @@
    private void initSite() {
        count ++;
        ArrayList<Integer> staNos = getStaNo();
        if(count > 77) {
        if(count > 48) {
            // 站点编号
            for (Integer siteId : staNos) {
                StaProtocol staProtocol = station.get(siteId);
@@ -193,17 +246,37 @@
        return result;
    }
    public static int getOffsetFromSiteId(int siteId) {
        if (siteId < 100 || siteId > 450) {
            throw new IllegalArgumentException("siteId 超出有效范围: " + siteId);
        }
        return (siteId - 100) * 8;
    }
    public static int getOffsetFromSiteId2(int siteId) {
        if (siteId < 100 || siteId > 450) {
            throw new IllegalArgumentException("siteId 超出有效范围: " + siteId);
        }
        return (siteId - 100) * 6;
    }
    /**
     * 读取状态 ====> 整块plc
     */
    private void read() throws InterruptedException {
//        // 更新入出库模式
//        updateIoMode();
//        if (slave.getId()==1) return;
        ArrayList<Integer> staNos = getStaNo();
        ArrayList<Integer> staNosErrList = getStaNoErr();
        ArrayList<Integer> staNoWeight = getStaNoWeight();
        int staNoSize = staNos.size();
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNoSize*8));
        if (result.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (500 * 8));
        OperateResultExOne<byte[]> resultWeight = siemensS7Net.Read("DB102.0", (short)  (8 * 4));
        if (result.IsSuccess && resultWeight.IsSuccess) {
            int j = 0;
            for(int i = 0 ; i<staNoSize;i++){
                Integer siteId = staNos.get(i); // 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
@@ -211,11 +284,17 @@
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result.Content, i*8));     // 工作号
                boolean[] status = null;
                int offset = getOffsetFromSiteId(siteId);//取余获得对应db块数据
                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i*8 + 4));   // 目标站
                staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result.Content, offset));     // 工作号
                if (staNoWeight.contains(siteId)) {
                    staProtocol.setWeight(siemensS7Net.getByteTransform().TransInt32(resultWeight.Content, j));
                    j = j + 4;
                }
                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, offset+ 4));   // 目标站
                status = siemensS7Net.getByteTransform().TransBool(result.Content, offset+ 6, 2);
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, i*8 + 6, 2);
                staProtocol.setAutoing(status[0]);  // 自动
                staProtocol.setLoading(status[1]);  // 有物
                staProtocol.setInEnable(status[2]); // 可入
@@ -230,139 +309,115 @@
                }
            }
        }
        BasRgvMapService basRgvMapService = SpringUtils.getBean(BasRgvMapService.class);
        //RGV小车1
        Thread.sleep(100);
        OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB50.200",(short)10);
        if (result3.IsSuccess) {
            BasRgvMap basRgvMap = basRgvMapService.selectByRgvNo((int) siemensS7Net.getByteTransform().TransInt16(result3.Content, 0));
            if (!Cools.isEmpty(basRgvMap)){
                Integer siteId = 1;
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setAutoing(true);
                staProtocol.setNearbySta(String.valueOf(siemensS7Net.getByteTransform().TransInt16(result3.Content, 8)));
            }
        }
        //RGV小车2
        Thread.sleep(100);
        OperateResultExOne<byte[]> result4 = siemensS7Net.Read("DB50.212",(short)10);
        if (result4.IsSuccess) {
            BasRgvMap basRgvMap = basRgvMapService.selectByRgvNo((int) siemensS7Net.getByteTransform().TransInt16(result4.Content, 0));
            if (!Cools.isEmpty(basRgvMap)){
                Integer siteId = 2;
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setAutoing(true);
                staProtocol.setNearbySta(String.valueOf(siemensS7Net.getByteTransform().TransInt16(result4.Content, 8)));
            }
        }
        //RGV小车3
        Thread.sleep(100);
        OperateResultExOne<byte[]> result5 = siemensS7Net.Read("DB50.224",(short)10);
        if (result5.IsSuccess) {
            BasRgvMap basRgvMap = basRgvMapService.selectByRgvNo((int) siemensS7Net.getByteTransform().TransInt16(result5.Content, 0));
            if (!Cools.isEmpty(basRgvMap)){
                Integer siteId = 3;
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setAutoing(true);
                staProtocol.setNearbySta(String.valueOf(siemensS7Net.getByteTransform().TransInt16(result5.Content, 8)));
            }
        }
        //条码扫描器
        ArrayList<Integer> barcodeList = BarcodeList;
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.840", (short) (barcodeList.size() * 8));
        if (result2.IsSuccess) {
            for (int i = 0; i < barcodeList.size(); i++) {
                Integer barcodeId = barcodeList.get(i);
                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,8, "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId + 1);
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
            //条码扫描器
            ArrayList<Integer> barcodeList = getBarcodeList();
            OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.2848", (short) (barcodeList.size() * 8));
            if (result2.IsSuccess) {
                for (int i = 0; i < barcodeList.size(); i++) {
                    Integer barcodeId = barcodeList.get(i);
                    String barcode = siemensS7Net.getByteTransform().TransString(result2.Content, i * 8, 8, "UTF-8");
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId);
                    if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                        barcodeThread.setBarcode(barcode);
                    }
                }
            }
        }
        //外形检测
        ArrayList<Integer> staNoErrs = staNosErrList;
        int staNoErrsSize = staNoErrs.size();
        OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.800", (short) (staNoErrsSize*8));
        if (resultErr.IsSuccess){
            for (int i = 0;i<3;i++){
                Integer siteId = staNoErrs.get(i); // 站点编号
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*4+2, 1);
                StaProtocol staProtocol = station.get(siteId);
                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.setBarcodeErr(status[6]);
            }
        }
        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB13.2", (short) (staNoSize*4));
        if (resultErr2.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
                Integer siteId = staNos.get(i); // 站点编号
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*4, 1);
                StaProtocol staProtocol = station.get(siteId);
                staProtocol.setBreakerErr(status[0]);
                staProtocol.setInfraredErr(status[1]);
                staProtocol.setOutTimeErr(status[2]);
                staProtocol.setSeizeSeatErr(status[3]);
                staProtocol.setWrkYgoodsN(status[4]);
                staProtocol.setInverterErr(status[5]);
                staProtocol.setContactErr(status[6]);
                staProtocol.setUpcontactErr(status[7]);
            }
        }
        if (result.IsSuccess) {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
            // 根据实时信息更新数据库
            try {
                List<BasDevp> basDevps = new ArrayList<>();
                for (Integer siteId : staNos) {
            //外形检测
            ArrayList<Integer> staNoErrs = staNosErrList;
            int staNoErrsSize = staNoErrs.size();
            OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.2808", (short) (staNoErrsSize * 4));
            if (resultErr.IsSuccess) {
                for (int i = 0; i < staNoErrsSize; i++) {
                    Integer siteId = staNoErrs.get(i); // 站点编号
                    boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i * 4 + 2, 1);
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                    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.setBarcodeErr(status[6]);
                }
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                    throw new Exception("更新数据库数据失败");
                }
            } catch (Exception e) {
                initSite();
                e.printStackTrace();
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [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"+" - 3"+" - 更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            }
        } 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()));
//        //plc故障
//        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.1728", (short) (staNoSize*4));
//        if (resultErr2.IsSuccess) {
//            for (int i = 0; i < staNoSize; i++) {
//                Integer siteId = staNos.get(i); // 站点编号
//                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*4, 1);
//                StaProtocol staProtocol = station.get(siteId);
//                staProtocol.setBreakerErr(status[0]);
//                staProtocol.setInfraredErr(status[1]);
//                staProtocol.setOutTimeErr(status[2]);
//                staProtocol.setSeizeSeatErr(status[3]);
//                staProtocol.setWrkYgoodsN(status[4]);
//                staProtocol.setInverterErr(status[5]);
//                staProtocol.setContactErr(status[6]);
//                staProtocol.setUpcontactErr(status[7]);
//
//            }
//        }
//        OperateResultExOne<byte[]> resultErr3 = siemensS7Net.Read("DB13.400", (short) 3);
//        if (resultErr3.IsSuccess) {
//            if (staError1 == null){
//                staError1 = new StaError1();
//            }
//            boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr3.Content, 0, 3);
//            staError1.setEMERGENCY_Stop_Core(status[0]);
//            staError1.setEMERGENCY_STOP1(status[1]);
//            staError1.setEMERGENCY_STOP2(status[2]);
//            staError1.setEMERGENCY_STOP3(status[3]);
//            staError1.setEMERGENCY_STOP4(status[4]);
//
//            staError1.setDoor_EMERGENCY_STOP1(status[5]);
//            staError1.setDoor_EMERGENCY_STOP2(status[6]);
//            staError1.setDoor_EMERGENCY_STOP3(status[7]);
//            staError1.setDoor_EMERGENCY_STOP4(status[8]);
//            staError1.setDoor_EMERGENCY_STOP5(status[9]);
//            staError1.setDoor_EMERGENCY_STOP6(status[10]);
//
//            staError1.setDoor_Open_Err1(status[11]);
//            staError1.setDoor_Open_Err1(status[12]);
//            staError1.setDoor_Open_Err1(status[13]);
//            staError1.setDoor_Open_Err1(status[14]);
//            staError1.setDoor_Open_Err1(status[15]);
//            staError1.setDoor_Open_Err1(status[16]);
//
//        }
            if (result.IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId()));
                // 根据实时信息更新数据库
                try {
                    List<BasDevp> basDevps = new ArrayList<>();
                    for (Integer siteId : staNos) {
                        StaProtocol staProtocol = station.get(siteId);
                        basDevps.add(staProtocol.toSqlModel());
                    }
                    BasDevpService basDevpService = SpringContextUtil.getBean(BasDevpService.class);
                    //BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                    if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                        throw new Exception("更新数据库数据失败");
                    }
                } catch (Exception e) {
                    initSite();
                    e.printStackTrace();
                    OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [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" + " - 3" + " - 更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
                }
            } 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());
            }
        }
    }
    /**
     * 写入 ID+目标站 =====> 单站点写入
@@ -372,16 +427,19 @@
            return;
        }
        ArrayList<Integer> staNos = getStaNo();
        int index = staNos.indexOf(staProtocol.getSiteId());
//        int index = staNos.indexOf(staProtocol.getSiteId());
        int offset = getOffsetFromSiteId2(staProtocol.getSiteId());//取余获得对应db块数据
        OperateResult write = null;
        OperateResult write1 = null;
        //任务下发次数
        int writeCount = 0;
        do {
            write = siemensS7Net.Write("DB100." + index*6, staProtocol.getWorkNo());    // 工作号
            write = siemensS7Net.Write("DB100." + offset, staProtocol.getWorkNo());    // 工作号
            Thread.sleep(200);
            write1 = siemensS7Net.Write("DB100." + (index*6+4), staProtocol.getStaNo().shortValue());    // 目标站
            write1 = siemensS7Net.Write("DB100." + (offset+4), staProtocol.getStaNo().shortValue());    // 目标站
            if(write.IsSuccess && write1.IsSuccess){
                log.error("写入输送线命令成功。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                break;
            }
            else {
@@ -397,10 +455,22 @@
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol)));
            News.error("SiemensDevp"+" - 4"+" - 写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol));
        } else {
            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));
        }
//        else {
//            //dang 101,201zhan xaifazhiling qingchu tiaoxingsaomaolide shuju shanchu
//            if (staProtocol.getSiteId()==101){
//                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 1);
//                log.info("qingchutiaoma,{}",barcodeThread.getBarcode());
//                barcodeThread.clearBarcode();
//            }
//            if (staProtocol.getSiteId()==201){
//                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 2);
//                log.info("qingchutiaoma,{}",barcodeThread.getBarcode());
//                barcodeThread.clearBarcode();
//            }
//            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));
//        }
    }
    // 更新入出库模式