]> www.average.org Git - loctrkd.git/commitdiff
beesure: implement phone book commands
authorEugene Crosser <crosser@average.org>
Fri, 15 Jul 2022 11:01:05 +0000 (13:01 +0200)
committerEugene Crosser <crosser@average.org>
Fri, 15 Jul 2022 11:01:05 +0000 (13:01 +0200)
loctrkd/beesure.py

index 9346b55979eaf49540172a2b61810d1812084031..34108e12d25993c59d64d09b72c331d186cbf728 100755 (executable)
@@ -171,6 +171,21 @@ def l3str(x: Union[str, List[str]]) -> List[str]:
     return lx
 
 
+def pblist(x: Union[str, List[Tuple[str, str]]]) -> List[Tuple[str, str]]:
+    if isinstance(x, str):
+
+        def splitpair(s: str) -> Tuple[str, str]:
+            a, b = s.split(":")
+            return a, b
+
+        lx = [splitpair(el) for el in x.split(",")]
+    else:
+        lx = x
+    if len(lx) > 5:
+        raise ValueError(str(lx) + " has too many elements (max 5)")
+    return lx
+
+
 class MetaPkt(type):
     """
     For each class corresponding to a message, automatically create
@@ -439,6 +454,29 @@ class FLOWER(BeeSurePkt):
         return str(self.number)
 
 
+class _PHB(BeeSurePkt):
+    OUT_KWARGS: Tuple[Tuple[str, Callable[[Any], Any], Any], ...] = (
+        ("entries", pblist, []),
+    )
+
+    def out_encode(self) -> str:
+        self.entries: List[Tuple[str, str]]
+        return ",".join(
+            [
+                ",".join((num, name.encode("utf_16_be").hex()))
+                for name, num in self.entries
+            ]
+        )
+
+
+class PHB(_PHB):
+    pass
+
+
+class PHB2(_PHB):
+    pass
+
+
 class POWEROFF(BeeSurePkt):
     pass