From 00e3e465859114706fa2f802a9b4d3dbbee17a36 Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期五, 13 十一月 2020 16:54:51 +0800 Subject: [PATCH] # --- src/main/java/com/zy/core/thread/DevpThread.java | 62 +++++++++++++++++++++++++++++-- 1 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/zy/core/thread/DevpThread.java b/src/main/java/com/zy/core/thread/DevpThread.java index b75d3f5..90d5837 100644 --- a/src/main/java/com/zy/core/thread/DevpThread.java +++ b/src/main/java/com/zy/core/thread/DevpThread.java @@ -6,6 +6,9 @@ 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.BasDevp; +import com.zy.asrs.service.BasDevpService; import com.zy.core.ThreadHandler; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.OutputQueue; @@ -17,7 +20,9 @@ import lombok.extern.slf4j.Slf4j; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -102,7 +107,7 @@ /** * 璇诲彇鐘舵�� ====> 鏁村潡plc */ - private void read(){ + private void read() throws InterruptedException { OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) 40); if (result.IsSuccess) { for (int i = 1; i <= 8; i++) { @@ -116,6 +121,7 @@ staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2+20)); // 鐩爣绔� } } + Thread.sleep(100); OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB100.40", (short) 8); if (result1.IsSuccess) { for (int i = 1; i <= 8; i++) { @@ -126,10 +132,42 @@ staProtocol.setInEnable(status[2]); // 鍙叆 staProtocol.setOutEnable(status[3]);// 鍙嚭 staProtocol.setEmptyMk(status[4]); // 绌烘澘淇″彿 + + if (!staProtocol.isPakMk() && !staProtocol.isLoading()) { + staProtocol.setPakMk(true); + } } } + // 鍙拌溅浣嶇疆 + OperateResultExOne<Short> result2 = siemensS7Net.ReadInt16("DB100.48"); + if (result2.IsSuccess) { + StaProtocol staProtocol = station.get(8); + staProtocol.setPosition(result2.Content); + } else { + OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆�8鍙峰彴杞﹁幏鍙栧疄鏃朵綅缃け璐ワ紒锛侊紒", DateUtils.convert(new Date()))); + log.error("8鍙峰彴杞﹁幏鍙栧疄鏃朵綅缃け璐ワ紒锛侊紒"); + } + if (result.IsSuccess && result1.IsSuccess) { + OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId())); + + // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴� + try { + List<BasDevp> basDevps = new ArrayList<>(); + for (int i = 1; i <= 8; i++) { + StaProtocol staProtocol = station.get(i); + basDevps.add(staProtocol.toSqlModel()); + } + BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); + if (!basDevpService.updateBatchById(basDevps)) { + throw new Exception("鏇存柊鏁版嵁搴撴暟鎹け璐�"); + } + } catch (Exception e) { + OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戞洿鏂版暟鎹簱鏁版嵁澶辫触 ===>> [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("鏇存柊鏁版嵁搴撴暟鎹け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); + } + } else { 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())); log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); @@ -139,11 +177,12 @@ /** * 鍐欏叆 ID+鐩爣绔� =====> 鍗曠珯鐐瑰啓鍏� */ - private void write(StaProtocol staProtocol){ + private void write(StaProtocol staProtocol) throws InterruptedException { if (null == staProtocol) { return; } OperateResult write = siemensS7Net.Write("DB100." + (staProtocol.getSiteId() - 1) * 2, staProtocol.getWorkNo()); // 宸ヤ綔鍙� + Thread.sleep(300); OperateResult write1 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) * 2 + 20), staProtocol.getStaNo()); // 鐩爣绔� // boolean[] status = new boolean[8]; // status[0] = staProtocol.isAutoing(); @@ -153,10 +192,15 @@ // status[4] = staProtocol.isEmptyMk(); // OperateResult write2 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) + 40), status); // 鐘舵�� if (!write.IsSuccess || !write1.IsSuccess) { + staProtocol = station.get(staProtocol.getSiteId()); + if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) { + staProtocol.setPakMk(true); + } OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol))); log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol)); } else { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); + 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)); } } @@ -207,12 +251,22 @@ } } + /** + * 璁剧疆鍏ュ簱鏍囪 + */ + public void setPakMk(Integer siteId, boolean pakMk) { + StaProtocol staProtocol = station.get(siteId); + if (null != staProtocol) { + staProtocol.setPakMk(pakMk); + } + } + @Override public void close() { siemensS7Net.ConnectClose(); } - public static void main(String[] args) { + public static void main(String[] args) throws Exception { DevpSlave slave = new DevpSlave(); slave.setIp("192.168.2.125"); DevpThread devpThread = new DevpThread(slave); -- Gitblit v1.9.1