X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fwsgateway.py;h=79937d8e5b1f5d1835e1dfab8a627f2844701f31;hb=311d3cc7b0692e66edb9b9bb9285b2bfc094d571;hp=2f6811e45225e6b478a9dcf8796ec4c63b9e68d0;hpb=fc32607043c038d5d0f358b7fadadacd4a707ce0;p=loctrkd.git diff --git a/gps303/wsgateway.py b/gps303/wsgateway.py index 2f6811e..79937d8 100644 --- a/gps303/wsgateway.py +++ b/gps303/wsgateway.py @@ -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)