""" Store zmq broadcasts to sqlite """
-from getopt import getopt
+from configparser import ConfigParser
+from datetime import datetime, timezone
from logging import getLogger
-from logging.handlers import SysLogHandler
-import sys
-from time import time
import zmq
from . import common
from .evstore import initdb, stow
-from .gps303proto import parse_message
+from .gps303proto import proto_of_message
+from .zmsg import Bcast
log = getLogger("gps303/storage")
-def runserver(conf):
+
+def runserver(conf: ConfigParser) -> None:
dbname = conf.get("storage", "dbfn")
log.info('Using Sqlite3 database "%s"', dbname)
initdb(dbname)
- zctx = zmq.Context()
- zsub = zctx.socket(zmq.SUB)
+ # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
+ zctx = zmq.Context() # type: ignore
+ zsub = zctx.socket(zmq.SUB) # type: ignore
zsub.connect(conf.get("collector", "publishurl"))
zsub.setsockopt(zmq.SUBSCRIBE, b"")
try:
while True:
- zmsg = zsub.recv()
- imei = zmsg[1:17].decode()
- packet = zmsg[17:]
- msg = parse_message(packet)
- log.debug("From IMEI %s: %s", imei, msg)
- stow("", time(), imei, msg.length, msg.PROTO, msg.payload)
+ zmsg = Bcast(zsub.recv())
+ log.debug(
+ "%s IMEI %s from %s at %s: %s",
+ "I" if zmsg.is_incoming else "O",
+ zmsg.imei,
+ zmsg.peeraddr,
+ datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
+ zmsg.packet.hex(),
+ )
+ stow(
+ is_incoming=zmsg.is_incoming,
+ peeraddr=str(zmsg.peeraddr),
+ when=zmsg.when,
+ imei=zmsg.imei,
+ proto=proto_of_message(zmsg.packet),
+ packet=zmsg.packet,
+ )
except KeyboardInterrupt:
pass