1
+ package operations
2
+
3
+ import Configuration
4
+ import EnvironmentVariables
5
+ import PlcMemory
6
+ import Trace
7
+ import org.slf4j.LoggerFactory
8
+ import java.util.concurrent.CancellationException
9
+
10
+ class TraceOperation (
11
+ private val configuration : Configuration , private val memory : PlcMemory , environmentVariables : EnvironmentVariables
12
+
13
+ ) : BaseOperation(environmentVariables, configuration) {
14
+ companion object {
15
+ val logger = LoggerFactory .getLogger(" TraceOperation" )
16
+ }
17
+
18
+ fun traceOperation (element : Trace ) {
19
+ var valueToTrace : String = " "
20
+ var variable = configuration.registers.getVarConfiguration(element.symbol)
21
+ if (variable == null ) {
22
+ TraceOperation .logger.error(" Symbol ${element.symbol} not found during Trace execution" )
23
+ throw CancellationException (" Error - Trace" )
24
+ } else {
25
+ when (variable.addressType) {
26
+
27
+ AddressType .HOLDING_REGISTER -> {
28
+ // get the current value
29
+
30
+ if (variable.datatype == " FLOAT32" ) {
31
+ var currentValue = memory.readHoldingRegister(variable.address.toInt(), 2 )
32
+ if (currentValue.isEmpty()) {
33
+ SubOperation .logger.error(" Trace Operation - Unable to get value of ${element.symbol} address ${variable.address} " )
34
+ throw CancellationException (" Error - Trace" )
35
+ }
36
+ val intValue = ((currentValue[1 ].toInt() shl 16 ) or (currentValue[0 ].toInt() and 0xFFFF ))
37
+ val currentFloatValue = java.lang.Float .intBitsToFloat(intValue)
38
+ valueToTrace = currentValue.toString()
39
+ } else {
40
+ var currentValue = memory.readHoldingRegister(variable.address.toInt(), 1 )
41
+ if (currentValue.isEmpty()) {
42
+ SubOperation .logger.error(" Trace Operation - Unable to get value of ${element.symbol} address ${variable.address} " )
43
+ throw CancellationException (" Error - Add" )
44
+ }
45
+ valueToTrace = currentValue.first().toInt().toString()
46
+ }
47
+ }
48
+
49
+ AddressType .INPUT_REGISTER -> {
50
+ val currentValue = memory.readInputRegister(variable.address.toInt(), 1 )
51
+ valueToTrace = currentValue.first().toString()
52
+ }
53
+
54
+ AddressType .COIL -> {
55
+ val currentValue = memory.readCoilStatus(variable.address.toInt(), 1 )
56
+ valueToTrace = currentValue.first().toString()
57
+ }
58
+
59
+ AddressType .DISCRETE_INPUT -> {
60
+ val currentValue = memory.readInputStatus(variable.address.toInt(), 1 )
61
+ valueToTrace = currentValue.first().toString()
62
+ }
63
+
64
+ }
65
+ TraceOperation .logger.info(" TRACE - Symbol: ${element.symbol} Value: $valueToTrace " )
66
+
67
+ }
68
+ }
69
+
70
+ }
0 commit comments