]> www.average.org Git - loctrkd.git/blobdiff - gps303/opencellid.py
Rename gps303proto to zx303proto
[loctrkd.git] / gps303 / opencellid.py
index 1932fa6980c32309cea1628feae10478256b4416..583d2e167b98af216b7a05360cf7d6666ca49c6d 100644 (file)
@@ -3,18 +3,27 @@ Lookaside backend to query local opencellid database
 """
 
 from sqlite3 import connect
+from typing import Any, Dict, List, Tuple
 
 __all__ = "init", "lookup"
 
 ldb = None
 
 
-def init(conf):
+def init(conf: Dict[str, Any]) -> None:
     global ldb
     ldb = connect(conf["opencellid"]["dbfn"])
 
 
-def lookup(mcc, mnc, gsm_cells, __):
+def shut() -> None:
+    if ldb is not None:
+        ldb.close()
+
+
+def lookup(
+    mcc: int, mnc: int, gsm_cells: List[Tuple[int, int, int]], __: Any
+) -> Tuple[float, float]:
+    assert ldb is not None
     lc = ldb.cursor()
     lc.execute("""attach database ":memory:" as mem""")
     lc.execute("create table mem.seen (locac int, cellid int, signal int)")
@@ -34,7 +43,7 @@ def lookup(mcc, mnc, gsm_cells, __):
     )
     data = list(lc.fetchall())
     if not data:
-        return None, None
+        return 0.0, 0.0
     sumsig = sum([1 / sig for _, _, sig in data])
     nsigs = [1 / sig / sumsig for _, _, sig in data]
     avlat = sum([lat * nsig for (lat, _, _), nsig in zip(data, nsigs)])
@@ -48,14 +57,14 @@ def lookup(mcc, mnc, gsm_cells, __):
 if __name__.endswith("__main__"):
     from datetime import datetime, timezone
     import sys
-    from .gps303proto import *
+    from .zx303proto import *
 
     db = connect(sys.argv[1])
     c = db.cursor()
     c.execute(
         """select tstamp, packet from events
             where proto in (?, ?)""",
-        (WIFI_POSITIONING.PROTO, WIFI_OFFLINE_POSITIONING.PROTO),
+        (proto_name(WIFI_POSITIONING), proto_name(WIFI_OFFLINE_POSITIONING)),
     )
     init({"opencellid": {"dbfn": sys.argv[2]}})
     for timestamp, packet in c: