js server. added interaction with main server
This commit is contained in:
@@ -6,8 +6,8 @@ const fs = require("fs");
|
||||
const main = require("./main.js");
|
||||
const directionClass = main.protoConstructorControl.DirectionRequest;
|
||||
|
||||
function Car(uid) {
|
||||
this.uid = uid;
|
||||
function Car() {
|
||||
this.uid = "";
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.angle = 0;
|
||||
@@ -47,7 +47,7 @@ function resume() {
|
||||
|
||||
function move(delta) {
|
||||
var deltaSeconds = delta / 1000;
|
||||
// console.log("x=" + this.x + " y=" + this.y + " angle=" + this.angle);
|
||||
console.log("x=" + this.x + " y=" + this.y + " angle=" + this.angle);
|
||||
if (this.paused) {
|
||||
return;
|
||||
}
|
||||
@@ -107,8 +107,8 @@ function setDirection(directionByte, callBack) {
|
||||
fs.appendFile(main.transportFilePath, directionByte, "binary", function (error) {
|
||||
var code = 0;
|
||||
var errorMsg = "";
|
||||
console.log(error);
|
||||
if (error) {
|
||||
console.log(error);
|
||||
errorMsg = error;
|
||||
}
|
||||
if (callBack != null) {
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Created by user on 7/22/16.
|
||||
*/
|
||||
const main = require("./main.js");
|
||||
const http = require("http");
|
||||
function sendData(data, url, callBack) {
|
||||
var options = {
|
||||
hostname: main.serverAddress,
|
||||
port: main.serverPort,
|
||||
path: url,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Length': Buffer.byteLength(data)
|
||||
}
|
||||
};
|
||||
var req = http.request(options, function (response) {
|
||||
var answerData = [];
|
||||
response.on("data", function (datas) {
|
||||
for (var i = 0; i < datas.length; i++) {
|
||||
answerData.push(datas[i]);
|
||||
}
|
||||
});
|
||||
response.on("end", function () {
|
||||
callBack(answerData)
|
||||
});
|
||||
});
|
||||
//todo errors
|
||||
req.write(data);
|
||||
req.end();
|
||||
}
|
||||
|
||||
exports.sendData = sendData;
|
||||
@@ -5,9 +5,26 @@
|
||||
const main = require("./../main.js");
|
||||
|
||||
function handle(httpContent, response) {
|
||||
var routeClass = main.protoConstructorRoute.RouteRequest;
|
||||
var routeObject = routeClass.decode(httpContent);
|
||||
console.log(routeObject)//todo сделать
|
||||
const routeClass = main.protoConstructorRoute.RouteRequest;
|
||||
const routeObject = routeClass.decode(httpContent);
|
||||
const wayPoints = routeObject.way_points;
|
||||
main.thisCar.setPath(wayPoints);
|
||||
|
||||
var routeResponse = new main.protoConstructorRoute.RouteResponse({
|
||||
"code": 0,
|
||||
"errorMsg": ""
|
||||
});
|
||||
|
||||
var byteBuffer = routeResponse.encode();
|
||||
var byteArray = [];
|
||||
for (var i = 0; i < byteBuffer.limit; i++) {
|
||||
byteArray.push(byteBuffer.buffer[i]);
|
||||
}
|
||||
|
||||
response.writeHead(200, {"Content-Type": "text/plain", "Content-length": byteArray.length});
|
||||
response.write(new Buffer(byteArray));
|
||||
response.end();
|
||||
|
||||
}
|
||||
|
||||
exports.handler = handle;
|
||||
+17
-4
@@ -8,6 +8,7 @@ const commander = require("commander");
|
||||
const protoBuf = require("protobufjs");
|
||||
const fs = require("fs");
|
||||
const udev = require("udev");
|
||||
const client = require("./client.js");
|
||||
|
||||
//parsing command line args
|
||||
commander
|
||||
@@ -33,18 +34,30 @@ if (commander.flash) {
|
||||
const builderCarkot = protoBuf.loadProtoFile((commander.protopath ? commander.protopath : "./proto/") + "carkot.proto");
|
||||
const builderControl = protoBuf.loadProtoFile((commander.protopath ? commander.protopath : "./proto/" + "direction.proto"));
|
||||
const builderRoute = protoBuf.loadProtoFile((commander.protopath ? commander.protopath : "./proto/" + "route.proto"));
|
||||
const builderConnect = protoBuf.loadProtoFile((commander.protopath ? commander.protopath : "./proto/" + "connect.proto"));
|
||||
|
||||
var executeShell = "./st-flash";
|
||||
exports.protoConstructorCarkot = builderCarkot.build("carkot");
|
||||
exports.protoConstructorControl = builderControl.build("carkot");
|
||||
exports.protoConstructorRoute = builderRoute.build("carkot");
|
||||
exports.protoConstructorConnect = builderConnect.build("carkot");
|
||||
exports.commandPrefix = executeShell + " write";
|
||||
exports.binFilePath = (commander.flash ? commander.flash : "./") + "flash.bin";
|
||||
exports.transportFilePath = "";
|
||||
exports.transportFilePath = "/dev/ttyACM0";//todo плохо:)
|
||||
exports.serverAddress = "127.0.0.1";
|
||||
exports.serverPort = 7925;
|
||||
|
||||
//init this car
|
||||
var uid = "";//todo connect to srv
|
||||
var car = require("./Car").getCar(uid);
|
||||
var connectRequest = new exports.protoConstructorConnect.ConnectionRequest({
|
||||
// "ip": "192.168.43.135",//todo its ip of this server in local wifi network. hardcore is bad here:)
|
||||
"ip": "127.0.0.1",//todo for tests on local mashine
|
||||
"port": 8888
|
||||
});
|
||||
client.sendData(connectRequest.encode().buffer, "/connect", function (data) {
|
||||
var connectionResponse = exports.protoConstructorConnect.ConnectionResponse.decode(data);
|
||||
exports.thisCar.uid = connectionResponse.uid
|
||||
});
|
||||
var car = require("./Car").getCar();
|
||||
exports.thisCar = car;
|
||||
|
||||
//handlers for client requests
|
||||
@@ -52,7 +65,7 @@ var handle = {};
|
||||
handle["/loadBin"] = require("./handlers/loadBinHandler").handler;
|
||||
handle["/control"] = require("./handlers/controlHandler").handler;
|
||||
handle["/route"] = require("./handlers/setRouteHandler").handler;
|
||||
exports.transportFilePath = "/dev/ttyACM0";
|
||||
|
||||
//add event handlers from udev monitor (add device and remove device)
|
||||
const monitor = udev.monitor();
|
||||
monitor.on('add', function (device) {
|
||||
|
||||
@@ -16,6 +16,8 @@ function start(route, handlers) {
|
||||
});
|
||||
|
||||
request.on("end", function () {
|
||||
console.log(pathname);
|
||||
console.log(httpContent);
|
||||
route(handlers, pathname, httpContent, response);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user