+
+
+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
+ selector = " or ".join(
+ (f"(is_incoming = ? and proto = ?)" for _ in range(len(matchlist)))
+ )
+ cur = DB.cursor()
+ cur.execute(
+ f"""select is_incoming, tstamp, packet from events
+ where ({selector}) and imei = ?
+ order by tstamp desc limit ?""",
+ tuple(item for sublist in matchlist for item in sublist)
+ + (imei, backlog),
+ )
+ result = list(cur)
+ cur.close()
+ return list(reversed(result))