X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fopencellid.py;h=583d2e167b98af216b7a05360cf7d6666ca49c6d;hb=bf48ccad4b4b91e7d7e09d1087f5953bc2db97d7;hp=0131dc0305bdb6a4e385f7392470c7aecf5bba52;hpb=5e1ae25333928e02eed519652256bee2e8e06671;p=loctrkd.git diff --git a/gps303/opencellid.py b/gps303/opencellid.py index 0131dc0..583d2e1 100644 --- a/gps303/opencellid.py +++ b/gps303/opencellid.py @@ -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, 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, 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,19 +57,19 @@ def lookup(mcc, 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: obj = parse_message(packet) - avlat, avlon = lookup(obj.mcc, obj.gsm_cells, obj.wifi_aps) + avlat, avlon = lookup(obj.mcc, obj.mnc, obj.gsm_cells, obj.wifi_aps) print( "{} {:+#010.8g},{:+#010.8g}".format( datetime.fromtimestamp(timestamp), avlat, avlon