From 79603e53c78e535dba4bfbc69b8c3b5c184012c5 Mon Sep 17 00:00:00 2001
From: Junjie <xjj@123>
Date: 星期一, 08 四月 2024 09:16:05 +0800
Subject: [PATCH] #

---
 zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/SiemensDevpThread.java |  236 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 233 insertions(+), 3 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 3590546..e465c7d 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
@@ -1,21 +1,251 @@
 package com.zy.asrs.wcs.rcs.thread.impl;
 
+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.zy.asrs.framework.common.Cools;
+import com.zy.asrs.framework.common.DateUtils;
+import com.zy.asrs.framework.exception.CoolException;
+import com.zy.asrs.wcs.core.utils.RedisUtil;
+import com.zy.asrs.wcs.rcs.News;
+import com.zy.asrs.wcs.rcs.cache.MessageQueue;
+import com.zy.asrs.wcs.rcs.cache.OutputQueue;
+import com.zy.asrs.wcs.rcs.entity.Device;
+import com.zy.asrs.wcs.rcs.model.Task;
+import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
 import com.zy.asrs.wcs.rcs.model.protocol.StaProtocol;
 import com.zy.asrs.wcs.rcs.thread.DevpThread;
+import lombok.extern.slf4j.Slf4j;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
-public class SiemensDevpThread implements DevpThread {
+@Slf4j
+public class SiemensDevpThread implements DevpThread, Runnable {
+
+    private Device device;
+    private RedisUtil redisUtil;
+    private SiemensS7Net siemensS7Net;
 
     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);
+    }};
+
+    /**
+     * 鏉$爜鏁伴噺
+     */
+    private int barcodeSize = 2;
+
+    private ArrayList<Integer> getStaNo() {
+        switch (device.getId().intValue()) {
+            case 1:
+                return staNos1;
+            default:
+                throw new CoolException("鑾峰彇绔欑偣澶辫触锛�");
+        }
+    }
+
     @Override
-    public boolean writeWorkNo(short workNo) {
+    @SuppressWarnings("InfiniteLoopStatement")
+    public void run() {
+        connect();
+        while (true) {
+            try {
+                int step = 1;
+                Task task = MessageQueue.poll(SlaveType.Devp, device.getId().intValue());
+                if (task != null) {
+                    step = task.getStep();
+                }
+                switch (step) {
+                    // 璇绘暟鎹�
+                    case 1:
+                        read();
+                        break;
+                    default:
+                        break;
+                }
+                // 蹇冭烦
+//                heartbeat();
+                Thread.sleep(400);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+
+        }
+    }
+
+    private void read() throws InterruptedException {
+        ArrayList<Integer> staNos = getStaNo();
+        int staNoSize = staNos.size();
+        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); // 绔欑偣缂栧彿
+                StaProtocol staProtocol = station.get(siteId);
+                if (null == staProtocol) {
+                    staProtocol = new StaProtocol();
+                    staProtocol.setSiteId(siteId);
+                    station.put(siteId, staProtocol);
+                }
+                staProtocol.setStaNo( siemensS7Net.getByteTransform().TransInt16(result.Content, i*8 + 4 ));   // 鐩爣绔�
+                Thread.sleep(300);
+                staProtocol.setWorkNo((short) siemensS7Net.getByteTransform().TransInt32(result.Content, i * 8));     // 宸ヤ綔鍙�
+
+                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, i*8+6, 2);
+                staProtocol.setAutoing(status[0]);  // 鑷姩
+                staProtocol.setLoading(status[1]);  // 鏈夌墿
+                staProtocol.setInEnable(status[2]); // 鍙叆
+                staProtocol.setOutEnable(status[3]);// 鍙嚭
+                staProtocol.setEmptyMk(status[4]);  // 绌烘澘淇″彿
+                staProtocol.setFullPlt(status[5]);  // 婊℃墭鐩�
+                staProtocol.setHigh(status[6]);     // 楂樺簱浣�
+                staProtocol.setLow(status[7]);      // 浣庡簱浣�
+
+                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
+                    staProtocol.setPakMk(true);
+                }
+            }
+        }
+
+        // 鏉$爜鎵弿鍣�
+        Thread.sleep(200);
+        OperateResultExOne<byte[]> result2 = null;
+        result2 = siemensS7Net.Read("DB101.840.0", (short) (barcodeSize * 8));
+        if (result2.IsSuccess) {
+            for (int i = 0; i < barcodeSize; i++) {
+                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content, i * 8, 8, "UTF-8");
+//                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1);
+//                if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
+//                    barcodeThread.setBarcode(barcode);
+//                }
+            }
+        }
+
+        Thread.sleep(200);
+        ArrayList<Integer> 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); // 绔欑偣缂栧彿
+                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]);//鍚庤秴闄�
+                staProtocol.setHighErr(status[2]);//瓒呴珮
+                staProtocol.setLeftErr(status[3]);//宸﹁秴闄�
+                staProtocol.setRightErr(status[4]);//鍙宠秴闄�
+                staProtocol.setWeightErr(status[5]);//瓒呴噸
+                staProtocol.setBarcodeErr(status[6]);//鎵爜澶辫触
+            }
+        }
+
+
+
+        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("鏇存柊鏁版嵁搴撴暟鎹け璐�");
+//                }
+            } 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()));
+                News.error("鏇存柊鏁版嵁搴撴暟鎹け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot());
+            }
+
+        } else {
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戣鍙栬緭閫佺嚎plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
+//            log.error("璇诲彇杈撻�佺嚎plc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot());
+        }
+    }
+
+    @Override
+    public boolean connect() {
+        boolean result = false;
+        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, device.getIp());
+        siemensS7Net.setRack(device.getRack().byteValue());
+        siemensS7Net.setSlot(device.getSlot().byteValue());
+        OperateResult connect = siemensS7Net.ConnectServer();
+        if(connect.IsSuccess){
+            result = true;
+            OutputQueue.DEVP.offer(MessageFormat.format( "銆恵0}銆戣緭閫佺嚎plc杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
+            News.info("杈撻�佺嚎plc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", device.getId(), device.getIp(), device.getPort());
+        } else {
+            OutputQueue.DEVP.offer(MessageFormat.format( "銆恵0}銆戣緭閫佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
+            News.error("杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", device.getId(), device.getIp(), device.getPort());
+        }
+        // siemensS7Net.ConnectClose();
+        return result;
+    }
+
+    @Override
+    public void close() {
+
+    }
+
+    @Override
+    public boolean writeWorkNo(int siteId, short workNo) {
+        ArrayList<Integer> staNos = getStaNo();
+        int index = staNos.indexOf(siteId);
+        OperateResult write = siemensS7Net.Write("DB100." + index*6, workNo);    // 宸ヤ綔鍙�
+
+        if (!write.IsSuccess) {
+            StaProtocol staProtocol = station.get(siteId);
+            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
+                staProtocol.setPakMk(true);
+            }
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", device.getId(), JSON.toJSON(staProtocol)));
+            log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", device.getId(), JSON.toJSON(staProtocol));
+        } else {
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆� 杈撻�佺嚎鍛戒护涓嬪彂 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(workNo)));
+            log.info("杈撻�佺嚎鍛戒护涓嬪彂 [id:{}] >>>>> 鍛戒护涓嬪彂锛� {}",  device.getId(), JSON.toJSON(workNo));
+            return true;
+        }
         return false;
     }
 
     @Override
-    public boolean writeStaNo(short staNo) {
+    public boolean writeStaNo(int siteId,short staNo) {
+        ArrayList<Integer> staNos = getStaNo();
+        int index = staNos.indexOf(siteId);
+        OperateResult write = siemensS7Net.Write("DB100." + (index*6+4), staNo);    // 鐩爣绔�
+
+        if (!write.IsSuccess) {
+            StaProtocol staProtocol = station.get(siteId);
+            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
+                staProtocol.setPakMk(true);
+            }
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", device.getId(), JSON.toJSON(staProtocol)));
+            log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", device.getId(), JSON.toJSON(staProtocol));
+        } else {
+            OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆� 杈撻�佺嚎鍛戒护涓嬪彂 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(staNo)));
+            log.info("杈撻�佺嚎鍛戒护涓嬪彂 [id:{}] >>>>> 鍛戒护涓嬪彂锛� {}",  device.getId(), JSON.toJSON(staNo));
+            return true;
+        }
         return false;
     }
+
+    @Override
+    public Map<Integer, StaProtocol> getStation() {
+        return this.station;
+    }
 }

--
Gitblit v1.9.1