]> www.average.org Git - loctrkd.git/commitdiff
reimplement backlog query again
authorEugene Crosser <crosser@average.org>
Mon, 9 May 2022 21:16:38 +0000 (23:16 +0200)
committerEugene Crosser <crosser@average.org>
Mon, 9 May 2022 21:19:25 +0000 (23:19 +0200)
gps303/backlog.py [deleted file]
gps303/evstore.py
gps303/wsgateway.py
webdemo/index.html

diff --git a/gps303/backlog.py b/gps303/backlog.py
deleted file mode 100644 (file)
index 1322286..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-""" Get backlog from evstore """
-
-from .opencellid import qry_cell
-from .evstore import initdb, fetch
-from .gps303proto import GPS_POSITIONING, WIFI_POSITIONING, parse_message
-
-
-def blinit(evdb):
-    initdb(evdb)
-
-
-def backlog(imei, backlog):
-    result = []
-    for packet in fetch(
-        imei, (GPS_POSITIONING.PROTO, WIFI_POSITIONING.PROTO), backlog
-    ):
-        msg = parse_message(packet)
-    return reversed(result)
index bb47f3fb21c9c34c178955dbfad7e84e09828dbe..a52a64c1e5ad468676349ad299e6fb14b174eceb 100644 (file)
@@ -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))
index d66070e2f57a7781fe76a8249ac176536e3192d4..79937d8e5b1f5d1835e1dfab8a627f2844701f31 100644 (file)
@@ -18,7 +18,7 @@ from wsproto.utilities import RemoteProtocolError
 import zmq
 
 from . import common
-from .backlog import blinit, backlog
+from .evstore import initdb, fetch
 from .gps303proto import (
     GPS_POSITIONING,
     WIFI_POSITIONING,
@@ -30,6 +30,29 @@ log = getLogger("gps303/wsgateway")
 htmlfile = None
 
 
+def backlog(imei, numback):
+    result = []
+    for is_incoming, timestamp, packet in fetch(
+        imei,
+        ((True, GPS_POSITIONING.PROTO), (False, WIFI_POSITIONING.PROTO)),
+        numback,
+    ):
+        msg = parse_message(packet, is_incoming=is_incoming)
+        result.append(
+            {
+                "imei": imei,
+                "timestamp": str(
+                    datetime.fromtimestamp(timestamp).astimezone(
+                        tz=timezone.utc
+                    )
+                ),
+                "longitude": msg.longitude,
+                "latitude": msg.latitude,
+            }
+        )
+    return result
+
+
 def try_http(data, fd, e):
     global htmlfile
     try:
@@ -235,7 +258,7 @@ class Clients:
 def runserver(conf):
     global htmlfile
 
-    blinit(conf.get("storage", "dbfn"))
+    initdb(conf.get("storage", "dbfn"))
     htmlfile = conf.get("wsgateway", "htmlfile")
     zctx = zmq.Context()
     zsub = zctx.socket(zmq.SUB)
@@ -314,10 +337,10 @@ def runserver(conf):
                         for msg in received:
                             log.debug("Received from %d: %s", sk, msg)
                             if msg.get("type", None) == "subscribe":
-                                imei = msg.get("imei")
+                                imeis = msg.get("imei")
                                 numback = msg.get("backlog", 5)
-                                for elem in imei:
-                                    tosend.extend(backlog(elem, numback))
+                                for imei in imeis:
+                                    tosend.extend(backlog(imei, numback))
                         towrite.add(sk)
                 elif fl & zmq.POLLOUT:
                     log.debug("Write now open for fd %d", sk)
index d94ab92cbcd826b20c18ef9fa37167ecede2d12a..c3fafb4cff3e55f804d5a3abfd45f12f31caea29 100644 (file)
                        imei: Array.from(imeis),
                        type: "subscribe",
                        date: Date.now(),
-                       backlog: 1
+                       backlog: maxmarkers
                };
                console.log("sending" + JSON.stringify(msg));
                ws.send(JSON.stringify(msg));