+++ /dev/null
-""" 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)
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))
import zmq
from . import common
-from .backlog import blinit, backlog
+from .evstore import initdb, fetch
from .gps303proto import (
GPS_POSITIONING,
WIFI_POSITIONING,
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:
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)
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)
imei: Array.from(imeis),
type: "subscribe",
date: Date.now(),
- backlog: 1
+ backlog: maxmarkers
};
console.log("sending" + JSON.stringify(msg));
ws.send(JSON.stringify(msg));