wuyouming666
2024-02-23 692c1a185d768a18c2007348806d775b2fbdeaa7
springboot-vue3/src/main/java/com/example/springboot/entity/device/PlcParameterObject.java
@@ -1,8 +1,11 @@
package com.example.springboot.entity.device;
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 {
@@ -64,23 +67,57 @@
            return null;
    }
    /**
     * 根据参数标识 获取某个参数实例
     * 
     * @param codeid 参数标识
     * @param codeids 参数标识
     * @return 获取某个参数实例
     */
    public List<String> getPlcParameterValues(List<String> codeids) {
        List<String> arrayList = new ArrayList();
        List<String> arrayList = new ArrayList<>();
        if (plcParameterList != null) {
            Map<String, PlcParameterInfo> resultMap = new LinkedHashMap<>(); // 使用 LinkedHashMap 保留插入顺序
            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
                if (codeids.contains(plcParameterInfo.getCodeId().toString()))
                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;
    }
    /**
     * 添加参数实例
     * 
@@ -102,14 +139,24 @@
     */
    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);
@@ -145,5 +192,11 @@
       return l;
   }
    public static String byteToHexString(byte[] bytes) {
        String str = new String(bytes, StandardCharsets.UTF_8);
        return str;
    }
}