+from .zmsg import Bcast, Resp, topic
+
+log = getLogger("gps303/lookaside")
+
+
+def runserver(conf):
+ zctx = zmq.Context()
+ zsub = zctx.socket(zmq.SUB)
+ zsub.connect(conf.get("collector", "publishurl"))
+ zsub.setsockopt(zmq.SUBSCRIBE, topic(WIFI_POSITIONING.PROTO))
+ zpush = zctx.socket(zmq.PUSH)
+ zpush.connect(conf.get("collector", "listenurl"))
+
+ try:
+ while True:
+ zmsg = Bcast(zsub.recv())
+ msg = parse_message(zmsg.packet)
+ log.debug(
+ "IMEI %s from %s at %s: %s",
+ zmsg.imei,
+ zmsg.peeraddr,
+ datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
+ msg,
+ )
+ lat, lon = qry_cell(
+ conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
+ )
+ resp = Resp(
+ imei=zmsg.imei,
+ when=zmsg.when, # not the current time, but the original!
+ packet=msg.Out(latitude=lat, longitude=lon).packed,
+ )
+ log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
+ zpush.send(resp.packed)
+
+ except KeyboardInterrupt:
+ pass
+