X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fevstore.py;h=a52a64c1e5ad468676349ad299e6fb14b174eceb;hb=e84104c8d7e93efc4ab2f543e7dfef4cc0208187;hp=bb47f3fb21c9c34c178955dbfad7e84e09828dbe;hpb=8bf5efdf4f5d069b108e6fd8140f5ebd930b538e;p=loctrkd.git diff --git a/gps303/evstore.py b/gps303/evstore.py index bb47f3f..a52a64c 100644 --- a/gps303/evstore.py +++ b/gps303/evstore.py @@ -53,16 +53,21 @@ def stow(**kwargs): DB.commit() -def fetch(imei, protos, backlog): +def fetch(imei, matchlist, backlog): + # matchlist is a list of tuples (is_incoming, proto) + # returns a list of tuples (is_incoming, timestamp, packet) assert DB is not None - protosel = ", ".join(["?" for _ in range(len(protos))]) + selector = " or ".join( + (f"(is_incoming = ? and proto = ?)" for _ in range(len(matchlist))) + ) cur = DB.cursor() cur.execute( - f"""select packet from events - where proto in ({protosel}) and imei = ? + f"""select is_incoming, tstamp, packet from events + where ({selector}) and imei = ? order by tstamp desc limit ?""", - protos + (imei, backlog), + tuple(item for sublist in matchlist for item in sublist) + + (imei, backlog), ) - result = [row[0] for row in cur] + result = list(cur) cur.close() - return result + return list(reversed(result))