]> www.average.org Git - loctrkd.git/blobdiff - loctrkd/watch.py
abstract protocol selection in `common`
[loctrkd.git] / loctrkd / watch.py
index 7221d2b716595fb05623f1f66a8d476aaeee9d3c..bda952c259da36299fd5f8859b3c33363983720b 100644 (file)
@@ -8,28 +8,13 @@ from typing import Any, cast, List
 import zmq
 
 from . import common
+from .protomodule import ProtoModule
 from .zmsg import Bcast
 
 log = getLogger("loctrkd/watch")
 
 
-class ProtoModule:
-    PROTO_PREFIX: str
-
-    @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
@@ -40,10 +25,12 @@ def runserver(conf: ConfigParser) -> None:
         while True:
             zmsg = Bcast(zsub.recv())
             print("I" if zmsg.is_incoming else "O", zmsg.proto, zmsg.imei)
-            for pmod in pmods:
-                if zmsg.proto.startswith(pmod.PROTO_PREFIX):
-                    msg = pmod.parse_message(zmsg.packet, zmsg.is_incoming)
-                    print(msg)
+            pmod = common.pmod_for_proto(zmsg.proto)
+            if pmod is not None:
+                msg = pmod.parse_message(zmsg.packet, zmsg.is_incoming)
+                print(msg)
+                if zmsg.is_incoming and hasattr(msg, "rectified"):
+                    print(msg.rectified())
     except KeyboardInterrupt:
         pass