From 5a9d4631921a2f65d5ea25a5bbaaf92b8e2f6cb7 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sun, 30 Jul 2023 15:42:02 +0200 Subject: [PATCH] made GPS a static class --- src/bin/GPS.hx | 8 +++--- src/bin/pathfinder/PFClient.hx | 6 ++--- src/kernel/Init.hx | 2 +- src/kernel/gps/GPS.hx | 48 ++++++++++++++++------------------ src/kernel/gps/INS.hx | 10 +++---- src/kernel/net/Net.hx | 2 +- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/bin/GPS.hx b/src/bin/GPS.hx index faf071c..0acc230 100644 --- a/src/bin/GPS.hx +++ b/src/bin/GPS.hx @@ -16,13 +16,13 @@ class GPS extends CLIAppBase { var pos: Pos3 = new Vec3(x, y, z); - kernel.gps.GPS.instance.setManualPosition(pos); + kernel.gps.GPS.setManualPosition(pos); return true; }," "); 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 { diff --git a/src/bin/pathfinder/PFClient.hx b/src/bin/pathfinder/PFClient.hx index be2202a..12640c2 100644 --- a/src/bin/pathfinder/PFClient.hx +++ b/src/bin/pathfinder/PFClient.hx @@ -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 = [ 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(); }); }}}), diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index 39d3c28..454c22b 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -37,7 +37,7 @@ class Init { Routing.init(); Net.init(); - GPS.instance = new GPS(); + GPS.init(); INS.instance = new INS(); // Register default terminate handler diff --git a/src/kernel/gps/GPS.hx b/src/kernel/gps/GPS.hx index 492ca1c..60a17f9 100644 --- a/src/kernel/gps/GPS.hx +++ b/src/kernel/gps/GPS.hx @@ -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) -> Void = null; + private static var futureResolve: (pos:Null) -> 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 { + public static function getPosition():Null { 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> { + public static function locate():Future> { // TODO: implenet a timeout // TODO: dont send a request twice if the last one is still pending or we moved return new Future>((resolve)->{ - this.futureResolve = resolve; + futureResolve = resolve; sendPositionRequest(); return null; }); } - private function resolveFuture(pos:Null) { - this.futureResolve(pos); + private static function resolveFuture(pos:Null) { + 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, dist: Float,iface: INetworkInterface) { + private static function handlePackage(pack:Package, dist: Float,iface: INetworkInterface) { switch (pack.type) { case GPSRequest: if (!shouldRespond) return; @@ -159,7 +157,7 @@ class GPS { } } - private function calculatePosition():Null { + private static function calculatePosition():Null { 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 { + private static function trilateration(p1:Pos3,p2:Pos3,p3: Pos3,r1:Float,r2:Float,r3:Float):Pair { var a2b = p2 - p1; var a2c = p3 - p1; diff --git a/src/kernel/gps/INS.hx b/src/kernel/gps/INS.hx index d843a09..99768b7 100644 --- a/src/kernel/gps/INS.hx +++ b/src/kernel/gps/INS.hx @@ -75,9 +75,9 @@ class INS { private function move(dir: Null) { 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 { @@ -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); }); diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index 7f33714..a9d1e49 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -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.