]> www.average.org Git - loctrkd.git/commitdiff
cleanup of gps303proto
authorEugene Crosser <crosser@average.org>
Wed, 27 Apr 2022 20:40:23 +0000 (22:40 +0200)
committerEugene Crosser <crosser@average.org>
Wed, 27 Apr 2022 20:40:23 +0000 (22:40 +0200)
gps303/gps303proto.py
gps303/opencellid.py

index 31073d8ceacb6ff541d926fb78c72587ff91acf9..04f535057ae2d6b95d7a3f25c75de2309cd3a293 100755 (executable)
@@ -17,13 +17,11 @@ Forewarnings:
 from datetime import datetime, timezone
 from enum import Enum
 from inspect import isclass
-from logging import getLogger
 from struct import pack, unpack
 
 __all__ = (
     "class_by_prefix",
     "inline_response",
-    "make_object",
     "parse_message",
     "proto_by_name",
     "Respond",
@@ -64,10 +62,9 @@ __all__ = (
     "VIBRATION_RECEIVED",
     "POSITION_UPLOAD_INTERVAL",
     "SOS_ALARM",
+    "UNKNOWN_B3",
 )
 
-log = getLogger("gps303")
-
 
 def intx(x):
     if isinstance(x, str):
@@ -595,6 +592,17 @@ class SOS_ALARM(GPS303Pkt):
     PROTO = 0x99
 
 
+class UNKNOWN_B3(GPS303Pkt):
+    PROTO = 0xb3
+    IN_KWARGS = (("asciidata", str, ""),)
+
+    @classmethod
+    def from_packet(cls, length, payload):
+        self = super().from_packet(length, payload)
+        self.asciidata = payload.decode()
+        return self
+
+
 # Build dicts protocol number -> class and class name -> protocol number
 CLASSES = {}
 PROTOS = {}
@@ -640,29 +648,13 @@ def inline_response(packet):
     return None
 
 
-def make_object(length, proto, payload):
+def parse_message(packet):
+    """ From a packet (without framing bytes) derive the XXX.In object """
+    length, proto = unpack("BB", packet[:2])
+    payload = packet[2:]
     if proto in CLASSES:
         return CLASSES[proto].from_packet(length, payload)
     else:
         retobj = UNKNOWN.from_packet(length, payload)
         retobj.PROTO = proto  # Override class attr with object attr
         return retobj
-
-
-def parse_message(packet):
-    length, proto = unpack("BB", packet[:2])
-    payload = packet[2:]
-    adjust = 2 if proto == STATUS.PROTO else 4  # Weird special case
-    if (
-        proto not in (WIFI_POSITIONING.PROTO, WIFI_OFFLINE_POSITIONING.PROTO)
-        and length > 1
-        and len(payload) + adjust != length
-    ):
-        log.warning(
-            "With proto %d length is %d but payload length is %d+%d",
-            proto,
-            length,
-            len(payload),
-            adjust,
-        )
-    return make_object(length, proto, payload)
index 843e595e78003fd3b228154b4297d93045dddb76..436d559bd6ca4699e2fa97d041410d4318f9ae8d 100644 (file)
@@ -63,12 +63,12 @@ if __name__.endswith("__main__"):
     db = connect(sys.argv[1])
     c = db.cursor()
     c.execute(
-        """select timestamp, imei, clntaddr, length, proto, payload from events
+        """select select tstamp, packet from events
             where proto in (?, ?)""",
         (WIFI_POSITIONING.PROTO, WIFI_OFFLINE_POSITIONING.PROTO),
     )
-    for timestamp, imei, clntaddr, length, proto, payload in c:
-        obj = make_object(length, proto, payload)
+    for timestamp, imei, clntaddr, proto, payload in c:
+        obj = parse_message(packet)
         avlat, avlon = qry_cell(sys.argv[2], obj.mcc, obj.gsm_cells)
         print(
             "{} {:+#010.8g},{:+#010.8g}".format(