added config file with settings to flash client
This commit is contained in:
@@ -1,44 +1,51 @@
|
|||||||
import com.google.protobuf.ByteString
|
import com.google.protobuf.ByteString
|
||||||
import com.martiansoftware.jsap.FlaggedOption
|
import com.martiansoftware.jsap.FlaggedOption
|
||||||
import com.martiansoftware.jsap.JSAP
|
import com.martiansoftware.jsap.JSAP
|
||||||
|
import com.martiansoftware.jsap.JSAPResult
|
||||||
import io.netty.buffer.Unpooled
|
import io.netty.buffer.Unpooled
|
||||||
import io.netty.handler.codec.http.*
|
import io.netty.handler.codec.http.*
|
||||||
import proto.Carkot
|
import proto.Carkot
|
||||||
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.io.PrintWriter
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/11/16.
|
* Created by user on 7/11/16.
|
||||||
*/
|
*/
|
||||||
|
val configFileName = "./config.cfg"
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val jsap: JSAP = JSAP()
|
val jsap: JSAP = JSAP()
|
||||||
setOptions(jsap)
|
setOptions(jsap)
|
||||||
|
|
||||||
val config = jsap.parse(args)
|
val clArgsConfig = jsap.parse(args)
|
||||||
if (!config.success() || config.getBoolean("help")) {
|
if (!clArgsConfig.success() || clArgsConfig.getBoolean("help")) {
|
||||||
println(jsap.getHelp())
|
println(jsap.getHelp())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
val fileConfig = readFileConfig()
|
||||||
val host = config.getString("host")
|
//if argument get in console, then its primary, else from config, else default
|
||||||
val port = config.getInt("port")
|
val host = getActualValue("host", clArgsConfig, fileConfig)
|
||||||
val base = config.getString("mcuSystem")
|
val port = getActualValue("port", clArgsConfig, fileConfig, "8888").toInt()
|
||||||
val pathToFile = config.getString("binFile")
|
val mcuSystem = getActualValue("mcuSystem", clArgsConfig, fileConfig, "0x08000000")
|
||||||
|
val flashFilePath = getActualValue("flash", clArgsConfig, fileConfig)
|
||||||
|
val actualValues = mapOf<String, String>(Pair("host", host), Pair("port", port.toString()), Pair("mcuSystem", mcuSystem), Pair("flash", flashFilePath))
|
||||||
|
saveFileConfig(actualValues)
|
||||||
var fileBytes: ByteArray = ByteArray(0);
|
var fileBytes: ByteArray = ByteArray(0);
|
||||||
try {
|
try {
|
||||||
FileInputStream(pathToFile).use { fis ->
|
FileInputStream(flashFilePath).use { fis ->
|
||||||
fileBytes = ByteArray(fis.available())
|
fileBytes = ByteArray(fis.available())
|
||||||
fis.read(fileBytes)
|
fis.read(fileBytes)
|
||||||
}
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
println("error in read file $pathToFile")
|
println("error in read file $flashFilePath")
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
val bytesBinTest = Carkot.Upload.newBuilder().setData(ByteString.copyFrom(fileBytes)).setBase(base).build().toByteArray()
|
val bytesBinTest = Carkot.Upload.newBuilder().setData(ByteString.copyFrom(fileBytes)).setBase(mcuSystem).build().toByteArray()
|
||||||
val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/loadBin", Unpooled.copiedBuffer(bytesBinTest));
|
val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/loadBin", Unpooled.copiedBuffer(bytesBinTest));
|
||||||
request.headers().set(HttpHeaderNames.HOST, host)
|
request.headers().set(HttpHeaderNames.HOST, host)
|
||||||
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
|
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
|
||||||
@@ -51,11 +58,62 @@ fun main(args: Array<String>) {
|
|||||||
println(client.ClientHandler.requestResult.stdOut)
|
println(client.ClientHandler.requestResult.stdOut)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setOptions(jsap:JSAP) {
|
fun saveFileConfig(actualValues: Map<String, String>) {
|
||||||
val opthost = FlaggedOption("host").setStringParser(JSAP.STRING_PARSER).setRequired(true).setShortFlag('h').setLongFlag("host")
|
var dataToFile = ""
|
||||||
val optPort = FlaggedOption("port").setStringParser(JSAP.INTEGER_PARSER).setDefault("8888").setRequired(false).setShortFlag('p').setLongFlag("port")
|
actualValues.forEach { e -> dataToFile += ("${e.key}:${e.value}\n") }
|
||||||
val optBinPath = FlaggedOption("binFile").setStringParser(JSAP.STRING_PARSER).setRequired(true).setShortFlag('f').setLongFlag("binFile")
|
val file = File(configFileName)
|
||||||
val optmcuSystem = FlaggedOption("mcuSystem").setStringParser(JSAP.STRING_PARSER).setRequired(false).setDefault("0x08000000").setShortFlag('s')
|
if (!file.exists()) {
|
||||||
|
try {
|
||||||
|
file.createNewFile()
|
||||||
|
|
||||||
|
} catch (e: IOException) {
|
||||||
|
println("error creating config file. parameters don't saved")
|
||||||
|
e.printStackTrace()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PrintWriter(file).use { printWriter ->
|
||||||
|
printWriter.write(dataToFile)
|
||||||
|
printWriter.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getActualValue(argName: String, commandLineConfig: JSAPResult, fileConfig: Map<String, String>, defaultValue: String = ""): String {
|
||||||
|
return if (commandLineConfig.getString(argName) != null) {
|
||||||
|
commandLineConfig.getString(argName)
|
||||||
|
} else {
|
||||||
|
fileConfig.getOrElse(argName, {
|
||||||
|
defaultValue
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun readFileConfig(): Map<String, String> {
|
||||||
|
val result = mutableMapOf<String, String>()
|
||||||
|
try {
|
||||||
|
Scanner(File(configFileName)).use { scanner ->
|
||||||
|
while (scanner.hasNext()) {
|
||||||
|
val currentString = scanner.nextLine()
|
||||||
|
val keyValuePair = currentString.split(":")
|
||||||
|
if (keyValuePair.size != 2) {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
result.put(keyValuePair[0], keyValuePair[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setOptions(jsap: JSAP) {
|
||||||
|
val opthost = FlaggedOption("host").setStringParser(JSAP.STRING_PARSER).setRequired(false).setShortFlag('h').setLongFlag("host")
|
||||||
|
val optPort = FlaggedOption("port").setStringParser(JSAP.INTEGER_PARSER).setRequired(false).setShortFlag('p').setLongFlag("port")
|
||||||
|
val optBinPath = FlaggedOption("flash").setStringParser(JSAP.STRING_PARSER).setRequired(false).setShortFlag('f').setLongFlag("flash")
|
||||||
|
val optmcuSystem = FlaggedOption("mcuSystem").setStringParser(JSAP.STRING_PARSER).setRequired(false).setShortFlag('s')
|
||||||
val optHelp = FlaggedOption("help").setStringParser(JSAP.BOOLEAN_PARSER).setRequired(false).setDefault("false").setLongFlag("help")
|
val optHelp = FlaggedOption("help").setStringParser(JSAP.BOOLEAN_PARSER).setRequired(false).setDefault("false").setLongFlag("help")
|
||||||
jsap.registerParameter(opthost)
|
jsap.registerParameter(opthost)
|
||||||
jsap.registerParameter(optPort)
|
jsap.registerParameter(optPort)
|
||||||
|
|||||||
Reference in New Issue
Block a user