make sending type message before protobuf request message
This commit is contained in:
@@ -1,2 +1,51 @@
|
|||||||
@native
|
@native
|
||||||
fun require(name: String): dynamic = noImpl
|
fun require(name: String): dynamic = noImpl
|
||||||
|
|
||||||
|
fun <T> 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
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ class McTransport() : MCConnectObserver<String> {
|
|||||||
McConditionMonitor.instance.addObserver(this)
|
McConditionMonitor.instance.addObserver(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendBytes(bytes: ByteArray) {
|
private fun sendBytes(bytes: ByteArray) {
|
||||||
val bytesHeader = encodeInt(bytes.size)
|
val bytesHeader = encodeInt(bytes.size)
|
||||||
println("write: $bytesHeader")
|
println("write: $bytesHeader")
|
||||||
writeStream.write(js("new Buffer(bytesHeader)"))
|
writeStream.write(js("new Buffer(bytesHeader)"))
|
||||||
@@ -17,6 +17,29 @@ class McTransport() : MCConnectObserver<String> {
|
|||||||
writeStream.write(js("new Buffer(bytes)"))
|
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) {
|
private fun initStreams(pathToFile: String) {
|
||||||
writeStream = fs.createWriteStream(pathToFile)
|
writeStream = fs.createWriteStream(pathToFile)
|
||||||
readStream = fs.createReadStream(pathToFile)
|
readStream = fs.createReadStream(pathToFile)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import control.car.ControllerToUsb
|
import control.car.ControllerToUsb
|
||||||
import net.Client
|
import net.Client
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import net.server.handlers.ProtoType
|
|
||||||
import net.server.handlers.debug.Memory
|
import net.server.handlers.debug.Memory
|
||||||
import net.server.handlers.flash.LoadBin
|
import net.server.handlers.flash.LoadBin
|
||||||
import net.server.handlers.main.GetLocation
|
import net.server.handlers.main.GetLocation
|
||||||
@@ -27,7 +26,6 @@ fun main(args: Array<String>) {
|
|||||||
handlers.put("/route", SetRoute(carController))
|
handlers.put("/route", SetRoute(carController))
|
||||||
handlers.put("/getLocation", GetLocation())
|
handlers.put("/getLocation", GetLocation())
|
||||||
handlers.put("/debug/memory", Memory())
|
handlers.put("/debug/memory", Memory())
|
||||||
handlers.put("/protoType", ProtoType())
|
|
||||||
|
|
||||||
Client.instance.connectToServer(config.getCarIp(), carServerPort)
|
Client.instance.connectToServer(config.getCarIp(), carServerPort)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package control.car
|
package control.car
|
||||||
|
|
||||||
import CodedOutputStream
|
|
||||||
import RouteRequest
|
import RouteRequest
|
||||||
import control.Controller
|
import control.Controller
|
||||||
import mcTransport
|
import mcTransport
|
||||||
@@ -9,16 +8,12 @@ class ControllerToUsb : Controller {
|
|||||||
|
|
||||||
override fun executeRoute(route: RouteRequest) {
|
override fun executeRoute(route: RouteRequest) {
|
||||||
println("Execute Route:")
|
println("Execute Route:")
|
||||||
val protoSize = route.getSizeNoTag()
|
|
||||||
val routeBytes = ByteArray(protoSize)
|
|
||||||
|
|
||||||
route.writeTo(CodedOutputStream(routeBytes))
|
|
||||||
|
|
||||||
mcTransport.setCallBack { bytes ->
|
mcTransport.setCallBack { bytes ->
|
||||||
println("Read $bytes;")
|
println("Read $bytes;")
|
||||||
}
|
}
|
||||||
|
|
||||||
mcTransport.sendBytes(routeBytes)
|
mcTransport.sendProtoBuf(route)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getSensorData(degrees: IntArray): IntArray {
|
override fun getSensorData(degrees: IntArray): IntArray {
|
||||||
|
|||||||
@@ -1,14 +1,5 @@
|
|||||||
package net.server.handlers
|
package net.server.handlers
|
||||||
|
|
||||||
import CodedOutputStream
|
|
||||||
import DebugResponseMemoryStats
|
|
||||||
import DirectionResponse
|
|
||||||
import LocationResponse
|
|
||||||
import RouteDoneRequest
|
|
||||||
import RouteRequest
|
|
||||||
import RouteResponse
|
|
||||||
import UploadResult
|
|
||||||
|
|
||||||
abstract class AbstractHandler {
|
abstract class AbstractHandler {
|
||||||
|
|
||||||
fun execute(data: List<Byte>, response: dynamic) {
|
fun execute(data: List<Byte>, response: dynamic) {
|
||||||
@@ -21,49 +12,4 @@ abstract class AbstractHandler {
|
|||||||
|
|
||||||
|
|
||||||
abstract fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit)
|
abstract fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit)
|
||||||
|
|
||||||
protected fun <T> 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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,8 +3,10 @@ package net.server.handlers.debug
|
|||||||
import DebugRequest
|
import DebugRequest
|
||||||
import DebugResponseMemoryStats
|
import DebugResponseMemoryStats
|
||||||
import McState
|
import McState
|
||||||
|
import encodeProtoBuf
|
||||||
import mcTransport
|
import mcTransport
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
|
import CodedInputStream
|
||||||
|
|
||||||
class Memory : AbstractHandler {
|
class Memory : AbstractHandler {
|
||||||
|
|
||||||
@@ -27,6 +29,8 @@ class Memory : AbstractHandler {
|
|||||||
mcTransport.setCallBack { bytes ->
|
mcTransport.setCallBack { bytes ->
|
||||||
callback.invoke(bytes)
|
callback.invoke(bytes)
|
||||||
}
|
}
|
||||||
mcTransport.sendBytes(data)
|
val message = fromServerObjectBuilder.build()
|
||||||
|
message.mergeFrom(CodedInputStream(data))
|
||||||
|
mcTransport.sendProtoBuf(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,8 @@ package net.server.handlers.flash
|
|||||||
import CodedInputStream
|
import CodedInputStream
|
||||||
import Upload
|
import Upload
|
||||||
import UploadResult
|
import UploadResult
|
||||||
import mcTransport
|
import encodeProtoBuf
|
||||||
|
import fs
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import require
|
import require
|
||||||
|
|
||||||
@@ -23,13 +24,18 @@ class LoadBin : AbstractHandler {
|
|||||||
override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) {
|
override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) {
|
||||||
val message = fromServerObjectBuilder.build()
|
val message = fromServerObjectBuilder.build()
|
||||||
message.mergeFrom(CodedInputStream(data))
|
message.mergeFrom(CodedInputStream(data))
|
||||||
val responseMessage = toServerObjectBuilder.build()
|
fs.writeFile("./flash.bin", js("new Buffer(data)"), fun(err: dynamic) {
|
||||||
mcTransport.sendBytes(message.data)
|
if (err) {
|
||||||
val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000"
|
println("error in save flash.bin file\n $err")
|
||||||
exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
|
val responseMessage = toServerObjectBuilder.setResultCode(14).build()
|
||||||
val resultCode = if (err != null) 15 else 0
|
callback.invoke(encodeProtoBuf(responseMessage))
|
||||||
responseMessage.resultCode = resultCode
|
}
|
||||||
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))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package net.server.handlers.main
|
|||||||
|
|
||||||
import CarState
|
import CarState
|
||||||
import LocationResponse
|
import LocationResponse
|
||||||
|
import encodeProtoBuf
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
|
|
||||||
class GetLocation : AbstractHandler {
|
class GetLocation : AbstractHandler {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import McState
|
|||||||
import RouteRequest
|
import RouteRequest
|
||||||
import RouteResponse
|
import RouteResponse
|
||||||
import control.Controller
|
import control.Controller
|
||||||
|
import encodeProtoBuf
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
|
|
||||||
class SetRoute : AbstractHandler {
|
class SetRoute : AbstractHandler {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import CodedInputStream
|
|||||||
import DirectionRequest
|
import DirectionRequest
|
||||||
import DirectionResponse
|
import DirectionResponse
|
||||||
import control.emulator.ControllerEmulator.MoveDirection
|
import control.emulator.ControllerEmulator.MoveDirection
|
||||||
|
import encodeProtoBuf
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
|
|
||||||
class Control : AbstractHandler {
|
class Control : AbstractHandler {
|
||||||
@@ -18,28 +19,28 @@ class Control : AbstractHandler {
|
|||||||
|
|
||||||
override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) {
|
override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) {
|
||||||
//TODO now this handler don't make nothing. need fix:)
|
//TODO now this handler don't make nothing. need fix:)
|
||||||
val message = fromServerObjectBuilder.build()
|
// val message = fromServerObjectBuilder.build()
|
||||||
message.mergeFrom(CodedInputStream(data))
|
// message.mergeFrom(CodedInputStream(data))
|
||||||
val commandNumber = message.command
|
// val commandNumber = message.command
|
||||||
val sid = message.sid
|
// val sid = message.sid
|
||||||
val command = when (commandNumber) {
|
// val command = when (commandNumber) {
|
||||||
DirectionRequest.Command.stop -> {
|
// DirectionRequest.Command.stop -> {
|
||||||
MoveDirection.STOP
|
// MoveDirection.STOP
|
||||||
}
|
// }
|
||||||
DirectionRequest.Command.forward -> {
|
// DirectionRequest.Command.forward -> {
|
||||||
MoveDirection.FORWARD
|
// MoveDirection.FORWARD
|
||||||
}
|
// }
|
||||||
DirectionRequest.Command.backward -> {
|
// DirectionRequest.Command.backward -> {
|
||||||
MoveDirection.BACKWARD
|
// MoveDirection.BACKWARD
|
||||||
}
|
// }
|
||||||
DirectionRequest.Command.right -> {
|
// DirectionRequest.Command.right -> {
|
||||||
MoveDirection.RIGHT
|
// MoveDirection.RIGHT
|
||||||
}
|
// }
|
||||||
DirectionRequest.Command.left -> {
|
// DirectionRequest.Command.left -> {
|
||||||
MoveDirection.LEFT
|
// MoveDirection.LEFT
|
||||||
}
|
// }
|
||||||
else -> MoveDirection.STOP
|
// else -> MoveDirection.STOP
|
||||||
}
|
// }
|
||||||
val resultCode: Int = 0
|
val resultCode: Int = 0
|
||||||
val resultMessage = toServerObjectBuilder.setCode(resultCode).build()
|
val resultMessage = toServerObjectBuilder.setCode(resultCode).build()
|
||||||
callback.invoke(encodeProtoBuf(resultMessage))
|
callback.invoke(encodeProtoBuf(resultMessage))
|
||||||
|
|||||||
Reference in New Issue
Block a user