package com.mes.common;
|
|
import java.util.ArrayList;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
public class PlcBitObject {
|
|
// 该模块数据类型,数据起始位置
|
private String plcAddressBegin;
|
// 数据地址长度:第一参数到最后一个参数的长度
|
private int plcAddressLength;
|
private ArrayList<PlcBitInfo> plcBitList;
|
|
/**
|
* @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<PlcBitInfo> getBitList() {
|
return plcBitList;
|
}
|
|
/**
|
* 根据参数标识 获取某个参数实例
|
*
|
* @param codeid 参数标识
|
* @return 获取某个参数实例
|
*/
|
public PlcBitInfo getPlcBit(String codeid) {
|
if (plcBitList != null) {
|
for (PlcBitInfo plcbitInfo : plcBitList) {
|
if (plcbitInfo.getCodeId().equals(codeid))
|
return plcbitInfo;
|
}
|
return null;
|
} else
|
return null;
|
}
|
|
/**
|
* 根据参数标识 获取某个参数实例
|
*
|
* @param codeid 参数标识
|
* @return 获取某个参数实例
|
*/
|
public List<Boolean> getPlcBitValues(List<String> codeids) {
|
List<Boolean> arrayList = new ArrayList<>();
|
if (plcBitList != null) {
|
Map<String, Boolean> resultMap = new LinkedHashMap<>(); // 使用 LinkedHashMap 保留插入顺序
|
for (PlcBitInfo plcBitInfo : plcBitList) {
|
if (codeids.contains(plcBitInfo.getCodeId().toString())) {
|
resultMap.put(plcBitInfo.getCodeId().toString(), plcBitInfo.getValue());
|
}
|
}
|
for (String codeId : codeids) { // 按照传入参数的顺序遍历
|
Boolean value = resultMap.get(codeId);
|
if (value != null) {
|
arrayList.add(value);
|
} else {
|
arrayList.add(null); // 如果找不到对应的值,添加 null
|
}
|
}
|
}
|
return arrayList;
|
}
|
|
|
public List<String> getAddressListByCodeId(List<String> codeIdList) {
|
List<String> addressList = new ArrayList<>();
|
for (String codeId : codeIdList) {
|
for (PlcBitInfo plcBitInfo : plcBitList) {
|
if (plcBitInfo.getCodeId().equals(codeId)) {
|
int index = plcBitInfo.getAddressIndex();
|
String address = plcBitInfo.getAddress(index);
|
if (address != null) {
|
addressList.add(address);
|
}
|
}
|
}
|
}
|
return addressList;
|
}
|
|
|
/**
|
* 添加参数实例
|
*
|
* @param param 参数实例
|
*/
|
public void addPlcBit(PlcBitInfo param) {
|
if (plcBitList != null)
|
plcBitList.add(param);
|
else {
|
plcBitList = new ArrayList<PlcBitInfo>();
|
plcBitList.add(param);
|
}
|
}
|
|
/**
|
* 根据PLC返回的数据 给参数实例赋值
|
*
|
* @param plcValueArray PLC读取回来的byte类型数据集合
|
*/
|
public void setPlcBitList(List<Boolean> plcValueArray) {
|
if (plcBitList != null) {
|
for (PlcBitInfo plcbitInfo : plcBitList) {
|
plcbitInfo.setValue(plcValueArray.get(plcbitInfo.getAddressIndex()));
|
}
|
}
|
}
|
|
|
|
}
|