Skip to content

Commit 58f46cc

Browse files
committed
Added command parameters
1 parent d1851cc commit 58f46cc

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

src/main/kotlin/ConfigurationParser.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1+
import java.io.BufferedReader
2+
import java.io.File
13
import java.io.StringReader
24
import javax.xml.bind.JAXBContext
35
import javax.xml.bind.JAXBException
46
import javax.xml.bind.annotation.*
57

68
class ConfigurationParser {
79

10+
private var fileName: String = ""
11+
fun setFileName(file: String) {
12+
fileName = file
13+
}
814
private fun load(): Device? {
915
try {
1016
val context = JAXBContext.newInstance(Device::class.java, Set::class.java, Random::class.java, Delay::class.java, Linear::class.java, Add::class.java, Sub::class.java)
1117
val unmarshaller = context.createUnmarshaller()
12-
val reader = StringReader(this::class.java.classLoader.getResource("configuration.xml")!!.readText())
13-
val device = unmarshaller.unmarshal(reader) as Device
14-
return device
18+
if(fileName.isEmpty()) {
19+
val reader = StringReader(this::class.java.classLoader.getResource("configuration.xml")!!.readText())
20+
val device = unmarshaller.unmarshal(reader) as Device
21+
return device
22+
}else{
23+
val bufferedReader: BufferedReader = File(fileName).bufferedReader()
24+
val device = unmarshaller.unmarshal(bufferedReader) as Device
25+
return device
26+
}
1527
} catch (e: JAXBException) {
1628
e.printStackTrace()
1729
}

src/main/kotlin/Main.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,45 @@ import kotlin.system.exitProcess
1717
description = ["Modbus TCP Simulator"])
1818
class Checksum : Callable<Int> {
1919

20-
@Option(names = ["-f", "--file"], description = ["configuration file (JSON)"])
21-
lateinit var file: File
20+
@Option(names = ["-f", "--file"], description = ["custom simulation configuration file (JSON)"])
21+
var file = ""
2222

2323
@Option(names = ["-p", "--port"], description = ["server TCP port"])
2424
var port = "502"
2525

26+
@Option(names = ["-sr", "--simulation-random"], description = ["random number simulation"])
27+
var simulationRandomValues = false
28+
29+
2630

2731
private lateinit var plcMemory: PlcMemory
2832
private lateinit var plcSimulation: PlcSimulation
33+
private lateinit var modbusServer: ModbusServer
2934

3035
override fun call(): Int {
3136
val mainCoroutineScope = CoroutineScope(Dispatchers.Default)
3237
val configuration = ConfigurationParser()
33-
plcMemory = PlcMemory(configuration)
34-
plcSimulation = PlcSimulation(configuration, plcMemory, mainCoroutineScope)
38+
39+
if(simulationRandomValues && file.isNotEmpty()){
40+
println("-f and -sr cannot be mixed, one of the simulations must be chosen")
41+
return -1
42+
}
3543
//val fileContents = Files.readAllBytes(file.toPath())
3644
//val digest = MessageDigest.getInstance(port).digest(fileContents)
3745
//println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
38-
var modbusServer = ModbusServer(plcMemory)
46+
if(simulationRandomValues) {
47+
modbusServer = ModbusServer(plcMemory)
48+
}else {
49+
if (file.isNotEmpty()) {
50+
// -f file based (Custom simulation)
51+
configuration.setFileName(file)
52+
}
53+
// if not set default, reading internal xml
54+
plcMemory = PlcMemory(configuration)
55+
plcSimulation = PlcSimulation(configuration, plcMemory, mainCoroutineScope)
56+
modbusServer = ModbusServer(plcMemory)
57+
}
58+
3959
try {
4060
modbusServer.start()
4161
modbusServer.block()

0 commit comments

Comments
 (0)