From f39fd71eedfd828e715d33356a8c38ffbe66eb1a Mon Sep 17 00:00:00 2001 From: wuyouming666 <2265557248@qq.com> Date: 星期五, 24 五月 2024 15:28:09 +0800 Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/HangZhouMes --- hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java | 207 ++++++++++++++++++++++++++++++++++----------------- 1 files changed, 137 insertions(+), 70 deletions(-) diff --git a/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java index e4f45f6..dae4ca1 100644 --- a/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java +++ b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java @@ -1,11 +1,11 @@ package com.mes.device; +import com.github.xingshuangs.iot.utils.IntegerUtil; +import com.github.xingshuangs.iot.utils.ShortUtil; + 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; +import java.util.*; public class PlcParameterObject { @@ -13,8 +13,8 @@ private String plcAddressBegin; // 鏁版嵁鍦板潃闀垮害锛氱涓�鍙傛暟鍒版渶鍚庝竴涓弬鏁扮殑闀垮害 private int plcAddressLength; - private ArrayList<PlcParameterInfo> plcParameterList; - + //private ArrayList<PlcParameterInfo> plcParameterList; + private LinkedHashMap<String,PlcParameterInfo> plcParameterMap; /** @@ -48,46 +48,50 @@ /** * @return 鑾峰彇鍙傛暟瀹炰緥闆嗗悎 */ - public ArrayList<PlcParameterInfo> getPlcParameterList() { - return plcParameterList; + public LinkedHashMap<String,PlcParameterInfo> getPlcParameterMap() { + return plcParameterMap; } /** * 鏍规嵁鍙傛暟鏍囪瘑 鑾峰彇鏌愪釜鍙傛暟瀹炰緥 - * + * * @param codeid 鍙傛暟鏍囪瘑 * @return 鑾峰彇鏌愪釜鍙傛暟瀹炰緥 */ public PlcParameterInfo getPlcParameter(String codeid) { - if (plcParameterList != null) { + if (plcParameterMap != null) { + return plcParameterMap.get(codeid); + } else { + return null; + } + /*if (plcParameterList != null) { for (PlcParameterInfo plcParameterInfo : plcParameterList) { if (plcParameterInfo.getCodeId().equals(codeid)) return plcParameterInfo; } return null; } else - return null; + 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 淇濈暀鎻掑叆椤哄簭 + if (plcParameterMap != 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); + PlcParameterInfo plcParameterInfo = plcParameterMap.get(codeId); if (plcParameterInfo != null) { arrayList.add(plcParameterInfo.getValue()); } else { @@ -98,52 +102,78 @@ return arrayList; } - + /** + * 鏍规嵁鍙傛暟鏍囪瘑 鑾峰彇浼犲叆鍙傛暟鐨刾lc鍦板潃 + * + * @param codeIdList 鍙傛暟鏍囪瘑 + * @return 浼犲叆鍙傛暟鐨刾lc鍦板潃 + */ 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 (plcParameterMap!=null) + { + for (String codeId : codeIdList) { + String address = plcParameterMap.get(codeId).getAddress(); if (address != null) { addressList.add(address); } - } - } + } } return addressList; } - - - - /** * 娣诲姞鍙傛暟瀹炰緥 - * + * * @param param 鍙傛暟瀹炰緥 */ public void addPlcParameter(PlcParameterInfo param) { - if (plcParameterList != null) + if (plcParameterMap != null) { + plcParameterMap.put(param.getCodeId(), param); + } + else { + plcParameterMap =new LinkedHashMap<String,PlcParameterInfo>(); + plcParameterMap.put(param.getCodeId(), 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) { - + if (plcParameterMap != null) { + Collection<PlcParameterInfo> values= plcParameterMap.values(); + for (PlcParameterInfo plcParameterInfo :values) { + 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(ShortUtil.toUInt16(valueList))); + } else if (plcParameterInfo.getAddressLength() == 4) { + plcParameterInfo.setValue(String.valueOf(IntegerUtil.toUInt32(valueList))); + } + else if (plcParameterInfo.getAddressLength() >10) { + plcParameterInfo.setValue((byteToHexString(valueList))); + } else { + plcParameterInfo.setValue((byteToHexString(valueList))); + } + } + } +/* if (plcParameterList != null) { + for (PlcParameterInfo plcParameterInfo : plcParameterList) { - + byte[] valueList = new byte[plcParameterInfo.getAddressLength()]; // System.out.println(plcParameterInfo.getAddressLength()); @@ -152,54 +182,91 @@ Array.setByte(valueList, i, plcValueArray[plcParameterInfo.getAddressIndex() + i]); } - if (plcParameterInfo.getAddressLength()==2) { - plcParameterInfo.setValue(String.valueOf(byte2short(valueList))); + if (plcParameterInfo.getAddressLength() == 2) { + plcParameterInfo.setValue(String.valueOf(byte2short(valueList))); + } else if (plcParameterInfo.getAddressLength() == 4) { + plcParameterInfo.setValue(String.valueOf(byte2int(valueList))); } - else if (plcParameterInfo.getAddressLength()==14) { + else if (plcParameterInfo.getAddressLength() >10) { plcParameterInfo.setValue((byteToHexString(valueList))); - } - - else - { + } else { String valuestr = new String(valueList); plcParameterInfo.setValue(valuestr); } } + }*/ + } + /** + * 鎶婂啓鍏ュ�艰浆鍖栦负byte[] + * @param param 鍙傛暟瀹炰緥 + * @param data 鍐欏叆鍊肩殑瀛楃绫诲瀷 + */ + public byte[] setValueToBytes(PlcParameterInfo param, String data) { + if (param.getAddressLength() == 2) { + return ShortUtil.toByteArray(Integer.parseInt(data)); + + } else if (param.getAddressLength() == 4) { + + return IntegerUtil.toByteArray(Long.parseLong(data)); + } + else if (param.getAddressLength() >10) { + return data.getBytes(); + } else { + return data.getBytes(); } } - /** + + /** * 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浣嶈繘琛屽垎鍒瓨鍌� + public static byte[] short2byte(short s) { + byte[] b = new byte[2]; + for (int i = 0; i < 2; i++) { + int offset = 16 - (i + 1) * 8; //璁$畻鍋忕Щ閲� + 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; } + /** + * 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; + } - + /** + * byte[]绫诲瀷杞瑂hort + * + * @param b byte[]绫诲瀷鍊� + */ + public static int byte2int(byte[] b) { + int l = 0; + for (int i = 0; i < 4; i++) { + l <<= 8; //<<=鍜屾垜浠殑 +=鏄竴鏍风殑锛屾剰鎬濆氨鏄� l = l << 8 + l |= (b[3-i] & 0xff); //鍜屼笂闈篃鏄竴鏍风殑 l = l | (b[i]&0xff) + } + return l; + } + public static byte[] int2byte(int s){ + byte[] b = new byte[2]; + for(int i = 0; i < 4; i++){ + int offset = 16 - (i+1)*8; //鍥犱负byte鍗�4涓瓧鑺傦紝鎵�浠ヨ璁$畻鍋忕Щ閲� + b[i] = (byte)((s >> offset)&0xff); //鎶�32浣嶅垎涓�4涓�8浣嶈繘琛屽垎鍒瓨鍌� + } + return b; + } + public static String byteToHexString(byte[] bytes) { + String str = new String(bytes, StandardCharsets.UTF_8).trim(); + return str; + } } \ No newline at end of file -- Gitblit v1.8.0