made GPS a static class
This commit is contained in:
		
							parent
							
								
									97c906e013
								
							
						
					
					
						commit
						5a9d463192
					
				@ -16,13 +16,13 @@ class GPS extends CLIAppBase {
 | 
			
		||||
	
 | 
			
		||||
			var pos: Pos3 = new Vec3<Float>(x, y, z);
 | 
			
		||||
	
 | 
			
		||||
			kernel.gps.GPS.instance.setManualPosition(pos);
 | 
			
		||||
			kernel.gps.GPS.setManualPosition(pos);
 | 
			
		||||
	
 | 
			
		||||
			return true;
 | 
			
		||||
		},"<x> <y> <z>");
 | 
			
		||||
 | 
			
		||||
		registerSyncSubcommand("status",(args)->{
 | 
			
		||||
			var pos = kernel.gps.GPS.instance.getPosition();
 | 
			
		||||
			var pos = kernel.gps.GPS.getPosition();
 | 
			
		||||
			if (pos != null) {
 | 
			
		||||
				handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
 | 
			
		||||
			} else {
 | 
			
		||||
@ -30,7 +30,7 @@ class GPS extends CLIAppBase {
 | 
			
		||||
				return true;
 | 
			
		||||
			}
 | 
			
		||||
	
 | 
			
		||||
			var acc = kernel.gps.GPS.instance.getAccuracy();
 | 
			
		||||
			var acc = kernel.gps.GPS.getAccuracy();
 | 
			
		||||
			if (acc == 1){
 | 
			
		||||
				handle.writeLine("Accuracy: Low");
 | 
			
		||||
			} else if (acc == 2){
 | 
			
		||||
@ -50,7 +50,7 @@ class GPS extends CLIAppBase {
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		registerAsyncSubcommand("locate",(args)->{
 | 
			
		||||
			return kernel.gps.GPS.instance.locate().map((pos)->{
 | 
			
		||||
			return kernel.gps.GPS.locate().map((pos)->{
 | 
			
		||||
				if (pos != null) {
 | 
			
		||||
					handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
 | 
			
		||||
				} else {
 | 
			
		||||
 | 
			
		||||
@ -35,14 +35,14 @@ class PFClient implements Process {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function render() {
 | 
			
		||||
        var acc = kernel.gps.GPS.instance.getAccuracy();
 | 
			
		||||
        var pos: Pos3 = kernel.gps.GPS.instance.getPosition() ?? {x: 0, y: 0, z: 0};
 | 
			
		||||
        var acc = kernel.gps.GPS.getAccuracy();
 | 
			
		||||
        var pos: Pos3 = kernel.gps.GPS.getPosition() ?? {x: 0, y: 0, z: 0};
 | 
			
		||||
 | 
			
		||||
        var childre: Array<UIElement> = [
 | 
			
		||||
            new TextElement('Acc: ${acc}'),
 | 
			
		||||
            new TextElement('Pos: X:${pos.x} Y:${pos.y} Z:${pos.z}'),
 | 
			
		||||
            new TextElement('UPDATE', { style: {bgColor: Gray}, uiEvents: {onClick: () -> {
 | 
			
		||||
                kernel.gps.GPS.instance.locate().handle((pos) ->{
 | 
			
		||||
                kernel.gps.GPS.locate().handle((pos) ->{
 | 
			
		||||
                    this.requestRender();
 | 
			
		||||
                });
 | 
			
		||||
            }}}),
 | 
			
		||||
 | 
			
		||||
@ -37,7 +37,7 @@ class Init {
 | 
			
		||||
		Routing.init();
 | 
			
		||||
		Net.init();
 | 
			
		||||
 | 
			
		||||
		GPS.instance = new GPS();
 | 
			
		||||
		GPS.init();
 | 
			
		||||
		INS.instance = new INS();
 | 
			
		||||
 | 
			
		||||
		// Register default terminate handler
 | 
			
		||||
 | 
			
		||||
@ -15,22 +15,20 @@ using tink.CoreApi;
 | 
			
		||||
	You need at least 3 computers that know their position to determine the position of the computer.
 | 
			
		||||
**/
 | 
			
		||||
class GPS {
 | 
			
		||||
	public static var instance:GPS;
 | 
			
		||||
	private static var shouldRespond = true;
 | 
			
		||||
	private static var shouldDoWholeNumberCheck = true;
 | 
			
		||||
	private static var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed)
 | 
			
		||||
	private static var cachedPosition:Pos3;
 | 
			
		||||
	private static var lastPositionResponse: Array<{pos:Pos3,dist:Float}> = [];
 | 
			
		||||
 | 
			
		||||
	private var shouldRespond = true;
 | 
			
		||||
	private var shouldDoWholeNumberCheck = true;
 | 
			
		||||
	private var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed)
 | 
			
		||||
	private var cachedPosition:Pos3;
 | 
			
		||||
	private var lastPositionResponse: Array<{pos:Pos3,dist:Float}> = [];
 | 
			
		||||
 | 
			
		||||
	private var futureResolve: (pos:Null<Pos3>) -> Void = null;
 | 
			
		||||
	private static var futureResolve: (pos:Null<Pos3>) -> Void = null;
 | 
			
		||||
 | 
			
		||||
	@:allow(kernel.Init)
 | 
			
		||||
	private function new() {
 | 
			
		||||
		this.loadCachedPosition();
 | 
			
		||||
	private static function init() {
 | 
			
		||||
		loadCachedPosition();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function setManualPosition(pos:Pos3) {
 | 
			
		||||
	public static function setManualPosition(pos:Pos3) {
 | 
			
		||||
		var kvstore = new KVStore("gps");
 | 
			
		||||
		kvstore.set("mpos",pos);
 | 
			
		||||
		kvstore.save();
 | 
			
		||||
@ -42,39 +40,39 @@ class GPS {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@:allow(kernel.gps.INS)
 | 
			
		||||
	private function setINSPosition(pos:Pos3) {
 | 
			
		||||
	private static function setINSPosition(pos:Pos3) {
 | 
			
		||||
		cachedPosition = pos;
 | 
			
		||||
		posAccuracy = 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function getPosition():Null<Pos3> {
 | 
			
		||||
	public static function getPosition():Null<Pos3> {
 | 
			
		||||
		return cachedPosition;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function getAccuracy():Int {
 | 
			
		||||
	public static function getAccuracy():Int {
 | 
			
		||||
		return posAccuracy;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function invalidatePosition() {
 | 
			
		||||
	public static function invalidatePosition() {
 | 
			
		||||
		cachedPosition = null;
 | 
			
		||||
		posAccuracy = 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function locate():Future<Null<Pos3>> {
 | 
			
		||||
	public static function locate():Future<Null<Pos3>> {
 | 
			
		||||
		// TODO: implenet a timeout
 | 
			
		||||
		// TODO: dont send a request twice if the last one is still pending or we moved
 | 
			
		||||
		return new Future<Null<Pos3>>((resolve)->{
 | 
			
		||||
			this.futureResolve = resolve;
 | 
			
		||||
			futureResolve = resolve;
 | 
			
		||||
			sendPositionRequest();
 | 
			
		||||
			return null;
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function resolveFuture(pos:Null<Pos3>) {
 | 
			
		||||
		this.futureResolve(pos);
 | 
			
		||||
	private static function resolveFuture(pos:Null<Pos3>) {
 | 
			
		||||
		futureResolve(pos);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function persistCachedPositon() {
 | 
			
		||||
	private static function persistCachedPositon() {
 | 
			
		||||
		if (cachedPosition == null) return;
 | 
			
		||||
		var kvstore = new KVStore("gps");
 | 
			
		||||
 | 
			
		||||
@ -82,7 +80,7 @@ class GPS {
 | 
			
		||||
		kvstore.save();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function loadCachedPosition() {
 | 
			
		||||
	private static function loadCachedPosition() {
 | 
			
		||||
		var kvstore = new KVStore("gps");
 | 
			
		||||
		kvstore.load();
 | 
			
		||||
 | 
			
		||||
@ -119,12 +117,12 @@ class GPS {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function sendPositionRequest() {
 | 
			
		||||
	private static function sendPositionRequest() {
 | 
			
		||||
		Net.brodcastGPSRequest();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@:allow(kernel.net.Net)
 | 
			
		||||
	private function handlePackage(pack:Package<Noise>, dist: Float,iface: INetworkInterface) {
 | 
			
		||||
	private static function handlePackage(pack:Package<Noise>, dist: Float,iface: INetworkInterface) {
 | 
			
		||||
		switch (pack.type) {
 | 
			
		||||
			case GPSRequest:
 | 
			
		||||
				if (!shouldRespond) return;
 | 
			
		||||
@ -159,7 +157,7 @@ class GPS {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function calculatePosition():Null<Pos3> {
 | 
			
		||||
	private static function calculatePosition():Null<Pos3> {
 | 
			
		||||
		if (lastPositionResponse.length < 3) return null;
 | 
			
		||||
 | 
			
		||||
		// do a simple trilateration with the last 3 responses for now
 | 
			
		||||
@ -210,7 +208,7 @@ class GPS {
 | 
			
		||||
	/**
 | 
			
		||||
		Determines the position(s) of a point given 3 other points and the distance to each of them.
 | 
			
		||||
	**/
 | 
			
		||||
	private function trilateration(p1:Pos3,p2:Pos3,p3: Pos3,r1:Float,r2:Float,r3:Float):Pair<Pos3,Pos3> {
 | 
			
		||||
	private static function trilateration(p1:Pos3,p2:Pos3,p3: Pos3,r1:Float,r2:Float,r3:Float):Pair<Pos3,Pos3> {
 | 
			
		||||
		var a2b = p2 - p1;
 | 
			
		||||
		var a2c = p3 - p1;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -75,9 +75,9 @@ class INS {
 | 
			
		||||
 | 
			
		||||
	private function move(dir: Null<Pos3>) {
 | 
			
		||||
		Log.debug('INS move: $dir');
 | 
			
		||||
		var pos = GPS.instance.getPosition();
 | 
			
		||||
		var pos = GPS.getPosition();
 | 
			
		||||
		var newPos = pos + dir;
 | 
			
		||||
		GPS.instance.setINSPosition(newPos);
 | 
			
		||||
		GPS.setINSPosition(newPos);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function getHeading():Null<Pos3> {
 | 
			
		||||
@ -94,7 +94,7 @@ class INS {
 | 
			
		||||
				return null;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			GPS.instance.locate().handle((pos1)->{
 | 
			
		||||
			GPS.locate().handle((pos1)->{
 | 
			
		||||
				Log.debug('pos1: $pos1');
 | 
			
		||||
				if (pos1 == null) {
 | 
			
		||||
					Log.warn("GPS not available for 1st position");
 | 
			
		||||
@ -110,7 +110,7 @@ class INS {
 | 
			
		||||
					return;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				GPS.instance.locate().handle((pos2)->{
 | 
			
		||||
				GPS.locate().handle((pos2)->{
 | 
			
		||||
					Log.debug('pos2: $pos2');
 | 
			
		||||
					if (pos2 == null) {
 | 
			
		||||
						Log.warn("GPS not available for 2nd position");
 | 
			
		||||
@ -127,7 +127,7 @@ class INS {
 | 
			
		||||
 | 
			
		||||
					this.heading = heading;
 | 
			
		||||
					moveBack(moved);
 | 
			
		||||
					GPS.instance.setINSPosition(pos1);
 | 
			
		||||
					GPS.setINSPosition(pos1);
 | 
			
		||||
 | 
			
		||||
					resolve(Noise);
 | 
			
		||||
				});
 | 
			
		||||
 | 
			
		||||
@ -90,7 +90,7 @@ class Net {
 | 
			
		||||
						return;
 | 
			
		||||
					}
 | 
			
		||||
					// Delegate to GPS
 | 
			
		||||
					GPS.instance.handlePackage(cast pack,dist,interf);
 | 
			
		||||
					GPS.handlePackage(cast pack,dist,interf);
 | 
			
		||||
			}
 | 
			
		||||
		}else{
 | 
			
		||||
			// New message received but its not ment for us. Forward if possible.
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user