]> www.average.org Git - loctrkd.git/commitdiff
wsgateway with new message distribution model
authorEugene Crosser <crosser@average.org>
Mon, 9 May 2022 16:57:04 +0000 (18:57 +0200)
committerEugene Crosser <crosser@average.org>
Mon, 9 May 2022 16:57:04 +0000 (18:57 +0200)
gps303/wsgateway.py
webdemo/index.html

index 00770ebd88032c35f9b9e7172dba3c63233d7434..2f6811e45225e6b478a9dcf8796ec4c63b9e68d0 100644 (file)
@@ -1,6 +1,7 @@
 """ Websocket Gateway """
 
-from json import loads
+from datetime import datetime, timezone
+from json import dumps, loads
 from logging import getLogger
 from socket import socket, AF_INET6, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
 from time import time
@@ -171,8 +172,8 @@ class Client:
         return True  # TODO: check subscriptions
 
     def send(self, message):
-        if self.ready and message.imei in self.imeis:
-            self.ws_data += self.ws.send(Message(data=message.json))
+        if self.ready and message["imei"] in self.imeis:
+            self.ws_data += self.ws.send(Message(data=dumps(message)))
 
     def write(self):
         if self.ws_data:
@@ -212,7 +213,7 @@ class Clients:
     def send(self, msg):
         towrite = set()
         for fd, clnt in self.by_fd.items():
-            if clnt.wants(msg.imei):
+            if clnt.wants(msg["imei"]):
                 clnt.send(msg)
                 towrite.add(fd)
         return towrite
@@ -255,7 +256,6 @@ def runserver(conf):
         while True:
             neededsubs = clients.subs()
             for imei in neededsubs - activesubs:
-                log.debug("topics: %s", [tpc.hex() for tpc in [topic(GPS_POSITIONING.PROTO, True, imei), topic(WIFI_POSITIONING.PROTO, False, imei)]])
                 zsub.setsockopt(
                     zmq.SUBSCRIBE,
                     topic(GPS_POSITIONING.PROTO, True, imei),
@@ -284,13 +284,31 @@ def runserver(conf):
                 if sk is zsub:
                     while True:
                         try:
-                            buf = zsub.recv(zmq.NOBLOCK)
-                            zmsg = Bcast(buf)
-                            log.debug("zmq packet: %s", buf.hex())
-                            # zmsg = Bcast(zsub.recv(zmq.NOBLOCK))
-                            msg = parse_message(zmsg.packet)
-                            tosend.append(zmsg)
-                            log.debug("Got %s", zmsg)
+                            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,
+                                    }
+                                )
                         except zmq.Again:
                             break
                 elif sk == tcpfd:
index e6e1795cd76048f2440db33b2471f996222a025d..d94ab92cbcd826b20c18ef9fa37167ecede2d12a 100644 (file)
@@ -91,7 +91,7 @@
                        locations.shift();
                }
                console.log("new marker list " + JSON.stringify(locations));
-               tstamp.innerHTML = msg.devtime;
+               tstamp.innerHTML = msg.timestamp;
                var lonLat;
                // TODO: figure how to have multiple markers and/or track
                for (const loc of locations) {