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, LocEvt, Resp
14 log = getLogger("gps303/lookaside")
19 zpub = zctx.socket(zmq.PUB)
20 oldmask = umask(0o117)
21 zpub.bind(conf.get("lookaside", "publishurl"))
23 zsub = zctx.socket(zmq.SUB)
24 zsub.connect(conf.get("collector", "publishurl"))
27 "GPS_OFFLINE_POSITIONING",
30 topic = pack("B", proto_by_name(protoname))
31 zsub.setsockopt(zmq.SUBSCRIBE, topic)
32 zpush = zctx.socket(zmq.PUSH)
33 zpush.connect(conf.get("collector", "listenurl"))
37 zmsg = Bcast(zsub.recv())
38 msg = parse_message(zmsg.packet)
40 "IMEI %s from %s at %s: %s",
43 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
46 if isinstance(msg, WIFI_POSITIONING):
49 conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
52 imei=zmsg.imei, packet=msg.Out(lat=lat, lon=lon).packed
54 log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
55 zpush.send(resp.packed)
70 except KeyboardInterrupt:
74 if __name__.endswith("__main__"):
75 runserver(common.init(log))