from inspect import isclass
from struct import error, pack, unpack
from time import time
+from types import SimpleNamespace
from typing import (
Any,
Callable,
Union,
)
+from .common import CoordReport, HintReport, StatusReport
from .protomodule import ProtoClass
__all__ = (
"proto_handled",
"parse_message",
"probe_buffer",
- "proto_name",
"DecodeError",
"Respond",
)
-PROTO_PREFIX = "ZX:"
+PROTO_PREFIX: str = "ZX:"
### Deframer ###
# 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()
ttup = (tup[0] % 100,) + tup[1:6]
return pack("BBBBBB", *ttup)
- def rectified(self) -> Dict[str, Any]: # JSON-able dict
- return {
- "type": "location",
- "devtime": str(self.devtime),
- "speed": self.speed,
- "direction": self.heading,
- "latitude": self.latitude,
- "longitude": self.longitude,
- }
+ 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):
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
]
)
- def rectified(self) -> Dict[str, Any]: # JSON-able dict
- return {
- "type": "approximate_location",
- "devtime": str(self.devtime),
- "mcc": self.mcc,
- "mnc": self.mnc,
- "base_stations": self.gsm_cells,
- "wifi_aps": self.wifi_aps,
- }
+ 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=[("<UNKNOWN>", mac, sig) for mac, sig in self.wifi_aps],
+ )
class WIFI_OFFLINE_POSITIONING(_WIFI_POSITIONING):
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]:
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")
]