|  |  |  | 
|---|
|  |  |  | 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.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; | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Data | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | public class RgvThread implements Runnable, ThreadHandler { | 
|---|
|  |  |  | public class RgvThread implements Runnable, RgvThread2 { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private SiemensS7Net siemensNet; | 
|---|
|  |  |  | private RgvSlave slave; | 
|---|
|  |  |  | 
|---|
|  |  |  | * 工位2复位信号 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private boolean resetFlag2 = false; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //锁定标记 | 
|---|
|  |  |  | private boolean PakMk = true; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public RgvThread(RgvSlave slave) { | 
|---|
|  |  |  | this.slave = slave; | 
|---|
|  |  |  | 
|---|
|  |  |  | case 1: | 
|---|
|  |  |  | readStatus(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | // 工位1、2写入数据 | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | write((RgvCommand) task.getData()); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | //                    //工位2写入数据 | 
|---|
|  |  |  | //                    case 5: | 
|---|
|  |  |  | //                        write2((RgvCommand) task.getData()); | 
|---|
|  |  |  | //                        break; | 
|---|
|  |  |  | // 复位 | 
|---|
|  |  |  | case 3: | 
|---|
|  |  |  | RgvCommand command = (RgvCommand) task.getData(); | 
|---|
|  |  |  | 
|---|
|  |  |  | command.setTaskMode1(RgvTaskModeType.NONE); // 任务模式 | 
|---|
|  |  |  | command.setSourceStaNo1((short)0);     // 源站 | 
|---|
|  |  |  | command.setDestinationStaNo1((short)0);     // 目标站 | 
|---|
|  |  |  | command.setTaskNo2((short) 0); // 工作号 | 
|---|
|  |  |  | command.setAckFinish2((short) 1);  // 任务完成确认位 | 
|---|
|  |  |  | command.setTaskMode2(RgvTaskModeType.NONE); // 任务模式 | 
|---|
|  |  |  | command.setSourceStaNo2((short)0);     // 源站 | 
|---|
|  |  |  | command.setDestinationStaNo2((short)0);     // 目标站 | 
|---|
|  |  |  | command.setCommand((short)0); | 
|---|
|  |  |  | write(command); | 
|---|
|  |  |  | write1(command); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | // 回原点  避让 | 
|---|
|  |  |  | case 9: | 
|---|
|  |  |  | 
|---|
|  |  |  | 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.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; | 
|---|
|  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 30); | 
|---|
|  |  |  | if (result.IsSuccess) { | 
|---|
|  |  |  | if (null == rgvProtocol) { | 
|---|
|  |  |  | if (null == rgvProtocol || rgvProtocol.getRgvNo() == null) { | 
|---|
|  |  |  | rgvProtocol = new RgvProtocol(); | 
|---|
|  |  |  | rgvProtocol.setRgvNo(slave.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } 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()); | 
|---|
|  |  |  | //                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()); | 
|---|
|  |  |  | //            log.error("读取RGV plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|---|
|  |  |  | initRgv(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | //        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) { | 
|---|
|  |  |  | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | siemensNet.Write("DB100.20", command.getCommand()); | 
|---|
|  |  |  | 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[2] = command.getTaskMode1(); | 
|---|
|  |  |  | array[3] = command.getSourceStaNo1(); | 
|---|
|  |  |  | array[4] = command.getDestinationStaNo1(); | 
|---|
|  |  |  | siemensNet.Write("DB100.20", command.getCommand()); | 
|---|
|  |  |  | //        siemensNet.Write("DB100.10", command.getCommand()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | OperateResult result = siemensNet.Write("DB100.0", array); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (command.getAckFinish1() == 0) { | 
|---|
|  |  |  | short commandFinish = 1;  //工位1任务写入 | 
|---|
|  |  |  | Thread.sleep(100L); | 
|---|
|  |  |  | result = siemensNet.Write("DB100.20", commandFinish); | 
|---|
|  |  |  | 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 { | 
|---|
|  |  |  | // 日志记录 | 
|---|
|  |  |  | 
|---|
|  |  |  | null | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | bean.insert(basRgvOpt); | 
|---|
|  |  |  | } catch (Exception ignore) {} | 
|---|
|  |  |  | } catch (Exception ignore) { | 
|---|
|  |  |  | log.error(ignore.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (result != null && result.IsSuccess) { | 
|---|
|  |  |  | Thread.sleep(200); | 
|---|