[storage]
dbfn = /var/lib/gps303/gps303.sqlite
+[lookaside]
+publishurl = ipc:///var/lib/gps303/locevt
+
[opencellid]
dbfn = /var/lib/opencellid/opencellid.sqlite
[storage]
dbfn = /home/crosser/src/gps303/gps303.sqlite
+[lookaside]
+publishurl = ipc:///tmp/locevt
+
[opencellid]
dbfn = /home/crosser/src/gps303/opencellid.sqlite
if self.dtime == b"\0\0\0\0\0\0":
self.devtime = None
else:
+ yr, mo, da, hr, mi, se = unpack("BBBBBB", self.dtime)
self.devtime = datetime(
- *unpack("BBBBBB", self.dtime), tzinfo=timezone.utc
+ 2000 + yr, mo, da, hr, mi, se, tzinfo=timezone.utc
)
self.gps_data_length = payload[6] >> 4
self.gps_nb_sat = payload[6] & 0x0F
from . import common
from .gps303proto import parse_message, proto_by_name, WIFI_POSITIONING
from .opencellid import qry_cell
-from .zmsg import Bcast, Resp
+from .zmsg import Bcast, LocEvt, Resp
log = getLogger("gps303/lookaside")
def runserver(conf):
zctx = zmq.Context()
+ zpub = zctx.socket(zmq.PUB)
+ zpub.bind(conf.get("lookaside", "publishurl"))
zsub = zctx.socket(zmq.SUB)
zsub.connect(conf.get("collector", "publishurl"))
- topic = pack("B", proto_by_name("WIFI_POSITIONING"))
- zsub.setsockopt(zmq.SUBSCRIBE, topic)
+ for protoname in (
+ "GPS_POSITIONING",
+ "GPS_OFFLINE_POSITIONING",
+ "WIFI_POSITIONING",
+ ):
+ topic = pack("B", proto_by_name(protoname))
+ zsub.setsockopt(zmq.SUBSCRIBE, topic)
zpush = zctx.socket(zmq.PUSH)
zpush.connect(conf.get("collector", "listenurl"))
datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
msg,
)
- if not isinstance(msg, WIFI_POSITIONING):
- log.error(
- "IMEI %s from %s at %s: %s",
- zmsg.imei,
- zmsg.peeraddr,
- datetime.fromtimestamp(zmsg.when).astimezone(
- tz=timezone.utc
- ),
- msg,
+ if isinstance(msg, WIFI_POSITIONING):
+ is_gps = False
+ lat, lon = qry_cell(
+ conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
)
- continue
- lat, lon = qry_cell(
- conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
+ resp = Resp(
+ imei=zmsg.imei, packet=msg.Out(lat=lat, lon=lon).packed
+ )
+ log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
+ zpush.send(resp.packed)
+ else:
+ is_gps = True
+ lat = msg.latitude
+ lon = msg.longitude
+ zpub.send(
+ LocEvt(
+ imei=zmsg.imei,
+ devtime=msg.devtime,
+ is_gps=is_gps,
+ lat=lat,
+ lon=lon,
+ ).packed
)
- resp = Resp(imei=zmsg.imei, packet=msg.Out(lat=lat, lon=lon).packed)
- log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
- zpush.send(resp.packed)
except KeyboardInterrupt:
pass
--- /dev/null
+""" Watch for locevt and print them """
+
+from datetime import datetime, timezone
+from logging import getLogger
+import zmq
+
+from . import common
+from .zmsg import LocEvt
+
+log = getLogger("gps303/watch")
+
+
+def runserver(conf):
+ zctx = zmq.Context()
+ zsub = zctx.socket(zmq.SUB)
+ zsub.connect(conf.get("lookaside", "publishurl"))
+ zsub.setsockopt(zmq.SUBSCRIBE, b"")
+
+ try:
+ while True:
+ zmsg = LocEvt(zsub.recv())
+ print(zmsg)
+ except KeyboardInterrupt:
+ pass
+
+
+if __name__.endswith("__main__"):
+ runserver(common.init(log))
""" Zeromq messages """
+from datetime import datetime, timezone
import ipaddress as ip
from struct import pack, unpack
-__all__ = "Bcast", "Resp"
+__all__ = "Bcast", "LocEvt", "Resp"
def pack_peer(peeraddr):
def decode(self, buffer):
self.imei = buffer[:16].decode()
self.packet = buffer[16:]
+
+
+class LocEvt(_Zmsg):
+ """Zmq message with original or approximated location from lookaside"""
+
+ KWARGS = (
+ ("imei", "0000000000000000"),
+ ("devtime", datetime(1970, 1, 1, tzinfo=timezone.utc)),
+ ("lat", 0.0),
+ ("lon", 0.0),
+ ("is_gps", True),
+ )
+
+ @property
+ def packed(self):
+ return self.imei.encode() + pack(
+ "!dddB",
+ self.devtime.replace(tzinfo=timezone.utc).timestamp(),
+ self.lat,
+ self.lon,
+ int(self.is_gps),
+ )
+
+ def decode(self, buffer):
+ self.imei = buffer[:16].decode()
+ when, self.lat, self.lon, is_gps = unpack("!dddB", buffer[16:])
+ self.devtime = datetime.fromtimestamp(when).astimezone(tz=timezone.utc)
+ self.is_gps = bool(is_gps)