From 7718b010be3af0be22ae68376dce2d60df4a1885 Mon Sep 17 00:00:00 2001 From: MaximZaitsev Date: Wed, 13 Jul 2016 10:57:36 +0300 Subject: [PATCH] corrected car net server --- car_net/stFlashClient/src/Main.kt | 51 ++++++++++++++++++++----------- car_net/stFlashServer/handlers.js | 3 +- car_net/stFlashServer/main.js | 19 ++++-------- car_net/stFlashServer/router.js | 1 - 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/car_net/stFlashClient/src/Main.kt b/car_net/stFlashClient/src/Main.kt index b2696f3f21b..7e07bbb179c 100644 --- a/car_net/stFlashClient/src/Main.kt +++ b/car_net/stFlashClient/src/Main.kt @@ -1,10 +1,13 @@ import com.google.protobuf.ByteString import com.martiansoftware.jsap.FlaggedOption import com.martiansoftware.jsap.JSAP +import com.martiansoftware.jsap.JSAPResult import io.netty.buffer.Unpooled import io.netty.handler.codec.http.* import io.netty.util.CharsetUtil import proto.Carkot +import java.io.FileInputStream +import java.io.IOException import java.util.* /** @@ -12,28 +15,42 @@ import java.util.* */ fun main(args: Array) { - //2 argm path to bin and server ip - //1 option - 0x000... - - val jsap:JSAP = JSAP(); - val optDebug = FlaggedOption("debug").setStringParser(JSAP.BOOLEAN_PARSER).setDefault("false").setRequired(false).setShortFlag('d') - val optDebug1 = FlaggedOption("verbose").setStringParser(JSAP.INTEGER_PARSER).setDefault("0").setRequired(true).setShortFlag('v') - jsap.registerParameter(optDebug) - jsap.registerParameter(optDebug1) + val jsap: JSAP = JSAP(); + val opthost = FlaggedOption("host").setStringParser(JSAP.STRING_PARSER).setRequired(true).setShortFlag('h').setLongFlag("host") + val optPort = FlaggedOption("port").setStringParser(JSAP.INTEGER_PARSER).setDefault("8888").setRequired(false).setShortFlag('p').setLongFlag("port") + val optBinPath = FlaggedOption("binFile").setStringParser(JSAP.STRING_PARSER).setRequired(true).setShortFlag('f').setLongFlag("binFile") + val optmcuSystem = FlaggedOption("mcuSystem").setStringParser(JSAP.STRING_PARSER).setRequired(false).setDefault("0x08000000").setShortFlag('s') + val optHelp = FlaggedOption("help").setStringParser(JSAP.BOOLEAN_PARSER).setRequired(false).setDefault("false").setLongFlag("help") + jsap.registerParameter(opthost) + jsap.registerParameter(optPort) + jsap.registerParameter(optBinPath) + jsap.registerParameter(optmcuSystem) + jsap.registerParameter(optHelp) val config = jsap.parse(args) - println("verbose ${config.getInt("verbose")}") - println("debug ${config.getBoolean("debug")}") - println(jsap.getHelp()) + if (!config.success() || config.getBoolean("help")) { + println(jsap.getHelp()) + return + } + val host = config.getString("host") + val port = config.getInt("port") + val base = config.getString("mcuSystem") + val pathToFile = config.getString("binFile") - System.exit(-1) - val host = "127.0.0.1" - val port = 8888 - val base = "0x08000000" -// val client = client.Client - val bytesBinTest = Carkot.Upload.newBuilder().setData(ByteString.copyFrom(ByteArray(5, {x->0}))).setBase(base).build().toByteArray() + var fileBytes: ByteArray = ByteArray(0); + try { + FileInputStream(pathToFile).use { fis -> + fileBytes = ByteArray(fis.available()) + fis.read(fileBytes) + } + } catch (e: IOException) { + e.printStackTrace() + println("error in read file $pathToFile") + return; + } + val bytesBinTest = Carkot.Upload.newBuilder().setData(ByteString.copyFrom(fileBytes)).setBase(base).build().toByteArray() val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/loadBin", Unpooled.copiedBuffer(bytesBinTest)); request.headers().set(HttpHeaderNames.HOST, host) request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE) diff --git a/car_net/stFlashServer/handlers.js b/car_net/stFlashServer/handlers.js index c957a01dc55..28b36919f16 100644 --- a/car_net/stFlashServer/handlers.js +++ b/car_net/stFlashServer/handlers.js @@ -3,7 +3,7 @@ */ const fs = require("fs"); const main = require("./main.js"); -var exec = require('child_process').exec; +const exec = require('child_process').exec; function loadBin(httpContent, response) { @@ -53,6 +53,5 @@ function other(httpContent, response) { response.end(); } - exports.loadBin = loadBin; exports.other = other; \ No newline at end of file diff --git a/car_net/stFlashServer/main.js b/car_net/stFlashServer/main.js index f30644c9607..23ffe617c1d 100644 --- a/car_net/stFlashServer/main.js +++ b/car_net/stFlashServer/main.js @@ -4,33 +4,26 @@ var server = require("./server.js"); var router = require("./router.js"); -var handlers = require("./handlers.js"); var commander = require("commander"); const protoBuf = require("protobufjs"); - commander .version('1.0.0') .option('-p, --protopath [path]', 'path to proto file. default ./proto/carkot.proto') .option('-b, --binfile [path]', 'path to save bin file with name f.bin. default ./') - // .option('-b, --bbq-sauce', 'Add bbq sauce') - // .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble') .parse(process.argv); const builder = protoBuf.loadProtoFile(commander.protopath ? commander.protopath : "./proto/carkot.proto"); const protoConstructor = builder.build("carkot"); -if (commander.protopath) { - console.log("pew " + commander.protopath) -} +exports.protoConstructor = protoConstructor; +exports.commandPrefix = "./st-flash write"; +exports.binFilePath = (commander.binfile ? commander.binfile : "./") + "f.bin"; + +var handlers = require("./handlers.js"); var handle = {}; handle["/"] = handlers.other; handle["/loadBin"] = handlers.loadBin; handle["/other"] = handlers.other; -server.start(router.route, handle); -exports.protoConstructor = protoConstructor; -exports.commandPrefix = "./st-flash write"; -exports.binFilePath = (commander.binfile ? commander.binfile : "./") + "f.bin"; -console.log(exports.binFilePath); -console.log(exports.commandPrefix); \ No newline at end of file +server.start(router.route, handle); \ No newline at end of file diff --git a/car_net/stFlashServer/router.js b/car_net/stFlashServer/router.js index 708db286210..ae1c30e82bd 100644 --- a/car_net/stFlashServer/router.js +++ b/car_net/stFlashServer/router.js @@ -11,5 +11,4 @@ function route(handle, pathname, httpContent, response) { response.end(); } } - exports.route = route; \ No newline at end of file