X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=blobdiff_plain;f=loctrkd%2Fzx303proto.py;h=a7329c316326187c950580ec11a65843981ace2d;hp=236f5daa16a1252f3203e660e1718370148465a5;hb=2cf0fd9d215dda17eae4261ab7967367f6aa0028;hpb=be1de0bb68c5c4acf2edd6f04c5ea8e40d9f31fb diff --git a/loctrkd/zx303proto.py b/loctrkd/zx303proto.py index 236f5da..a7329c3 100755 --- a/loctrkd/zx303proto.py +++ b/loctrkd/zx303proto.py @@ -32,6 +32,7 @@ from typing import ( Union, ) +from .common import CoordReport, HintReport, StatusReport from .protomodule import ProtoClass __all__ = ( @@ -43,12 +44,12 @@ __all__ = ( "proto_handled", "parse_message", "probe_buffer", - "proto_name", "DecodeError", "Respond", ) -PROTO_PREFIX = "ZX:" +MODNAME = __name__.split(".")[-1] +PROTO_PREFIX: str = "ZX:" ### Deframer ### @@ -292,6 +293,11 @@ class GPS303Pkt(ProtoClass): # Overridden in subclasses, otherwise make empty payload return b"" + @classmethod + def proto_name(cls) -> str: + """Name of the command as used externally""" + return (PROTO_PREFIX + cls.__name__)[:16] + @property def packed(self) -> bytes: payload = self.encode() @@ -364,10 +370,12 @@ class _GPS_POSITIONING(GPS303Pkt): ttup = (tup[0] % 100,) + tup[1:6] return pack("BBBBBB", *ttup) - def rectified(self) -> SimpleNamespace: # JSON-able dict - return SimpleNamespace( - type="location", + def rectified(self) -> Tuple[str, CoordReport]: # JSON-able dict + return MODNAME, CoordReport( devtime=str(self.devtime), + battery_percentage=None, + accuracy=None, + altitude=None, speed=self.speed, direction=self.heading, latitude=self.latitude, @@ -412,6 +420,9 @@ class STATUS(GPS303Pkt): def out_encode(self) -> bytes: # Set interval in minutes return pack("B", self.upload_interval) + def rectified(self) -> Tuple[str, StatusReport]: + return MODNAME, StatusReport(battery_percentage=self.batt) + class HIBERNATION(GPS303Pkt): # Server can send to send devicee to sleep PROTO = 0x14 @@ -490,14 +501,14 @@ class _WIFI_POSITIONING(GPS303Pkt): ] ) - def rectified(self) -> SimpleNamespace: # JSON-able dict - return SimpleNamespace( - type="approximate_location", + def rectified(self) -> Tuple[str, HintReport]: + return MODNAME, HintReport( devtime=str(self.devtime), + battery_percentage=None, mcc=self.mcc, mnc=self.mnc, - base_stations=self.gsm_cells, - wifi_aps=self.wifi_aps, + gsm_cells=self.gsm_cells, + wifi_aps=[("", mac, sig) for mac, sig in self.wifi_aps], ) @@ -820,14 +831,8 @@ def proto_handled(proto: str) -> bool: return proto.startswith(PROTO_PREFIX) -def proto_name(obj: Union[Type[GPS303Pkt], GPS303Pkt]) -> str: - return PROTO_PREFIX + ( - obj.__class__.__name__ if isinstance(obj, GPS303Pkt) else obj.__name__ - ) - - def proto_of_message(packet: bytes) -> str: - return proto_name(CLASSES.get(packet[1], UNKNOWN)) + return CLASSES.get(packet[1], UNKNOWN).proto_name() def imei_from_packet(packet: bytes) -> Optional[str]: @@ -887,7 +892,13 @@ def parse_message(packet: bytes, is_incoming: bool = True) -> GPS303Pkt: def exposed_protos() -> List[Tuple[str, bool]]: return [ - (proto_name(cls), cls.RESPOND is Respond.EXT) + (cls.proto_name(), cls.RESPOND is Respond.EXT) for cls in CLASSES.values() if hasattr(cls, "rectified") ] + + +def make_response(cmd: str, imei: str, **kwargs: Any) -> Optional[GPS303Pkt]: + if cmd == "poweroff": + return HIBERNATION.Out() + return None