X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=loctrkd%2Fwatch.py;h=738cfe230be94dfb415a6c40b21045e8e1583960;hb=cffceea601459816660ab46d46be2f3e9370c2fe;hp=a8a53b0dd62f04938bef519e200796986a45a3a4;hpb=dbdf9d63af31770ad57302e16b17a2fdc526773f;p=loctrkd.git diff --git a/loctrkd/watch.py b/loctrkd/watch.py index a8a53b0..738cfe2 100644 --- a/loctrkd/watch.py +++ b/loctrkd/watch.py @@ -2,17 +2,36 @@ from configparser import ConfigParser from datetime import datetime, timezone +from importlib import import_module from logging import getLogger +from typing import Any, cast, List import zmq from . import common -from .zx303proto import parse_message from .zmsg import Bcast log = getLogger("loctrkd/watch") +class ProtoModule: + @staticmethod + def proto_handled(proto: str) -> bool: + ... + + @staticmethod + def parse_message(packet: bytes, is_incoming: bool = True) -> Any: + ... + + +pmods: List[ProtoModule] = [] + + def runserver(conf: ConfigParser) -> None: + global pmods + pmods = [ + cast(ProtoModule, import_module("." + modnm, __package__)) + for modnm in conf.get("collector", "protocols").split(",") + ] # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?! zctx = zmq.Context() # type: ignore zsub = zctx.socket(zmq.SUB) # type: ignore @@ -22,8 +41,11 @@ def runserver(conf: ConfigParser) -> None: try: while True: zmsg = Bcast(zsub.recv()) - msg = parse_message(zmsg.packet, zmsg.is_incoming) - print("I" if zmsg.is_incoming else "O", zmsg.imei, msg) + print("I" if zmsg.is_incoming else "O", zmsg.proto, zmsg.imei) + for pmod in pmods: + if pmod.proto_handled(zmsg.proto.startswith): + msg = pmod.parse_message(zmsg.packet, zmsg.is_incoming) + print(msg) except KeyboardInterrupt: pass