]> www.average.org Git - loctrkd.git/blobdiff - gps303/wsgateway.py
wsgateway with new message distribution model
[loctrkd.git] / gps303 / wsgateway.py
index 80926fbceb53b381d067eb2560d82cbeb2aa9aad..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
@@ -257,20 +258,20 @@ def runserver(conf):
             for imei in neededsubs - activesubs:
                 zsub.setsockopt(
                     zmq.SUBSCRIBE,
-                    topic(GPS_POSITIONING.PROTO, True),
+                    topic(GPS_POSITIONING.PROTO, True, imei),
                 )
                 zsub.setsockopt(
                     zmq.SUBSCRIBE,
-                    topic(WIFI_POSITIONING.PROTO, False),
+                    topic(WIFI_POSITIONING.PROTO, False, imei),
                 )
             for imei in activesubs - neededsubs:
                 zsub.setsockopt(
                     zmq.UNSUBSCRIBE,
-                    topic(GPS_POSITIONING.PROTO, True),
+                    topic(GPS_POSITIONING.PROTO, True, imei),
                 )
                 zsub.setsockopt(
                     zmq.UNSUBSCRIBE,
-                    topic(WIFI_POSITIONING.PROTO, False),
+                    topic(WIFI_POSITIONING.PROTO, False, imei),
                 )
             activesubs = neededsubs
             log.debug("Subscribed to: %s", activesubs)
@@ -284,9 +285,30 @@ def runserver(conf):
                     while True:
                         try:
                             zmsg = Bcast(zsub.recv(zmq.NOBLOCK))
-                            msg = parse_message(zmsg.packet)
-                            tosend.append(zmsg)
-                            log.debug("Got %s", zmsg)
+                            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: