2019-07-10 13:11:46 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import show_help from '../help.mjs';
|
|
|
|
import { decode_payload } from '../ttn-app-server/DecodePayload.mjs';
|
2019-07-22 15:04:49 +00:00
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
// HACK
|
|
|
|
import awilix from 'awilix';
|
2019-07-10 13:11:46 +00:00
|
|
|
|
|
|
|
export default async function(c) {
|
|
|
|
let { ansi: a, log: l, settings } = c.cradle;
|
|
|
|
|
|
|
|
// 2: CLI Argument Parsing
|
|
|
|
let args = process.argv.slice(2); // Slice out the node binary name and the filename
|
|
|
|
let extras = [];
|
|
|
|
for(let i = 0; i < args.length; i++) {
|
|
|
|
if(!args[i].startsWith("-")) {
|
|
|
|
extras.push(args[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(args[i]) {
|
|
|
|
case "--help":
|
|
|
|
case "-h":
|
|
|
|
show_help(c.cradle);
|
|
|
|
process.exit();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "--version":
|
|
|
|
case "-v":
|
|
|
|
console.log(program.version);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Add more arguments here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3: Environment Variable Parsing
|
|
|
|
|
|
|
|
// process.env.XYZ
|
|
|
|
|
|
|
|
// 4: Run
|
|
|
|
if(extras.length < 1) {
|
|
|
|
console.error(`${a.fred}${a.hicol}Error: No subcommand specified.${a.reset}`);
|
|
|
|
show_help(c.cradle);
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
l.log(`${a.fgreen}${a.hicol}*** LoRaWAN Signal Mapper ***${a.reset}`);
|
|
|
|
|
|
|
|
switch(extras[0]) {
|
|
|
|
case "ttn-app-server":
|
|
|
|
l.log(`${a.fgreen}${a.hicol}Starting The Things Network application server${a.reset}`);
|
|
|
|
|
|
|
|
let app_server = c.resolve("TTNAppServer");
|
|
|
|
await app_server.run();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "decode-test":
|
|
|
|
l.log(`${a.fgreen}${a.hicol}Decoding message${a.reset}`);
|
|
|
|
if(process.argv.length < 4) {
|
2019-07-11 13:03:27 +00:00
|
|
|
l.error("Error: No message specified. Specify the message to decode in single quotes directly after 'decode-test'.");
|
2019-07-10 13:11:46 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
l.log("Input: ", process.argv[3]);
|
|
|
|
l.log("Output: ", decode_payload(process.argv[3]));
|
|
|
|
break;
|
|
|
|
|
2019-07-11 13:03:27 +00:00
|
|
|
case "process-data":
|
|
|
|
l.log(`${a.fgreen}${a.hicol}Processing microSD card data${a.reset}`);
|
|
|
|
if(process.argv.length < 4) {
|
|
|
|
l.error("Error: No filename specified. Do so by specifying a filename after the 'process-data' subcommand.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let filepath = process.argv[3];
|
|
|
|
|
|
|
|
let data_processor = c.cradle.DataProcessor;
|
|
|
|
await data_processor.process(filepath);
|
|
|
|
l.log("Processing complete!");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2019-07-18 16:22:37 +00:00
|
|
|
case "train-ai":
|
2019-07-22 15:04:49 +00:00
|
|
|
// Do ninja dynamic import
|
|
|
|
// This avoid loading TensorFlow when it's not needed, as it causes
|
|
|
|
// a crash if the AVX2 CPU instruction set isn't available for some
|
|
|
|
// reason. Hrm.
|
|
|
|
let AITrainer = (await import(path.join(c.cradle.root_dir, "train-ai/AITrainer.mjs"))).default;
|
|
|
|
|
|
|
|
c.register({
|
|
|
|
AITrainer: awilix.asClass(AITrainer)
|
|
|
|
});
|
|
|
|
|
2019-07-18 16:22:37 +00:00
|
|
|
l.log(`${a.fgreen}${a.hicol}Training AIs${a.reset}`);
|
|
|
|
let ai_trainer = c.cradle.AITrainer;
|
|
|
|
await ai_trainer.train_all();
|
|
|
|
break;
|
|
|
|
|
2019-07-10 13:11:46 +00:00
|
|
|
default:
|
2019-07-11 13:03:27 +00:00
|
|
|
l.error(`Error: Subcommand '${extras[0]}' not recognised.`);
|
|
|
|
l.error(`Perhaps you mistyped it, or it hasn't been implemented yet?`);
|
|
|
|
l.error(`Try invoking this program with --help to see a list of supported subcommands.`);
|
2019-07-10 13:11:46 +00:00
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|