New file |
| | |
| | | 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.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.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, ThreadHandler { |
| | | |
| | | private SiemensS7Net siemensNet; |
| | | private RgvSlave slave; |
| | | private RgvProtocol rgvProtocol; |
| | | /** |
| | | * 工位1复位信号 |
| | | */ |
| | | private boolean resetFlag1 = false; |
| | | /** |
| | | * 工位2复位信号 |
| | | */ |
| | | private boolean resetFlag2 = false; |
| | | |
| | | 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; |
| | | // 写入数据 |
| | | case 2: |
| | | write((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.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); |
| | | 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) 34); |
| | | if (result.IsSuccess) { |
| | | if (null == rgvProtocol) { |
| | | 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.setTaskNo2(siemensNet.getByteTransform().TransInt16(result.Content, 14)); |
| | | rgvProtocol.setStatus2(siemensNet.getByteTransform().TransInt16(result.Content, 16)); |
| | | rgvProtocol.setLoaded2(siemensNet.getByteTransform().TransInt16(result.Content, 18)); |
| | | rgvProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 20)); |
| | | rgvProtocol.setHeart(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)) { |
| | | if (resetFlag1) { |
| | | RgvCommand rgvCommand = new RgvCommand(); |
| | | rgvCommand.setAckFinish1((short)1); |
| | | if (write(rgvCommand)) { |
| | | resetFlag1 = false; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 工位2复位信号 |
| | | if (rgvProtocol.getStatusType2().equals(RgvStatusType.WAITING)) { |
| | | 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){} |
| | | |
| | | } 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[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; |
| | | } |
| | | |
| | | 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(); |
| | | |
| | | OperateResult result = siemensNet.Write("DB100.0", array); |
| | | |
| | | if (command.getAckFinish1() == 0) { |
| | | short commandFinish = 1; //工位1任务写入 |
| | | 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 工位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; |
| | | } |
| | | |
| | | siemensNet.Write("DB100.20", command.getCommand()); |
| | | |
| | | command.setRgvNo(slave.getId()); |
| | | short[] array = new short[5]; |
| | | array[0] = command.getAckFinish2(); |
| | | array[1] = command.getTaskNo2(); |
| | | array[2] = command.getTaskMode2(); |
| | | array[3] = command.getSourceStaNo2(); |
| | | array[4] = command.getDestinationStaNo2(); |
| | | |
| | | 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); |
| | | |
| | | } |
| | | |
| | | } |