1
+ package operations
2
+
3
+ import AddressType
4
+ import Configuration
5
+ import PlcMemory
6
+ import Random
7
+ import java.util.concurrent.CancellationException
8
+
9
+
10
+ fun randomOperation (element : Random , configuration : Configuration , memory : PlcMemory ){
11
+ println (" Random symbol ${element.symbol} value min ${element.valueMin} value max ${element.valueMax} " )
12
+ var variable = configuration.registers.getVarConfiguration(element.symbol)
13
+ if (variable == null ) {
14
+ println (" ERROR: Symbol ${element.symbol} not found during Set execution" )
15
+ throw CancellationException (" Error - Random" )
16
+ } else {
17
+ if (variable.addressType == AddressType .COIL || variable.addressType == AddressType .DISCRETE_INPUT ){
18
+ println (" ERROR: Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Random operation" )
19
+ throw CancellationException (" Error - Random" )
20
+ }
21
+
22
+ when (variable.addressType) {
23
+
24
+ AddressType .HOLDING_REGISTER -> {
25
+ // get the current value
26
+ // add
27
+ // set back the new value
28
+
29
+ if (variable.datatype == " FLOAT32" ) {
30
+ val random: Float = element.valueMin.toFloat() + kotlin.random.Random .nextFloat() * (element.valueMax.toFloat() - element.valueMin.toFloat())
31
+ setHoldingRegisterFloat32(random, memory, variable)
32
+ } else {
33
+ val random: Int = element.valueMin.toInt() + kotlin.random.Random .nextInt() * (element.valueMax.toInt() - element.valueMin.toInt())
34
+ setHoldingRegisterInt16(memory, variable, random.toShort())
35
+ }
36
+ }
37
+ AddressType .INPUT_REGISTER -> {
38
+ val random: Int = element.valueMin.toInt() + kotlin.random.Random .nextInt() * (element.valueMax.toInt() - element.valueMin.toInt())
39
+ memory.setInputRegister(variable.address.toInt(),random.toShort())
40
+ }
41
+
42
+ else -> {
43
+ throw CancellationException (" Error - Random" )
44
+ }
45
+ }
46
+ }
47
+ }
0 commit comments