From 779933be3276c148f9a718aa19534b9b801d6bf8 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 31 May 2023 18:57:10 +0200 Subject: [PATCH] refactored GPS bin --- src/bin/GPS.hx | 139 ++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 83 deletions(-) diff --git a/src/bin/GPS.hx b/src/bin/GPS.hx index 04beb69..faf071c 100644 --- a/src/bin/GPS.hx +++ b/src/bin/GPS.hx @@ -1,97 +1,70 @@ package bin; -import kernel.ps.ProcessHandle; -import kernel.ps.Process; +import lib.CLIAppBase; import kernel.gps.INS; import lib.Pos3; import lib.Vec.Vec3; using tink.CoreApi; -class GPS implements Process { - private var handle:ProcessHandle; - - public function new() {} - - public function run(handle: ProcessHandle):Void { - this.handle = handle; - - var subcommand = handle.args[0]; - var subcommand_args = handle.args.slice(1); - - switch (subcommand) { - case "set": - handle.close(setManuelPos(subcommand_args)); - case "status": - handle.close(getGPSStatus()); - case "locate": - kernel.gps.GPS.instance.locate().handle((pos)->{ - if (pos != null) { - handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}'); - handle.close(true); - } else { - handle.writeLine("Position not available"); - handle.close(false); - } - }); - case "ins": - INS.instance.align().handle(()->{ - handle.writeLine("INS aligned"); - handle.close(true); - }); - default: - handle.writeLine("Unknown subcommand: " + subcommand); - printHelp(); - handle.close(false); - } - } - - private function printHelp(){ - handle.writeLine("GPS commands:"); - handle.writeLine("set - set manual position"); - handle.writeLine("status - get current position and accuracy"); - handle.writeLine("locate - get current position"); - handle.writeLine("ins - align INS"); - } - - private function setManuelPos(args: Array): Bool { - var x: Float = Std.parseFloat(args[0]); - var y: Float = Std.parseFloat(args[1]); - var z: Float = Std.parseFloat(args[2]); - - var pos: Pos3 = new Vec3(x, y, z); - - kernel.gps.GPS.instance.setManualPosition(pos); - - return true; - } - - private function getGPSStatus(): Bool { - - var pos = kernel.gps.GPS.instance.getPosition(); - if (pos != null) { - handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}'); - } else { - handle.writeLine("Position not available"); +class GPS extends CLIAppBase { + public function new() { + registerSyncSubcommand("set", (args)->{ + var x: Float = Std.parseFloat(args[0]); + var y: Float = Std.parseFloat(args[1]); + var z: Float = Std.parseFloat(args[2]); + + var pos: Pos3 = new Vec3(x, y, z); + + kernel.gps.GPS.instance.setManualPosition(pos); + return true; - } + }," "); - var acc = kernel.gps.GPS.instance.getAccuracy(); - if (acc == 1){ - handle.writeLine("Accuracy: Low"); - } else if (acc == 2){ - handle.writeLine("Accuracy: Medium"); - } else if (acc == 3){ - handle.writeLine("Accuracy: High"); - } + registerSyncSubcommand("status",(args)->{ + var pos = kernel.gps.GPS.instance.getPosition(); + if (pos != null) { + handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}'); + } else { + handle.writeLine("Position not available"); + return true; + } + + var acc = kernel.gps.GPS.instance.getAccuracy(); + if (acc == 1){ + handle.writeLine("Accuracy: Low"); + } else if (acc == 2){ + handle.writeLine("Accuracy: Medium"); + } else if (acc == 3){ + handle.writeLine("Accuracy: High"); + } + + var ins = INS.instance.getHeading(); + if (ins != null) { + handle.writeLine('INS heading: ${ins.x} y:${ins.y} z:${ins.z}'); + } else { + handle.writeLine("INS heading not available"); + } + + return true; + }); - var ins = INS.instance.getHeading(); - if (ins != null) { - handle.writeLine('INS heading: ${ins.x} y:${ins.y} z:${ins.z}'); - } else { - handle.writeLine("INS heading not available"); - } + registerAsyncSubcommand("locate",(args)->{ + return kernel.gps.GPS.instance.locate().map((pos)->{ + if (pos != null) { + handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}'); + } else { + handle.writeLine("Position not available"); + } + return true; + }); + }); - return true; + registerAsyncSubcommand("ins",(args)->{ + return INS.instance.align().map((_)->{ + handle.writeLine("INS aligned"); + return true; + }); + }); } }