make cl args for run tests and run server as emulator. fixed errors.
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
cd build/js
|
cd build/js
|
||||||
node main.js
|
node main.js $*
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ fun setTimeout(callBack: () -> Unit, ms: Int): dynamic = noImpl
|
|||||||
|
|
||||||
fun <T> encodeProtoBuf(protoMessage: T): ByteArray {
|
fun <T> encodeProtoBuf(protoMessage: T): ByteArray {
|
||||||
val routeBytes: ByteArray
|
val routeBytes: ByteArray
|
||||||
println(protoMessage.toString())
|
|
||||||
when (protoMessage) {
|
when (protoMessage) {
|
||||||
is LocationResponse -> {
|
is LocationResponse -> {
|
||||||
val protoSize = protoMessage.getSizeNoTag()
|
val protoSize = protoMessage.getSizeNoTag()
|
||||||
|
|||||||
@@ -20,10 +20,29 @@ fun main(args: Array<String>) {
|
|||||||
js("process.exit(1)")
|
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<String, AbstractHandler> = mutableMapOf()
|
val handlers: MutableMap<String, AbstractHandler> = mutableMapOf()
|
||||||
|
|
||||||
val carController = ControllerEmulator()
|
|
||||||
|
|
||||||
handlers.put("/rc/control", Control())
|
handlers.put("/rc/control", Control())
|
||||||
handlers.put("/loadBin", LoadBin())
|
handlers.put("/loadBin", LoadBin())
|
||||||
handlers.put("/sonar", GetSonarData(carController))
|
handlers.put("/sonar", GetSonarData(carController))
|
||||||
|
|||||||
@@ -49,11 +49,14 @@ private fun testCarEmulator() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var testNum = 1
|
||||||
|
|
||||||
private fun eq(value1: Double, value2: Double, msg: String, eps: Double) {
|
private fun eq(value1: Double, value2: Double, msg: String, eps: Double) {
|
||||||
if (Math.abs(value1 - value2) > eps) {
|
if (Math.abs(value1 - value2) > eps) {
|
||||||
println("test failed! $msg")
|
println("test failed! $msg")
|
||||||
println("returned: $value1")
|
println("returned: $value1")
|
||||||
println("waited: $value2")
|
println("waited: $value2")
|
||||||
|
} else {
|
||||||
|
println("test ${testNum++} ok!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ class ControllerEmulator : Controller {
|
|||||||
if (currentCommandIdx == commands.size) {
|
if (currentCommandIdx == commands.size) {
|
||||||
val responseMessage = RouteResponse.BuilderRouteResponse(0).build()
|
val responseMessage = RouteResponse.BuilderRouteResponse(0).build()
|
||||||
callBack.invoke(encodeProtoBuf(responseMessage))
|
callBack.invoke(encodeProtoBuf(responseMessage))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
val currentCommand = commands.get(currentCommandIdx)
|
val currentCommand = commands.get(currentCommandIdx)
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,20 @@ package net.server.handlers
|
|||||||
abstract class AbstractHandler {
|
abstract class AbstractHandler {
|
||||||
|
|
||||||
fun execute(data: List<Byte>, response: dynamic) {
|
fun execute(data: List<Byte>, response: dynamic) {
|
||||||
getBytesResponse(data.toByteArray(), { resultBytes ->
|
|
||||||
|
val callBack = { resultBytes: ByteArray ->
|
||||||
val resultBuffer = js("new Buffer(resultBytes)")
|
val resultBuffer = js("new Buffer(resultBytes)")
|
||||||
response.write(resultBuffer)
|
response.write(resultBuffer)
|
||||||
response.end()
|
response.end()
|
||||||
})
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
getBytesResponse(data.toByteArray(), callBack)
|
||||||
|
} catch (e: dynamic) {
|
||||||
|
println("error in executing handler!")
|
||||||
|
println(e.toString())
|
||||||
|
callBack.invoke(ByteArray(0))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user