Skip to content

Commit e5f700b

Browse files
committed
Added random operation
1 parent 7b5b1b3 commit e5f700b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/main/kotlin/PlcSimulation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PlcSimulation(
2222
}
2323

2424
is Random -> {
25-
println("Random symbol ${element.symbol} valueMax ${element.valueMax} valueMin ${element.valueMin}")
25+
randomOperation(element, configuration, memory)
2626
}
2727

2828
is Delay -> {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)