X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=loctrkd%2Fzx303proto.py;h=aff3405691baa596ca36ca90e00f7067880cef3f;hb=ca67cd29fc86054f08bcbf4995d484bab77a4e60;hp=454214eb0c22d7f9e8713e7b367059f279886bb7;hpb=cbb7603aebedf517d7b7c61ff6de02d3e7193409;p=loctrkd.git diff --git a/loctrkd/zx303proto.py b/loctrkd/zx303proto.py index 454214e..aff3405 100755 --- a/loctrkd/zx303proto.py +++ b/loctrkd/zx303proto.py @@ -19,6 +19,7 @@ from enum import Enum from inspect import isclass from struct import error, pack, unpack from time import time +from types import SimpleNamespace from typing import ( Any, Callable, @@ -31,6 +32,7 @@ from typing import ( Union, ) +from .common import CoordReport, HintReport, StatusReport from .protomodule import ProtoClass __all__ = ( @@ -42,50 +44,11 @@ __all__ = ( "proto_handled", "parse_message", "probe_buffer", - "proto_name", "DecodeError", "Respond", - "GPS303Pkt", - "UNKNOWN", - "LOGIN", - "SUPERVISION", - "HEARTBEAT", - "GPS_POSITIONING", - "GPS_OFFLINE_POSITIONING", - "STATUS", - "HIBERNATION", - "RESET", - "WHITELIST_TOTAL", - "WIFI_OFFLINE_POSITIONING", - "TIME", - "PROHIBIT_LBS", - "GPS_LBS_SWITCH_TIMES", - "REMOTE_MONITOR_PHONE", - "SOS_PHONE", - "DAD_PHONE", - "MOM_PHONE", - "STOP_UPLOAD", - "GPS_OFF_PERIOD", - "DND_PERIOD", - "RESTART_SHUTDOWN", - "DEVICE", - "ALARM_CLOCK", - "STOP_ALARM", - "SETUP", - "SYNCHRONOUS_WHITELIST", - "RESTORE_PASSWORD", - "WIFI_POSITIONING", - "MANUAL_POSITIONING", - "BATTERY_CHARGE", - "CHARGER_CONNECTED", - "CHARGER_DISCONNECTED", - "VIBRATION_RECEIVED", - "POSITION_UPLOAD_INTERVAL", - "SOS_ALARM", - "UNKNOWN_B3", ) -PROTO_PREFIX = "ZX:" +PROTO_PREFIX: str = "ZX:" ### Deframer ### @@ -329,6 +292,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() @@ -401,6 +369,18 @@ class _GPS_POSITIONING(GPS303Pkt): ttup = (tup[0] % 100,) + tup[1:6] return pack("BBBBBB", *ttup) + def rectified(self) -> CoordReport: # JSON-able dict + return CoordReport( + devtime=str(self.devtime), + battery_percentage=None, + accuracy=None, + altitude=None, + speed=self.speed, + direction=self.heading, + latitude=self.latitude, + longitude=self.longitude, + ) + class GPS_POSITIONING(_GPS_POSITIONING): PROTO = 0x10 @@ -439,6 +419,9 @@ class STATUS(GPS303Pkt): def out_encode(self) -> bytes: # Set interval in minutes return pack("B", self.upload_interval) + def rectified(self) -> StatusReport: + return StatusReport(battery_percentage=self.batt) + class HIBERNATION(GPS303Pkt): # Server can send to send devicee to sleep PROTO = 0x14 @@ -517,6 +500,16 @@ class _WIFI_POSITIONING(GPS303Pkt): ] ) + def rectified(self) -> HintReport: + return HintReport( + devtime=str(self.devtime), + battery_percentage=None, + mcc=self.mcc, + mnc=self.mnc, + gsm_cells=self.gsm_cells, + wifi_aps=[("", mac, sig) for mac, sig in self.wifi_aps], + ) + class WIFI_OFFLINE_POSITIONING(_WIFI_POSITIONING): PROTO = 0x17 @@ -837,14 +830,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]: @@ -904,7 +891,7 @@ def parse_message(packet: bytes, is_incoming: bool = True) -> GPS303Pkt: def exposed_protos() -> List[Tuple[str, bool]]: return [ - (proto_name(GPS_POSITIONING), True), - (proto_name(WIFI_POSITIONING), False), - (proto_name(STATUS), True), + (cls.proto_name(), cls.RESPOND is Respond.EXT) + for cls in CLASSES.values() + if hasattr(cls, "rectified") ]