huang
5 天以前 0dfdc8148cc266fd3e877183c5b162fb986d5c65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.mes.utils;
 
import com.github.xingshuangs.iot.protocol.s7.serializer.S7Parameter;
import com.github.xingshuangs.iot.protocol.s7.serializer.S7Variable;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @Author : zhoush
 * @Date: 2025/9/3 15:43
 * @Description:
 */
public class S7ParameterUtils {
 
 
    /**
     * 获取单个地址信息
     *
     * @param targetClass
     * @param fieldName
     * @param value
     * @param <T>
     * @return
     */
    public static <T> List<S7Parameter> getSingleS7ParameterList(Class<T> targetClass, String fieldName, Object value) {
        List<S7Parameter> s7ParameterList = new ArrayList<>();
        try {
            Field field = targetClass.getDeclaredField(fieldName);
            final S7Variable s7Variable = field.getAnnotation(S7Variable.class);
            if (s7Variable == null) {
                return new ArrayList<>();
            }
            s7ParameterList.add(new S7Parameter(s7Variable.address(), s7Variable.type(), s7Variable.count(), value));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return s7ParameterList;
    }
 
    /**
     * 获取单个地址信息
     *
     * @param targetClass
     * @param dbArea
     * @param beginIndex
     * @param fieldName
     * @param value
     * @param <T>
     * @return
     */
    public static <T> List<S7Parameter> getSingleS7ParameterList(Class<T> targetClass, String dbArea, Integer beginIndex, String fieldName, Object value) {
        List<S7Parameter> s7ParameterList = new ArrayList<>();
        try {
            Field field = targetClass.getDeclaredField(fieldName);
            final S7Variable s7Variable = field.getAnnotation(S7Variable.class);
            if (s7Variable == null) {
                return new ArrayList<>();
            }
            s7ParameterList.add(new S7Parameter(dbArea + beginIndex + s7Variable.address(), s7Variable.type(), s7Variable.count(), value));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return s7ParameterList;
    }
 
    /**
     * 获取多个地址信息:注意value的值要与字段名一致
     *
     * @param targetClass
     * @param fieldNameList
     * @param values
     * @param <T>
     * @return
     */
    public static <T> List<S7Parameter> getS7ParameterList(Class<T> targetClass, List<String> fieldNameList, List<Object> values) {
        List<S7Parameter> s7ParameterList = new ArrayList<>();
        for (int i = 0; i < fieldNameList.size(); i++) {
            try {
                Field field = targetClass.getDeclaredField(fieldNameList.get(i));
                final S7Variable s7Variable = field.getAnnotation(S7Variable.class);
                if (s7Variable == null) {
                    continue;
                }
                s7ParameterList.add(new S7Parameter(s7Variable.address(), s7Variable.type(), s7Variable.count(), values.get(i)));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return s7ParameterList;
    }
 
 
    /**
     * 获取多个地址信息:注意value的值要与字段名一致
     *
     * @param targetClass
     * @param dbArea
     * @param beginIndex
     * @param fieldNameList
     * @param values
     * @param <T>
     * @return
     */
    public static <T> List<S7Parameter> getS7ParameterList(Class<T> targetClass, String dbArea, Integer beginIndex, List<String> fieldNameList, List<Object> values) {
        List<S7Parameter> s7ParameterList = new ArrayList<>();
        for (int i = 0; i < fieldNameList.size(); i++) {
            try {
                Field field = targetClass.getDeclaredField(fieldNameList.get(i));
                final S7Variable s7Variable = field.getAnnotation(S7Variable.class);
                if (s7Variable == null) {
                    continue;
                }
                s7ParameterList.add(new S7Parameter(dbArea + beginIndex + s7Variable.address(), s7Variable.type(), s7Variable.count(), values.get(i)));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return s7ParameterList;
    }
}