1 """ Estimate coordinates from WIFI_POSITIONING and send back """
3 from datetime import datetime, timezone
4 from importlib import import_module
5 from logging import getLogger
7 from struct import pack
11 from .gps303proto import parse_message, WIFI_POSITIONING
12 from .zmsg import Bcast, Resp, topic
14 log = getLogger("gps303/lookaside")
18 if conf.get("lookaside", "backend") == "opencellid":
19 qry = import_module(".opencellid", __package__)
21 raise NotImplementedError(
22 "Lookaside only implements opencellid backend"
26 zsub = zctx.socket(zmq.SUB)
27 zsub.connect(conf.get("collector", "publishurl"))
28 zsub.setsockopt(zmq.SUBSCRIBE, topic(WIFI_POSITIONING.PROTO))
29 zpush = zctx.socket(zmq.PUSH)
30 zpush.connect(conf.get("collector", "listenurl"))
34 zmsg = Bcast(zsub.recv())
35 msg = parse_message(zmsg.packet)
37 "IMEI %s from %s at %s: %s",
40 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
43 lat, lon = qry.lookup(msg.mcc, msg.gsm_cells, msg.wifi_aps)
46 when=zmsg.when, # not the current time, but the original!
47 packet=msg.Out(latitude=lat, longitude=lon).packed,
49 log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
50 zpush.send(resp.packed)
52 except KeyboardInterrupt:
56 if __name__.endswith("__main__"):
57 runserver(common.init(log))