]> average.org Git - loctrkd.git/blobdiff - loctrkd/zx303proto.py
collector: close old connection on new login
[loctrkd.git] / loctrkd / zx303proto.py
index ebead92e0e28c88f64517547ed8ac1828fa5d301..8c21418cd7a13c9af9f03c57d9d345f8c5592a50 100755 (executable)
@@ -35,9 +35,9 @@ __all__ = (
     "Stream",
     "class_by_prefix",
     "inline_response",
+    "proto_handled",
     "parse_message",
     "probe_buffer",
-    "proto_by_name",
     "proto_name",
     "DecodeError",
     "Respond",
@@ -92,10 +92,6 @@ class Stream:
     def __init__(self) -> None:
         self.buffer = b""
 
-    @staticmethod
-    def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes:
-        return b"xx" + buffer + b"\r\n"
-
     def recv(self, segment: bytes) -> List[Union[bytes, str]]:
         """
         Process next segment of the stream. Return successfully deframed
@@ -150,6 +146,10 @@ class Stream:
         return ret
 
 
+def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes:
+    return b"xx" + buffer + b"\r\n"
+
+
 ### Parser/Constructor ###
 
 
@@ -864,6 +864,10 @@ if True:  # just to indent the code, sorry!
 def class_by_prefix(
     prefix: str,
 ) -> Union[Type[GPS303Pkt], List[Tuple[str, int]]]:
+    if prefix.startswith(PROTO_PREFIX):
+        pname = prefix[len(PROTO_PREFIX) :]
+    else:
+        raise KeyError(pname)
     lst = [
         (name, proto)
         for name, proto in PROTOS.items()
@@ -875,16 +879,16 @@ 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__
     )
 
 
-def proto_by_name(name: str) -> int:
-    return PROTOS.get(name, -1)
-
-
 def proto_of_message(packet: bytes) -> str:
     return proto_name(CLASSES.get(packet[1], UNKNOWN))