| package com.zy.core.thread; | 
|   | 
| import HslCommunication.Core.Types.OperateResult; | 
| import HslCommunication.Core.Types.OperateResultExOne; | 
| import HslCommunication.Profinet.Siemens.SiemensPLCS; | 
| 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.BasRgv; | 
| import com.zy.asrs.entity.BasRgvOpt; | 
| import com.zy.asrs.service.BasRgvOptService; | 
| import com.zy.asrs.service.BasRgvService; | 
| import com.zy.common.utils.News; | 
| import com.zy.core.RgvThread2; | 
| import com.zy.core.ThreadHandler; | 
| import com.zy.core.cache.MessageQueue; | 
| import com.zy.core.cache.OutputQueue; | 
| import com.zy.core.enums.RgvStatusType; | 
| import com.zy.core.enums.RgvTaskModeType; | 
| import com.zy.core.enums.SlaveType; | 
| import com.zy.core.model.RgvSlave; | 
| import com.zy.core.model.Task; | 
| import com.zy.core.model.command.CrnCommand; | 
| import com.zy.core.model.command.RgvCommand; | 
| import com.zy.core.model.protocol.RgvProtocol; | 
| import lombok.Data; | 
| import lombok.extern.slf4j.Slf4j; | 
|   | 
| import java.text.MessageFormat; | 
| import java.util.Date; | 
|   | 
| /** | 
|  * RGV线程 | 
|  * Created by vincent on 2022/11/29 | 
|  */ | 
| @Data | 
| @Slf4j | 
| public class RgvThread implements Runnable, RgvThread2 { | 
|   | 
|     private SiemensS7Net siemensNet; | 
|     private RgvSlave slave; | 
|     private RgvProtocol rgvProtocol; | 
|     /** | 
|      * 工位1复位信号 | 
|      */ | 
|     private boolean resetFlag1 = false; | 
|     /** | 
|      * 工位2复位信号 | 
|      */ | 
|     private boolean resetFlag2 = false; | 
|   | 
|     //锁定标记 | 
|     private boolean PakMk = true; | 
|   | 
|     public RgvThread(RgvSlave slave) { | 
|         this.slave = slave; | 
|     } | 
|   | 
|     @Override | 
|     @SuppressWarnings("InfiniteLoopStatement") | 
|     public void run() { | 
|         this.connect(); | 
|         while (true) { | 
|             try { | 
|                 int step = 1; | 
|                 Task task = MessageQueue.poll(SlaveType.Rgv, slave.getId()); | 
|                 if (task != null) { | 
|                     step = task.getStep(); | 
|                 } | 
|                 switch (step) { | 
|                     // 读数据 | 
|                     case 1: | 
|                         readStatus(); | 
|                         break; | 
| //                    // 工位1、2写入数据 | 
| //                    case 2: | 
| //                        write((RgvCommand) task.getData()); | 
| //                        break; | 
|                     //工位1写入数据 | 
|                     case 4: | 
|                         write1((RgvCommand) task.getData()); | 
|                         break; | 
| //                    //工位2写入数据 | 
| //                    case 5: | 
| //                        write2((RgvCommand) task.getData()); | 
| //                        break; | 
|                     // 复位 | 
|                     case 3: | 
|                         RgvCommand command = (RgvCommand) task.getData(); | 
|                         if (null == command) { | 
|                             command = new RgvCommand(); | 
|                         } | 
|                         command.setRgvNo(slave.getId()); // RGV编号 | 
|                         command.setTaskNo1((short) 0); // 工作号 | 
|                         command.setAckFinish1((short) 1);  // 任务完成确认位 | 
|                         command.setTaskMode1(RgvTaskModeType.NONE); // 任务模式 | 
|                         command.setSourceStaNo1((short)0);     // 源站 | 
|                         command.setDestinationStaNo1((short)0);     // 目标站 | 
|                         command.setCommand((short)0); | 
|                         write1(command); | 
|                         break; | 
|                     // 回原点  避让 | 
|                     case 9: | 
|                         RgvCommand commandAvoidanceXY = (RgvCommand) task.getData(); | 
|                         if (null == commandAvoidanceXY) { | 
|                             commandAvoidanceXY = new RgvCommand(); | 
|                         } | 
|                         commandAvoidanceXY.setRgvNo(slave.getId()); // RGV编号 | 
|                         commandAvoidanceXY.setTaskNo1((short) 9999); // 工作号 | 
|                         commandAvoidanceXY.setAckFinish1((short) 1);  // 任务完成确认位 | 
|                         commandAvoidanceXY.setTaskMode1(RgvTaskModeType.GO_ORIGIN); // 任务模式 | 
|                         commandAvoidanceXY.setSourceStaNo1((short)0);     // 源站 | 
|                         commandAvoidanceXY.setDestinationStaNo1((short)0);     // 目标站 | 
| //                        commandAvoidanceXY.setTaskNo2((short) 0); // 工作号 | 
| //                        commandAvoidanceXY.setAckFinish2((short) 1);  // 任务完成确认位 | 
| //                        commandAvoidanceXY.setTaskMode2(RgvTaskModeType.GO_ORIGIN); // 任务模式 | 
| //                        commandAvoidanceXY.setSourceStaNo2((short)0);     // 源站 | 
| //                        commandAvoidanceXY.setDestinationStaNo2((short)0);     // 目标站 | 
|                         commandAvoidanceXY.setCommand((short)0); | 
|                         write(commandAvoidanceXY); | 
|                         break; | 
|                     default: | 
|                         break; | 
|                 } | 
|                 Thread.sleep(500); | 
|             } catch (Exception e) { | 
| //                e.printStackTrace(); | 
|             } | 
|   | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 初始化RGV状态 | 
|      */ | 
|     private void initRgv() { | 
|         if (null == rgvProtocol) { | 
|             rgvProtocol = new RgvProtocol(); | 
|         } | 
|         rgvProtocol.setMode((short) -1); | 
|         rgvProtocol.setStatus((short)-1); | 
|         rgvProtocol.setTaskNo1((short)0); | 
|         rgvProtocol.setStatus1((short)-1); | 
|         rgvProtocol.setLoaded1((short)0); | 
|         rgvProtocol.setWalkPos((short)0); | 
|         rgvProtocol.setRgvPos((short)0); | 
| //        rgvProtocol.setTaskNo2((short)0); | 
| //        rgvProtocol.setStatus2((short)-1); | 
| //        rgvProtocol.setLoaded2((short)0); | 
|         rgvProtocol.setAlarm((short)0); | 
|         rgvProtocol.setxSpeed((short) 0); | 
|         rgvProtocol.setxDistance((short) 0); | 
|         rgvProtocol.setxDuration((short) 0); | 
|     } | 
|   | 
|     @Override | 
|     public boolean connect() { | 
|         boolean result = false; | 
|         siemensNet = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp()); | 
|         siemensNet.setRack(slave.getRack().byteValue()); | 
|         siemensNet.setSlot(slave.getSlot().byteValue()); | 
|         OperateResult connect = siemensNet.ConnectServer(); | 
|         if(connect.IsSuccess){ | 
|             result = true; | 
|             OutputQueue.RGV.offer(MessageFormat.format( "【{0}】RGV 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.info("RGV plc连接成功 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); | 
|         } else { | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】RGV 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("RGV plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); | 
|             initRgv(); | 
|         } | 
| //        siemensNet.ConnectClose(); | 
|         return result; | 
|     } | 
|   | 
|     /** | 
|      * 读取状态 | 
|      */ | 
|     private void readStatus(){ | 
|         try { | 
|             OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 30); | 
|             if (result.IsSuccess) { | 
|                 if (null == rgvProtocol || rgvProtocol.getRgvNo() == null) { | 
|                     rgvProtocol = new RgvProtocol(); | 
|                     rgvProtocol.setRgvNo(slave.getId()); | 
|                 } | 
|                 rgvProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0)); | 
|                 rgvProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 2)); | 
|                 rgvProtocol.setTaskNo1(siemensNet.getByteTransform().TransInt16(result.Content, 4)); | 
|                 rgvProtocol.setStatus1(siemensNet.getByteTransform().TransInt16(result.Content, 6)); | 
|                 rgvProtocol.setLoaded1(siemensNet.getByteTransform().TransInt16(result.Content, 8)); | 
|                 rgvProtocol.setRgvPos(siemensNet.getByteTransform().TransInt16(result.Content, 10)); | 
|                 rgvProtocol.setWalkPos(siemensNet.getByteTransform().TransInt16(result.Content, 12)); | 
|                 rgvProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 14)); | 
| //                rgvProtocol.setStatus2(siemensNet.getByteTransform().TransInt16(result.Content, 16)); | 
|                 rgvProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 18)); | 
| //                rgvProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 20)); | 
|                 rgvProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 22)); | 
| //                rgvProtocol.setTemp1(siemensNet.getByteTransform().TransInt16(result.Content, 24)); | 
|                 rgvProtocol.setTemp2(siemensNet.getByteTransform().TransInt16(result.Content, 26)); | 
| //                rgvProtocol.setTemp3(siemensNet.getByteTransform().TransInt16(result.Content, 28)); | 
| //                rgvProtocol.setTemp4(siemensNet.getByteTransform().TransInt16(result.Content, 30)); | 
| //                rgvProtocol.setTemp5(siemensNet.getByteTransform().TransInt16(result.Content, 32)); | 
| //                rgvProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 28)); | 
| //                rgvProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 40)); | 
| //                rgvProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 48)); | 
|   | 
|                 OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId())); | 
|   | 
|                 // 工位1复位信号 | 
|                 if (rgvProtocol.getStatusType1().equals(RgvStatusType.WAITING) | 
|                         || rgvProtocol.getStatusType1().equals(RgvStatusType.FETCHWAITING)) { | 
|                     if (resetFlag1) { | 
|                         RgvCommand rgvCommand = new RgvCommand(); | 
|                         rgvCommand.setAckFinish1((short)1); | 
|                         if (write(rgvCommand)) { | 
|                             resetFlag1 = false; | 
|                         } | 
|                     } | 
|                 } | 
|   | 
|                 // 工位2复位信号 | 
| //                if (rgvProtocol.getStatusType2().equals(RgvStatusType.WAITING) | 
| //                        || rgvProtocol.getStatusType2().equals(RgvStatusType.FETCHWAITING)) { | 
| //                    if (resetFlag2) { | 
| //                        RgvCommand rgvCommand = new RgvCommand(); | 
| //                        rgvCommand.setAckFinish2((short)1); | 
| //                        if (write(rgvCommand)) { | 
| //                            resetFlag2 = false; | 
| //                        } | 
| //                    } | 
| //                } | 
|   | 
|                 try { | 
|                     // 根据实时信息更新数据库 | 
|                     BasRgvService basRgvService = SpringUtils.getBean(BasRgvService.class); | 
|                     BasRgv basRgv = new BasRgv(); | 
|                     basRgv.setRgvNo(slave.getId()); | 
|                     basRgv.setRgvSts((int)rgvProtocol.getMode()); | 
|                     if (!basRgvService.updateById(rgvProtocol.toSqlModel(basRgv))){ | 
|                         log.error("RGV plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); | 
|                     } | 
|                 } catch (Exception ignore){ | 
|                     System.out.println(ignore); | 
|                 } | 
|   | 
|             } else { | 
|                 initRgv(); | 
|                 OutputQueue.RGV.offer(MessageFormat.format("【{0}】读取RGV 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("读取RGV plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); | 
|             } | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】读取RGV plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); | 
| //            log.error("读取RGV plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|             initRgv(); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 工位1、2同时写入数据 | 
|      */ | 
|     private boolean write(RgvCommand command) throws InterruptedException { | 
|         if (null == command) { | 
|             log.error("RGV写入命令为空"); | 
|             return false; | 
|         } | 
| //        convertRow(command); | 
|         command.setRgvNo(slave.getId()); | 
|         short[] array = new short[11]; | 
|         array[0] = command.getAckFinish1(); | 
|         array[1] = command.getTaskNo1(); | 
|         array[2] = command.getTaskMode1(); | 
|         array[3] = command.getSourceStaNo1(); | 
|         array[4] = command.getDestinationStaNo1(); | 
| //        array[0] = command.getAckFinish1(); | 
| //        array[1] = command.getTaskNo1(); | 
| //        array[2] = command.getTaskMode1(); | 
| //        array[3] = command.getSourceStaNo1(); | 
| //        array[4] = command.getDestinationStaNo1(); | 
| //        array[5] = command.getAckFinish2(); | 
| //        array[6] = command.getTaskNo2(); | 
| //        array[7] = command.getTaskMode2(); | 
| //        array[8] = command.getSourceStaNo2(); | 
| //        array[9] = command.getDestinationStaNo2(); | 
| //        array[10] = command.getCommand(); | 
|         OperateResult result = siemensNet.Write("DB100.0", array); | 
|   | 
| //        if (command.getAckFinish1() == 0 && command.getAckFinish2() == 0) { | 
| //            short commandFinish = 3;  //工位1、2任务同时写入 | 
| //            Thread.sleep(100L); | 
| //            result = siemensNet.Write("DB100.20", commandFinish); | 
| //        } | 
|   | 
|         try { | 
|             // 日志记录 | 
|             BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); | 
|             BasRgvOpt basRgvOpt = new BasRgvOpt( | 
|                     command.getTaskNo1().intValue(), | 
|                     command.getTaskNo2().intValue(), | 
|                     command.getRgvNo(), | 
|                     new Date(), | 
|                     command.getTaskModeType1().toString(), | 
|                     command.getSourceStaNo1().intValue(), | 
|                     command.getDestinationStaNo1().intValue(), | 
|                     command.getSourceStaNo2().intValue(), | 
|                     command.getDestinationStaNo2().intValue(), | 
|                     null, | 
|                     new Date(), | 
|                     null | 
|             ); | 
|             bean.insert(basRgvOpt); | 
|         } catch (Exception ignore) {} | 
|   | 
|         if (result != null && result.IsSuccess) { | 
|             Thread.sleep(200); | 
|             this.readStatus(); | 
|             log.info("RGV 命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); | 
|             return true; | 
|         } else { | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); | 
|             log.error("写入RGV plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|             return false; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 工位1写入数据 | 
|      */ | 
|     private boolean write1(RgvCommand command) throws InterruptedException { | 
|         if (null == command) { | 
|             log.error("RGV写入命令为空"); | 
|             return false; | 
|         } | 
|   | 
|         OperateResultExOne<byte[]> result3 = siemensNet.Read("DB100.0", (short) 12); | 
|         OperateResultExOne<byte[]> result4 = siemensNet.Read("DB100.10", (short) 2); | 
|         if (result3.IsSuccess){ | 
|             RgvCommand one = new RgvCommand(); | 
| //                one.setAckFinish1(siemensNet.getByteTransform().TransInt16(resultRead.Content, 0)); | 
|             one.setTaskNo1(siemensNet.getByteTransform().TransInt16(result3.Content, 2)); | 
|             one.setTaskMode1(siemensNet.getByteTransform().TransInt16(result3.Content, 4)); | 
|             one.setSourceStaNo1(siemensNet.getByteTransform().TransInt16(result3.Content, 6)); | 
|             one.setDestinationStaNo1(siemensNet.getByteTransform().TransInt16(result3.Content, 8)); | 
|             one.setCommand(siemensNet.getByteTransform().TransInt16(result4.Content, 0)); | 
|   | 
|             News.error("RGV命令下发前读取状态[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one)); | 
|         } | 
|   | 
|   | 
|         OperateResult result8 = siemensNet.Write("DB100.10", (short) 0); | 
|         if (result8.IsSuccess){ | 
|             News.error("下发前把车子确认位置为0"); | 
|         } | 
|   | 
| //        siemensNet.Write("DB100.20", command.getCommand()); | 
|   | 
|         command.setRgvNo(slave.getId()); | 
|         short[] array = new short[5]; | 
|         array[0] = command.getAckFinish1(); | 
|         array[1] = command.getTaskNo1(); | 
|         array[2] = command.getTaskMode1(); | 
|         array[3] = command.getSourceStaNo1(); | 
|         array[4] = command.getDestinationStaNo1(); | 
| //        siemensNet.Write("DB100.10", command.getCommand()); | 
|   | 
|         OperateResult result = siemensNet.Write("DB100.0", array); | 
|   | 
|         if (!result.IsSuccess){ | 
|             News.error("写入RGVplc数据失败,重新添加任务到队列 ===> [id:{}],{}",slave.getId(),JSON.toJSON(command)); | 
|             MessageQueue.offer(SlaveType.Rgv,slave.getId(),new Task(4,command)); | 
|             Thread.sleep(100); | 
|             readStatus(); | 
|             return false; | 
|         } | 
|   | 
|         //RGV任务写入后,回读一次,看是否成功 | 
|         Thread.sleep(400); | 
|         try { | 
|             OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 12); | 
|             if (resultRead.IsSuccess){ | 
|                 RgvCommand one = new RgvCommand(); | 
| //                one.setAckFinish1(siemensNet.getByteTransform().TransInt16(resultRead.Content, 0)); | 
|                 one.setTaskNo1(siemensNet.getByteTransform().TransInt16(resultRead.Content, 2)); | 
|                 one.setTaskMode1(siemensNet.getByteTransform().TransInt16(resultRead.Content, 4)); | 
|                 one.setSourceStaNo1(siemensNet.getByteTransform().TransInt16(resultRead.Content, 6)); | 
|                 one.setDestinationStaNo1(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8)); | 
|                 if ( | 
|                     !command.getTaskNo1().equals(one.getTaskNo1()) || | 
|                         !command.getTaskMode1().equals(one.getTaskMode1()) || | 
|                         !command.getSourceStaNo1().equals(one.getSourceStaNo1()) || | 
|                         !command.getDestinationStaNo1().equals(one.getDestinationStaNo1()) | 
|                 ){ | 
|                     try{ | 
|                         News.error("RGV命令地址写入后回读失败[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one)); | 
|                     }catch (Exception e){ | 
|                         try{ | 
|                             News.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSON(command),JSON.toJSON(resultRead)); | 
|                         }catch (Exception e1){ | 
|                             News.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSON(command)); | 
|                         } | 
|                     } | 
|                     News.error("Rgv命令回读失败后,重新添加任务到队列 ===>> [id:{}],{}", slave.getId(), JSON.toJSON(command)); | 
|                     MessageQueue.offer(SlaveType.Rgv, slave.getId(), new Task(4, command)); | 
|                     Thread.sleep(100); | 
|                     readStatus(); | 
|                     return false; | 
|                 }else { | 
|                     News.info("RGV命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one)); | 
|                 } | 
|             } | 
|         }catch (Exception e){ | 
|             News.error("RGV命令地址写入后回读出错"); | 
|         } | 
|   | 
|         if (command.getAckFinish1() == 0) { | 
|             if (result.IsSuccess) { | 
|                 Thread.sleep(300); | 
|                 //任务下发次数 | 
|                 int writeCount2 = 0; | 
|   | 
|                 do { | 
|                     writeCount2++; | 
|                     short commandFinish = (short) 1; | 
|                     result = siemensNet.Write("DB100.10", commandFinish); | 
|                     if(result.IsSuccess){ | 
|                         //RGV任务写入后,回读一次,看是否成功 | 
|                         Thread.sleep(200); | 
|                         OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.10", (short) 2); | 
|                         if (resultRead.IsSuccess) { | 
|                             commandFinish=siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); | 
|                             if (commandFinish != 1){ | 
|                                 News.error("RGV任务确认位"+commandFinish+"写入数据与回读数据不一致!"+"循环执行次数:"+writeCount2+"次"); | 
|                             }else{ | 
|                                 //任务命令写入成功 | 
|                                 News.info("RGV任务确认位"+commandFinish+"回读成功!"+"循环执行次数:"+writeCount2+"次"); | 
|                                 break; | 
|                             } | 
|                         }else { | 
|                             News.error("RGV任务确认位"+commandFinish+"回读失败!"+"循环执行次数:"+writeCount2+"次"); | 
|                         } | 
|                     } else { | 
|                         News.error("RGV任务确认位"+commandFinish+"写入失败!"+"循环执行次数:"+writeCount2+"次"); | 
|                     } | 
|                 }while (writeCount2<5); | 
|             } | 
|         } | 
|   | 
| //        if (command.getAckFinish1() == 0) { | 
| //            short commandFinish = 1;  //工位1任务写入 | 
| //            Thread.sleep(200); | 
| //            result = siemensNet.Write("DB100.10", commandFinish); | 
| //        } | 
|   | 
|         try { | 
|             // 日志记录 | 
|             BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); | 
|             BasRgvOpt basRgvOpt = new BasRgvOpt( | 
|                     command.getTaskNo1().intValue(), | 
|                     command.getTaskNo2().intValue(), | 
|                     command.getRgvNo(), | 
|                     new Date(), | 
|                     command.getTaskModeType1().toString(), | 
|                     command.getSourceStaNo1().intValue(), | 
|                     command.getDestinationStaNo1().intValue(), | 
|                     command.getSourceStaNo2().intValue(), | 
|                     command.getDestinationStaNo2().intValue(), | 
|                     null, | 
|                     new Date(), | 
|                     null | 
|             ); | 
|             bean.insert(basRgvOpt); | 
|         } catch (Exception ignore) { | 
|             log.error(ignore.getMessage()); | 
|         } | 
|   | 
|         if (result != null && result.IsSuccess) { | 
|             Thread.sleep(200); | 
|             this.readStatus(); | 
|             log.info("RGV 工位1命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 工位1命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); | 
|             return true; | 
|         } else { | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc工位1数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); | 
|             log.error("写入RGV plc工位1数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|             return false; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 工位2写入数据 | 
|      */ | 
|     private boolean write2(RgvCommand command) throws InterruptedException { | 
|         if (null == command) { | 
|             log.error("RGV写入命令为空"); | 
|             return false; | 
|         } | 
|   | 
|         command.setRgvNo(slave.getId()); | 
|         short[] array = new short[6]; | 
|         array[0] = command.getAckFinish2(); | 
|         array[1] = command.getTaskNo2(); | 
|         array[2] = command.getTaskMode2(); | 
|         array[3] = command.getSourceStaNo2(); | 
|         array[4] = command.getDestinationStaNo2(); | 
|         array[5] = command.getCommand(); | 
|   | 
|         OperateResult result = siemensNet.Write("DB100.10", array); | 
|   | 
|         if (command.getAckFinish2() == 0) { | 
|             short commandFinish = 2;  //工位2任务写入 | 
|             Thread.sleep(100L); | 
|             result = siemensNet.Write("DB100.20", commandFinish); | 
|         } | 
|   | 
|         try { | 
|             // 日志记录 | 
|             BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); | 
|             BasRgvOpt basRgvOpt = new BasRgvOpt( | 
|                     command.getTaskNo1().intValue(), | 
|                     command.getTaskNo2().intValue(), | 
|                     command.getRgvNo(), | 
|                     new Date(), | 
|                     command.getTaskModeType1().toString(), | 
|                     command.getSourceStaNo1().intValue(), | 
|                     command.getDestinationStaNo1().intValue(), | 
|                     command.getSourceStaNo2().intValue(), | 
|                     command.getDestinationStaNo2().intValue(), | 
|                     null, | 
|                     new Date(), | 
|                     null | 
|             ); | 
|             bean.insert(basRgvOpt); | 
|         } catch (Exception ignore) {} | 
|   | 
|         if (result != null && result.IsSuccess) { | 
|             Thread.sleep(200); | 
|             this.readStatus(); | 
|             log.info("RGV 工位2命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 工位2命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); | 
|             return true; | 
|         } else { | 
|             OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc工位2数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); | 
|             log.error("写入RGV plc工位2数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|             return false; | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public void close() { | 
|         siemensNet.ConnectClose(); | 
|     } | 
|   | 
|     /******************************************************************************************/ | 
|     /**************************************** 测试专用 *****************************************/ | 
|     /*****************************************************************************************/ | 
|     public static void main(String[] args) throws InterruptedException { | 
|         RgvSlave slave = new RgvSlave(); | 
|         slave.setId(1); | 
|         slave.setIp("192.168.6.9"); | 
|         slave.setRack(0); | 
|         slave.setSlot(0); | 
|         RgvThread rgvThread = new RgvThread(slave); | 
|         rgvThread.connect(); | 
|         rgvThread.readStatus(); | 
|         System.out.println(JSON.toJSONString(rgvThread.rgvProtocol)); | 
|         Thread.sleep(3000L); | 
|   | 
|     } | 
|   | 
| } |