1 import googlemaps as gmaps
2 from typing import Any, Dict, List, Tuple
7 def init(conf: Dict[str, Any]) -> None:
9 with open(conf["googlemaps"]["accesstoken"], encoding="ascii") as fl:
10 token = fl.read().rstrip()
11 gclient = gmaps.Client(key=token)
21 gsm_cells: List[Tuple[int, int, int]],
22 wifi_aps: List[Tuple[str, int]],
23 ) -> Tuple[float, float]:
24 assert gclient is not None
26 "home_mobile_country_code": mcc,
27 "home_mobile_network_code": mnc,
33 "locationAreaCode": loc,
35 "signalStrength": sig,
37 for loc, cellid, sig in gsm_cells
39 "wifi_access_points": [
40 {"macAddress": mac, "signalStrength": sig} for mac, sig in wifi_aps
43 result = gclient.geolocate(**kwargs)
44 if "location" in result:
45 return result["location"]["lat"], result["location"]["lng"]
47 raise ValueError("google geolocation: " + str(result))
50 if __name__.endswith("__main__"):
51 from datetime import datetime, timezone
52 from sqlite3 import connect
54 from .gps303proto import *
56 db = connect(sys.argv[1])
59 """select tstamp, packet from events
60 where proto in (?, ?)""",
61 (WIFI_POSITIONING.PROTO, WIFI_OFFLINE_POSITIONING.PROTO),
63 init({"googlemaps": {"accesstoken": sys.argv[2]}})
65 for timestamp, packet in c:
66 obj = parse_message(packet)
68 avlat, avlon = lookup(obj.mcc, obj.mnc, obj.gsm_cells, obj.wifi_aps)
70 "{} {:+#010.8g},{:+#010.8g}".format(
71 datetime.fromtimestamp(timestamp), avlat, avlon