From f0cb9170c26a72a525e41743ff7e9d2c21b3f89b Mon Sep 17 00:00:00 2001 From: MaximZaitsev Date: Tue, 12 Jul 2016 14:31:21 +0300 Subject: [PATCH] created command line args in flash server --- car_net/stFlashServer/.gitignore | 1 + car_net/stFlashServer/handlers.js | 18 +++---- car_net/stFlashServer/main.js | 79 +++++++++--------------------- car_net/stFlashServer/package.json | 16 ++++++ 4 files changed, 47 insertions(+), 67 deletions(-) create mode 100644 car_net/stFlashServer/package.json diff --git a/car_net/stFlashServer/.gitignore b/car_net/stFlashServer/.gitignore index 28e0ca18aae..7bbb37d4055 100644 --- a/car_net/stFlashServer/.gitignore +++ b/car_net/stFlashServer/.gitignore @@ -1,4 +1,5 @@ .idea/workspace.xml .idea/uiDesigner.xml protobufjs +node_modules out diff --git a/car_net/stFlashServer/handlers.js b/car_net/stFlashServer/handlers.js index ca4e9d84364..110d370dc06 100644 --- a/car_net/stFlashServer/handlers.js +++ b/car_net/stFlashServer/handlers.js @@ -2,29 +2,27 @@ * Created by user on 7/11/16. */ const fs = require("fs"); -const protoBuf = require("./protobufjs"); -const builder = protoBuf.loadProtoFile("./proto/carkot.proto");//todo перенести как аргумент при запуске. -const protoConstructor = builder.build("carkot"); -var commandPrefix = "./data/macosx-x86_64/st-flash write"; -var binFilePath = "./f.bin"; +const main = require("./main.js"); function loadBin(httpContent, response) { - var uploadClass = protoConstructor.Upload; + var uploadClass = main.protoConstructor.Upload; var uploadObject = uploadClass.decode(httpContent); fs.writeFile(binFilePath, uploadObject.data.buffer, "binary", function (error) { - var uploadResultClass = protoConstructor.UploadResult; + var uploadResultClass = main.protoConstructor.UploadResult; var code = 0; + var stdErr = ""; if (error) { code = 2; + stdErr = error.toString(); } else { code = 0; - console.log(commandPrefix + " " + binFilePath + " " + uploadObject.base); + console.log(main.commandPrefix + " " + main.binFilePath + " " + uploadObject.base); } var resultObject = new uploadResultClass({ "stdOut": "", "resultCode": code, - "stdErr": "" + "stdErr": stdErr }); var byteBuffer = resultObject.encode(); var byteArray = []; @@ -37,7 +35,7 @@ function loadBin(httpContent, response) { }); } -function other(request, response) { +function other(httpContent, response) { console.log("other"); response.writeHead(200, {"Content-Type": "text/plain"}); response.end(); diff --git a/car_net/stFlashServer/main.js b/car_net/stFlashServer/main.js index 685490834b0..78e42401a89 100644 --- a/car_net/stFlashServer/main.js +++ b/car_net/stFlashServer/main.js @@ -5,67 +5,32 @@ var server = require("./server.js"); var router = require("./router.js"); var handlers = require("./handlers.js"); +var commander = require("commander"); +const protoBuf = require("protobufjs"); + +commander + .version('1.0.0') + .option('-p, --protopath [path]', 'path to proto file. default ./proto/carkot.proto') + .option('-b, --binfile [path]', 'path to save bin file with name f.bin. default ./') + // .option('-b, --bbq-sauce', 'Add bbq sauce') + // .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble') + .parse(process.argv); + +const builder = protoBuf.loadProtoFile(commander.protopath ? commander.protopath : "./proto/carkot.proto"); +const protoConstructor = builder.build("carkot"); + +if (commander.protopath) { + console.log("pew " + commander.protopath) +} var handle = {}; handle["/"] = handlers.other; handle["/loadBin"] = handlers.loadBin; handle["/other"] = handlers.other; - -// UploadR = upRes.UploadResult; -// var res = new UploadR({"error": "test error!"}); -// console.log(res.encode()); -// -// -// var testBack = UploadR.decode(res.encode()); -// console.log(testBack); - -// console.log(process.argv); server.start(router.route, handle); - -// var http = require("http"); -// -// var global_closure = null; -// -// // nodejs/javascript closures -// http.createServer(function(request, response) { -// // console.log("test"); -// global_closure && global_closure(); -// -// // console.log(request); -// // response.writeHead(200, {"Content-Type": "text/plain"}); -// // response.write("Hello World"); -// -// function closure() { -// console.log(request.toString()); -// } -// global_closure = closure; -// -// //writeToJournal().getDBData().processDBData().sendResponse().commitJournal(); -// function writeToJournal() { -// console.log(request); -// getDBData(processDBData); -// getDBData(function() {}); -// } -// -// function getDBData(callback) { -// db.getData({ id : request.query['id'] }, onDbDataReady); -// var i = 0; -// -// function onDbDataReady(err, someDBData) { -// if (err) { -// return response.error(); -// } -// console.log(i); -// callback(someDBData); -// } -// } -// -// function processDBData(dbData) { -// function sendResponse() { -// -// } -// } -// -// response.end(); -// }).listen(8888); +exports.protoConstructor = protoConstructor; +exports.commandPrefix = "./data/macosx-x86_64/st-flash write"; +exports.binFilePath = (commander.binfile ? commander.binfile : "./") + "f.bin"; +console.log(exports.binFilePath); +console.log(exports.commandPrefix); \ No newline at end of file diff --git a/car_net/stFlashServer/package.json b/car_net/stFlashServer/package.json new file mode 100644 index 00000000000..38ecf0cb442 --- /dev/null +++ b/car_net/stFlashServer/package.json @@ -0,0 +1,16 @@ +{ + "name": "stflashserver", + "version": "1.0.0", + "description": "", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node main.js" + }, + "author": "maxim", + "license": "ISC", + "dependencies": { + "commander": "^2.9.0", + "protobufjs": "^5.0.1" + } +}