From 1888de5a5dd4bdb85f3fb745341920a9996b278e Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Thu, 26 May 2022 19:24:59 +0200 Subject: [PATCH] Add googlemaps lookaside backend --- debian/gps303.conf | 4 ++- gps303/googlemaps.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ gps303/lookaside.py | 26 ++++++++---------- gps303/mkgpx.py | 2 +- gps303/opencellid.py | 4 +-- 5 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 gps303/googlemaps.py diff --git a/debian/gps303.conf b/debian/gps303.conf index f19b4af..9d5ed3c 100644 --- a/debian/gps303.conf +++ b/debian/gps303.conf @@ -13,7 +13,9 @@ htmlfile = /var/lib/gps303/index.html dbfn = /var/lib/gps303/gps303.sqlite [lookaside] -# "opencellid" is the only implemented backend for the time being +# "opencellid" and "googlemaps" can be here. Both require an access token, +# though googlemaps is only online, while opencellid backend looks up a +# local database, that can be updated once a week or once a month. backend = opencellid [opencellid] diff --git a/gps303/googlemaps.py b/gps303/googlemaps.py new file mode 100644 index 0000000..bb19a64 --- /dev/null +++ b/gps303/googlemaps.py @@ -0,0 +1,65 @@ +import googlemaps as gmaps +from sqlite3 import connect + +gclient = None + + +def init(conf): + global gclient + with open(conf["googlemaps"]["accesstoken"], encoding="ascii") as fl: + token = fl.read().rstrip() + gclient = gmaps.Client(key=token) + + +def lookup(mcc, mnc, gsm_cells, wifi_aps): + kwargs = { + "home_mobile_country_code": mcc, + "home_mobile_network_code": mnc, + "radio_type": "gsm", + "carrier": "O2", + "consider_ip": False, + "cell_towers": [ + { + "locationAreaCode": loc, + "cellId": cellid, + "signalStrength": sig, + } + for loc, cellid, sig in gsm_cells + ], + "wifi_access_points": [ + {"macAddress": mac, "signalStrength": sig} for mac, sig in wifi_aps + ], + } + result = gclient.geolocate(**kwargs) + if "location" in result: + return result["location"]["lat"], result["location"]["lng"] + else: + raise ValueError("google geolocation: " + str(result)) + + +if __name__.endswith("__main__"): + from datetime import datetime, timezone + import sys + from .gps303proto 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), + ) + init({"googlemaps": {"accesstoken": sys.argv[2]}}) + count = 0 + for timestamp, packet in c: + obj = parse_message(packet) + print(obj) + avlat, avlon = lookup(obj.mcc, obj.mnc, obj.gsm_cells, obj.wifi_aps) + print( + "{} {:+#010.8g},{:+#010.8g}".format( + datetime.fromtimestamp(timestamp), avlat, avlon + ) + ) + count += 1 + if count > 10: + break diff --git a/gps303/lookaside.py b/gps303/lookaside.py index 6924757..7655f55 100644 --- a/gps303/lookaside.py +++ b/gps303/lookaside.py @@ -15,12 +15,7 @@ log = getLogger("gps303/lookaside") def runserver(conf): - if conf.get("lookaside", "backend") == "opencellid": - qry = import_module(".opencellid", __package__) - else: - raise NotImplementedError( - "Lookaside only implements opencellid backend" - ) + qry = import_module("." + conf.get("lookaside", "backend"), __package__) qry.init(conf) zctx = zmq.Context() zsub = zctx.socket(zmq.SUB) @@ -40,14 +35,17 @@ def runserver(conf): datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc), msg, ) - lat, lon = qry.lookup(msg.mcc, msg.gsm_cells, msg.wifi_aps) - resp = Resp( - imei=zmsg.imei, - when=zmsg.when, # not the current time, but the original! - packet=msg.Out(latitude=lat, longitude=lon).packed, - ) - log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp) - zpush.send(resp.packed) + try: + lat, lon = qry.lookup(msg.mcc, msg.mnc, msg.gsm_cells, msg.wifi_aps) + resp = Resp( + imei=zmsg.imei, + when=zmsg.when, # not the current time, but the original! + packet=msg.Out(latitude=lat, longitude=lon).packed, + ) + log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp) + zpush.send(resp.packed) + except Exception as e: + log.warning("Lookup for %s resulted in %s", msg, e) except KeyboardInterrupt: pass diff --git a/gps303/mkgpx.py b/gps303/mkgpx.py index 061b54a..55cc943 100644 --- a/gps303/mkgpx.py +++ b/gps303/mkgpx.py @@ -39,7 +39,7 @@ xmlns="http://www.topografix.com/GPX/1/1"> for tstamp, packet in c: msg = parse_message(packet) if isinstance(msg, (WIFI_POSITIONING, WIFI_OFFLINE_POSITIONING)): - lat, lon = ocid.lookup(msg.mcc, msg.gsm_cells, msg.wifi_aps) + lat, lon = ocid.lookup(msg.mcc, msg.mnc, msg.gsm_cells, msg.wifi_aps) if lat is None or lon is None: continue elif isinstance(msg, (GPS_POSITIONING, GPS_OFFLINE_POSITIONING)): diff --git a/gps303/opencellid.py b/gps303/opencellid.py index 0131dc0..1932fa6 100644 --- a/gps303/opencellid.py +++ b/gps303/opencellid.py @@ -14,7 +14,7 @@ def init(conf): ldb = connect(conf["opencellid"]["dbfn"]) -def lookup(mcc, gsm_cells, _): +def lookup(mcc, mnc, gsm_cells, __): lc = ldb.cursor() lc.execute("""attach database ":memory:" as mem""") lc.execute("create table mem.seen (locac int, cellid int, signal int)") @@ -60,7 +60,7 @@ if __name__.endswith("__main__"): 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 -- 2.39.2