#ZH
野心家
2025-05-27 1b9f41e12e3ee8ac8bbc388eab7585300bdab75a
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -46,13 +46,15 @@
    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 int barcodeSize = 4;
    private int barcodeSize = 3;
    /**
     * 入出库模式
@@ -183,13 +185,18 @@
                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]);      // 就绪
@@ -206,16 +213,38 @@
        //条码
        Thread.sleep(200);
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.840.0",(short) (barcodeSize*8));
        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(200);
        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) {
@@ -229,19 +258,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());
@@ -261,26 +298,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;
        //任务下发次数
@@ -288,15 +325,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);
@@ -325,8 +362,8 @@
                log.error("写入输送线命令失败。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                //更新指令日志
                commandInfoLog.setDeviceLog("指令下发失败");
                commandInfoLogService.insert(commandInfoLog);
//                commandInfoLog.setDeviceLog("指令下发失败");
//                commandInfoLogService.insert(commandInfoLog);
            }
            Thread.sleep(200);
        }
@@ -412,6 +449,28 @@
        }
    }
    /**
     * 设置入库标记
     */
    @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();