add car ip to config file, add new route executor (send proto to usb)

This commit is contained in:
MaximZaitsev
2016-08-17 12:07:45 +03:00
parent bc8d741d63
commit bb4d1f2d25
6 changed files with 52 additions and 14 deletions
+11 -3
View File
@@ -5,17 +5,21 @@ class Config(val configFileName: String) {
private var serverIp = "127.0.0.1"
private var thisCarIp = "127.0.0.1"
fun loadConfig(): Boolean {
val data: String = fs.readFileSync(configFileName, "utf8")
data.split("\n").forEach { line ->
val keyValue = line.split(":")
if (keyValue.size == 2) {
if (!line.equals("")) {
if (keyValue.size != 2) {
return false
}
when (keyValue[0]) {
"ip" -> serverIp = keyValue[1]
"thisIp" -> thisCarIp = keyValue[1]
}
} else {
return false
}
}
return true
@@ -24,4 +28,8 @@ class Config(val configFileName: String) {
fun getIp(): String {
return serverIp
}
fun getCarIp(): String {
return thisCarIp
}
}
+2 -3
View File
@@ -14,10 +14,8 @@ import net.server.handlers.rc.Heartbeat
val deltaTimeLocationRefresh: Int = 100//ms
val serverPort: Int = 8888
val serverIp: String = "127.0.0.1"
val mainServerAddress = "127.0.0.1"
val mainServerPort = 7925
val config = Config("config.cfg")
val fs = require("fs")
fun main(args: Array<String>) {
@@ -25,6 +23,7 @@ fun main(args: Array<String>) {
println("incorrect config format!")
return
}
val handlers: MutableMap<String, AbstractHandler> = mutableMapOf()
handlers.put("/rc/control", Control(DirectionRequest.BuilderDirectionRequest(DirectionRequest.Command.fromIntToCommand(0), 0), DirectionResponse.BuilderDirectionResponse(0)))
handlers.put("/rc/connect", Connect(SessionUpResponse.BuilderSessionUpResponse(0, 0)))
+1 -1
View File
@@ -10,7 +10,7 @@ class McTransport() {
private var callback: (bytes: ByteArray) -> Unit = {}
fun writeToFile(bytes: ByteArray) {
println("writewrite uiii!")
println("write: " + bytes)
val bytesT = bytes
writeStream.write(js("new Buffer(bytesT)"));
// writeStream.end();
+6 -5
View File
@@ -1,5 +1,6 @@
import carControl.ControlImpl
import carControl.RouteExecutorImpl
import carControl.RouteExecutorToUsb
import exceptions.RcControlException
/**
@@ -26,17 +27,17 @@ class MicroController private constructor() {
this.modelID = "5740"
this.transportFilePath = ""
this.car = Car(RouteExecutorImpl(), ControlImpl())
this.car = Car(RouteExecutorToUsb(), ControlImpl())
}
fun start() {
if (!isConnected()) {
connectToServer(serverIp, serverPort)
connectToServer(config.getCarIp(), serverPort)
}
this.transportFilePath = "./testTtyAcm"//todo need init
mcTransport.initStreams(transportFilePath)
// this.transportFilePath = "./testTtyAcm"//todo need init
// mcTransport.initStreams(transportFilePath)
mcTransport.setCallBack { bytes ->
println("read: " + bytes.toString())
}
@@ -99,7 +100,7 @@ class MicroController private constructor() {
}
fun connectToServer(thisIp: String, thisPort: Int) {
val requestObject = ConnectionRequest.BuilderConnectionRequest(serverIp.split(".").map { str -> parseInt(str, 10) }.toIntArray(), serverPort).build()
val requestObject = ConnectionRequest.BuilderConnectionRequest(config.getCarIp().split(".").map { str -> parseInt(str, 10) }.toIntArray(), serverPort).build()
val bytes = ByteArray(requestObject.getSizeNoTag())
requestObject.writeTo(CodedOutputStream(bytes))
net.sendRequest(js("new Buffer(bytes)"), "/connect", { resultData ->
@@ -0,0 +1,30 @@
package carControl
import RouteRequest
import mcTransport
import CodedOutputStream
/**
* Created by user on 8/17/16.
*/
class RouteExecutorToUsb : RouteExecutor {
override fun executeRoute(route: RouteRequest) {
val protoSize = route.getSizeNoTag()
val routeBytes = ByteArray(protoSize)
route.writeTo(CodedOutputStream(routeBytes))
val fullBytes = ByteArray(route.getSizeNoTag() + 4)
for (i in 0..routeBytes.size - 1) {
fullBytes[i + 4] = routeBytes[i]
}
fullBytes[0] = protoSize.shr(24).toByte()
fullBytes[1] = protoSize.shr(16).toByte()
fullBytes[2] = protoSize.shr(8).toByte()
fullBytes[3] = protoSize.toByte()
mcTransport.writeToFile(fullBytes)
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
package net
import require
import mainServerAddress
import mainServerPort
import config
/**
* Created by user on 7/28/16.
@@ -11,7 +11,7 @@ val http = require("http")
fun sendRequest(dataBuffer: dynamic, url: String, successCallback: (responseData: ByteArray) -> Unit, errorCallback: (err: dynamic) -> Unit) {
val options: dynamic = {}
val serverAddress = mainServerAddress
val serverAddress = config.getIp()
val port = mainServerPort
js("options = {hostname:serverAddress, port:port, path:url, method:'POST'}")