| | |
| | | import com.core.common.Cools; |
| | | import com.core.common.DateUtils; |
| | | import com.core.common.SpringUtils; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.asrs.entity.BasDevp; |
| | | import com.zy.asrs.mapper.BasDevpMapper; |
| | | import com.zy.asrs.service.BasDevpService; |
| | | import com.zy.common.utils.News; |
| | | import com.zy.core.DevpThread; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.IoModeType; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.model.DevpSlave; |
| | | import com.zy.core.model.Task; |
| | |
| | | private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>(); |
| | | private short heartBeatVal = 1; |
| | | private int barcodeSize = 2; |
| | | public static final ArrayList<Integer> staNos = new ArrayList<Integer>() {{ |
| | | public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{ |
| | | add(100);add(101);add(102);add(103);add(104);add(105);add(106);add(107);add(108);add(109); |
| | | add(110);add(111); |
| | | add(200);add(201);add(202);add(203);add(204);add(205);add(206);add(207);add(208);add(209); |
| | | add(210);add(211); |
| | | add(300); |
| | | }}; |
| | | public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{ |
| | | add(112);add(113);add(114); |
| | | }}; |
| | | public static final ArrayList<Integer> staNos3 = new ArrayList<Integer>() {{ |
| | | add(212);add(213);add(214); |
| | | }}; |
| | | |
| | | public boolean charge0; |
| | |
| | | public SiemensDevpThread(DevpSlave slave) { |
| | | this.slave = slave; |
| | | } |
| | | |
| | | /** |
| | | * 入出库模式 |
| | | * 0:未知 |
| | | * 1:入库启动中 |
| | | * 2.入库模式 |
| | | * 3.出库启动中 (不能生成入库工作档) |
| | | * 4.出库模式 |
| | | */ |
| | | public IoModeType ioModeOf1F = IoModeType.NONE; |
| | | public IoModeType ioModeOf2F = IoModeType.NONE; |
| | | |
| | | private ArrayList<Integer> getStaNo() { |
| | | switch (slave.getId()) { |
| | | case 1: |
| | | return staNos1; |
| | | case 2: |
| | | return staNos2; |
| | | case 3: |
| | | return staNos3; |
| | | default: |
| | | throw new CoolException("服务器异常"); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | |
| | | break; |
| | | // 写数据 ID+目标站 |
| | | case 2: |
| | | // write((StaProtocol)task.getData(), 0); |
| | | write((StaProtocol)task.getData()); |
| | | Thread.sleep(300); |
| | | read(); |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】输送线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())); |
| | | News.error("输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } |
| | | siemensS7Net.ConnectClose(); |
| | | // siemensS7Net.ConnectClose(); |
| | | return result; |
| | | } |
| | | |
| | |
| | | * 读取状态 ====> 整块plc |
| | | */ |
| | | private void read() throws InterruptedException { |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) 50); |
| | | // 更新入出库模式 |
| | | updateIoMode(); |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) (staNos.size()*2)); |
| | | if (result.IsSuccess) { |
| | | for (int i = 0; i < 25; i++) { |
| | | for (int i = 0; i < staNos.size(); i++) { |
| | | Integer siteId = staNos.get(i); // 站点编号 |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null == staProtocol) { |
| | |
| | | } |
| | | } |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> result0 = siemensS7Net.Read("DB101.0", (short) 50); |
| | | OperateResultExOne<byte[]> result0 = siemensS7Net.Read("DB101.0", (short) (staNos.size()*2)); |
| | | if (result0.IsSuccess) { |
| | | for (int i = 0; i < 25; i++) { |
| | | for (int i = 0; i < staNos.size(); i++) { |
| | | Integer siteId = staNos.get(i); // 站点编号 |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null == staProtocol) { |
| | |
| | | } |
| | | } |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB102.0", (short) 50); |
| | | OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB102.0", (short) (staNos.size()*2)); |
| | | if (result1.IsSuccess) { |
| | | for (int i = 0; i < 25; i++) { |
| | | for (int i = 0; i < staNos.size(); i++) { |
| | | Integer siteId = staNos.get(i); // 站点编号 |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(result1.Content, i*2, 1); |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(result1.Content, i * 2, 1); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | staProtocol.setAutoing(status[0]); // 自动 |
| | | staProtocol.setLoading(status[1]); // 有物 |
| | |
| | | |
| | | if (!staProtocol.isPakMk() && !staProtocol.isLoading()) { |
| | | staProtocol.setPakMk(true); |
| | | } else if (staProtocol.getSiteId().equals(300)) { |
| | | BasDevpMapper basDevpMapper = SpringUtils.getBean(BasDevpMapper.class); |
| | | BasDevp basDevp = basDevpMapper.selectByDevNo(300); |
| | | if (!basDevp.getWrkNo().equals(basDevp.getWrkNo1())) { |
| | | staProtocol.setPakMk(true); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //读条码 |
| | | Thread.sleep(100); |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB100.100",(short)(barcodeSize*8)); |
| | | if (result2.IsSuccess) { |
| | | for (int i = 0; i < barcodeSize; i++) { |
| | |
| | | } |
| | | |
| | | // 充电信号位 |
| | | Thread.sleep(100); |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB102.50", (short)1); |
| | | if (result3.IsSuccess) { |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(result3.Content, 0, 1); |
| | |
| | | if (null == staProtocol) { |
| | | return; |
| | | } |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | int index = staNos.indexOf(staProtocol.getSiteId()); |
| | | OperateResult write = siemensS7Net.Write("DB100." + index*2, staProtocol.getWorkNo()); // 工作号 |
| | | Thread.sleep(500); |
| | | OperateResult write1 = siemensS7Net.Write("DB101." + index*2, staProtocol.getStaNo()); // 目标站 |
| | | |
| | | if (!write.IsSuccess || !write1.IsSuccess) { |
| | | OperateResult writeId,writeDest; |
| | | // //任务ID下发次数 |
| | | int idCount = 0; |
| | | boolean idFlag = false; |
| | | while(idCount < 5){ |
| | | writeId = siemensS7Net.Write("DB100." + index*2, staProtocol.getWorkNo()); // 工作号 |
| | | if(writeId.IsSuccess){ |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> readId = siemensS7Net.Read("DB100." + index*2, (short)2); |
| | | if(readId.IsSuccess){ |
| | | short workNo = siemensS7Net.getByteTransform().TransInt16(readId.Content, 0); |
| | | if(staProtocol.getWorkNo().equals(workNo)){ |
| | | //工作号写入成功 |
| | | idFlag = true; |
| | | break; |
| | | } else {//返回结果是成功了,但是真实值不相同 |
| | | idCount++; |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线工作号后返回成功,但是读取工作号值不一致。输送线plc编号={1},站点数据={2},写入次数={3}", |
| | | slave.getId(), JSON.toJSON(staProtocol),idCount)); |
| | | log.error("写入输送线工作号后返回成功,但是读取工作号值不一致。输送线plc编号={},{},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), idCount); |
| | | } |
| | | } else { |
| | | idCount++; |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线工作号后读取工作号失败。输送线plc编号={1},站点数据={2},写入次数={3}", |
| | | slave.getId(), JSON.toJSON(staProtocol), idCount)); |
| | | log.error("写入输送线工作号后读取工作号失败{}。输送线plc编号={},站点数据={},写入次数={}", readId.Message, slave.getId(), JSON.toJSON(staProtocol), idCount); |
| | | } |
| | | } else { |
| | | idCount++; |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线工作号失败。输送线plc编号={1},站点数据={2},写入次数={3}", |
| | | slave.getId(), JSON.toJSON(staProtocol),idCount)); |
| | | log.error("写入输送线工作号失败{}。输送线plc编号={},站点数据={},写入次数={}", writeId.Message, slave.getId(), JSON.toJSON(staProtocol), idCount); |
| | | } |
| | | Thread.sleep(200); |
| | | } |
| | | |
| | | //写ID尝试了5次还是失败了 |
| | | if(!idFlag){ |
| | | staProtocol = station.get(staProtocol.getSiteId()); |
| | | if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) { |
| | | staProtocol.setPakMk(true); |
| | | } |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线工作号尝试5次失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol))); |
| | | log.error("写入输送线工作号尝试5次失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | |
| | | //重新添加数据到任务队列 |
| | | boolean result = MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol)); |
| | | // read();//读取1次设备状态 |
| | | return; |
| | | } |
| | | |
| | | //任务目标站下发次数 |
| | | int destCount = 0; |
| | | boolean destFlag = false; |
| | | while(destCount < 5){ |
| | | writeDest = siemensS7Net.Write("DB101." + index*2, staProtocol.getStaNo()); // 目标站 |
| | | if(writeDest.IsSuccess){ |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> readDest = siemensS7Net.Read("DB101." + index*2, (short)2); |
| | | if(readDest.IsSuccess){ |
| | | short staNo = siemensS7Net.getByteTransform().TransInt16(readDest.Content, 0); |
| | | if(staProtocol.getStaNo().equals(staNo)){ |
| | | //目标站写入成功 |
| | | destFlag = true; |
| | | break; |
| | | } else {//返回结果是成功了,但是真实值不相同 |
| | | destCount++; |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线目标站后返回成功,但是读取目标站值不一致。输送线plc编号={1},站点数据={2},写入次数={3}", |
| | | slave.getId(), JSON.toJSON(staProtocol),destCount)); |
| | | log.error("写入输送线目标站后返回成功,但是读取目标站值不一致。输送线plc编号={},{},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), destCount); |
| | | } |
| | | } else { |
| | | destCount++; |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线目标站后读取目标站失败。输送线plc编号={1},站点数据={2},写入次数={3}", |
| | | slave.getId(), JSON.toJSON(staProtocol), destCount)); |
| | | log.error("写入输送线目标站后读取目标站失败{}。输送线plc编号={},站点数据={},写入次数={}", readDest.Message, slave.getId(), JSON.toJSON(staProtocol), destCount); |
| | | } |
| | | } else { |
| | | destCount++; |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线目标站失败。输送线plc编号={1},站点数据={2},写入次数={3}", |
| | | slave.getId(), JSON.toJSON(staProtocol),destCount)); |
| | | log.error("写入输送线目标站失败{}。输送线plc编号={},站点数据={},写入次数={}", writeDest.Message, slave.getId(), JSON.toJSON(staProtocol), destCount); |
| | | } |
| | | Thread.sleep(200); |
| | | } |
| | | |
| | | //写目标站尝试了5次还是失败了 |
| | | if(!destFlag){ |
| | | staProtocol = station.get(staProtocol.getSiteId()); |
| | | if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) { |
| | | staProtocol.setPakMk(true); |
| | | } |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线目标站尝试5次失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol))); |
| | | log.error("写入输送线目标站尝试5次失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | |
| | | //重新添加数据到任务队列 |
| | | boolean result = MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol)); |
| | | // read();//读取1次设备状态 |
| | | return; |
| | | } |
| | | |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发成功 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); |
| | | log.info("输送线命令下发 [id:{}] >>>>> 命令下发成功: {}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | } |
| | | |
| | | /** |
| | | * 写入 ID+目标站 =====> 单站点写入 |
| | | */ |
| | | private void write(StaProtocol staProtocol, int times) throws InterruptedException { |
| | | if (null == staProtocol || times > 3) { |
| | | return; |
| | | } |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | int index = staNos.indexOf(staProtocol.getSiteId()); |
| | | OperateResult write = siemensS7Net.Write("DB100." + index*2, staProtocol.getWorkNo()); // 工作号 |
| | | Thread.sleep(200); |
| | | OperateResult write1 = siemensS7Net.Write("DB101." + index*2, staProtocol.getStaNo()); // 目标站 |
| | | |
| | | if (!write.IsSuccess || !write1.IsSuccess) { |
| | | // MessageQueue.offer(SlaveType.Devp, slave.getId(), new Task(2, staProtocol)); |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol))); |
| | | News.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | News.error("写入输送线站点数据失败==>>{}--{},重新添加任务到队列。输送线plc编号={},站点数据={}", write.Message, write1.Message, slave.getId(), JSON.toJSON(staProtocol)); |
| | | times++; |
| | | write(staProtocol, times); |
| | | // staProtocol = station.get(staProtocol.getSiteId()); |
| | | // if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) { |
| | | // staProtocol.setPakMk(true); |
| | | // } |
| | | } else { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); |
| | | News.info("输送线命令下发 [id:{}] >>>>> 命令下发: {}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | } |
| | | } |
| | | |
| | | // 更新入出库模式 |
| | | private void updateIoMode() throws InterruptedException { |
| | | if (this.ioModeOf1F != IoModeType.NONE) { |
| | | if (!siemensS7Net.Write("DB100.180", this.ioModeOf1F.id).IsSuccess) { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线103入出库模式失败。输送线plc编号={1}", slave.getId())); |
| | | News.error("写入输送线103入出库模式失败。输送线plc编号={}", slave.getId()); |
| | | } |
| | | } |
| | | if (this.ioModeOf2F != IoModeType.NONE) { |
| | | if (!siemensS7Net.Write("DB100.182", this.ioModeOf2F.id).IsSuccess) { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线203入出库模式失败。输送线plc编号={1}", slave.getId())); |
| | | News.error("写入输送线203入出库模式失败。输送线plc编号={}", slave.getId()); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | siemensS7Net.ConnectClose(); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // System.out.println(staNos.indexOf(129)); |
| | | System.out.println(staNos.size()); |
| | | for (int i = 0; i<staNos.size(); i++) { |
| | | // System.out.println(i*2); |
| | | // System.out.println(i*2 + 200); |
| | | // System.out.println(i); |
| | | } |
| | | // int index = staNos.indexOf(128); |
| | | // System.out.println(index*2); |
| | | // System.out.println(index*2 + 200); |
| | | } |
| | | |
| | | // public static void main(String[] args) throws Exception { |
| | | // DevpSlave slave = new DevpSlave(); |
| | | // slave.setIp("192.168.2.125"); |
| | | // SiemensDevpThread devpThread = new SiemensDevpThread(slave); |
| | | // devpThread.connect(); |
| | | // devpThread.read(); |
| | | // // 写 |
| | | public static void main(String[] args) throws Exception { |
| | | DevpSlave slave = new DevpSlave(); |
| | | slave.setIp("10.10.10.61"); |
| | | slave.setPort(102); |
| | | slave.setRack(0); |
| | | slave.setSlot(0); |
| | | SiemensDevpThread devpThread = new SiemensDevpThread(slave); |
| | | devpThread.connect(); |
| | | // 写 |
| | | // StaProtocol staProtocol = devpThread.getStation().get(1); |
| | | // staProtocol.setWorkNo((short) 232); |
| | | // staProtocol.setStaNo((short) 6); |
| | |
| | | // // 读 |
| | | // devpThread.read(); |
| | | // System.out.println(JSON.toJSONString(devpThread.station)); |
| | | // |
| | | // } |
| | | |
| | | } |
| | | |
| | | } |