""" 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
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:
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
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)
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)
+ 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: