simple refactoring
This commit is contained in:
@@ -43,6 +43,33 @@ fun main(args: Array<String>) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun encodeInt(i: Int): ByteArray {
|
||||||
|
val result = ByteArray(4)
|
||||||
|
result[0] = i.shr(24).toByte()
|
||||||
|
result[1] = i.shr(16).toByte()
|
||||||
|
result[2] = i.shr(8).toByte()
|
||||||
|
result[3] = i.toByte()
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun decodeInt(bytes: ByteArray): Int {
|
||||||
|
var result = 0
|
||||||
|
result += bytes[3]
|
||||||
|
result += bytes[2].toInt().shl(8)
|
||||||
|
result += bytes[1].toInt().shl(16)
|
||||||
|
result += bytes[0].toInt().shl(24)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun encodeProtoBuf(protoMessage: dynamic): ByteArray {
|
||||||
|
val protoSize = protoMessage.getSizeNoTag()
|
||||||
|
val routeBytes = ByteArray(protoSize)
|
||||||
|
|
||||||
|
protoMessage.writeTo(CodedOutputStream(routeBytes))
|
||||||
|
return routeBytes
|
||||||
|
}
|
||||||
|
|
||||||
@native
|
@native
|
||||||
fun require(name: String): dynamic {
|
fun require(name: String): dynamic {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -2,44 +2,19 @@ package carControl
|
|||||||
|
|
||||||
import RouteRequest
|
import RouteRequest
|
||||||
import mcTransport
|
import mcTransport
|
||||||
import CodedOutputStream
|
import encodeInt
|
||||||
|
import decodeInt
|
||||||
fun encode(i: Int): ByteArray {
|
import encodeProtoBuf
|
||||||
val result = ByteArray(4)
|
|
||||||
result[0] = i.shr(24).toByte()
|
|
||||||
result[1] = i.shr(16).toByte()
|
|
||||||
result[2] = i.shr(8).toByte()
|
|
||||||
result[3] = i.toByte()
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
fun decode(bytes: ByteArray): Int {
|
|
||||||
var result = 0
|
|
||||||
result += bytes[3]
|
|
||||||
result += bytes[2].toInt().shl(8)
|
|
||||||
result += bytes[1].toInt().shl(16)
|
|
||||||
result += bytes[0].toInt().shl(24)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
fun encodeRouteRequest(route: RouteRequest): ByteArray {
|
|
||||||
val protoSize = route.getSizeNoTag()
|
|
||||||
val routeBytes = ByteArray(protoSize)
|
|
||||||
|
|
||||||
route.writeTo(CodedOutputStream(routeBytes))
|
|
||||||
return routeBytes
|
|
||||||
}
|
|
||||||
|
|
||||||
class RouteExecutorToUsb : RouteExecutor {
|
class RouteExecutorToUsb : RouteExecutor {
|
||||||
|
|
||||||
override fun executeRoute(route: RouteRequest) {
|
override fun executeRoute(route: RouteRequest) {
|
||||||
println("Execute Route:")
|
println("Execute Route:")
|
||||||
val size = encode(route.getSizeNoTag())
|
val size = encodeInt(route.getSizeNoTag())
|
||||||
val routeBytes = encodeRouteRequest(route)
|
val routeBytes = encodeProtoBuf(route)
|
||||||
|
|
||||||
mcTransport.setCallBack { bytes ->
|
mcTransport.setCallBack { bytes ->
|
||||||
println("Read $bytes; decoded: ${decode(bytes)}")
|
println("Read $bytes; decoded: ${decodeInt(bytes)}")
|
||||||
}
|
}
|
||||||
|
|
||||||
mcTransport.writeToFile(size)
|
mcTransport.writeToFile(size)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package net.server.handlers.flash
|
|||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import require
|
import require
|
||||||
import CodedInputStream
|
import CodedInputStream
|
||||||
import CodedOutputStream
|
import encodeProtoBuf
|
||||||
import mcTransport
|
import mcTransport
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,28 +25,13 @@ 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))
|
||||||
var resultCode = 0
|
|
||||||
val responseMessage = toServerObjectBuilder.build()
|
val responseMessage = toServerObjectBuilder.build()
|
||||||
mcTransport.writeToFile(message.data)
|
mcTransport.writeToFile(message.data)
|
||||||
// if (error != null) {
|
|
||||||
// resultCode = 14
|
|
||||||
// responseMessage.resultCode = resultCode
|
|
||||||
// val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
|
|
||||||
// responseMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
// callback.invoke(resultByteArray)
|
|
||||||
// } else {
|
|
||||||
val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000"
|
val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000"
|
||||||
exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
|
exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
|
||||||
if (err != null) {
|
val resultCode = if (err != null) 15 else 0
|
||||||
resultCode = 15
|
|
||||||
} else {
|
|
||||||
resultCode = 0
|
|
||||||
}
|
|
||||||
responseMessage.resultCode = resultCode
|
responseMessage.resultCode = resultCode
|
||||||
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
|
callback.invoke(encodeProtoBuf(responseMessage))
|
||||||
responseMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
callback.invoke(resultByteArray)
|
|
||||||
})
|
})
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package net.server.handlers.main
|
package net.server.handlers.main
|
||||||
|
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import CodedOutputStream
|
import encodeProtoBuf
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/28/16.
|
* Created by user on 7/28/16.
|
||||||
@@ -17,9 +17,7 @@ class GetLocation : AbstractHandler {
|
|||||||
override fun getBytesResponse(data: ByteArray, callback: (ByteArray) -> Unit) {
|
override fun getBytesResponse(data: ByteArray, callback: (ByteArray) -> Unit) {
|
||||||
val car = MicroController.instance.car
|
val car = MicroController.instance.car
|
||||||
val locationData = LocationResponse.LocationData.BuilderLocationData(car.x.toInt(), car.y.toInt(), car.angle.toInt()).build()
|
val locationData = LocationResponse.LocationData.BuilderLocationData(car.x.toInt(), car.y.toInt(), car.angle.toInt()).build()
|
||||||
val resultMessage = toServerObjectBuilder.setLocationResponseData(locationData).build()
|
val responseMessage = toServerObjectBuilder.setLocationResponseData(locationData).build()
|
||||||
val resultByteArray = ByteArray(resultMessage.getSizeNoTag())
|
callback.invoke(encodeProtoBuf(responseMessage))
|
||||||
resultMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
callback.invoke(resultByteArray)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@ package net.server.handlers.main
|
|||||||
|
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import CodedInputStream
|
import CodedInputStream
|
||||||
import CodedOutputStream
|
import encodeProtoBuf
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/28/16.
|
* Created by user on 7/28/16.
|
||||||
@@ -23,9 +23,7 @@ class SetRoute : AbstractHandler {
|
|||||||
message.mergeFrom(CodedInputStream(data))
|
message.mergeFrom(CodedInputStream(data))
|
||||||
car.routeExecutor.executeRoute(message)
|
car.routeExecutor.executeRoute(message)
|
||||||
val responseMessage = toServerObjectBuilder.setCode(0).build()
|
val responseMessage = toServerObjectBuilder.setCode(0).build()
|
||||||
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
|
callback.invoke(encodeProtoBuf(responseMessage))
|
||||||
responseMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
callback.invoke(resultByteArray)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@ package net.server.handlers.rc
|
|||||||
|
|
||||||
import exceptions.RcControlException
|
import exceptions.RcControlException
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import CodedOutputStream
|
import encodeProtoBuf
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/27/16.
|
* Created by user on 7/27/16.
|
||||||
@@ -25,11 +25,8 @@ class Connect : AbstractHandler {
|
|||||||
resultCode = 13
|
resultCode = 13
|
||||||
sid = 0
|
sid = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
val responseMessage = toServerObjectBuilder.setCode(resultCode).setSid(sid).build()
|
val responseMessage = toServerObjectBuilder.setCode(resultCode).setSid(sid).build()
|
||||||
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
|
callback.invoke(encodeProtoBuf(responseMessage))
|
||||||
responseMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
callback.invoke(resultByteArray)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ import exceptions.RcControlException
|
|||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import DirectionRequest
|
import DirectionRequest
|
||||||
import CodedInputStream
|
import CodedInputStream
|
||||||
import CodedOutputStream
|
import encodeProtoBuf
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/27/16.
|
* Created by user on 7/27/16.
|
||||||
@@ -52,8 +52,6 @@ class Control : AbstractHandler {
|
|||||||
resultCode = 12
|
resultCode = 12
|
||||||
}
|
}
|
||||||
val resultMessage = toServerObjectBuilder.setCode(resultCode).build()
|
val resultMessage = toServerObjectBuilder.setCode(resultCode).build()
|
||||||
val resultByteArray = ByteArray(resultMessage.getSizeNoTag())
|
callback.invoke(encodeProtoBuf(resultMessage))
|
||||||
resultMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
callback.invoke(resultByteArray)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@ package net.server.handlers.rc
|
|||||||
import exceptions.RcControlException
|
import exceptions.RcControlException
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import CodedInputStream
|
import CodedInputStream
|
||||||
import CodedOutputStream
|
import encodeProtoBuf
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/27/16.
|
* Created by user on 7/27/16.
|
||||||
@@ -30,8 +30,6 @@ class Disconnect : AbstractHandler {
|
|||||||
resultCode = 12
|
resultCode = 12
|
||||||
}
|
}
|
||||||
val responseMessage = toServerObjectBuilder.setCode(resultCode).build()
|
val responseMessage = toServerObjectBuilder.setCode(resultCode).build()
|
||||||
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
|
callback.invoke(encodeProtoBuf(responseMessage))
|
||||||
responseMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
callback.invoke(resultByteArray)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@ package net.server.handlers.rc
|
|||||||
import exceptions.RcControlException
|
import exceptions.RcControlException
|
||||||
import net.server.handlers.AbstractHandler
|
import net.server.handlers.AbstractHandler
|
||||||
import CodedInputStream
|
import CodedInputStream
|
||||||
import CodedOutputStream
|
import encodeProtoBuf
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/27/16.
|
* Created by user on 7/27/16.
|
||||||
@@ -29,8 +29,6 @@ class Heartbeat : AbstractHandler {
|
|||||||
resultCode = 12
|
resultCode = 12
|
||||||
}
|
}
|
||||||
val responseMessage = toServerObjectBuilder.setCode(resultCode).build()
|
val responseMessage = toServerObjectBuilder.setCode(resultCode).build()
|
||||||
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
|
callback.invoke(encodeProtoBuf(responseMessage))
|
||||||
responseMessage.writeTo(CodedOutputStream(resultByteArray))
|
|
||||||
callback.invoke(resultByteArray)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user