]> www.average.org Git - loctrkd.git/blobdiff - loctrkd/beesure.py
Implement sending commands from the web interface
[loctrkd.git] / loctrkd / beesure.py
index f3f58b65c566701a8287a68d91e30c0c7911722f..e782c736189ca5d74a3f0cc188c7973672660c13 100755 (executable)
@@ -20,21 +20,30 @@ from typing import (
     TYPE_CHECKING,
     Union,
 )
+from types import SimpleNamespace
+
+from .protomodule import ProtoClass
+from .common import (
+    CoordReport,
+    HintReport,
+    StatusReport,
+    Report,
+)
 
 __all__ = (
     "Stream",
     "class_by_prefix",
     "enframe",
+    "exposed_protos",
     "inline_response",
+    "proto_handled",
     "parse_message",
     "probe_buffer",
-    "proto_by_name",
-    "proto_name",
     "DecodeError",
     "Respond",
-    "LK",
 )
 
+MODNAME = __name__.split(".")[-1]
 PROTO_PREFIX = "BS:"
 
 ### Deframer ###
@@ -108,7 +117,7 @@ class Stream:
             else:
                 msgs.append(
                     f"Packet does not end with ']'"
-                    f" at {self.datalen+20}: {self.buffer=!r}"
+                    f" at {self.datalen+20}: {self.buffer[:64]=!r}"
                 )
             self.buffer = self.buffer[self.datalen + 21 :]
             self.datalen = 0
@@ -132,6 +141,14 @@ def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes:
 ### Parser/Constructor ###
 
 
+class classproperty:
+    def __init__(self, f: Callable[[Any], str]) -> None:
+        self.f = f
+
+    def __get__(self, obj: Any, owner: Any) -> str:
+        return self.f(owner)
+
+
 class DecodeError(Exception):
     def __init__(self, e: Exception, **kwargs: Any) -> None:
         super().__init__(e)
@@ -159,54 +176,29 @@ def boolx(x: Union[str, bool]) -> bool:
     return x
 
 
-class MetaPkt(type):
-    """
-    For each class corresponding to a message, automatically create
-    two nested classes `In` and `Out` that also inherit from their
-    "nest". Class attribute `IN_KWARGS` defined in the "nest" is
-    copied to the `In` nested class under the name `KWARGS`, and
-    likewise, `OUT_KWARGS` of the nest class is copied as `KWARGS`
-    to the nested class `Out`. In addition, method `encode` is
-    defined in both classes equal to `in_encode()` and `out_encode()`
-    respectively.
-    """
+def l3str(x: Union[str, List[str]]) -> List[str]:
+    if isinstance(x, str):
+        lx = x.split(",")
+    else:
+        lx = x
+    if len(lx) != 3 or not all(isinstance(el, str) for el in x):
+        raise ValueError(str(lx) + " is not a list of three strings")
+    return lx
 
-    if TYPE_CHECKING:
 
-        def __getattr__(self, name: str) -> Any:
-            pass
+def pblist(x: Union[str, List[Tuple[str, str]]]) -> List[Tuple[str, str]]:
+    if isinstance(x, str):
 
-        def __setattr__(self, name: str, value: Any) -> None:
-            pass
+        def splitpair(s: str) -> Tuple[str, str]:
+            a, b = s.split(":")
+            return a, b
 
-    def __new__(
-        cls: Type["MetaPkt"],
-        name: str,
-        bases: Tuple[type, ...],
-        attrs: Dict[str, Any],
-    ) -> "MetaPkt":
-        newcls = super().__new__(cls, name, bases, attrs)
-        newcls.In = super().__new__(
-            cls,
-            name + ".In",
-            (newcls,) + bases,
-            {
-                "KWARGS": newcls.IN_KWARGS,
-                "decode": newcls.in_decode,
-                "encode": newcls.in_encode,
-            },
-        )
-        newcls.Out = super().__new__(
-            cls,
-            name + ".Out",
-            (newcls,) + bases,
-            {
-                "KWARGS": newcls.OUT_KWARGS,
-                "decode": newcls.out_decode,
-                "encode": newcls.out_encode,
-            },
-        )
-        return newcls
+        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 Respond(Enum):
@@ -215,9 +207,9 @@ class Respond(Enum):
     EXT = 2  # Birirectional, use external responder
 
 
-class BeeSurePkt(metaclass=MetaPkt):
+class BeeSurePkt(ProtoClass):
+    BINARY = False
     RESPOND = Respond.NON  # Do not send anything back by default
-    PROTO: str
     IN_KWARGS: Tuple[Tuple[str, Callable[[Any], Any], Any], ...] = ()
     OUT_KWARGS: Tuple[Tuple[str, Callable[[Any], Any], Any], ...] = ()
     KWARGS: Tuple[Tuple[str, Callable[[Any], Any], Any], ...] = ()
@@ -237,11 +229,15 @@ class BeeSurePkt(metaclass=MetaPkt):
         Construct the object _either_ from (length, payload),
         _or_ from the values of individual fields
         """
+        self.payload: Union[List[str], bytes]
         assert not args or (len(args) == 4 and not kwargs)
         if args:  # guaranteed to be two arguments at this point
             self.vendor, self.imei, self.datalength, self.payload = args
             try:
-                self.decode(*self.payload)
+                if isinstance(self.payload, list):
+                    self.decode(*self.payload)
+                else:
+                    self.decode(self.payload)
             except error as e:
                 raise DecodeError(e, obj=self)
         else:
@@ -267,7 +263,7 @@ class BeeSurePkt(metaclass=MetaPkt):
             ),
         )
 
-    def decode(self, *args: str) -> None:
+    def decode(self, *args: Any) -> None:
         ...
 
     def in_decode(self, *args: str) -> None:
@@ -289,98 +285,293 @@ class BeeSurePkt(metaclass=MetaPkt):
 
     def out_encode(self) -> str:
         # Overridden in subclasses, otherwise command verb only
-        return self.PROTO
+        return ""
+
+    @classproperty
+    def PROTO(cls: "BeeSurePkt") -> str:
+        """Name of the class without possible .In / .Out suffix"""
+        proto: str
+        try:
+            proto, _ = cls.__name__.split(".")
+        except ValueError:
+            proto = cls.__name__
+        return proto
+
+    @classmethod
+    def proto_name(cls) -> str:
+        """Name of the command as used externally"""
+        return PROTO_PREFIX + cls.PROTO[:16]
 
     @property
     def packed(self) -> bytes:
-        buffer = self.encode().encode()
-        return f"[LT*0000000000*{len(buffer):04X}*".encode() + buffer + b"]"
+        data = self.encode()
+        payload = self.PROTO + "," + data if data else self.PROTO
+        return f"[LT*0000000000*{len(payload):04X}*{payload}]".encode()
 
 
 class UNKNOWN(BeeSurePkt):
-    PROTO = "UNKNOWN"
+    pass
+
+
+class _SET_PHONE(BeeSurePkt):
+    OUT_KWARGS = (("phonenumber", str, ""),)
+
+    def out_encode(self) -> str:
+        self.phonenumber: str
+        return self.phonenumber
+
+
+class _LOC_DATA(BeeSurePkt):
+    def in_decode(self, *args: str) -> None:
+        p = SimpleNamespace()
+        _id = lambda x: x
+        for (obj, attr, func), val in zip(
+            (
+                (p, "date", _id),
+                (p, "time", _id),
+                (self, "gps_valid", lambda x: x == "A"),
+                (p, "lat", float),
+                (p, "nors", lambda x: 1 if x == "N" else -1),
+                (p, "lon", float),
+                (p, "eorw", lambda x: 1 if x == "E" else -1),
+                (self, "speed", float),
+                (self, "direction", float),
+                (self, "altitude", float),
+                (self, "num_of_sats", int),
+                (self, "gsm_strength_percentage", int),
+                (self, "battery_percentage", int),
+                (self, "pedometer", int),
+                (self, "tubmling_times", int),
+                (self, "device_status", lambda x: int(x, 16)),
+                (self, "gsm_cells_number", int),
+                (self, "connect_base_station_number", int),
+                (self, "mcc", int),
+                (self, "mnc", int),
+            ),
+            args[:20],
+        ):
+            setattr(obj, attr, func(val))  # type: ignore
+        rest_args = args[20:]
+        # (area_id, cell_id, strength)*
+        self.gsm_cells: List[Tuple[int, int, int]] = [
+            tuple(int(el) for el in rest_args[i * 3 : 3 + i * 3])  # type: ignore
+            for i in range(self.gsm_cells_number)
+        ]
+        rest_args = rest_args[3 * self.gsm_cells_number :]
+        self.wifi_aps_number = int(rest_args[0])
+        # (SSID, MAC, strength)*
+        self.wifi_aps = [
+            (
+                rest_args[1 + i * 3],
+                rest_args[2 + i * 3],
+                int(rest_args[3 + i * 3]),
+            )
+            for i in range(self.wifi_aps_number)
+        ]
+        rest_args = rest_args[1 + 3 * self.wifi_aps_number :]
+        self.positioning_accuracy = float(rest_args[0])
+        self.devtime = (
+            datetime.strptime(
+                p.date + p.time,
+                "%d%m%y%H%M%S",
+            )
+            # .replace(tzinfo=timezone.utc)
+            # .astimezone(tz=timezone.utc)
+        )
+        self.latitude = p.lat * p.nors
+        self.longitude = p.lon * p.eorw
+
+    def rectified(self) -> Tuple[str, Report]:
+        # self.gps_valid is supposed to mean it, but it does not. Perfectly
+        # good looking coordinates, with ten satellites, still get 'V'.
+        # I suspect that in reality, 'A' means "hint data is absent".
+        if self.gps_valid or self.num_of_sats > 3:
+            return MODNAME, CoordReport(
+                devtime=str(self.devtime),
+                battery_percentage=self.battery_percentage,
+                accuracy=self.positioning_accuracy,
+                altitude=self.altitude,
+                speed=self.speed,
+                direction=self.direction,
+                latitude=self.latitude,
+                longitude=self.longitude,
+            )
+        else:
+            return MODNAME, HintReport(
+                devtime=str(self.devtime),
+                battery_percentage=self.battery_percentage,
+                mcc=self.mcc,
+                mnc=self.mnc,
+                gsm_cells=self.gsm_cells,
+                wifi_aps=self.wifi_aps,
+            )
+
+
+class AL(_LOC_DATA):
+    RESPOND = Respond.INL
+
+
+class CALL(_SET_PHONE):
+    pass
+
+
+class CENTER(_SET_PHONE):
+    pass
+
+
+class CONFIG(BeeSurePkt):
+    pass
+
+
+class CR(BeeSurePkt):
+    pass
+
+
+class FIND(BeeSurePkt):
+    pass
+
+
+class FLOWER(BeeSurePkt):
+    OUT_KWARGS = (("number", int, 1),)
+
+    def out_encode(self) -> str:
+        self.number: int
+        return str(self.number)
+
+
+class ICCID(BeeSurePkt):
+    pass
 
 
 class LK(BeeSurePkt):
-    PROTO = "LK"
     RESPOND = Respond.INL
 
     def in_decode(self, *args: str) -> None:
         numargs = len(args)
+        if numargs > 0:
+            self.step = args[0]
         if numargs > 1:
-            self.step = args[1]
+            self.tumbling_number = args[1]
         if numargs > 2:
-            self.tumbling_number = args[2]
-        if numargs > 3:
-            self.battery_percentage = args[3]
+            self.battery_percentage = args[2]
 
     def in_encode(self) -> str:
         return "LK"
 
 
-class CONFIG(BeeSurePkt):
-    PROTO = "CONFIG"
+class LZ(BeeSurePkt):
+    OUT_KWARGS = (("language", int, 1), ("timezone", int, 0))
 
+    def out_encode(self) -> str:
+        return f"{self.language},{self.timezone}"
 
-class ICCID(BeeSurePkt):
-    PROTO = "ICCID"
 
+class MESSAGE(BeeSurePkt):
+    OUT_KWARGS = (("message", str, ""),)
+
+    def out_encode(self) -> str:
+        return str(self.message.encode("utf_16_be").hex())
 
-class UD(BeeSurePkt):
-    PROTO = "UD"
 
-    def in_decode(self, *args: str) -> None:
-        (
-            _,
-            self.date,
-            self.time,
-            self.gps_valid,
-            self.lat,
-            self.nors,
-            self.lon,
-            self.eorw,
-            self.speed,
-            self.direction,
-            self.altitude,
-            self.num_of_sats,
-            self.gsm_strength_percentage,
-            self.battery_percentage,
-            self.pedometer,
-            self.tubmling_times,
-            self.device_status,
-        ) = args[:17]
-        rest_args = args[17:]
-        self.base_stations_number = int(rest_args[0])
-        self.base_stations = rest_args[1 : 4 + 3 * self.base_stations_number]
-        rest_args = rest_args[3 + 3 * self.base_stations_number + 1 :]
-        self.wifi_ap_number = int(rest_args[0])
-        self.wifi_ap = rest_args[1 : self.wifi_ap_number]
-        # rest_args = rest_args[self_wifi_ap_number+1:]
-        self.positioning_accuracy = rest_args[-1]
-
-
-class UD2(BeeSurePkt):
-    PROTO = "UD2"
+class MONITOR(BeeSurePkt):
+    pass
+
+
+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
+
+
+class RESET(BeeSurePkt):
+    pass
+
+
+class SOS(BeeSurePkt):
+    OUT_KWARGS = (("phonenumbers", l3str, ["", "", ""]),)
+
+    def out_encode(self) -> str:
+        self.phonenumbers: List[str]
+        return ",".join(self.phonenumbers)
+
+
+class SOS1(_SET_PHONE):
+    pass
+
+
+class SOS2(_SET_PHONE):
+    pass
+
+
+class SOS3(_SET_PHONE):
+    pass
+
+
+class TK(BeeSurePkt):
+    BINARY = True
+    RESPOND = Respond.INL
+
+    def in_decode(self, *args: Any) -> None:
+        assert len(args) == 1 and isinstance(args[0], bytes)
+        self.amr_data = (
+            args[0]
+            .replace(b"}*", b"*")
+            .replace(b"},", b",")
+            .replace(b"}[", b"[")
+            .replace(b"}]", b"]")
+            .replace(b"}}", b"}")
+        )
+
+    def out_encode(self) -> str:
+        return "1"  # 0 - receive failure, 1 - receive success
 
 
 class TKQ(BeeSurePkt):
-    PROTO = "TKQ"
     RESPOND = Respond.INL
 
 
 class TKQ2(BeeSurePkt):
-    PROTO = "TKQ2"
     RESPOND = Respond.INL
 
 
-class AL(BeeSurePkt):
-    PROTO = "AL"
-    RESPOND = Respond.INL
+class UD(_LOC_DATA):
+    pass
+
+
+class UD2(_LOC_DATA):
+    pass
+
+
+class UPLOAD(BeeSurePkt):
+    OUT_KWARGS = (("interval", int, 600),)
+
+    def out_encode(self) -> str:
+        return str(self.interval)
 
 
 # Build dicts protocol number -> class and class name -> protocol number
 CLASSES = {}
-PROTOS = {}
 if True:  # just to indent the code, sorry!
     for cls in [
         cls
@@ -389,37 +580,38 @@ if True:  # just to indent the code, sorry!
         and issubclass(cls, BeeSurePkt)
         and not name.startswith("_")
     ]:
-        if hasattr(cls, "PROTO"):
-            CLASSES[cls.PROTO] = cls
-            PROTOS[cls.__name__] = cls.PROTO
+        CLASSES[cls.__name__] = cls
 
 
 def class_by_prefix(
     prefix: str,
-) -> Union[Type[BeeSurePkt], List[Tuple[str, str]]]:
-    lst = [
-        (name, proto)
-        for name, proto in PROTOS.items()
-        if name.upper().startswith(prefix.upper())
-    ]
-    if len(lst) != 1:
-        return lst
-    _, proto = lst[0]
-    return CLASSES[proto]
+) -> Union[Type[BeeSurePkt], List[str]]:
+    if prefix.startswith(PROTO_PREFIX):
+        pname = prefix[len(PROTO_PREFIX) :].upper()
+    else:
+        raise KeyError(pname)
+    lst = [name for name in CLASSES.keys() if name.upper().startswith(pname)]
+    for proto in lst:
+        if len(lst) == 1:  # unique prefix match
+            return CLASSES[proto]
+        if proto == pname:  # exact match
+            return CLASSES[proto]
+    return lst
 
 
-def proto_name(obj: Union[MetaPkt, BeeSurePkt]) -> str:
-    return PROTO_PREFIX + (
-        obj.__class__.__name__ if isinstance(obj, BeeSurePkt) else obj.__name__
-    )
+def proto_handled(proto: str) -> bool:
+    return proto.startswith(PROTO_PREFIX)
 
 
-def proto_by_name(name: str) -> str:
-    return PROTO_PREFIX + PROTOS.get(name, "UNKNOWN")
+def _local_proto(packet: bytes) -> str:
+    try:
+        return packet[20:-1].split(b",")[0].decode()
+    except UnicodeDecodeError:
+        return "UNKNOWN"
 
 
 def proto_of_message(packet: bytes) -> str:
-    return PROTO_PREFIX + packet[20:-1].split(b",")[0].decode()
+    return PROTO_PREFIX + _local_proto(packet)
 
 
 def imei_from_packet(packet: bytes) -> Optional[str]:
@@ -434,7 +626,7 @@ def is_goodbye_packet(packet: bytes) -> bool:
 
 
 def inline_response(packet: bytes) -> Optional[bytes]:
-    proto = packet[20:-1].split(b",")[0].decode()
+    proto = _local_proto(packet)
     if proto in CLASSES:
         cls = CLASSES[proto]
         if cls.RESPOND is Respond.INL:
@@ -449,24 +641,52 @@ def probe_buffer(buffer: bytes) -> bool:
 def parse_message(packet: bytes, is_incoming: bool = True) -> BeeSurePkt:
     """From a packet (without framing bytes) derive the XXX.In object"""
     toskip, vendor, imei, datalength = _framestart(packet)
-    payload = packet[20:-1].decode().split(",")
-    proto = payload[0] if len(payload) > 0 else ""
-    if proto not in CLASSES:
-        cause: Union[DecodeError, ValueError, IndexError] = ValueError(
-            f"Proto {proto} is unknown"
-        )
+    bsplits = packet[20:-1].split(b",", 1)
+    try:
+        proto = bsplits[0].decode("ascii")
+    except UnicodeDecodeError:
+        proto = str(bsplits[0])
+    if len(bsplits) == 2:
+        rest = bsplits[1]
     else:
+        rest = b""
+    if proto in CLASSES:
+        cls = CLASSES[proto].In if is_incoming else CLASSES[proto].Out
+        payload = (
+            # Some people encode their SSIDs in non-utf8
+            rest
+            if cls.BINARY
+            else rest.decode("Windows-1252").split(",")
+        )
         try:
-            if is_incoming:
-                return CLASSES[proto].In(vendor, imei, datalength, payload)
-            else:
-                return CLASSES[proto].Out(vendor, imei, datalength, payload)
+            return cls(vendor, imei, datalength, payload)
         except (DecodeError, ValueError, IndexError) as e:
-            cause = e
+            cause: Union[DecodeError, ValueError, IndexError] = e
+    else:
+        payload = rest
+        cause = ValueError(f"Proto {proto} is unknown")
     if is_incoming:
         retobj = UNKNOWN.In(vendor, imei, datalength, payload)
     else:
         retobj = UNKNOWN.Out(vendor, imei, datalength, payload)
-    retobj.PROTO = proto  # Override class attr with object attr
+    retobj.proto = proto  # Override class attr with object attr
     retobj.cause = cause
     return retobj
+
+
+def exposed_protos() -> List[Tuple[str, bool]]:
+    return [
+        (cls.proto_name(), False)
+        for cls in CLASSES.values()
+        if hasattr(cls, "rectified")
+    ]
+
+
+def make_response(cmd: str, imei: str, **kwargs: Any) -> Optional[BeeSurePkt]:
+    if cmd == "poweroff":
+        return POWEROFF.Out()
+    elif cmd == "refresh":
+        return MONITOR.Out()
+    elif cmd == "message":
+        return MESSAGE.Out(message=kwargs.get("txt", "Hello"))
+    return None