X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=loctrkd%2Fgooglemaps.py;fp=loctrkd%2Fgooglemaps.py;h=418523d45eb9581dce89d07664c48a5eeb775926;hb=dbdf9d63af31770ad57302e16b17a2fdc526773f;hp=0000000000000000000000000000000000000000;hpb=bf48ccad4b4b91e7d7e09d1087f5953bc2db97d7;p=loctrkd.git diff --git a/loctrkd/googlemaps.py b/loctrkd/googlemaps.py new file mode 100644 index 0000000..418523d --- /dev/null +++ b/loctrkd/googlemaps.py @@ -0,0 +1,76 @@ +import googlemaps as gmaps +from typing import Any, Dict, List, Tuple + +gclient = None + + +def init(conf: Dict[str, Any]) -> None: + global gclient + with open(conf["googlemaps"]["accesstoken"], encoding="ascii") as fl: + token = fl.read().rstrip() + gclient = gmaps.Client(key=token) + + +def shut() -> None: + return + + +def lookup( + mcc: int, + mnc: int, + gsm_cells: List[Tuple[int, int, int]], + wifi_aps: List[Tuple[str, int]], +) -> Tuple[float, float]: + assert gclient is not None + 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 + from sqlite3 import connect + import sys + from .zx303proto import * + + db = connect(sys.argv[1]) + c = db.cursor() + c.execute( + """select tstamp, packet from events + where proto in (?, ?)""", + (proto_name(WIFI_POSITIONING), proto_name(WIFI_OFFLINE_POSITIONING)), + ) + 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