*
L
2025-07-14 4cd0f495beb0cca53a6c49c4e8ccdacad10c6961
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -97,6 +97,9 @@
    public IoModeType ioModeOf4F = IoModeType.NONE;
//    public IoModeType ioMode = IoModeType.NONE;
    private boolean connectDev = false;
    public SiemensDevpThread(DevpSlave slave) {
        this.slave = slave;
    }
@@ -126,7 +129,107 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        connect();
        connectDev = this.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 initSite() {
        try{
            ArrayList<Integer> staNos = getStaNo();
            // 站点编号
            for (Integer siteId : staNos) {
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setSiteId(siteId);
                staProtocol.setWorkNo(0);    // ID
                staProtocol.setAutoing(false);      // 自动
                staProtocol.setLoading(false);      // 有物
                staProtocol.setInEnable(false);     // 可入
                staProtocol.setOutEnable(false);    // 可出
                staProtocol.setEmptyMk(false);      // 空板信号
                staProtocol.setStaNo(0);     // 目标站
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
            }
            // 根据实时信息更新数据库
            try {
                List<BasDevp> basDevps = new ArrayList<>();
                for (Integer siteId : staNos) {
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                }
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                    throw new Exception("更新数据库数据失败");
                }
            } catch (Exception e) {
                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()));
                log.error("更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            }
        } catch (Exception e){
            try{
                log.error(e.getMessage());
            } catch (Exception e1){
            }
            log.error("初始化站点状态异常");
        }
    }
    private void devConnect() {
        while (true) {
            try {
                Thread.sleep(1000L);
                if(!connectDev){
                    try {
                        connectDev = this.connect();
                        Thread.sleep(100L);
                    } catch (Exception e){
                    }
                }
            } catch (Exception e) {
                log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                initSite();
//                e.printStackTrace();
            }
        }
    }
    private void writeStatusDev() {
        while (true) {
            try {
                int step = 1;
@@ -137,14 +240,10 @@
                switch (step) {
                    // 读数据
                    case 1:
                        read();
//                        read();
                        break;
                    // 写数据 ID+目标站
                    case 2:
//                        write3((StaProtocol)task.getData());
//                        Thread.sleep(400);
//                        write4((StaProtocol)task.getData());
//                        Thread.sleep(400);
                        write((StaProtocol)task.getData());
                        break;
                    // 写数据 ID+目标站 码垛完成  给输送线写任务完成
@@ -169,10 +268,44 @@
//                heartbeat();
                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());
            }
        }
    }
    private void readStatusDev() {
        while (true) {
            try {
                if(!connectDev){
                    try {
                        Thread.sleep(1000L);
                    } catch (Exception e){
                    }
                    continue;
                }
                Thread.sleep(200);
                read();
            } catch (Exception e) {
                log.error("RGV读线程异常"+e.getMessage());
                log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                initSite();
//                e.printStackTrace();
            }
        }
    }
    @Override
@@ -249,10 +382,6 @@
                staProtocol.setFullPlt(status[5]);  // 满托盘
                staProtocol.setHigh(status[6]);     // 高库位
                staProtocol.setLow(status[7]);      // 低库位
                if (siteId==118){
                    staProtocol.setAutoing(false);  // 自动
                }
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
@@ -387,6 +516,7 @@
        } 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());
            initSite();
        }
    }
@@ -597,6 +727,17 @@
        }
    }
    /**
     * 设置入库标记
     */
    @Override
    public void setReportSign(Integer siteId, boolean pakMk) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setReportSign(pakMk);
        }
    }
    @Override
    public void close() {
        siemensS7Net.ConnectClose();