]> www.average.org Git - loctrkd.git/blobdiff - gps303/wsgateway.py
reimplement backlog query again
[loctrkd.git] / gps303 / wsgateway.py
index 2f6811e45225e6b478a9dcf8796ec4c63b9e68d0..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)
@@ -287,28 +310,18 @@ def runserver(conf):
                             zmsg = Bcast(zsub.recv(zmq.NOBLOCK))
                             msg = parse_message(zmsg.packet, zmsg.is_incoming)
                             log.debug("Got %s with %s", zmsg, msg)
-                            if isinstance(msg, GPS_POSITIONING):
-                                tosend.append(
-                                    {
-                                        "imei": zmsg.imei,
-                                        "timestamp": str(msg.devtime),
-                                        "longitude": msg.longitude,
-                                        "latitude": msg.latitude,
-                                    }
-                                )
-                            elif isinstance(msg, WIFI_POSITIONING):
-                                tosend.append(
-                                    {
-                                        "imei": zmsg.imei,
-                                        "timestamp": str(
-                                            datetime.fromtimestamp(
-                                                zmsg.when
-                                            ).astimezone(tz=timezone.utc)
-                                        ),
-                                        "longitude": msg.longitude,
-                                        "latitude": msg.latitude,
-                                    }
-                                )
+                            tosend.append(
+                                {
+                                    "imei": zmsg.imei,
+                                    "timestamp": str(
+                                        datetime.fromtimestamp(
+                                            zmsg.when
+                                        ).astimezone(tz=timezone.utc)
+                                    ),
+                                    "longitude": msg.longitude,
+                                    "latitude": msg.latitude,
+                                }
+                            )
                         except zmq.Again:
                             break
                 elif sk == tcpfd:
@@ -324,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)