1 """ Estimate coordinates from WIFI_POSITIONING and send back """
3 from datetime import datetime, timezone
4 from logging import getLogger
5 from struct import pack
9 from .gps303proto import parse_message, proto_by_name, WIFI_POSITIONING
10 from .opencellid import qry_cell
11 from .zmsg import Bcast, Resp
13 log = getLogger("gps303/lookaside")
18 zsub = zctx.socket(zmq.SUB)
19 zsub.connect(conf.get("collector", "publishurl"))
20 topic = pack("B", proto_by_name("WIFI_POSITIONING"))
21 zsub.setsockopt(zmq.SUBSCRIBE, topic)
22 zpush = zctx.socket(zmq.PUSH)
23 zpush.connect(conf.get("collector", "listenurl"))
27 zmsg = Bcast(zsub.recv())
28 msg = parse_message(zmsg.packet)
30 "IMEI %s from %s at %s: %s",
33 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
36 if not isinstance(msg, WIFI_POSITIONING):
38 "IMEI %s from %s at %s: %s",
41 datetime.fromtimestamp(zmsg.when).astimezone(
48 conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
50 resp = Resp(imei=zmsg.imei, packet=msg.Out(lat=lat, lon=lon).packed)
51 log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
52 zpush.send(resp.packed)
54 except KeyboardInterrupt:
58 if __name__.endswith("__main__"):
59 runserver(common.init(log))