refactored GPS bin

This commit is contained in:
Djeeberjr 2023-05-31 18:57:10 +02:00
parent 06b3e37138
commit 779933be32

View File

@ -1,97 +1,70 @@
package bin; package bin;
import kernel.ps.ProcessHandle; import lib.CLIAppBase;
import kernel.ps.Process;
import kernel.gps.INS; import kernel.gps.INS;
import lib.Pos3; import lib.Pos3;
import lib.Vec.Vec3; import lib.Vec.Vec3;
using tink.CoreApi; using tink.CoreApi;
class GPS implements Process { class GPS extends CLIAppBase {
private var handle:ProcessHandle; 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]);
public function new() {} var pos: Pos3 = new Vec3<Float>(x, y, z);
public function run(handle: ProcessHandle):Void { kernel.gps.GPS.instance.setManualPosition(pos);
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 <x> <y> <z> - 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<String>): 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<Float>(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");
return true; return true;
} },"<x> <y> <z>");
var acc = kernel.gps.GPS.instance.getAccuracy(); registerSyncSubcommand("status",(args)->{
if (acc == 1){ var pos = kernel.gps.GPS.instance.getPosition();
handle.writeLine("Accuracy: Low"); if (pos != null) {
} else if (acc == 2){ handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
handle.writeLine("Accuracy: Medium"); } else {
} else if (acc == 3){ handle.writeLine("Position not available");
handle.writeLine("Accuracy: High"); return true;
} }
var ins = INS.instance.getHeading(); var acc = kernel.gps.GPS.instance.getAccuracy();
if (ins != null) { if (acc == 1){
handle.writeLine('INS heading: ${ins.x} y:${ins.y} z:${ins.z}'); handle.writeLine("Accuracy: Low");
} else { } else if (acc == 2){
handle.writeLine("INS heading not available"); handle.writeLine("Accuracy: Medium");
} } else if (acc == 3){
handle.writeLine("Accuracy: High");
}
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");
}
return true;
});
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;
});
});
registerAsyncSubcommand("ins",(args)->{
return INS.instance.align().map((_)->{
handle.writeLine("INS aligned");
return true;
});
});
} }
} }