Skip to content

Commit c880d36

Browse files
committed
Refactored code
1 parent 58f46cc commit c880d36

File tree

1 file changed

+74
-52
lines changed

1 file changed

+74
-52
lines changed

src/main/kotlin/PlcSimulation.kt

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import kotlinx.coroutines.*
2+
import java.lang.Float
23

34

45
class PlcSimulation(
@@ -17,58 +18,7 @@ class PlcSimulation(
1718
simulationConfiguration.randomElements.forEach { element ->
1819
when (element) {
1920
is Set -> {
20-
println("Set symbol ${element.symbol} value ${element.value}")
21-
var variable = configuration.registers.getVarConfiguration(element.symbol)
22-
if (variable == null) {
23-
println("ERROR: Symbol ${element.symbol} not found during Set execution")
24-
throw CancellationException("Error - Set")
25-
}else {
26-
if ((variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT) && (variable.value != "0" && variable.value != "1")) {
27-
println("ERROR: Invalid value format. Symbol ${element.symbol} is of BOOL type and supports only values 0 or 1 not ${variable.value}")
28-
}
29-
when (variable.addressType) {
30-
AddressType.HOLDING_REGISTER -> {
31-
if(variable.datatype == "FLOAT32"){
32-
val floatValue = element.value.toFloat()
33-
val intValue = java.lang.Float.floatToIntBits(floatValue)
34-
val lowWord = intValue and 0xFFFF
35-
val highWord = (intValue ushr 16) and 0xFFFF
36-
memory.presetMultipleRegisters(
37-
mutableListOf<Pair<Int, Short>>(
38-
Pair<Int, Short>(variable.address.toInt(),
39-
lowWord.toShort()),
40-
Pair<Int, Short>(variable.address.toInt() + 1,
41-
highWord.toShort()),
42-
)
43-
)
44-
}else{
45-
//Int16
46-
memory.presetMultipleRegisters(
47-
mutableListOf<Pair<Int, Short>>(
48-
Pair<Int, Short>(variable.address.toInt(),
49-
element.value.toShort())
50-
)
51-
)
52-
}
53-
}
54-
AddressType.COIL -> {
55-
memory.forceSingleCoil(
56-
variable.address.toInt(),
57-
element.value.toBooleanFromBinary()
58-
)
59-
}
60-
61-
AddressType.DISCRETE_INPUT -> {
62-
memory.setDiscreteInput(
63-
variable.address.toInt(),
64-
element.value.toBooleanFromBinary()
65-
)
66-
}
67-
AddressType.INPUT_REGISTER -> {
68-
memory.setInputRegister(variable.address.toInt(), element.value.toShort())
69-
}
70-
}
71-
}
21+
setOperation(element, configuration, memory)
7222
}
7323

7424
is Random -> {
@@ -111,4 +61,76 @@ class PlcSimulation(
11161

11262
}
11363

64+
private fun setOperation(element: Set, configuration: Configuration, memory: PlcMemory) {
65+
println("Set symbol ${element.symbol} value ${element.value}")
66+
var variable = configuration.registers.getVarConfiguration(element.symbol)
67+
if (variable == null) {
68+
println("ERROR: Symbol ${element.symbol} not found during Set execution")
69+
throw CancellationException("Error - Set")
70+
} else {
71+
if ((variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT) && (variable.value != "0" && variable.value != "1")) {
72+
println("ERROR: Invalid value format. Symbol ${element.symbol} is of BOOL type and supports only values 0 or 1 not ${variable.value}")
73+
}
74+
when (variable.addressType) {
75+
AddressType.HOLDING_REGISTER -> {
76+
if (variable.datatype == "FLOAT32") {
77+
val floatValue = element.value.toFloat()
78+
setHoldingRegisterFloat32(floatValue, memory, variable)
79+
} else {
80+
//Int16
81+
val intValue = element.value.toShort()
82+
setHoldingRegisterInt16(memory, variable, intValue)
83+
}
84+
}
85+
86+
AddressType.COIL -> {
87+
memory.forceSingleCoil(
88+
variable.address.toInt(),
89+
element.value.toBooleanFromBinary()
90+
)
91+
}
92+
93+
AddressType.DISCRETE_INPUT -> {
94+
memory.setDiscreteInput(
95+
variable.address.toInt(),
96+
element.value.toBooleanFromBinary()
97+
)
98+
}
99+
100+
AddressType.INPUT_REGISTER -> {
101+
memory.setInputRegister(variable.address.toInt(), element.value.toShort())
102+
}
103+
}
104+
}
105+
}
106+
107+
private fun setHoldingRegisterInt16(memory: PlcMemory, variable: Register, intValue: Short) {
108+
memory.presetMultipleRegisters(
109+
mutableListOf<Pair<Int, Short>>(
110+
Pair<Int, Short>(
111+
variable.address.toInt(),
112+
intValue
113+
)
114+
)
115+
)
116+
}
117+
118+
private fun setHoldingRegisterFloat32(floatValue: kotlin.Float, memory: PlcMemory, variable: Register) {
119+
val intValue = Float.floatToIntBits(floatValue)
120+
val lowWord = intValue and 0xFFFF
121+
val highWord = (intValue ushr 16) and 0xFFFF
122+
memory.presetMultipleRegisters(
123+
mutableListOf<Pair<Int, Short>>(
124+
Pair<Int, Short>(
125+
variable.address.toInt(),
126+
lowWord.toShort()
127+
),
128+
Pair<Int, Short>(
129+
variable.address.toInt() + 1,
130+
highWord.toShort()
131+
),
132+
)
133+
)
134+
}
135+
114136
}

0 commit comments

Comments
 (0)