]> www.average.org Git - loctrkd.git/commitdiff
Don't make unneeded responses, better debug log
authorEugene Crosser <crosser@average.org>
Sat, 23 Apr 2022 13:26:48 +0000 (15:26 +0200)
committerEugene Crosser <crosser@average.org>
Sat, 23 Apr 2022 13:26:48 +0000 (15:26 +0200)
gps303/collector.py
gps303/gps303proto.py
gps303/termconfig.py

index 921833c083be0ac8aecda838a338a4f72b5f818c..8bcca303a627e75b9220fa89d0cdf5f81f761357 100644 (file)
@@ -100,6 +100,7 @@ class Clients:
 
     def add(self, clntsock, clntaddr):
         fd = clntsock.fileno()
+        log.info("Start serving fd %d from %s", fd, clntaddr)
         self.by_fd[fd] = Client(clntsock, clntaddr)
         return fd
 
@@ -121,6 +122,12 @@ class Clients:
             if proto_of_message(packet) == LOGIN.PROTO:  # Could do blindly...
                 self.by_imei[clnt.imei] = clnt
             result.append((clnt.imei, when, peeraddr, packet))
+            log.debug(
+                "Received from %s (IMEI %s): %s",
+                peeraddr,
+                clnt.imei,
+                packet.hex(),
+            )
         return result
 
     def response(self, resp):
index 27733a3fa48f246212d7d86e33a0106348ec651c..dcc335b184fc6fa59249ce2c692f37c54d1587d5 100755 (executable)
@@ -28,6 +28,7 @@ __all__ = (
     "make_response",
     "parse_message",
     "proto_by_name",
+    "Dir",
     "GPS303Pkt",
     "UNKNOWN",
     "LOGIN",
@@ -67,7 +68,7 @@ class Dir(Enum):
 
 class GPS303Pkt:
     PROTO: int
-    DIR = Dir.INLINE  # Most packets anticipate simple acknowledgement
+    DIR = Dir.IN  # Do not send anything back by default
 
     def __init__(self, *args, **kwargs):
         assert len(args) == 0
@@ -114,11 +115,11 @@ class GPS303Pkt:
 
 class UNKNOWN(GPS303Pkt):
     PROTO = 256  # > 255 is impossible in real packets
-    DIR = Dir.IN
 
 
 class LOGIN(GPS303Pkt):
     PROTO = 0x01
+    DIR = Dir.INLINE
     # Default response for ACK, can also respond with STOP_UPLOAD
 
     @classmethod
@@ -143,9 +144,12 @@ class SUPERVISION(GPS303Pkt):
 
 class HEARTBEAT(GPS303Pkt):
     PROTO = 0x08
+    DIR = Dir.INLINE
 
 
 class _GPS_POSITIONING(GPS303Pkt):
+    DIR = Dir.INLINE
+
     @classmethod
     def from_packet(cls, length, payload):
         self = super().from_packet(length, payload)
@@ -213,7 +217,7 @@ class STATUS(GPS303Pkt):
 
 class HIBERNATION(GPS303Pkt):
     PROTO = 0x14
-    DIR = Dir.EXT
+    DIR = Dir.INLINE
 
     @classmethod
     def response(cls):  # Server can send to send devicee to sleep
@@ -222,7 +226,6 @@ class HIBERNATION(GPS303Pkt):
 
 class RESET(GPS303Pkt):  # Device sends when it got reset SMS
     PROTO = 0x15
-    DIR = Dir.EXT
 
     @classmethod
     def response(cls):  # Server can send to initiate factory reset
@@ -269,6 +272,7 @@ class _WIFI_POSITIONING(GPS303Pkt):
 
 class WIFI_OFFLINE_POSITIONING(_WIFI_POSITIONING):
     PROTO = 0x17
+    DIR = Dir.INLINE
 
     @classmethod
     def inline_response(cls, packet):
@@ -279,6 +283,7 @@ class WIFI_OFFLINE_POSITIONING(_WIFI_POSITIONING):
 
 class TIME(GPS303Pkt):
     PROTO = 0x30
+    DIR = Dir.INLINE
 
     @classmethod
     def inline_response(cls, packet):
@@ -416,6 +421,7 @@ class STOP_ALARM(GPS303Pkt):
     def from_packet(cls, length, payload):
         self = super().from_packet(length, payload)
         self.flag = payload[0]
+        return self
 
 
 class SETUP(GPS303Pkt):
@@ -483,12 +489,12 @@ class WIFI_POSITIONING(_WIFI_POSITIONING):
 
 class MANUAL_POSITIONING(GPS303Pkt):
     PROTO = 0x80
-    DIR = Dir.EXT
+    DIR = Dir.OUT
 
     @classmethod
     def from_packet(cls, length, payload):
         self = super().from_packet(length, payload)
-        self.flag = payload[0]
+        self.flag = payload[0] if len(payload) > 0 else None
         self.reason = {
             1: "Incorrect time",
             2: "LBS less",
@@ -498,6 +504,7 @@ class MANUAL_POSITIONING(GPS303Pkt):
             6: "LBS prohibited, WiFi absent",
             7: "GPS spacing < 50 m",
         }.get(self.flag, "Unknown")
+        return self
 
     @classmethod
     def response(cls):
@@ -556,8 +563,11 @@ if True:  # just to indent the code, sorry!
 
 
 def class_by_prefix(prefix):
-    lst = [(name, proto) for name, proto in PROTOS.items()
-            if name.upper().startswith(prefix.upper())]
+    lst = [
+        (name, proto)
+        for name, proto in PROTOS.items()
+        if name.upper().startswith(prefix.upper())
+    ]
     if len(lst) != 1:
         return lst
     _, proto = lst[0]
index 1ab65ce937004f8bfeb5c2c99feef2934b190d76..fe1f2cd78f2410696ee53fe7ff8a11ef1f063dbc 100644 (file)
@@ -18,11 +18,7 @@ def runserver(conf):
     zsub = zctx.socket(zmq.SUB)
     zsub.connect(conf.get("collector", "publishurl"))
     for protoname in (
-        "SUPERVISION",
         "STATUS",
-        "RESET",
-        "WHITELIST_TOTAL",
-        "PROHIBIT_LBS",
         "SETUP",
         "POSITION_UPLOAD_INTERVAL",
     ):
@@ -42,6 +38,10 @@ def runserver(conf):
                 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
                 msg,
             )
+            if msg.DIR is not Dir.EXT:
+                log.error(
+                    "%s does not expect externally provided response", msg
+                )
             kwargs = {}
             if isinstance(msg, STATUS):
                 kwargs = {