X-Git-Url: http://www.average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fevstore.py;h=a52a64c1e5ad468676349ad299e6fb14b174eceb;hb=311d3cc7b0692e66edb9b9bb9285b2bfc094d571;hp=76173624bed9069359f202f46d07e0eba3cc8491;hpb=5e1e7a4d37a1e149d5e899dada7b55a863cd8e64;p=loctrkd.git diff --git a/gps303/evstore.py b/gps303/evstore.py index 7617362..a52a64c 100644 --- a/gps303/evstore.py +++ b/gps303/evstore.py @@ -20,8 +20,10 @@ def initdb(dbname): global DB DB = connect(dbname) try: - DB.execute("""alter table events add column - is_incoming int not null default TRUE""") + DB.execute( + """alter table events add column + is_incoming int not null default TRUE""" + ) except OperationalError: DB.execute(SCHEMA) @@ -50,14 +52,22 @@ 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 = ? + cur.execute( + f"""select is_incoming, tstamp, packet from events + where ({selector}) and imei = ? order by tstamp desc limit ?""", - protos + (imei, backlog)) - result = [row[0] for row in cur] + tuple(item for sublist in matchlist for item in sublist) + + (imei, backlog), + ) + result = list(cur) cur.close() - return result + return list(reversed(result))