From ba4cb894d37f24ac333b316cf9487dfc913eaf74 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Mon, 11 Jul 2022 23:13:24 +0200 Subject: [PATCH] function `proto_handled()` in proto modules --- loctrkd/beesure.py | 5 +++++ loctrkd/watch.py | 6 ++++-- loctrkd/zx303proto.py | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/loctrkd/beesure.py b/loctrkd/beesure.py index f3f58b6..8fa83f5 100755 --- a/loctrkd/beesure.py +++ b/loctrkd/beesure.py @@ -26,6 +26,7 @@ __all__ = ( "class_by_prefix", "enframe", "inline_response", + "proto_handled", "parse_message", "probe_buffer", "proto_by_name", @@ -408,6 +409,10 @@ def class_by_prefix( return CLASSES[proto] +def proto_handled(proto: str) -> bool: + return proto.startswith(PROTO_PREFIX) + + def proto_name(obj: Union[MetaPkt, BeeSurePkt]) -> str: return PROTO_PREFIX + ( obj.__class__.__name__ if isinstance(obj, BeeSurePkt) else obj.__name__ diff --git a/loctrkd/watch.py b/loctrkd/watch.py index 7221d2b..738cfe2 100644 --- a/loctrkd/watch.py +++ b/loctrkd/watch.py @@ -14,7 +14,9 @@ log = getLogger("loctrkd/watch") class ProtoModule: - PROTO_PREFIX: str + @staticmethod + def proto_handled(proto: str) -> bool: + ... @staticmethod def parse_message(packet: bytes, is_incoming: bool = True) -> Any: @@ -41,7 +43,7 @@ def runserver(conf: ConfigParser) -> None: 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): + if pmod.proto_handled(zmsg.proto.startswith): msg = pmod.parse_message(zmsg.packet, zmsg.is_incoming) print(msg) except KeyboardInterrupt: diff --git a/loctrkd/zx303proto.py b/loctrkd/zx303proto.py index c47c216..2c44a13 100755 --- a/loctrkd/zx303proto.py +++ b/loctrkd/zx303proto.py @@ -35,6 +35,7 @@ __all__ = ( "Stream", "class_by_prefix", "inline_response", + "proto_handled", "parse_message", "probe_buffer", "proto_by_name", @@ -875,6 +876,10 @@ def class_by_prefix( return CLASSES[proto] +def proto_handled(proto: str) -> bool: + return proto.startswith(PROTO_PREFIX) + + def proto_name(obj: Union[MetaPkt, GPS303Pkt]) -> str: return PROTO_PREFIX + ( obj.__class__.__name__ if isinstance(obj, GPS303Pkt) else obj.__name__ -- 2.43.0