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 serverIp = "127.0.0.1"
private var thisCarIp = "127.0.0.1"
fun loadConfig(): Boolean { fun loadConfig(): Boolean {
val data: String = fs.readFileSync(configFileName, "utf8") val data: String = fs.readFileSync(configFileName, "utf8")
data.split("\n").forEach { line -> data.split("\n").forEach { line ->
val keyValue = line.split(":") val keyValue = line.split(":")
if (keyValue.size == 2) { if (!line.equals("")) {
if (keyValue.size != 2) {
return false
}
when (keyValue[0]) { when (keyValue[0]) {
"ip" -> serverIp = keyValue[1] "ip" -> serverIp = keyValue[1]
"thisIp" -> thisCarIp = keyValue[1]
} }
} else {
return false
} }
} }
return true return true
@@ -24,4 +28,8 @@ class Config(val configFileName: String) {
fun getIp(): String { fun getIp(): String {
return serverIp 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 deltaTimeLocationRefresh: Int = 100//ms
val serverPort: Int = 8888 val serverPort: Int = 8888
val serverIp: String = "127.0.0.1"
val mainServerAddress = "127.0.0.1"
val mainServerPort = 7925 val mainServerPort = 7925
val config = Config("config.cfg") val config = Config("config.cfg")
val fs = require("fs") val fs = require("fs")
fun main(args: Array<String>) { fun main(args: Array<String>) {
@@ -25,6 +23,7 @@ fun main(args: Array<String>) {
println("incorrect config format!") println("incorrect config format!")
return return
} }
val handlers: MutableMap<String, AbstractHandler> = mutableMapOf() val handlers: MutableMap<String, AbstractHandler> = mutableMapOf()
handlers.put("/rc/control", Control(DirectionRequest.BuilderDirectionRequest(DirectionRequest.Command.fromIntToCommand(0), 0), DirectionResponse.BuilderDirectionResponse(0))) handlers.put("/rc/control", Control(DirectionRequest.BuilderDirectionRequest(DirectionRequest.Command.fromIntToCommand(0), 0), DirectionResponse.BuilderDirectionResponse(0)))
handlers.put("/rc/connect", Connect(SessionUpResponse.BuilderSessionUpResponse(0, 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 = {} private var callback: (bytes: ByteArray) -> Unit = {}
fun writeToFile(bytes: ByteArray) { fun writeToFile(bytes: ByteArray) {
println("writewrite uiii!") println("write: " + bytes)
val bytesT = bytes val bytesT = bytes
writeStream.write(js("new Buffer(bytesT)")); writeStream.write(js("new Buffer(bytesT)"));
// writeStream.end(); // writeStream.end();
+6 -5
View File
@@ -1,5 +1,6 @@
import carControl.ControlImpl import carControl.ControlImpl
import carControl.RouteExecutorImpl import carControl.RouteExecutorImpl
import carControl.RouteExecutorToUsb
import exceptions.RcControlException import exceptions.RcControlException
/** /**
@@ -26,17 +27,17 @@ class MicroController private constructor() {
this.modelID = "5740" this.modelID = "5740"
this.transportFilePath = "" this.transportFilePath = ""
this.car = Car(RouteExecutorImpl(), ControlImpl()) this.car = Car(RouteExecutorToUsb(), ControlImpl())
} }
fun start() { fun start() {
if (!isConnected()) { if (!isConnected()) {
connectToServer(serverIp, serverPort) connectToServer(config.getCarIp(), serverPort)
} }
this.transportFilePath = "./testTtyAcm"//todo need init // this.transportFilePath = "./testTtyAcm"//todo need init
mcTransport.initStreams(transportFilePath) // mcTransport.initStreams(transportFilePath)
mcTransport.setCallBack { bytes -> mcTransport.setCallBack { bytes ->
println("read: " + bytes.toString()) println("read: " + bytes.toString())
} }
@@ -99,7 +100,7 @@ class MicroController private constructor() {
} }
fun connectToServer(thisIp: String, thisPort: Int) { 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()) val bytes = ByteArray(requestObject.getSizeNoTag())
requestObject.writeTo(CodedOutputStream(bytes)) requestObject.writeTo(CodedOutputStream(bytes))
net.sendRequest(js("new Buffer(bytes)"), "/connect", { resultData -> 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 package net
import require import require
import mainServerAddress
import mainServerPort import mainServerPort
import config
/** /**
* Created by user on 7/28/16. * 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) { fun sendRequest(dataBuffer: dynamic, url: String, successCallback: (responseData: ByteArray) -> Unit, errorCallback: (err: dynamic) -> Unit) {
val options: dynamic = {} val options: dynamic = {}
val serverAddress = mainServerAddress val serverAddress = config.getIp()
val port = mainServerPort val port = mainServerPort
js("options = {hostname:serverAddress, port:port, path:url, method:'POST'}") js("options = {hostname:serverAddress, port:port, path:url, method:'POST'}")