From ecf8ba7c1bd6fc6a719c62a888327109795a3fda Mon Sep 17 00:00:00 2001 From: Junjie <540245094@qq.com> Date: 星期日, 05 五月 2024 14:13:11 +0800 Subject: [PATCH] # --- zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/SiemensDevpThread.java | 92 ++++++++++++++++++++++++++++++---------------- 1 files changed, 60 insertions(+), 32 deletions(-) diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/SiemensDevpThread.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/SiemensDevpThread.java index c611ee6..07a7218 100644 --- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/SiemensDevpThread.java +++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/SiemensDevpThread.java @@ -5,9 +5,12 @@ import HslCommunication.Profinet.Siemens.SiemensPLCS; import HslCommunication.Profinet.Siemens.SiemensS7Net; import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zy.asrs.framework.common.Cools; import com.zy.asrs.framework.common.DateUtils; -import com.zy.asrs.framework.exception.CoolException; +import com.zy.asrs.framework.common.SpringUtils; +import com.zy.asrs.wcs.core.entity.Station; +import com.zy.asrs.wcs.core.service.StationService; import com.zy.asrs.wcs.core.utils.RedisUtil; import com.zy.asrs.wcs.rcs.News; import com.zy.asrs.wcs.rcs.cache.MessageQueue; @@ -22,6 +25,7 @@ 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; @@ -34,10 +38,7 @@ private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>(); - public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{ - add(100);add(101);add(102);add(103);add(104);add(105); - add(200);add(201);add(202);add(203);add(204);add(205);add(206); - }}; + public static ArrayList<Station> stationList = new ArrayList<>(); /** * 鏉$爜鏁伴噺 @@ -49,12 +50,19 @@ this.redisUtil = redisUtil; } - private ArrayList<Integer> getStaNo() { - switch (Integer.parseInt(device.getDeviceNo())) { - case 1: - return staNos1; - default: - throw new CoolException("鑾峰彇绔欑偣澶辫触锛�"); + private ArrayList<Station> getStaNo() { + try { + if (stationList.isEmpty()) { + StationService stationService = SpringUtils.getBean(StationService.class); + List<Station> stations = stationService.list(new LambdaQueryWrapper<Station>() + .eq(Station::getDeviceId, device.getId()) + .eq(Station::getStatus, 1) + .eq(Station::getHostId, device.getHostId())); + stationList.addAll(stations); + } + return stationList; + } catch (Exception e) { + return stationList; } } @@ -88,12 +96,16 @@ } private void read() throws InterruptedException { - ArrayList<Integer> staNos = getStaNo(); + ArrayList<Station> staNos = getStaNo(); int staNoSize = staNos.size(); + if (station.isEmpty()) { + return; + } OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNoSize*8)); if (result.IsSuccess) { for (int i = 0; i < staNoSize; i++) { - Integer siteId = staNos.get(i); // 绔欑偣缂栧彿 + Station siteStation = staNos.get(i); + int siteId = Integer.parseInt(siteStation.getStaNo());// 绔欑偣缂栧彿 StaProtocol staProtocol = station.get(siteId); if (null == staProtocol) { staProtocol = new StaProtocol(); @@ -135,13 +147,13 @@ } Thread.sleep(200); - ArrayList<Integer> errorStaNo = getStaNo(); + ArrayList<Station> errorStaNo = getStaNo(); OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB101.800.0", (short) (errorStaNo.size() * 4)); if (result3.IsSuccess) { for (int i = 0; i < errorStaNo.size(); i++) { - Integer siteId = errorStaNo.get(i); // 绔欑偣缂栧彿 + Station siteStation = errorStaNo.get(i); + Integer siteId = Integer.parseInt(siteStation.getStaNo()); // 绔欑偣缂栧彿 StaProtocol staProtocol = station.get(siteId); -// staProtocol.setLocType1((short) siemensS7Net.getByteTransform().TransInt32(result3.Content, i * 4));//楂樹綆淇″彿 boolean[] status = siemensS7Net.getByteTransform().TransBool(result3.Content, (i * 4 + 2), 2); staProtocol.setFrontErr(status[0]);//鍓嶈秴闄� staProtocol.setBackErr(status[1]);//鍚庤秴闄� @@ -153,24 +165,26 @@ } } - - if (result.IsSuccess && !Cools.isEmpty(result) && result.IsSuccess) { OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), device.getId())); // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴� try { -// List<BasDevp> basDevps = new ArrayList<>(); -// for (Integer siteId : staNos) { -// StaProtocol staProtocol = station.get(siteId); -// basDevps.add(staProtocol.toSqlModel()); -// } -// -// BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); -// if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) { -// throw new Exception("鏇存柊鏁版嵁搴撴暟鎹け璐�"); -// } + List<Station> stations = new ArrayList<>(); + for (Station sta : getStaNo()) { + StaProtocol staProtocol = station.get(Integer.parseInt(sta.getStaNo())); + Station sqlModel = staProtocol.toSqlModel(sta); + stations.add(sqlModel); + } + + if (!stations.isEmpty()) { + StationService stationService = SpringUtils.getBean(StationService.class); + if (null != stationService && !stationService.updateBatchById(stations)) { + throw new Exception("鏇存柊鏁版嵁搴撴暟鎹け璐�"); + } + } + } catch (Exception e) { e.printStackTrace(); OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戞洿鏂版暟鎹簱鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot())); @@ -209,8 +223,8 @@ @Override public boolean writeWorkNo(int siteId, short workNo) { - ArrayList<Integer> staNos = getStaNo(); - int index = staNos.indexOf(siteId); + int index = findStaNosIndex(siteId); + OperateResult write = siemensS7Net.Write("DB100." + index*6, workNo); // 宸ヤ綔鍙� if (!write.IsSuccess) { @@ -230,8 +244,8 @@ @Override public boolean writeStaNo(int siteId,short staNo) { - ArrayList<Integer> staNos = getStaNo(); - int index = staNos.indexOf(siteId); + int index = findStaNosIndex(siteId); + OperateResult write = siemensS7Net.Write("DB100." + (index*6+4), staNo); // 鐩爣绔� if (!write.IsSuccess) { @@ -253,4 +267,18 @@ public Map<Integer, StaProtocol> getStation() { return this.station; } + + private int findStaNosIndex(int siteId) { + ArrayList<Station> staNos = getStaNo(); + + int index = -1; + for (int i = 0; i < staNos.size(); i++) { + Station sta = staNos.get(i); + if (Integer.parseInt(sta.getStaNo()) == siteId) { + index = i; + break; + } + } + return index; + } } -- Gitblit v1.9.1