#
Junjie
昨天 713e7cec14fc6ba8220721aae7c01f29ed4e6747
src/main/java/com/zy/core/network/real/ZyCrnV2RealConnect.java
@@ -6,9 +6,12 @@
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.network.api.ZyCrnConnectApi;
@@ -51,7 +54,7 @@
    @Override
    public ZyCrnStatusEntity getStatus() {
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 56);
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 62);
            if (result.IsSuccess) {
                ZyCrnStatusEntity crnStatus = new ZyCrnStatusEntity();
                crnStatus.setCrnNo(deviceConfig.getDeviceNo());
@@ -74,6 +77,43 @@
                crnStatus.setYDistance((double) siemensNet.getByteTransform().TransSingle(result.Content, 44));
                crnStatus.setXDuration((double) siemensNet.getByteTransform().TransSingle(result.Content, 48));
                crnStatus.setYDuration((double) siemensNet.getByteTransform().TransSingle(result.Content, 52));
                crnStatus.setTaskReceive((int) siemensNet.getByteTransform().TransInt16(result.Content, 60));
                if (crnStatus.getStatus() == 0 && crnStatus.getTaskReceive() == 0) {
                    OperateResultExOne<byte[]> taskResult = siemensNet.Read("DB100.0", (short) 20);
                    if (taskResult.IsSuccess) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(taskResult.Content, 2);
                        short taskMode = siemensNet.getByteTransform().TransInt16(taskResult.Content, 4);
                        short sourcePosX = siemensNet.getByteTransform().TransInt16(taskResult.Content, 6);
                        short sourcePosY = siemensNet.getByteTransform().TransInt16(taskResult.Content, 8);
                        short sourcePosZ = siemensNet.getByteTransform().TransInt16(taskResult.Content, 10);
                        short destinationPosX = siemensNet.getByteTransform().TransInt16(taskResult.Content, 12);
                        short destinationPosY = siemensNet.getByteTransform().TransInt16(taskResult.Content, 14);
                        short destinationPosZ = siemensNet.getByteTransform().TransInt16(taskResult.Content, 16);
                        short confirm = siemensNet.getByteTransform().TransInt16(taskResult.Content, 18);
                        if(taskNo != 0 || taskMode != 0 || sourcePosX != 0 || sourcePosY != 0 || sourcePosZ != 0 || destinationPosX != 0 || destinationPosY != 0 || destinationPosZ != 0 || confirm != 0) {
                            short[] array = new short[10];
                            array[0] = (short) 0;
                            array[1] = (short) 0;
                            array[2] = (short) 0;
                            array[3] = (short) 0;
                            array[4] = (short) 0;
                            array[5] = (short) 0;
                            array[6] = (short) 0;
                            array[7] = (short) 0;
                            array[8] = (short) 0;
                            array[9] = (short) 0;
                            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
                            Object lock = redisUtil.get(RedisKeyType.CRN_SEND_COMMAND_LOCK.key + deviceConfig.getDeviceNo());
                            if (lock == null) {
                                OperateResult taskClearResult = siemensNet.Write("DB100.0", array);
                                redisUtil.set(RedisKeyType.CLEAR_CRN_TASK_LIMIT.key + deviceConfig.getDeviceNo(), "lock", 3);
                            }
                        }
                    }
                }
                return crnStatus;
            } else {
@@ -90,6 +130,8 @@
    @Override
    public CommandResponse sendCommand(CrnCommand command) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        redisUtil.set(RedisKeyType.CRN_SEND_COMMAND_LOCK.key + deviceConfig.getDeviceNo(), "lock", 10);
        CommandResponse response = new CommandResponse(false);
        try {
            if (null == command) {
@@ -101,17 +143,21 @@
            int writeAck = 0;
            boolean commandResult = false;
            do {
                OperateResultExOne<byte[]> commandReadResult = siemensNet.Read("DB100.0", (short) 2);
                if (commandReadResult.IsSuccess) {
                    short commandRead = siemensNet.getByteTransform().TransInt16(commandReadResult.Content, 0);
                    if (commandRead == 1) {
                        Thread.sleep(500);
                    }else {
                        commandResult = true;
                        break;
                OperateResult result = siemensNet.Write("DB100.0", (short) 0);
                if (result.IsSuccess) {
                    OperateResultExOne<byte[]> commandReadResult = siemensNet.Read("DB100.0", (short) 2);
                    if (commandReadResult.IsSuccess) {
                        short commandRead = siemensNet.getByteTransform().TransInt16(commandReadResult.Content, 0);
                        if (commandRead == 1) {
                            Thread.sleep(500);
                        }else {
                            commandResult = true;
                            break;
                        }
                    }
                    writeAck++;
                }
                writeAck++;
                Thread.sleep(200);
            } while (writeAck < 5);
            if (!commandResult) {
@@ -120,7 +166,7 @@
            }
            short[] array = new short[9];
            array[0] = (short) 0;
            array[0] = command.getAckFinish().shortValue();
            array[1] = command.getTaskNo().shortValue();
            array[2] = command.getTaskMode().shortValue();
            array[3] = command.getSourcePosX().shortValue();
@@ -130,53 +176,69 @@
            array[7] = command.getDestinationPosY().shortValue();
            array[8] = command.getDestinationPosZ().shortValue();
            OperateResult result = siemensNet.Write("DB100.0", array);
            if (!result.IsSuccess) {
                response.setMessage("堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "任务状态位异常");
                return response;
            }
            int idx = 0;
            boolean commandDataResult = false;
            do {
                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20);
                if (resultRead.IsSuccess) {
                    short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                    short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4);
                    short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6);
                    short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8);
                    short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10);
                    short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12);
                    short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14);
                    short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 16);
                    if (taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) {
                        result = siemensNet.Write("DB100.0", array);
                    } else {
                        break;
                OperateResult result = siemensNet.Write("DB100.0", array);
                if (result.IsSuccess) {
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20);
                    if (resultRead.IsSuccess) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                        short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4);
                        short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6);
                        short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8);
                        short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10);
                        short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12);
                        short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14);
                        short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 16);
                        if (taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) {
                            result = siemensNet.Write("DB100.0", array);
                        } else {
                            commandDataResult = true;
                            break;
                        }
                    }
                }
                idx++;
                Thread.sleep(500);
                Thread.sleep(200);
            } while (idx < 5);
            OperateResult confirmResult = siemensNet.Write("DB100.18", (short) 1);
            if (!confirmResult.IsSuccess) {
            if (!commandDataResult) {
                response.setMessage("堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "堆垛机写入任务数据失败");
                return response;
            }
            int confirmIdx = 0;
            boolean confirmFlag = false;
            do {
                OperateResult confirmResult = siemensNet.Write("DB100.18", (short) 1);
                if (!confirmResult.IsSuccess) {
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.18", (short) 2);
                    if (resultRead.IsSuccess) {
                        short confirm = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                        if(confirm == 0) {
                            Thread.sleep(500);
                        }else {
                            confirmFlag = true;
                            break;
                        }
                    }
                }
                confirmIdx++;
                Thread.sleep(200);
            } while (confirmIdx < 5);
            if(!confirmFlag) {
                News.error("堆垛机写入确认数据失败 ===>> [id:{}]", command.getCrnNo());
                response.setMessage("堆垛机写入确认数据失败");
                return response;
            }
            if (result != null && result.IsSuccess) {
                News.info("堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), JSON.toJSON(command));
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), command.getCrnNo(), JSON.toJSON(command)));
                response.setResult(true);
                response.setMessage("命令下发成功");
            } else {
                News.error("写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}]", DateUtils.convert(new Date()), command.getCrnNo()));
                response.setResult(false);
                response.setMessage("命令下发失败");
            }
            News.info("堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), JSON.toJSON(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), command.getCrnNo(), JSON.toJSON(command)));
            response.setResult(true);
            response.setMessage("命令下发成功");
            redisUtil.set(RedisKeyType.CRN_SEND_COMMAND_LOCK.key + deviceConfig.getDeviceNo(), "lock", 5);
            return response;
        } catch (Exception e) {
            e.printStackTrace();