@@ -2,12 +2,16 @@ package operations
2
2
3
3
import Configuration
4
4
import EnvironmentVariables
5
+ import PlcMemory
6
+ import Register
5
7
import isNumeric
6
8
import java.rmi.NotBoundException
9
+ import java.util.concurrent.CancellationException
7
10
8
11
abstract class BaseOperation (
9
12
private val parameters : EnvironmentVariables ,
10
- private val configuration : Configuration
13
+ private val configuration : Configuration ,
14
+ private val memory : PlcMemory
11
15
) {
12
16
/* *
13
17
* processValue convert any variable name used as value to the actual value
@@ -27,9 +31,49 @@ abstract class BaseOperation(
27
31
// value is a symbolic register
28
32
var symbolicVariable = configuration.registers.getVarConfiguration(value)
29
33
if (symbolicVariable != null ) {
30
- return symbolicVariable.value
34
+ return resolveSymbolicVariable( symbolicVariable)
31
35
}
32
36
throw NotBoundException (" Invalid value, value is not numeric and also not a parameter nor a symbolic register" )
37
+ }
38
+
39
+ fun resolveSymbolicVariable (variable : Register ) : String {
40
+ when (variable.addressType) {
41
+
42
+ AddressType .HOLDING_REGISTER -> {
43
+ // get the current value
44
+ // Mult
45
+ // set back the new value
33
46
47
+ if (variable.datatype == " FLOAT32" ) {
48
+ var currentValue = memory.readHoldingRegister(variable.address.toInt(), 2 )
49
+ if (currentValue.isEmpty()){
50
+ throw CancellationException (" Error - Base Operation" )
51
+ }
52
+ val intValue = (( currentValue[1 ].toInt() shl 16 ) or (currentValue[0 ].toInt() and 0xFFFF ))
53
+ return java.lang.Float .intBitsToFloat(intValue).toString()
54
+ } else {
55
+ var currentValue = memory.readHoldingRegister(variable.address.toInt(), 1 )
56
+ if (currentValue.isEmpty()){
57
+ throw CancellationException (" Error - Base Operation" )
58
+ }
59
+ return currentValue.first().toInt().toString()
60
+ }
61
+ }
62
+ AddressType .INPUT_REGISTER -> {
63
+ val currentValue = memory.readInputRegister(variable.address.toInt(), 1 )
64
+ return currentValue.first().toShort().toString()
65
+ }
66
+ AddressType .COIL -> {
67
+ var currentValue = memory.readCoilStatus(variable.address.toInt(), 2 )
68
+ if (currentValue.isEmpty()){
69
+ throw CancellationException (" Error - Base Operation" )
70
+ }
71
+ return currentValue.first().toString()
72
+ }
73
+
74
+ else -> {
75
+ throw CancellationException (" Error - Base Operation" )
76
+ }
77
+ }
34
78
}
35
79
}
0 commit comments