|  |  | 
 |  |  | import com.core.common.SpringUtils; | 
 |  |  | import com.zy.asrs.entity.BasCrnOpt; | 
 |  |  | import com.zy.asrs.entity.BasCrnp; | 
 |  |  | import com.zy.asrs.entity.DeviceDataLog; | 
 |  |  | import com.zy.asrs.service.BasCrnOptService; | 
 |  |  | import com.zy.asrs.service.BasCrnpService; | 
 |  |  | import com.zy.asrs.service.DeviceDataLogService; | 
 |  |  | import com.zy.common.utils.News; | 
 |  |  | import com.zy.core.CrnThread; | 
 |  |  | import com.zy.core.cache.MessageQueue; | 
 |  |  | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  |  | 
 |  |  | import java.text.MessageFormat; | 
 |  |  | import java.util.Base64; | 
 |  |  | import java.util.Date; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  |                         command.setDestinationPosY((short)0);     // 目标库位列 | 
 |  |  |                         command.setDestinationPosZ((short)0);     // 目标库位层 | 
 |  |  |                         write(command); | 
 |  |  |                         break; | 
 |  |  |                     case 4://堆垛机移动 | 
 |  |  |                         writeMove((CrnCommand) task.getData()); | 
 |  |  |                         break; | 
 |  |  |                     default: | 
 |  |  |                         break; | 
 |  |  | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > 1000 * 5) { | 
 |  |  |                     //采集时间超过5s,保存一次数据记录 | 
 |  |  |                     //保存数据记录 | 
 |  |  |                     DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class); | 
 |  |  |                     DeviceDataLog deviceDataLog = new DeviceDataLog(); | 
 |  |  |                     deviceDataLog.setOriginData(Base64.getEncoder().encodeToString(result.Content)); | 
 |  |  |                     deviceDataLog.setWcsData(JSON.toJSONString(crnProtocol)); | 
 |  |  |                     deviceDataLog.setType("crn"); | 
 |  |  |                     deviceDataLog.setDeviceNo(crnProtocol.getCrnNo()); | 
 |  |  |                     deviceDataLog.setCreateTime(new Date()); | 
 |  |  |                     deviceDataLogService.insert(deviceDataLog); | 
 |  |  |  | 
 |  |  |                     //更新采集时间 | 
 |  |  |                     crnProtocol.setDeviceDataLog(System.currentTimeMillis()); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 try { | 
 |  |  |                     // 根据实时信息更新数据库 | 
 |  |  |                     BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class); | 
 |  |  | 
 |  |  |         OperateResult result = null; | 
 |  |  |         int idx = 0; | 
 |  |  |         do { | 
 |  |  |             OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 4); | 
 |  |  |             OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20); | 
 |  |  |             if (resultRead.IsSuccess) { | 
 |  |  |                 if (command.getAckFinish() == 0) { | 
 |  |  |                     short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2); | 
 |  |  |                     if(taskNo == 0) { | 
 |  |  |                     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; | 
 |  |  | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 写入数据 | 
 |  |  |      */ | 
 |  |  |     private boolean writeMove(CrnCommand command) throws InterruptedException { | 
 |  |  |         if (null == command) { | 
 |  |  |             News.error("SiemensCrn"+" - 6"+" - 堆垛机写入命令为空"); | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  | //        int writeAck = 0; | 
 |  |  | //        do { | 
 |  |  | //            OperateResult resultAck = siemensNet.Write("DB100.0", (short) 0); | 
 |  |  | //            if (resultAck.IsSuccess){ | 
 |  |  | //                Thread.sleep(200); | 
 |  |  | //                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 2); | 
 |  |  | //                short ack = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); | 
 |  |  | //                if (ack != 0) { | 
 |  |  | //                    writeAck++; | 
 |  |  | //                }else { | 
 |  |  | //                    News.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), "ack复位完成"); | 
 |  |  | //                    break; | 
 |  |  | //                } | 
 |  |  | //            } | 
 |  |  | //        }while (writeAck <5); | 
 |  |  |  | 
 |  |  | //        convertRow(command); | 
 |  |  |         command.setCrnNo(slave.getId()); | 
 |  |  |         short[] array = new short[10]; | 
 |  |  |         array[0] = command.getSourcePosX(); | 
 |  |  |         array[1] = command.getSourcePosY(); | 
 |  |  |         array[2] = command.getSourcePosZ(); | 
 |  |  |         array[3] = command.getTaskMode(); | 
 |  |  |  | 
 |  |  |         OperateResult result = null; | 
 |  |  |         int idx = 0; | 
 |  |  |         do { | 
 |  |  |             OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.26", (short) 2); | 
 |  |  |             if (resultRead.IsSuccess) { | 
 |  |  |                 short posY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); | 
 |  |  |                 if(posY == 0) { | 
 |  |  |                     result = siemensNet.Write("DB100.0", array); | 
 |  |  |                 }else { | 
 |  |  |                     break; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             idx++; | 
 |  |  |             Thread.sleep(500L); | 
 |  |  |         } while (idx < 5); | 
 |  |  |  | 
 |  |  |         if (command.getAckFinish() == 0) { | 
 |  |  |             short commandFinish = 1; | 
 |  |  |             int i = 0; | 
 |  |  |             do { | 
 |  |  |                 OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.26", (short) 2); | 
 |  |  |                 OperateResultExOne<byte[]> resultReadConfirm = siemensNet.Read("DB100.22", (short) 2); | 
 |  |  |                 if (resultRead.IsSuccess && resultReadConfirm.IsSuccess) { | 
 |  |  |                     short posY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); | 
 |  |  |                     short confirm = siemensNet.getByteTransform().TransInt16(resultReadConfirm.Content, 0); | 
 |  |  |                     if(posY != 0 && confirm == 0) { | 
 |  |  |                         result = siemensNet.Write("DB100.22", commandFinish); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 i++; | 
 |  |  |                 Thread.sleep(500L); | 
 |  |  |             } while (i < 5); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         this.crnProtocol.setLastCommandTime(System.currentTimeMillis()); | 
 |  |  |         try { | 
 |  |  |             // 日志记录 | 
 |  |  |             BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class); | 
 |  |  |             BasCrnOpt basCrnOpt = new BasCrnOpt( | 
 |  |  |                     command.getTaskNo().intValue(),    // 任务号 | 
 |  |  |                     command.getCrnNo(),    // 堆垛机[非空] | 
 |  |  |                     new Date(),    // 下发时间 | 
 |  |  |                     command.getTaskModeType().toString(),    // 模式 | 
 |  |  |                     command.getSourcePosX().intValue(),    // 源排 | 
 |  |  |                     command.getSourcePosY().intValue(),    // 源列 | 
 |  |  |                     command.getSourcePosZ().intValue(),    // 源层 | 
 |  |  |                     null,    // 源站 | 
 |  |  |                     command.getDestinationPosX().intValue(),    // 目标排 | 
 |  |  |                     command.getDestinationPosY().intValue(),    // 目标列 | 
 |  |  |                     command.getDestinationPosZ().intValue(),    // 目标层 | 
 |  |  |                     null,    // 目标站 | 
 |  |  |                     null,    // 响应结果 | 
 |  |  |                     null,    // 修改时间 | 
 |  |  |                     null    // 修改人员 | 
 |  |  |             ); | 
 |  |  |             bean.insert(basCrnOpt); | 
 |  |  |         } catch (Exception ignore) {} | 
 |  |  |  | 
 |  |  |         if (result != null && result.IsSuccess) { | 
 |  |  |             Thread.sleep(200); | 
 |  |  |             this.readStatus(); | 
 |  |  |             News.info("SiemensCrn"+" - 7"+" - 堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); | 
 |  |  |             OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); | 
 |  |  |             return true; | 
 |  |  |         } else { | 
 |  |  |             OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); | 
 |  |  |             News.error("SiemensCrn"+" - 8"+" - 写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void close() { |