#ZH
野心家
2025-05-28 8e19e67d1178e834a868f4a820d827ddf64c6e01
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -46,13 +46,17 @@
    private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>();
    private short heartBeatVal = 1;
    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(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);
    }};
    private boolean connectDev = false;
    /**
     * 条码数量
     */
    private int barcodeSize = 4;
    private int barcodeSize = 3;
    /**
     * 入出库模式
@@ -80,36 +84,53 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        connect();
        while (true) {
        connectDev = connect();
        while(!connectDev){
            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());
                        break;
                    default:
                        break;
                }
                // 心跳
//                heartbeat();
                Thread.sleep(400);
            } catch (Exception e) {
                e.printStackTrace();
            }
                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 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());
//                log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("dev", slave.getId(), "rgv连接失败"+e.getMessage());
                } catch (Exception e2){
//                    log.error("e2:"+e2.getMessage());
                }
                initSite();
//                e.printStackTrace();
            }
        }
    }
    /**
     * 初始化站点状态
     */
@@ -159,6 +180,61 @@
        return result;
    }
    private void writeStatusDev() {
        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());
                        break;
                    default:
                        break;
                }
                // 心跳
//                heartbeat();
                Thread.sleep(200);
            } catch (Exception e) {
//                e.printStackTrace();
            }
        }
    }
    private void readStatusDev() {
        while (true) {
            try {
                Thread.sleep(50);
//                System.out.println("读线程"+ slave.getId());
                read();
            } catch (Exception e) {
                log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV数据读取线程异常"+e.getMessage());
                } catch (Exception e2){
//                    log.error("e2:"+e2.getMessage());
                }
                initSite();
//                e.printStackTrace();
            }
        }
    }
    /**
     * 读取状态 ====> 整块plc
     */
@@ -183,13 +259,25 @@
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, i*8 + 6, 2);
                staProtocol.setAutoing(status[0]);  // 自动
//                staProtocol.setAutoing(true);  // 自动
                staProtocol.setLoading(status[1]);  // 有物
//                staProtocol.setLoading(true);  // 有物
                staProtocol.setInEnable(status[2]); // 可入
//                staProtocol.setInEnable(true); // 可入
                staProtocol.setOutEnable(status[3]);// 可出
//                staProtocol.setOutEnable(true);// 可出
                staProtocol.setEmptyMk(status[4]);  // 空板信号
                staProtocol.setFullPlt(status[5]);  // 满托盘
                staProtocol.setHigh(status[6]);     // 高库位
                staProtocol.setLow(status[7]);      // 低库位
                staProtocol.setStaOk(status[8]);      // 就绪
                if (!staProtocol.isPakMkWalk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                    staProtocol.setPakMkWalk(true);
                }
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
@@ -198,17 +286,39 @@
        }
        //条码
        Thread.sleep(200);
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.840.0",(short) (barcodeSize*8));
//        Thread.sleep(50);
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB400.100.0",(short) (barcodeSize*8));
        if (result2.IsSuccess) {
            for (int i = 0; i < barcodeSize; i++) {
                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,8, "UTF-8");
                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,6, "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1);
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
                }
            }
        }
        //条码
//        Thread.sleep(50);
        int[] staW = new int[]{1004,1014,1020};
        OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB400.148.0",(short) (staW.length*4));
        if (result3.IsSuccess) {
            for (int i = 0; i < staW.length; i++) {
                Integer siteId = staW[i]; // 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    station.put(siteId, staProtocol);
                }
                staProtocol.setSiteId(siteId);
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result3.Content, i*4 + 2, 2);
                staProtocol.setFrontErr(status[0]);  // 前超限
                staProtocol.setBackErr(status[1]);  // 后超限
                staProtocol.setHighErr(status[2]);  // 高超限
                staProtocol.setLeftErr(status[3]);  // 左超限
                staProtocol.setRightErr(status[4]); // 右超限
            }
        }
        if (result.IsSuccess) {
@@ -222,19 +332,27 @@
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                }
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                    throw new Exception("更新数据库数据失败");
                try{
                    BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                    if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                        throw new Exception("更新数据库数据失败");
                    }
                } catch (Exception e){
//                    log.error("e:"+e.getMessage());
                }
            } 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());
            }
            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
            deviceErrorService.deleteDeviceError("devp", slave.getId());
            try{
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.deleteDeviceError("devp", slave.getId());
            } catch (Exception e){
//                log.error("e:"+e.getMessage());
            }
        } 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());
@@ -254,26 +372,26 @@
        int index = staNos.indexOf(staProtocol.getSiteId());
        //更新命令日志
        CommandInfoLogService commandInfoLogService = SpringUtils.getBean(CommandInfoLogService.class);
        CommandInfoService commandInfoService = SpringUtils.getBean(CommandInfoService.class);
        CommandInfo commandInfo = commandInfoService.selectById(staProtocol.getCommandId());
        CommandInfoLog commandInfoLog = new CommandInfoLog();
        if (commandInfo != null) {
            commandInfoLog = JSON.parseObject(JSON.toJSONString(commandInfo), CommandInfoLog.class);
            commandInfoLog.setId(null);
        }else {
            Date now = new Date();
            commandInfoLog.setCommand(JSON.toJSONString(staProtocol));
            commandInfoLog.setCommandStatus(3);
            commandInfoLog.setStartTime(now);
            commandInfoLog.setExecuteTime(now);
            commandInfoLog.setCompleteTime(now);
            commandInfoLog.setDevice(SlaveType.Devp.toString());
            commandInfoLog.setWrkNo(9999);
            commandInfoLog.setTaskNo("9999");
            commandInfoLog.setCommandDesc("手动命令");
        }
//        //更新命令日志
//        CommandInfoLogService commandInfoLogService = SpringUtils.getBean(CommandInfoLogService.class);
//        CommandInfoService commandInfoService = SpringUtils.getBean(CommandInfoService.class);
//        CommandInfo commandInfo = commandInfoService.selectById(staProtocol.getCommandId());
//        CommandInfoLog commandInfoLog = new CommandInfoLog();
//        if (commandInfo != null) {
//            commandInfoLog = JSON.parseObject(JSON.toJSONString(commandInfo), CommandInfoLog.class);
//            commandInfoLog.setId(null);
//        }else {
//            Date now = new Date();
//            commandInfoLog.setCommand(JSON.toJSONString(staProtocol));
//            commandInfoLog.setCommandStatus(3);
//            commandInfoLog.setStartTime(now);
//            commandInfoLog.setExecuteTime(now);
//            commandInfoLog.setCompleteTime(now);
//            commandInfoLog.setDevice(SlaveType.Devp.toString());
//            commandInfoLog.setWrkNo(9999);
//            commandInfoLog.setTaskNo("9999");
//            commandInfoLog.setCommandDesc("手动命令");
//        }
        OperateResult writeResult;
        //任务下发次数
@@ -281,15 +399,15 @@
        //任务下发成功标识
        boolean writeFlag = false;
        while(writeCount < 5){
            OperateResult writeResult1 = siemensS7Net.Write("DB100." + index*6, staProtocol.getWorkNo());    // 工作号
            OperateResult writeResult2 = siemensS7Net.Write("DB100." + (index*6+4), staProtocol.getStaNo());    // 目标站
            OperateResult writeResult1 = siemensS7Net.Write("DB101." + index*8, staProtocol.getWorkNo());    // 工作号
            OperateResult writeResult2 = siemensS7Net.Write("DB101." + (index*8+4), staProtocol.getStaNo().shortValue());    // 目标站
            if(writeResult1.IsSuccess && writeResult2.IsSuccess){
                Thread.sleep(200);
                OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB101.0" + index*8, (short)4);
                //更新指令日志
                commandInfoLog.setDeviceLog("指令下发成功");
                commandInfoLogService.insert(commandInfoLog);
//                commandInfoLog.setDeviceLog("指令下发成功");
//                commandInfoLogService.insert(commandInfoLog);
                if(readResult.IsSuccess){
                    Integer workNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0);
@@ -318,8 +436,8 @@
                log.error("写入输送线命令失败。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                //更新指令日志
                commandInfoLog.setDeviceLog("指令下发失败");
                commandInfoLogService.insert(commandInfoLog);
//                commandInfoLog.setDeviceLog("指令下发失败");
//                commandInfoLogService.insert(commandInfoLog);
            }
            Thread.sleep(200);
        }
@@ -394,6 +512,39 @@
        }
    }
    /**
     * 设置入库标记
     */
    @Override
    public void setPakMkWalk(Integer siteId, boolean pakMkWalk) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setPakMkWalk(pakMkWalk);
        }
    }
    /**
     * 设置入库标记
     */
    @Override
    public void setGrossWt(Integer siteId, Double grossWt) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setGrossWt(grossWt);
        }
    }
    /**
     * 设置入库标记
     */
    @Override
    public void setErrorDev(Integer siteId, String msgErr) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setErrorDev(msgErr);
        }
    }
    @Override
    public void close() {
        siemensS7Net.ConnectClose();