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 数据区 读取所有数据所需的长度(以byte类型为基准)
|
*/
|
public int getPlcAddressLength() {
|
return plcAddressLength;
|
}
|
|
/**
|
* @return 设置:数据区 读取所有数据所需的长度(以byte类型为基准)
|
*/
|
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读取回来的byte类型数据集合
|
*/
|
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类型转byte[]
|
*
|
* @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[]类型转short
|
*
|
* @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;
|
}
|
|
|
}
|