From b612510f2479cc7fc04fbf9c4982742bee9e2ceb Mon Sep 17 00:00:00 2001
From: wang <3597712270@qq.com>
Date: 星期四, 18 四月 2024 16:55:46 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/HangZhouMes

---
 hangzhoumesParent/moduleService/CacheGlassModule/src/main/java/com/mes/common/PlcParameterObject.java |  195 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 195 insertions(+), 0 deletions(-)

diff --git a/hangzhoumesParent/moduleService/CacheGlassModule/src/main/java/com/mes/common/PlcParameterObject.java b/hangzhoumesParent/moduleService/CacheGlassModule/src/main/java/com/mes/common/PlcParameterObject.java
new file mode 100644
index 0000000..e06fc12
--- /dev/null
+++ b/hangzhoumesParent/moduleService/CacheGlassModule/src/main/java/com/mes/common/PlcParameterObject.java
@@ -0,0 +1,195 @@
+package com.mes.common;
+
+import java.lang.reflect.Array;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+public class PlcParameterObject {
+
+    // 璇ユā鍧楁暟鎹被鍨嬶紝鏁版嵁璧峰浣嶇疆
+    private String plcAddressBegin;
+    // 鏁版嵁鍦板潃闀垮害锛氱涓�鍙傛暟鍒版渶鍚庝竴涓弬鏁扮殑闀垮害
+    private int plcAddressLength;
+    private ArrayList<PlcParameterInfo> plcParameterList;
+
+    /**
+     * @return 鏁版嵁鍖哄紑濮嬪湴鍧�
+     */
+    public String getPlcAddressBegin() {
+        return plcAddressBegin;
+    }
+
+    /**
+     * @param plcAddressBegin 璁剧疆鏁版嵁鍖哄紑濮嬪湴鍧�
+     */
+    public void setPlcAddressBegin(String plcAddressBegin) {
+        this.plcAddressBegin = plcAddressBegin;
+    }
+
+    /**
+     * @return 鏁版嵁鍖� 璇诲彇鎵�鏈夋暟鎹墍闇�鐨勯暱搴︼紙浠yte绫诲瀷涓哄熀鍑嗭級
+     */
+    public int getPlcAddressLength() {
+        return plcAddressLength;
+    }
+
+    /**
+     * @return 璁剧疆锛氭暟鎹尯 璇诲彇鎵�鏈夋暟鎹墍闇�鐨勯暱搴︼紙浠yte绫诲瀷涓哄熀鍑嗭級
+     */
+    public void setPlcAddressLength(int plcAddressLength) {
+        this.plcAddressLength = plcAddressLength;
+    }
+
+    /**
+     * @return 鑾峰彇鍙傛暟瀹炰緥闆嗗悎
+     */
+    public ArrayList<PlcParameterInfo> getPlcParameterList() {
+        return plcParameterList;
+    }
+
+    /**
+     * 鏍规嵁鍙傛暟鏍囪瘑 鑾峰彇鏌愪釜鍙傛暟瀹炰緥
+     *
+     * @param codeid 鍙傛暟鏍囪瘑
+     * @return 鑾峰彇鏌愪釜鍙傛暟瀹炰緥
+     */
+    public PlcParameterInfo getPlcParameter(String codeid) {
+        if (plcParameterList != null) {
+            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
+                if (plcParameterInfo.getCodeId().equals(codeid))
+                    return plcParameterInfo;
+            }
+            return null;
+        } else
+            return null;
+    }
+
+
+    /**
+     * 鏍规嵁鍙傛暟鏍囪瘑 鑾峰彇鏌愪釜鍙傛暟瀹炰緥
+     *
+     * @param codeids 鍙傛暟鏍囪瘑
+     * @return 鑾峰彇鏌愪釜鍙傛暟瀹炰緥
+     */
+    public List<String> getPlcParameterValues(List<String> codeids) {
+        List<String> arrayList = new ArrayList<>();
+        if (plcParameterList != null) {
+            Map<String, PlcParameterInfo> resultMap = new LinkedHashMap<>(); // 浣跨敤 LinkedHashMap 淇濈暀鎻掑叆椤哄簭
+            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
+                if (codeids.contains(plcParameterInfo.getCodeId())) {
+                    resultMap.put(plcParameterInfo.getCodeId(), plcParameterInfo);
+                }
+            }
+            for (String codeId : codeids) { // 鎸夌収浼犲叆鍙傛暟鐨勯『搴忛亶鍘�
+                PlcParameterInfo plcParameterInfo = resultMap.get(codeId);
+                if (plcParameterInfo != null) {
+                    arrayList.add(plcParameterInfo.getValue());
+                } else {
+                    arrayList.add(null); // 濡傛灉鎵句笉鍒板搴旂殑鍊硷紝娣诲姞 null
+                }
+            }
+        }
+        return arrayList;
+    }
+
+
+    public List<String> getAddressListByCodeId(List<String> codeIdList) {
+        List<String> addressList = new ArrayList<>();
+        for (String codeId : codeIdList) {
+            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
+                if (plcParameterInfo.getCodeId().equals(codeId)) {
+                    int index = plcParameterInfo.getAddressIndex();
+                    String address = plcParameterInfo.getAddress(index);
+                    if (address != null) {
+                        addressList.add(address);
+                    }
+                }
+            }
+        }
+        return addressList;
+    }
+
+
+    /**
+     * 娣诲姞鍙傛暟瀹炰緥
+     *
+     * @param param 鍙傛暟瀹炰緥
+     */
+    public void addPlcParameter(PlcParameterInfo param) {
+        if (plcParameterList != null)
+            plcParameterList.add(param);
+        else {
+            plcParameterList = new ArrayList<PlcParameterInfo>();
+            plcParameterList.add(param);
+        }
+    }
+
+    /**
+     * 鏍规嵁PLC杩斿洖鐨勬暟鎹� 缁欏弬鏁板疄渚嬭祴鍊�
+     *
+     * @param plcValueArray PLC璇诲彇鍥炴潵鐨刡yte绫诲瀷鏁版嵁闆嗗悎
+     */
+    public void setPlcParameterList(byte[] plcValueArray) {
+        if (plcParameterList != null) {
+
+            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
+
+                byte[] valueList = new byte[plcParameterInfo.getAddressLength()];
+
+//                System.out.println(plcParameterInfo.getAddressLength());
+
+                for (int i = 0; i < plcParameterInfo.getAddressLength(); i++) {
+                    Array.setByte(valueList, i, plcValueArray[plcParameterInfo.getAddressIndex() + i]);
+
+                }
+                if (plcParameterInfo.getAddressLength() == 2) {
+                    plcParameterInfo.setValue(String.valueOf(byte2short(valueList)));
+                } else if (plcParameterInfo.getAddressLength() == 14) {
+                    plcParameterInfo.setValue((byteToHexString(valueList)));
+                } else {
+                    String valuestr = new String(valueList);
+                    plcParameterInfo.setValue(valuestr);
+                }
+            }
+        }
+    }
+
+    /**
+     * short绫诲瀷杞琤yte[]
+     *
+     * @param s short绫诲瀷鍊�
+     */
+    public static byte[] short2byte(short s) {
+        byte[] b = new byte[2];
+        for (int i = 0; i < 2; i++) {
+            int offset = 16 - (i + 1) * 8; //鍥犱负byte鍗�4涓瓧鑺傦紝鎵�浠ヨ璁$畻鍋忕Щ閲�
+            b[i] = (byte) ((s >> offset) & 0xff); //鎶�16浣嶅垎涓�2涓�8浣嶈繘琛屽垎鍒瓨鍌�
+        }
+        return b;
+    }
+
+    /**
+     * byte[]绫诲瀷杞瑂hort
+     *
+     * @param b byte[]绫诲瀷鍊�
+     */
+    public static short byte2short(byte[] b) {
+        short l = 0;
+        for (int i = 0; i < 2; i++) {
+            l <<= 8; //<<=鍜屾垜浠殑 +=鏄竴鏍风殑锛屾剰鎬濆氨鏄� l = l << 8
+            l |= (b[i] & 0xff); //鍜屼笂闈篃鏄竴鏍风殑  l = l | (b[i]&0xff)
+        }
+        return l;
+    }
+
+    public static String byteToHexString(byte[] bytes) {
+
+        String str = new String(bytes, StandardCharsets.UTF_8);
+        return str;
+    }
+
+
+}
\ No newline at end of file

--
Gitblit v1.8.0