--- /dev/null
+""" Get backlog from evstore """
+
+from .opencellid import qry_cell
+from .evstore import initdb, fetch
+from .gps303proto import GPS_POSITIONING, WIFI_POSITIONING, parse_message
+from .zmsg import LocEvt
+
+OCDB = None
+
+def blinit(evdb, ocdb):
+ global OCDB
+ OCDB = ocdb
+ initdb(evdb)
+
+def backlog(imei, backlog):
+ result = []
+ for packet in fetch(imei, (GPS_POSITIONING.PROTO, WIFI_POSITIONING.PROTO), backlog):
+ msg = parse_message(packet)
+ if isinstance(msg, GPS_POSITIONING):
+ result.append(LocEvt(devtime=msg.devtime, lon=msg.longitude,
+ lat=msg.latitude, is_gps=True, imei=imei))
+ elif isinstance(msg, WIFI_POSITIONING):
+ lat, lon = qry_cell(OCDB, msg.mcc, msg.gsm_cells)
+ result.append(LocEvt(devtime=msg.devtime, lon=lon,
+ lat=lat, is_gps=False, imei=imei))
+ return reversed(result)
from sqlite3 import connect
-__all__ = "initdb", "stow"
+__all__ = "fetch", "initdb", "stow"
DB = None
parms,
)
DB.commit()
+
+def fetch(imei, protos, backlog):
+ assert DB is not None
+ protosel = ", ".join(["?" for _ in range(len(protos))])
+ cur = DB.cursor()
+ cur.execute(f"""select packet from events
+ where proto in ({protosel}) and imei = ?
+ order by tstamp desc limit ?""",
+ protos + (imei, backlog))
+ result = [row[0] for row in cur]
+ cur.close()
+ return result
import zmq
from . import common
+from .backlog import blinit, backlog
from .zmsg import LocEvt
log = getLogger("gps303/wsgateway")
def recv(self, fd):
clnt = self.by_fd[fd]
- msgs = clnt.recv()
- if msgs is None:
- return None
- result = []
- for msg in msgs:
- log.debug("Received: %s", msg)
- result.append(msg)
- return result
+ return clnt.recv()
def send(self, msg):
towrite = set()
def runserver(conf):
global htmlfile
+ blinit(conf.get("storage", "dbfn"), conf.get("opencellid", "dbfn"))
htmlfile = conf.get("wsgateway", "htmlfile")
zctx = zmq.Context()
zsub = zctx.socket(zmq.SUB)
else:
for msg in received:
log.debug("Received from %d: %s", sk, msg)
+ if msg.get("type", None) == "subscribe":
+ imei = msg.get("imei")
+ if imei:
+ tosend.extend(backlog(imei[0], 5))
towrite.add(sk)
elif fl & zmq.POLLOUT:
log.debug("Write now open for fd %d", sk)