]> www.average.org Git - loctrkd.git/blob - gps303/watch.py
typeckecing: annotate watch.py
[loctrkd.git] / gps303 / watch.py
1 """ Watch for locevt and print them """
2
3 from configparser import ConfigParser
4 from datetime import datetime, timezone
5 from logging import getLogger
6 import zmq
7
8 from . import common
9 from .gps303proto import parse_message
10 from .zmsg import Bcast
11
12 log = getLogger("gps303/watch")
13
14
15 def runserver(conf: ConfigParser) -> None:
16     # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
17     zctx = zmq.Context()  # type: ignore
18     zsub = zctx.socket(zmq.SUB)  # type: ignore
19     zsub.connect(conf.get("collector", "publishurl"))
20     zsub.setsockopt(zmq.SUBSCRIBE, b"")
21
22     try:
23         while True:
24             zmsg = Bcast(zsub.recv())
25             msg = parse_message(zmsg.packet, zmsg.is_incoming)
26             print("I" if zmsg.is_incoming else "O", zmsg.imei, msg)
27     except KeyboardInterrupt:
28         pass
29
30
31 if __name__.endswith("__main__"):
32     runserver(common.init(log))