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