]> www.average.org Git - loctrkd.git/commitdiff
Broadcast location, gps and approximated
authorEugene Crosser <crosser@average.org>
Fri, 29 Apr 2022 22:18:00 +0000 (00:18 +0200)
committerEugene Crosser <crosser@average.org>
Fri, 29 Apr 2022 22:18:00 +0000 (00:18 +0200)
debian/gps303.conf
gps303.conf
gps303/gps303proto.py
gps303/lookaside.py
gps303/watch.py [new file with mode: 0644]
gps303/zmsg.py

index 10889ebe7a7944ffa7f349718568666c5a8a289b..2ee044bb7afee117665787dfa2de42986364ced7 100644 (file)
@@ -6,6 +6,9 @@ listenurl = ipc:///var/lib/gps303/responses
 [storage]
 dbfn = /var/lib/gps303/gps303.sqlite
 
+[lookaside]
+publishurl = ipc:///var/lib/gps303/locevt
+
 [opencellid]
 dbfn = /var/lib/opencellid/opencellid.sqlite
 
index 76693816bff2b0b7b447df9643bb6a1a3cd0a104..59ab59ba99ebd4a6e181950648343f781270ed99 100644 (file)
@@ -6,6 +6,9 @@ listenurl = ipc:///tmp/responses
 [storage]
 dbfn = /home/crosser/src/gps303/gps303.sqlite
 
+[lookaside]
+publishurl = ipc:///tmp/locevt
+
 [opencellid]
 dbfn = /home/crosser/src/gps303/opencellid.sqlite
 
index c19c7c1b300c78fe2ea511c1a7859f0c35c434e4..d07d4faa81edae0854d9cdea5eccfcc3b9e0fcbb 100755 (executable)
@@ -245,8 +245,9 @@ class _GPS_POSITIONING(GPS303Pkt):
         if self.dtime == b"\0\0\0\0\0\0":
             self.devtime = None
         else:
+            yr, mo, da, hr, mi, se = unpack("BBBBBB", self.dtime)
             self.devtime = datetime(
-                *unpack("BBBBBB", self.dtime), tzinfo=timezone.utc
+                2000 + yr, mo, da, hr, mi, se, tzinfo=timezone.utc
             )
         self.gps_data_length = payload[6] >> 4
         self.gps_nb_sat = payload[6] & 0x0F
index 93103a64b4d4eb1da61df72414c08f4b35c5bc33..085cbe9a31b65c65057d6fa150158560658ef4f4 100644 (file)
@@ -8,17 +8,24 @@ import zmq
 from . import common
 from .gps303proto import parse_message, proto_by_name, WIFI_POSITIONING
 from .opencellid import qry_cell
-from .zmsg import Bcast, Resp
+from .zmsg import Bcast, LocEvt, Resp
 
 log = getLogger("gps303/lookaside")
 
 
 def runserver(conf):
     zctx = zmq.Context()
+    zpub = zctx.socket(zmq.PUB)
+    zpub.bind(conf.get("lookaside", "publishurl"))
     zsub = zctx.socket(zmq.SUB)
     zsub.connect(conf.get("collector", "publishurl"))
-    topic = pack("B", proto_by_name("WIFI_POSITIONING"))
-    zsub.setsockopt(zmq.SUBSCRIBE, topic)
+    for protoname in (
+        "GPS_POSITIONING",
+        "GPS_OFFLINE_POSITIONING",
+        "WIFI_POSITIONING",
+    ):
+        topic = pack("B", proto_by_name(protoname))
+        zsub.setsockopt(zmq.SUBSCRIBE, topic)
     zpush = zctx.socket(zmq.PUSH)
     zpush.connect(conf.get("collector", "listenurl"))
 
@@ -33,23 +40,29 @@ def runserver(conf):
                 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
                 msg,
             )
-            if not isinstance(msg, WIFI_POSITIONING):
-                log.error(
-                    "IMEI %s from %s at %s: %s",
-                    zmsg.imei,
-                    zmsg.peeraddr,
-                    datetime.fromtimestamp(zmsg.when).astimezone(
-                        tz=timezone.utc
-                    ),
-                    msg,
+            if isinstance(msg, WIFI_POSITIONING):
+                is_gps = False
+                lat, lon = qry_cell(
+                    conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
                 )
-                continue
-            lat, lon = qry_cell(
-                conf["opencellid"]["dbfn"], msg.mcc, msg.gsm_cells
+                resp = Resp(
+                    imei=zmsg.imei, packet=msg.Out(lat=lat, lon=lon).packed
+                )
+                log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
+                zpush.send(resp.packed)
+            else:
+                is_gps = True
+                lat = msg.latitude
+                lon = msg.longitude
+            zpub.send(
+                LocEvt(
+                    imei=zmsg.imei,
+                    devtime=msg.devtime,
+                    is_gps=is_gps,
+                    lat=lat,
+                    lon=lon,
+                ).packed
             )
-            resp = Resp(imei=zmsg.imei, packet=msg.Out(lat=lat, lon=lon).packed)
-            log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
-            zpush.send(resp.packed)
 
     except KeyboardInterrupt:
         pass
diff --git a/gps303/watch.py b/gps303/watch.py
new file mode 100644 (file)
index 0000000..8ae9ab9
--- /dev/null
@@ -0,0 +1,28 @@
+""" Watch for locevt and print them """
+
+from datetime import datetime, timezone
+from logging import getLogger
+import zmq
+
+from . import common
+from .zmsg import LocEvt
+
+log = getLogger("gps303/watch")
+
+
+def runserver(conf):
+    zctx = zmq.Context()
+    zsub = zctx.socket(zmq.SUB)
+    zsub.connect(conf.get("lookaside", "publishurl"))
+    zsub.setsockopt(zmq.SUBSCRIBE, b"")
+
+    try:
+        while True:
+            zmsg = LocEvt(zsub.recv())
+            print(zmsg)
+    except KeyboardInterrupt:
+        pass
+
+
+if __name__.endswith("__main__"):
+    runserver(common.init(log))
index 56392007f1ff74ecd61cf59b8a95b1b9c2c06c48..80cf0003cbb97eee3baf55684a9085cb010d2c3f 100644 (file)
@@ -1,9 +1,10 @@
 """ Zeromq messages """
 
+from datetime import datetime, timezone
 import ipaddress as ip
 from struct import pack, unpack
 
-__all__ = "Bcast", "Resp"
+__all__ = "Bcast", "LocEvt", "Resp"
 
 
 def pack_peer(peeraddr):
@@ -119,3 +120,31 @@ class Resp(_Zmsg):
     def decode(self, buffer):
         self.imei = buffer[:16].decode()
         self.packet = buffer[16:]
+
+
+class LocEvt(_Zmsg):
+    """Zmq message with original or approximated location from lookaside"""
+
+    KWARGS = (
+        ("imei", "0000000000000000"),
+        ("devtime", datetime(1970, 1, 1, tzinfo=timezone.utc)),
+        ("lat", 0.0),
+        ("lon", 0.0),
+        ("is_gps", True),
+    )
+
+    @property
+    def packed(self):
+        return self.imei.encode() + pack(
+            "!dddB",
+            self.devtime.replace(tzinfo=timezone.utc).timestamp(),
+            self.lat,
+            self.lon,
+            int(self.is_gps),
+        )
+
+    def decode(self, buffer):
+        self.imei = buffer[:16].decode()
+        when, self.lat, self.lon, is_gps = unpack("!dddB", buffer[16:])
+        self.devtime = datetime.fromtimestamp(when).astimezone(tz=timezone.utc)
+        self.is_gps = bool(is_gps)