diff --git a/car_srv/kotlinSrv/src/JSImport.kt b/car_srv/kotlinSrv/src/JSImport.kt index 7da11dfb356..3df3171e8be 100644 --- a/car_srv/kotlinSrv/src/JSImport.kt +++ b/car_srv/kotlinSrv/src/JSImport.kt @@ -1,2 +1,51 @@ @native fun require(name: String): dynamic = noImpl + +fun encodeProtoBuf(protoMessage: T): ByteArray { + val routeBytes: ByteArray + if (protoMessage is LocationResponse) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else if (protoMessage is UploadResult) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else if (protoMessage is DebugResponseMemoryStats) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else if (protoMessage is DirectionResponse) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else if (protoMessage is RouteResponse) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else if (protoMessage is RouteRequest) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else if (protoMessage is TaskRequest) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else if (protoMessage is RouteDoneRequest) { + val protoSize = protoMessage.getSizeNoTag() + routeBytes = ByteArray(protoSize) + val codedOutput = CodedOutputStream(routeBytes) + protoMessage.writeTo(codedOutput) + } else { + println("PROTO MESSAGE DON'T ENCODE!") + routeBytes = ByteArray(0) + } + return routeBytes +} \ No newline at end of file diff --git a/car_srv/kotlinSrv/src/MCConnection.kt b/car_srv/kotlinSrv/src/MCConnection.kt index cc41690f068..9323879c26a 100644 --- a/car_srv/kotlinSrv/src/MCConnection.kt +++ b/car_srv/kotlinSrv/src/MCConnection.kt @@ -9,7 +9,7 @@ class McTransport() : MCConnectObserver { McConditionMonitor.instance.addObserver(this) } - fun sendBytes(bytes: ByteArray) { + private fun sendBytes(bytes: ByteArray) { val bytesHeader = encodeInt(bytes.size) println("write: $bytesHeader") writeStream.write(js("new Buffer(bytesHeader)")) @@ -17,6 +17,29 @@ class McTransport() : MCConnectObserver { writeStream.write(js("new Buffer(bytes)")) } + + fun sendProtoBuf(message: DebugRequest) { + val typeMessage = getProtoBufTypeMessage(TaskRequest.Type.DEBUG) + sendProtoBufType(typeMessage) + sendBytes(encodeProtoBuf(message)) + } + + fun sendProtoBuf(message: RouteRequest) { + val typeMessage = getProtoBufTypeMessage(TaskRequest.Type.ROUTE) + sendProtoBufType(typeMessage) + sendBytes(encodeProtoBuf(message)) + } + + private fun sendProtoBufType(messageType: TaskRequest) { + val typeBytes = encodeProtoBuf(messageType) + sendBytes(typeBytes) + } + + private fun getProtoBufTypeMessage(type: TaskRequest.Type): TaskRequest { + val result = TaskRequest.BuilderTaskRequest(type) + return result.build() + } + private fun initStreams(pathToFile: String) { writeStream = fs.createWriteStream(pathToFile) readStream = fs.createReadStream(pathToFile) diff --git a/car_srv/kotlinSrv/src/Main.kt b/car_srv/kotlinSrv/src/Main.kt index 0162766d76e..240b2670d16 100644 --- a/car_srv/kotlinSrv/src/Main.kt +++ b/car_srv/kotlinSrv/src/Main.kt @@ -1,7 +1,6 @@ import control.car.ControllerToUsb import net.Client import net.server.handlers.AbstractHandler -import net.server.handlers.ProtoType import net.server.handlers.debug.Memory import net.server.handlers.flash.LoadBin import net.server.handlers.main.GetLocation @@ -27,7 +26,6 @@ fun main(args: Array) { handlers.put("/route", SetRoute(carController)) handlers.put("/getLocation", GetLocation()) handlers.put("/debug/memory", Memory()) - handlers.put("/protoType", ProtoType()) Client.instance.connectToServer(config.getCarIp(), carServerPort) diff --git a/car_srv/kotlinSrv/src/control/car/ControllerToUsb.kt b/car_srv/kotlinSrv/src/control/car/ControllerToUsb.kt index 455ca800e28..b2f0f9c988b 100644 --- a/car_srv/kotlinSrv/src/control/car/ControllerToUsb.kt +++ b/car_srv/kotlinSrv/src/control/car/ControllerToUsb.kt @@ -1,6 +1,5 @@ package control.car -import CodedOutputStream import RouteRequest import control.Controller import mcTransport @@ -9,16 +8,12 @@ class ControllerToUsb : Controller { override fun executeRoute(route: RouteRequest) { println("Execute Route:") - val protoSize = route.getSizeNoTag() - val routeBytes = ByteArray(protoSize) - - route.writeTo(CodedOutputStream(routeBytes)) mcTransport.setCallBack { bytes -> println("Read $bytes;") } - mcTransport.sendBytes(routeBytes) + mcTransport.sendProtoBuf(route) } override fun getSensorData(degrees: IntArray): IntArray { diff --git a/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt b/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt index c505f9eac89..7bcbc8327db 100644 --- a/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt +++ b/car_srv/kotlinSrv/src/net/server/handlers/AbstractHandler.kt @@ -1,14 +1,5 @@ package net.server.handlers -import CodedOutputStream -import DebugResponseMemoryStats -import DirectionResponse -import LocationResponse -import RouteDoneRequest -import RouteRequest -import RouteResponse -import UploadResult - abstract class AbstractHandler { fun execute(data: List, response: dynamic) { @@ -21,49 +12,4 @@ abstract class AbstractHandler { abstract fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) - - protected fun encodeProtoBuf(protoMessage: T): ByteArray { - val routeBytes: ByteArray - if (protoMessage is LocationResponse) { - val protoSize = protoMessage.getSizeNoTag() - routeBytes = ByteArray(protoSize) - val codedOutput = CodedOutputStream(routeBytes) - protoMessage.writeTo(codedOutput) - } else if (protoMessage is UploadResult) { - val protoSize = protoMessage.getSizeNoTag() - routeBytes = ByteArray(protoSize) - val codedOutput = CodedOutputStream(routeBytes) - protoMessage.writeTo(codedOutput) - } else if (protoMessage is DebugResponseMemoryStats) { - val protoSize = protoMessage.getSizeNoTag() - routeBytes = ByteArray(protoSize) - val codedOutput = CodedOutputStream(routeBytes) - protoMessage.writeTo(codedOutput) - } else if (protoMessage is DirectionResponse) { - val protoSize = protoMessage.getSizeNoTag() - routeBytes = ByteArray(protoSize) - val codedOutput = CodedOutputStream(routeBytes) - protoMessage.writeTo(codedOutput) - } else if (protoMessage is RouteResponse) { - val protoSize = protoMessage.getSizeNoTag() - routeBytes = ByteArray(protoSize) - val codedOutput = CodedOutputStream(routeBytes) - protoMessage.writeTo(codedOutput) - } else if (protoMessage is RouteRequest) { - val protoSize = protoMessage.getSizeNoTag() - routeBytes = ByteArray(protoSize) - val codedOutput = CodedOutputStream(routeBytes) - protoMessage.writeTo(codedOutput) - } else if (protoMessage is RouteDoneRequest) { - val protoSize = protoMessage.getSizeNoTag() - routeBytes = ByteArray(protoSize) - val codedOutput = CodedOutputStream(routeBytes) - protoMessage.writeTo(codedOutput) - } else { - println("PROTO MESSAGE DON'T ENCODE!") - routeBytes = ByteArray(0) - } - return routeBytes - } - } \ No newline at end of file diff --git a/car_srv/kotlinSrv/src/net/server/handlers/ProtoType.kt b/car_srv/kotlinSrv/src/net/server/handlers/ProtoType.kt deleted file mode 100644 index 7e1c61dc187..00000000000 --- a/car_srv/kotlinSrv/src/net/server/handlers/ProtoType.kt +++ /dev/null @@ -1,24 +0,0 @@ -package net.server.handlers - -import McState -import TaskRequest -import mcTransport - -class ProtoType : AbstractHandler { - - val fromServerObjectBuilder: TaskRequest.BuilderTaskRequest - - constructor() : super() { - fromServerObjectBuilder = TaskRequest.BuilderTaskRequest(TaskRequest.TYPE.DEBUG) - } - - override fun getBytesResponse(data: ByteArray, callback: (ByteArray) -> Unit) { - if (!McState.instance.isConnected()) { - println("mc is disconnected!") - callback.invoke(ByteArray(0)) - return - } - mcTransport.sendBytes(data) - callback.invoke(ByteArray(0)) - } -} \ No newline at end of file diff --git a/car_srv/kotlinSrv/src/net/server/handlers/debug/Memory.kt b/car_srv/kotlinSrv/src/net/server/handlers/debug/Memory.kt index 3ac4de7236a..36966aeaee7 100644 --- a/car_srv/kotlinSrv/src/net/server/handlers/debug/Memory.kt +++ b/car_srv/kotlinSrv/src/net/server/handlers/debug/Memory.kt @@ -3,8 +3,10 @@ package net.server.handlers.debug import DebugRequest import DebugResponseMemoryStats import McState +import encodeProtoBuf import mcTransport import net.server.handlers.AbstractHandler +import CodedInputStream class Memory : AbstractHandler { @@ -27,6 +29,8 @@ class Memory : AbstractHandler { mcTransport.setCallBack { bytes -> callback.invoke(bytes) } - mcTransport.sendBytes(data) + val message = fromServerObjectBuilder.build() + message.mergeFrom(CodedInputStream(data)) + mcTransport.sendProtoBuf(message) } } \ No newline at end of file diff --git a/car_srv/kotlinSrv/src/net/server/handlers/flash/LoadBin.kt b/car_srv/kotlinSrv/src/net/server/handlers/flash/LoadBin.kt index 6cc81ba5b7a..f08d697ec9b 100644 --- a/car_srv/kotlinSrv/src/net/server/handlers/flash/LoadBin.kt +++ b/car_srv/kotlinSrv/src/net/server/handlers/flash/LoadBin.kt @@ -3,7 +3,8 @@ package net.server.handlers.flash import CodedInputStream import Upload import UploadResult -import mcTransport +import encodeProtoBuf +import fs import net.server.handlers.AbstractHandler import require @@ -23,13 +24,18 @@ class LoadBin : AbstractHandler { override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) { val message = fromServerObjectBuilder.build() message.mergeFrom(CodedInputStream(data)) - val responseMessage = toServerObjectBuilder.build() - mcTransport.sendBytes(message.data) - val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000" - exec(stFlashCommand, { err, stdOutRes, stdErrRes -> - val resultCode = if (err != null) 15 else 0 - responseMessage.resultCode = resultCode - callback.invoke(encodeProtoBuf(responseMessage)) + fs.writeFile("./flash.bin", js("new Buffer(data)"), fun(err: dynamic) { + if (err) { + println("error in save flash.bin file\n $err") + val responseMessage = toServerObjectBuilder.setResultCode(14).build() + callback.invoke(encodeProtoBuf(responseMessage)) + } + val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000" + exec(stFlashCommand, { err, stdOutRes, stdErrRes -> + val resultCode = if (err != null) 15 else 0 + val responseMessage = toServerObjectBuilder.setResultCode(resultCode).build() + callback.invoke(encodeProtoBuf(responseMessage)) + }) }) } } \ No newline at end of file diff --git a/car_srv/kotlinSrv/src/net/server/handlers/main/GetLocation.kt b/car_srv/kotlinSrv/src/net/server/handlers/main/GetLocation.kt index 02a042bb5da..606c8ca6820 100644 --- a/car_srv/kotlinSrv/src/net/server/handlers/main/GetLocation.kt +++ b/car_srv/kotlinSrv/src/net/server/handlers/main/GetLocation.kt @@ -2,6 +2,7 @@ package net.server.handlers.main import CarState import LocationResponse +import encodeProtoBuf import net.server.handlers.AbstractHandler class GetLocation : AbstractHandler { diff --git a/car_srv/kotlinSrv/src/net/server/handlers/main/SetRoute.kt b/car_srv/kotlinSrv/src/net/server/handlers/main/SetRoute.kt index 7e0db967e90..5e5a2ea05e0 100644 --- a/car_srv/kotlinSrv/src/net/server/handlers/main/SetRoute.kt +++ b/car_srv/kotlinSrv/src/net/server/handlers/main/SetRoute.kt @@ -5,6 +5,7 @@ import McState import RouteRequest import RouteResponse import control.Controller +import encodeProtoBuf import net.server.handlers.AbstractHandler class SetRoute : AbstractHandler { diff --git a/car_srv/kotlinSrv/src/net/server/handlers/rc/Control.kt b/car_srv/kotlinSrv/src/net/server/handlers/rc/Control.kt index 0642e316aac..cd453405220 100644 --- a/car_srv/kotlinSrv/src/net/server/handlers/rc/Control.kt +++ b/car_srv/kotlinSrv/src/net/server/handlers/rc/Control.kt @@ -4,6 +4,7 @@ import CodedInputStream import DirectionRequest import DirectionResponse import control.emulator.ControllerEmulator.MoveDirection +import encodeProtoBuf import net.server.handlers.AbstractHandler class Control : AbstractHandler { @@ -18,28 +19,28 @@ class Control : AbstractHandler { override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) { //TODO now this handler don't make nothing. need fix:) - val message = fromServerObjectBuilder.build() - message.mergeFrom(CodedInputStream(data)) - val commandNumber = message.command - val sid = message.sid - val command = when (commandNumber) { - DirectionRequest.Command.stop -> { - MoveDirection.STOP - } - DirectionRequest.Command.forward -> { - MoveDirection.FORWARD - } - DirectionRequest.Command.backward -> { - MoveDirection.BACKWARD - } - DirectionRequest.Command.right -> { - MoveDirection.RIGHT - } - DirectionRequest.Command.left -> { - MoveDirection.LEFT - } - else -> MoveDirection.STOP - } +// val message = fromServerObjectBuilder.build() +// message.mergeFrom(CodedInputStream(data)) +// val commandNumber = message.command +// val sid = message.sid +// val command = when (commandNumber) { +// DirectionRequest.Command.stop -> { +// MoveDirection.STOP +// } +// DirectionRequest.Command.forward -> { +// MoveDirection.FORWARD +// } +// DirectionRequest.Command.backward -> { +// MoveDirection.BACKWARD +// } +// DirectionRequest.Command.right -> { +// MoveDirection.RIGHT +// } +// DirectionRequest.Command.left -> { +// MoveDirection.LEFT +// } +// else -> MoveDirection.STOP +// } val resultCode: Int = 0 val resultMessage = toServerObjectBuilder.setCode(resultCode).build() callback.invoke(encodeProtoBuf(resultMessage))