From ee5390b0b5d208baea64e5844a8fdb9882798aaa Mon Sep 17 00:00:00 2001 From: MaximZaitsev Date: Tue, 23 Aug 2016 10:45:35 +0300 Subject: [PATCH] make cl args for run tests and run server as emulator. fixed errors. --- car_srv/kotlinSrv/run.sh | 2 +- car_srv/kotlinSrv/src/JSImport.kt | 1 - car_srv/kotlinSrv/src/Main.kt | 25 ++++++++++++++++--- car_srv/kotlinSrv/src/Test.kt | 3 +++ .../control/emulator/ControllerEmulator.kt | 1 + .../net/server/handlers/AbstractHandler.kt | 13 ++++++++-- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/car_srv/kotlinSrv/run.sh b/car_srv/kotlinSrv/run.sh index a7fadf55606..7637972e071 100755 --- a/car_srv/kotlinSrv/run.sh +++ b/car_srv/kotlinSrv/run.sh @@ -1,3 +1,3 @@ #!/bin/bash cd build/js -node main.js +node main.js $* diff --git a/car_srv/kotlinSrv/src/JSImport.kt b/car_srv/kotlinSrv/src/JSImport.kt index 2f742a675ad..24667236b51 100644 --- a/car_srv/kotlinSrv/src/JSImport.kt +++ b/car_srv/kotlinSrv/src/JSImport.kt @@ -6,7 +6,6 @@ fun setTimeout(callBack: () -> Unit, ms: Int): dynamic = noImpl fun encodeProtoBuf(protoMessage: T): ByteArray { val routeBytes: ByteArray - println(protoMessage.toString()) when (protoMessage) { is LocationResponse -> { val protoSize = protoMessage.getSizeNoTag() diff --git a/car_srv/kotlinSrv/src/Main.kt b/car_srv/kotlinSrv/src/Main.kt index 303fe4016d7..6619287a8bb 100644 --- a/car_srv/kotlinSrv/src/Main.kt +++ b/car_srv/kotlinSrv/src/Main.kt @@ -20,10 +20,29 @@ fun main(args: Array) { js("process.exit(1)") } + + val clArgs = js("process.argv") + var runAsEmulator = false + var runTests = false + for (arg in clArgs) { + when (arg) { + "-t" -> runTests = true + "-e" -> runAsEmulator = true + } + } + val carController = + if (runAsEmulator) { + McState.instance.connect("") + ControllerEmulator() + } else { + ControllerToUsb() + } + + if (runTests) { + runTests() + } + val handlers: MutableMap = mutableMapOf() - - val carController = ControllerEmulator() - handlers.put("/rc/control", Control()) handlers.put("/loadBin", LoadBin()) handlers.put("/sonar", GetSonarData(carController)) diff --git a/car_srv/kotlinSrv/src/Test.kt b/car_srv/kotlinSrv/src/Test.kt index b17367712d6..9155f44f93c 100644 --- a/car_srv/kotlinSrv/src/Test.kt +++ b/car_srv/kotlinSrv/src/Test.kt @@ -49,11 +49,14 @@ private fun testCarEmulator() { }) } +private var testNum = 1 private fun eq(value1: Double, value2: Double, msg: String, eps: Double) { if (Math.abs(value1 - value2) > eps) { println("test failed! $msg") println("returned: $value1") println("waited: $value2") + } else { + println("test ${testNum++} ok!") } } diff --git a/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt b/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt index ed234e6b551..ab9404fa420 100644 --- a/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt +++ b/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt @@ -146,6 +146,7 @@ class ControllerEmulator : Controller { if (currentCommandIdx == commands.size) { val responseMessage = RouteResponse.BuilderRouteResponse(0).build() callBack.invoke(encodeProtoBuf(responseMessage)) + return } val currentCommand = commands.get(currentCommandIdx) diff --git a/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt b/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt index 7bcbc8327db..aac80663e5f 100644 --- a/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt +++ b/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt @@ -3,11 +3,20 @@ package net.server.handlers abstract class AbstractHandler { fun execute(data: List, response: dynamic) { - getBytesResponse(data.toByteArray(), { resultBytes -> + + val callBack = { resultBytes: ByteArray -> val resultBuffer = js("new Buffer(resultBytes)") response.write(resultBuffer) response.end() - }) + } + + try { + getBytesResponse(data.toByteArray(), callBack) + } catch (e: dynamic) { + println("error in executing handler!") + println(e.toString()) + callBack.invoke(ByteArray(0)) + } }