]> www.average.org Git - loctrkd.git/commitdiff
protocols: introduce method `rectified()`
authorEugene Crosser <crosser@average.org>
Tue, 26 Jul 2022 21:43:29 +0000 (23:43 +0200)
committerEugene Crosser <crosser@average.org>
Tue, 26 Jul 2022 21:43:29 +0000 (23:43 +0200)
loctrkd/beesure.py
loctrkd/googlemaps.py
loctrkd/opencellid.py
loctrkd/termconfig.py
loctrkd/watch.py
loctrkd/zx303proto.py
test/test_storage.py

index 7f689c56ce1f15bc46ccf50832dcc4e38e811146..93216c98ca39ce8ce570aff7cb7033ee6b556620 100755 (executable)
@@ -36,7 +36,6 @@ __all__ = (
     "proto_name",
     "DecodeError",
     "Respond",
-    "LK",
 )
 
 PROTO_PREFIX = "BS:"
@@ -352,6 +351,30 @@ class _LOC_DATA(BeeSurePkt):
         self.latitude = p.lat * p.nors
         self.longitude = p.lon * p.eorw
 
+    def rectified(self) -> Dict[str, Any]:  # JSON-able dict
+        if self.gps_valid:
+            return {
+                "type": "location",
+                "devtime": str(self.devtime),
+                "battery_percentage": self.battery_percentage,
+                "accuracy": self.positioning_accuracy,
+                "altitude": self.altitude,
+                "speed": self.speed,
+                "direction": self.direction,
+                "latitude": self.latitude,
+                "longitude": self.longitude,
+            }
+        else:
+            return {
+                "type": "approximate_location",
+                "devtime": str(self.devtime),
+                "battery_percentage": self.battery_percentage,
+                "mcc": self.mcc,
+                "mnc": self.mnc,
+                "base_stations": self.base_stations,
+                "wifi_aps": self.wifi_aps,
+            }
+
 
 class AL(_LOC_DATA):
     RESPOND = Respond.INL
@@ -595,6 +618,7 @@ def parse_message(packet: bytes, is_incoming: bool = True) -> BeeSurePkt:
 
 def exposed_protos() -> List[Tuple[str, bool]]:
     return [
-        (proto_name(UD), True),
-        (proto_name(UD2), False),
+        (proto_name(cls), False)
+        for cls in CLASSES.values()
+        if hasattr(cls, "rectified")
     ]
index 418523d45eb9581dce89d07664c48a5eeb775926..881a2bd0500f95e9b567c2436e77292b3e4c8c42 100644 (file)
@@ -52,6 +52,7 @@ if __name__.endswith("__main__"):
     from sqlite3 import connect
     import sys
     from .zx303proto import *
+    from .zx303proto import WIFI_POSITIONING, WIFI_OFFLINE_POSITIONING
 
     db = connect(sys.argv[1])
     c = db.cursor()
index 583d2e167b98af216b7a05360cf7d6666ca49c6d..4d0edcebed8c2fa2331774815a1aa3d4069df165 100644 (file)
@@ -58,6 +58,7 @@ if __name__.endswith("__main__"):
     from datetime import datetime, timezone
     import sys
     from .zx303proto import *
+    from .zx303proto import WIFI_POSITIONING, WIFI_OFFLINE_POSITIONING
 
     db = connect(sys.argv[1])
     c = db.cursor()
index f8f4b77e56538d0b15e5eb6969e8f2b9472658f0..b1ea80ae691bb1b93174c50b5d58d8150d9065fd 100644 (file)
@@ -8,6 +8,7 @@ import zmq
 
 from . import common
 from .zx303proto import *
+from .zx303proto import STATUS, SETUP, POSITION_UPLOAD_INTERVAL
 from .zmsg import Bcast, Resp, topic
 
 log = getLogger("loctrkd/termconfig")
index 8e962bc2278dcef0b8eda170116dae0be702df2a..6d3dcd9fa8ed7eb1f4eba1cdacd266549e9b2519 100644 (file)
@@ -37,6 +37,8 @@ def runserver(conf: ConfigParser) -> None:
                 if pmod.proto_handled(zmsg.proto):
                     msg = pmod.parse_message(zmsg.packet, zmsg.is_incoming)
                     print(msg)
+                    if zmsg.is_incoming and hasattr(msg, "rectified"):
+                        print(msg.rectified())
     except KeyboardInterrupt:
         pass
 
index 454214eb0c22d7f9e8713e7b367059f279886bb7..e27eb5f72c985456b925594a30066973b252bb64 100755 (executable)
@@ -45,44 +45,6 @@ __all__ = (
     "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:"
@@ -401,6 +363,16 @@ class _GPS_POSITIONING(GPS303Pkt):
         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,
+        }
+
 
 class GPS_POSITIONING(_GPS_POSITIONING):
     PROTO = 0x10
@@ -517,6 +489,16 @@ class _WIFI_POSITIONING(GPS303Pkt):
             ]
         )
 
+    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,
+        }
+
 
 class WIFI_OFFLINE_POSITIONING(_WIFI_POSITIONING):
     PROTO = 0x17
@@ -904,7 +886,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),
+        (proto_name(cls), cls.RESPOND is Respond.EXT)
+        for cls in CLASSES.values()
+        if hasattr(cls, "rectified")
     ]
index 88dea4f40f759d4fb7668512bb024dc10c96733c..57c1d70d6cf50c230d78ad82e623c202d29c6973 100644 (file)
@@ -8,6 +8,15 @@ from typing import Any
 import unittest
 from .common import send_and_drain, TestWithServers
 from loctrkd.zx303proto import *
+from loctrkd.zx303proto import (
+    STATUS,
+    WIFI_POSITIONING,
+    WIFI_OFFLINE_POSITIONING,
+    WIFI_POSITIONING,
+    LOGIN,
+    HIBERNATION,
+    SETUP,
+)
 from loctrkd.ocid_dload import SCHEMA