1 """ Estimate coordinates from WIFI_POSITIONING and send back """
3 from datetime import datetime, timezone
4 from logging import getLogger
6 from struct import pack
10 from .gps303proto import parse_message, proto_by_name, WIFI_POSITIONING
11 from .opencellid import qry_cell
12 from .zmsg import Bcast, Resp
14 log = getLogger("gps303/lookaside")
19 zsub = zctx.socket(zmq.SUB)
20 zsub.connect(conf.get("collector", "publishurl"))
21 topic = pack("B", proto_by_name("WIFI_POSITIONING"))
22 zsub.setsockopt(zmq.SUBSCRIBE, topic)
23 zpush = zctx.socket(zmq.PUSH)
24 zpush.connect(conf.get("collector", "listenurl"))
28 zmsg = Bcast(zsub.recv())
29 msg = parse_message(zmsg.packet)
31 "IMEI %s from %s at %s: %s",
34 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
37 if not isinstance(msg, WIFI_POSITIONING):
39 "IMEI %s from %s at %s: %s",
42 datetime.fromtimestamp(zmsg.when).astimezone(
49 conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
51 resp = Resp(imei=zmsg.imei, packet=msg.Out(lat=lat, lon=lon).packed)
52 log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
53 zpush.send(resp.packed)
55 except KeyboardInterrupt:
59 if __name__.endswith("__main__"):
60 runserver(common.init(log))