-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package operations | ||
|
||
import AddressType | ||
import Configuration | ||
import PlcMemory | ||
import Random | ||
import java.util.concurrent.CancellationException | ||
|
||
|
||
fun randomOperation(element: Random, configuration: Configuration, memory: PlcMemory){ | ||
println("Random symbol ${element.symbol} value min ${element.valueMin} value max ${element.valueMax}") | ||
var variable = configuration.registers.getVarConfiguration(element.symbol) | ||
if (variable == null) { | ||
println("ERROR: Symbol ${element.symbol} not found during Set execution") | ||
throw CancellationException("Error - Random") | ||
} else { | ||
if(variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT){ | ||
println("ERROR: Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Random operation") | ||
throw CancellationException("Error - Random") | ||
} | ||
|
||
when (variable.addressType) { | ||
|
||
AddressType.HOLDING_REGISTER -> { | ||
//get the current value | ||
//add | ||
//set back the new value | ||
|
||
if (variable.datatype == "FLOAT32") { | ||
val random: Float = element.valueMin.toFloat() + kotlin.random.Random.nextFloat() * (element.valueMax.toFloat() - element.valueMin.toFloat()) | ||
setHoldingRegisterFloat32(random, memory, variable) | ||
} else { | ||
val random: Int = element.valueMin.toInt() + kotlin.random.Random.nextInt() * (element.valueMax.toInt() - element.valueMin.toInt()) | ||
setHoldingRegisterInt16(memory, variable, random.toShort()) | ||
} | ||
} | ||
AddressType.INPUT_REGISTER -> { | ||
val random: Int = element.valueMin.toInt() + kotlin.random.Random.nextInt() * (element.valueMax.toInt() - element.valueMin.toInt()) | ||
memory.setInputRegister(variable.address.toInt(),random.toShort()) | ||
} | ||
|
||
else -> { | ||
throw CancellationException("Error - Random") | ||
} | ||
} | ||
} | ||
} |