simple refactoring

This commit is contained in:
MaximZaitsev
2016-08-17 16:48:54 +03:00
parent 9c185b1f9a
commit c6e97d76d0
9 changed files with 49 additions and 75 deletions
+27
View File
@@ -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
fun require(name: String): dynamic {
return null
@@ -2,44 +2,19 @@ package carControl
import RouteRequest
import mcTransport
import CodedOutputStream
fun encode(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 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
}
import encodeInt
import decodeInt
import encodeProtoBuf
class RouteExecutorToUsb : RouteExecutor {
override fun executeRoute(route: RouteRequest) {
println("Execute Route:")
val size = encode(route.getSizeNoTag())
val routeBytes = encodeRouteRequest(route)
val size = encodeInt(route.getSizeNoTag())
val routeBytes = encodeProtoBuf(route)
mcTransport.setCallBack { bytes ->
println("Read $bytes; decoded: ${decode(bytes)}")
println("Read $bytes; decoded: ${decodeInt(bytes)}")
}
mcTransport.writeToFile(size)
@@ -3,7 +3,7 @@ package net.server.handlers.flash
import net.server.handlers.AbstractHandler
import require
import CodedInputStream
import CodedOutputStream
import encodeProtoBuf
import mcTransport
/**
@@ -25,28 +25,13 @@ class LoadBin : AbstractHandler {
override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) {
val message = fromServerObjectBuilder.build()
message.mergeFrom(CodedInputStream(data))
var resultCode = 0
val responseMessage = toServerObjectBuilder.build()
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"
exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
if (err != null) {
resultCode = 15
} else {
resultCode = 0
}
val resultCode = if (err != null) 15 else 0
responseMessage.resultCode = resultCode
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
responseMessage.writeTo(CodedOutputStream(resultByteArray))
callback.invoke(resultByteArray)
callback.invoke(encodeProtoBuf(responseMessage))
})
// }
}
}
@@ -1,7 +1,7 @@
package net.server.handlers.main
import net.server.handlers.AbstractHandler
import CodedOutputStream
import encodeProtoBuf
/**
* Created by user on 7/28/16.
@@ -17,9 +17,7 @@ class GetLocation : AbstractHandler {
override fun getBytesResponse(data: ByteArray, callback: (ByteArray) -> Unit) {
val car = MicroController.instance.car
val locationData = LocationResponse.LocationData.BuilderLocationData(car.x.toInt(), car.y.toInt(), car.angle.toInt()).build()
val resultMessage = toServerObjectBuilder.setLocationResponseData(locationData).build()
val resultByteArray = ByteArray(resultMessage.getSizeNoTag())
resultMessage.writeTo(CodedOutputStream(resultByteArray))
callback.invoke(resultByteArray)
val responseMessage = toServerObjectBuilder.setLocationResponseData(locationData).build()
callback.invoke(encodeProtoBuf(responseMessage))
}
}
@@ -2,7 +2,7 @@ package net.server.handlers.main
import net.server.handlers.AbstractHandler
import CodedInputStream
import CodedOutputStream
import encodeProtoBuf
/**
* Created by user on 7/28/16.
@@ -23,9 +23,7 @@ class SetRoute : AbstractHandler {
message.mergeFrom(CodedInputStream(data))
car.routeExecutor.executeRoute(message)
val responseMessage = toServerObjectBuilder.setCode(0).build()
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
responseMessage.writeTo(CodedOutputStream(resultByteArray))
callback.invoke(resultByteArray)
callback.invoke(encodeProtoBuf(responseMessage))
}
}
@@ -2,7 +2,7 @@ package net.server.handlers.rc
import exceptions.RcControlException
import net.server.handlers.AbstractHandler
import CodedOutputStream
import encodeProtoBuf
/**
* Created by user on 7/27/16.
@@ -25,11 +25,8 @@ class Connect : AbstractHandler {
resultCode = 13
sid = 0
}
val responseMessage = toServerObjectBuilder.setCode(resultCode).setSid(sid).build()
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
responseMessage.writeTo(CodedOutputStream(resultByteArray))
callback.invoke(resultByteArray)
callback.invoke(encodeProtoBuf(responseMessage))
}
}
@@ -6,7 +6,7 @@ import exceptions.RcControlException
import net.server.handlers.AbstractHandler
import DirectionRequest
import CodedInputStream
import CodedOutputStream
import encodeProtoBuf
/**
* Created by user on 7/27/16.
@@ -52,8 +52,6 @@ class Control : AbstractHandler {
resultCode = 12
}
val resultMessage = toServerObjectBuilder.setCode(resultCode).build()
val resultByteArray = ByteArray(resultMessage.getSizeNoTag())
resultMessage.writeTo(CodedOutputStream(resultByteArray))
callback.invoke(resultByteArray)
callback.invoke(encodeProtoBuf(resultMessage))
}
}
@@ -3,7 +3,7 @@ package net.server.handlers.rc
import exceptions.RcControlException
import net.server.handlers.AbstractHandler
import CodedInputStream
import CodedOutputStream
import encodeProtoBuf
/**
* Created by user on 7/27/16.
@@ -30,8 +30,6 @@ class Disconnect : AbstractHandler {
resultCode = 12
}
val responseMessage = toServerObjectBuilder.setCode(resultCode).build()
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
responseMessage.writeTo(CodedOutputStream(resultByteArray))
callback.invoke(resultByteArray)
callback.invoke(encodeProtoBuf(responseMessage))
}
}
@@ -3,7 +3,7 @@ package net.server.handlers.rc
import exceptions.RcControlException
import net.server.handlers.AbstractHandler
import CodedInputStream
import CodedOutputStream
import encodeProtoBuf
/**
* Created by user on 7/27/16.
@@ -29,8 +29,6 @@ class Heartbeat : AbstractHandler {
resultCode = 12
}
val responseMessage = toServerObjectBuilder.setCode(resultCode).build()
val resultByteArray = ByteArray(responseMessage.getSizeNoTag())
responseMessage.writeTo(CodedOutputStream(resultByteArray))
callback.invoke(resultByteArray)
callback.invoke(encodeProtoBuf(responseMessage))
}
}