#
Junjie
4 天以前 a3d44470b704e81d4f66399bed2f37529ddb31be
src/main/java/com/zy/core/network/real/ZyDualCrnRealConnect.java
@@ -108,145 +108,144 @@
    @Override
    public CommandResponse sendCommand(DualCrnCommand command) {
        CommandResponse response = new CommandResponse(false);
        try {
            if (null == command) {
                News.error("双工位堆垛机写入命令为空");
                response.setMessage("双工位堆垛机写入命令为空");
                return response;
            }
        // try {
        //     if (null == command) {
        //         News.error("双工位堆垛机写入命令为空");
        //         response.setMessage("双工位堆垛机写入命令为空");
        //         return response;
        //     }
            String address = "DB100.0";
            if (command.getStation() == 1) {
                //工位1
                address = "DB100.0";
            }else {
                //工位2
                address = "DB100.20";
            }
        //     String address = "DB100.0";
        //     if (command.getStation() == 1) {
        //         //工位1
        //         address = "DB100.0";
        //     }else {
        //         //工位2
        //         address = "DB100.20";
        //     }
            int writeAck = 0;
            boolean ackResult = false;
            do {
                OperateResult resultAck = siemensNet.Write(address, (short) 0);
                if (resultAck.IsSuccess) {
                    Thread.sleep(200);
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 2);
                    short ack = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                    if (ack != 0) {
                        writeAck++;
                    } else {
                        News.info("双工位堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), "ack复位完成");
                        ackResult = true;
                        break;
                    }
                }
            } while (writeAck < 5);
        //     int writeAck = 0;
        //     boolean ackResult = false;
        //     do {
        //         OperateResult resultAck = siemensNet.Write(address, (short) 0);
        //         if (resultAck.IsSuccess) {
        //             Thread.sleep(200);
        //             OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 2);
        //             short ack = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
        //             if (ack != 0) {
        //                 writeAck++;
        //             } else {
        //                 News.info("双工位堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), "ack复位完成");
        //                 ackResult = true;
        //                 break;
        //             }
        //         }
        //     } while (writeAck < 5);
            if (!ackResult) {
                response.setMessage("双工位堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "ack复位失败");
                return response;
            }
        //     if (!ackResult) {
        //         response.setMessage("双工位堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "ack复位失败");
        //         return response;
        //     }
            short[] array = new short[10];
            array[0] = command.getAckFinish();
            array[1] = command.getTaskNo();
            array[2] = command.getTaskMode();
            array[3] = command.getSourcePosX();
            array[4] = command.getSourcePosY();
            array[5] = command.getSourcePosZ();
            array[6] = command.getDestinationPosX();
            array[7] = command.getDestinationPosY();
            array[8] = command.getDestinationPosZ();
            array[9] = command.getCommand();
        //     short[] array = new short[10];
        //     array[1] = command.getTaskNo();
        //     array[2] = command.getTaskMode();
        //     array[3] = command.getSourcePosX();
        //     array[4] = command.getSourcePosY();
        //     array[5] = command.getSourcePosZ();
        //     array[6] = command.getDestinationPosX();
        //     array[7] = command.getDestinationPosY();
        //     array[8] = command.getDestinationPosZ();
        //     array[9] = command.getCommand();
            OperateResult result = null;
            int idx = 0;
            do {
                OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 20);
                if (resultRead.IsSuccess) {
                    if (command.getAckFinish() == 0) {
                        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(address, array);
                        } else {
                            break;
                        }
                    } else {
                        short ackFinish = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                        if (ackFinish != command.getAckFinish()) {
                            result = siemensNet.Write(address, array);
                        } else {
                            break;
                        }
                    }
                }
                idx++;
                Thread.sleep(500);
            } while (idx < 5);
        //     OperateResult result = null;
        //     int idx = 0;
        //     do {
        //         OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 20);
        //         if (resultRead.IsSuccess) {
        //             if (command.getAckFinish() == 0) {
        //                 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(address, array);
        //                 } else {
        //                     break;
        //                 }
        //             } else {
        //                 short ackFinish = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
        //                 if (ackFinish != command.getAckFinish()) {
        //                     result = siemensNet.Write(address, array);
        //                 } else {
        //                     break;
        //                 }
        //             }
        //         }
        //         idx++;
        //         Thread.sleep(500);
        //     } while (idx < 5);
            if (command.getAckFinish() == 0) {
                short commandFinish = 1;
                int i = 0;
                do {
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 4);
                    OperateResultExOne<byte[]> resultReadConfirm = siemensNet.Read(address + 18, (short) 2);
                    if (resultRead.IsSuccess && resultReadConfirm.IsSuccess) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                        short confirm = siemensNet.getByteTransform().TransInt16(resultReadConfirm.Content, 0);
                        if (taskNo != 0 && confirm == 0) {
                            result = siemensNet.Write(address + 18, commandFinish);
                        }
                    }
                    i++;
                    Thread.sleep(500);
                } while (i < 5);
            }
        //     if (command.getAckFinish() == 0) {
        //         short commandFinish = 1;
        //         int i = 0;
        //         do {
        //             OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 4);
        //             OperateResultExOne<byte[]> resultReadConfirm = siemensNet.Read(address + 18, (short) 2);
        //             if (resultRead.IsSuccess && resultReadConfirm.IsSuccess) {
        //                 short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
        //                 short confirm = siemensNet.getByteTransform().TransInt16(resultReadConfirm.Content, 0);
        //                 if (taskNo != 0 && confirm == 0) {
        //                     result = siemensNet.Write(address + 18, commandFinish);
        //                 }
        //             }
        //             i++;
        //             Thread.sleep(500);
        //         } while (i < 5);
        //     }
            if (result != null && result.IsSuccess) {
                News.info("SiemensDualCrn 双工位堆垛机命令下发[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("SiemensDualCrn 双工位堆垛机写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}]", DateUtils.convert(new Date()), command.getCrnNo()));
                response.setResult(false);
                response.setMessage("命令下发失败");
            }
        //     if (result != null && result.IsSuccess) {
        //         News.info("SiemensDualCrn 双工位堆垛机命令下发[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("SiemensDualCrn 双工位堆垛机写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
        //         OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}]", DateUtils.convert(new Date()), command.getCrnNo()));
        //         response.setResult(false);
        //         response.setMessage("命令下发失败");
        //     }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            String sourceLocNo = Utils.getLocNo(command.getSourcePosX(), command.getSourcePosY(), command.getSourcePosZ());
            String targetLocNo = Utils.getLocNo(command.getDestinationPosX(), command.getDestinationPosY(), command.getDestinationPosZ());
        //     return response;
        // } catch (Exception e) {
        //     e.printStackTrace();
        // }finally {
        //     String sourceLocNo = Utils.getLocNo(command.getSourcePosX(), command.getSourcePosY(), command.getSourcePosZ());
        //     String targetLocNo = Utils.getLocNo(command.getDestinationPosX(), command.getDestinationPosY(), command.getDestinationPosZ());
            // 日志记录
            BasCrnpOptService bean = SpringUtils.getBean(BasCrnpOptService.class);
            BasCrnpOpt basCrnpOpt = new BasCrnpOpt(
                    command.getTaskNo().intValue(),    // 任务号
                    command.getCrnNo(),    // 堆垛机[非空]
                    new Date(),    // 下发时间
                    String.valueOf(command.getTaskMode()),    // 模式
                    sourceLocNo, //源库位
                    targetLocNo, //目标库位
                    null,    // 修改时间
                    null,    // 修改人员
                    null,    // 备注
                    JSON.toJSONString(command),    // 指令
                    JSON.toJSONString(getStatus()),    // 系统状态
                    1,    // 下发状态{0:未下发,1:已下发}
                    JSON.toJSONString(response)    // 响应
            );
            bean.insert(basCrnpOpt);
        }
        //     // 日志记录
        //     BasCrnpOptService bean = SpringUtils.getBean(BasCrnpOptService.class);
        //     BasCrnpOpt basCrnpOpt = new BasCrnpOpt(
        //             command.getTaskNo().intValue(),    // 任务号
        //             command.getCrnNo(),    // 堆垛机[非空]
        //             new Date(),    // 下发时间
        //             String.valueOf(command.getTaskMode()),    // 模式
        //             sourceLocNo, //源库位
        //             targetLocNo, //目标库位
        //             null,    // 修改时间
        //             null,    // 修改人员
        //             null,    // 备注
        //             JSON.toJSONString(command),    // 指令
        //             JSON.toJSONString(getStatus()),    // 系统状态
        //             1,    // 下发状态{0:未下发,1:已下发}
        //             JSON.toJSONString(response)    // 响应
        //     );
        //     bean.insert(basCrnpOpt);
        // }
        return response;
    }
}