-
Notifications
You must be signed in to change notification settings - Fork 1
/
AttributeLoggingFilter.java
120 lines (107 loc) · 4.19 KB
/
AttributeLoggingFilter.java
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
package opcUaServer;
import java.lang.reflect.Field;
import java.util.function.Predicate;
import org.eclipse.milo.opcua.sdk.server.nodes.filters.AttributeFilter;
import org.eclipse.milo.opcua.sdk.server.nodes.filters.AttributeFilterContext.GetAttributeContext;
import org.eclipse.milo.opcua.sdk.server.nodes.filters.AttributeFilterContext.SetAttributeContext;
import org.eclipse.milo.opcua.stack.core.AttributeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Class to monitor skill parameter and update their values
*/
public class AttributeLoggingFilter implements AttributeFilter {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final Predicate<AttributeId> attributePredicate;
private Field field;
private Object skill;
public AttributeLoggingFilter() {
this(attributeId -> true);
}
public AttributeLoggingFilter(Predicate<AttributeId> attributePredicate) {
this.attributePredicate = attributePredicate;
}
/**
* Constructor of class {@link AttributeLoggingFilter}
*
* @param attributePredicate Filter logic to apply
* @param field field whose value has to be updated
* @param skill instance of skill to get field
*/
public AttributeLoggingFilter(Predicate<AttributeId> attributePredicate, Field field, Object skill) {
this.attributePredicate = attributePredicate;
this.field = field;
this.skill = skill;
}
/**
* When an attribute (here skill parameter) is queried, then the value of the
* corresponding skills field is updated to actual value of skill parameter in
* OpcUa
*/
@Override
public Object getAttribute(GetAttributeContext ctx, AttributeId attributeId) {
Object value = ctx.getAttribute(attributeId);
// only log external reads
if (!attributePredicate.test(attributeId) || !ctx.getSession().isPresent())
return value;
DataValue dataValue = (DataValue) value;
try {
// setField(field, field.getType(), dataValue, skill);
field.set(skill, dataValue.getValue().getValue());
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
/**
* When an attribute (here skill parameter) is set, then the value of the
* corresponding skills field is updated to actual value of skill parameter in
* OpcUa
*/
@Override
public void setAttribute(SetAttributeContext ctx, AttributeId attributeId, Object value) {
// only log external writes
if (!attributePredicate.test(attributeId) || !ctx.getSession().isPresent())
return;
DataValue dataValue = (DataValue) value;
try {
// setField(field, field.getType(), dataValue, skill);
field.set(skill, dataValue.getValue().getValue());
logger.info("set nodeId={} attributeId={} value={}", ctx.getNode().getNodeId(), attributeId, value);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ctx.setAttribute(attributeId, value);
}
/**
* Method to set value of field to actual value in OpcUa
*
* @param field field whose value should be changed
* @param type data type of field
* @param dataValue to get actual value
* @param skill instance of skill to get field
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
// public void setField(Field field, Class<?> type, DataValue dataValue, Object skill)
// throws IllegalArgumentException, IllegalAccessException {
// if (type == boolean.class) {
// field.set(skill, (boolean) dataValue.getValue().getValue());
// } else if (type == byte.class) {
// field.set(skill, (byte) dataValue.getValue().getValue());
// } else if (type == short.class) {
// field.set(skill, (short) dataValue.getValue().getValue());
// } else if (type == int.class) {
// field.set(skill, (int) dataValue.getValue().getValue());
// } else if (type == long.class) {
// field.set(skill, (long) dataValue.getValue().getValue());
// } else if (type == float.class) {
// field.set(skill, (float) dataValue.getValue().getValue());
// } else if (type == double.class) {
// field.set(skill, (double) dataValue.getValue().getValue());
// }
// }
}