self.latitude = p.lat * p.nors
self.longitude = p.lon * p.eorw
- def rectified(self) -> Dict[str, Any]: # JSON-able dict
+ def rectified(self) -> SimpleNamespace: # 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,
- }
+ return SimpleNamespace(
+ 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,
- }
+ return SimpleNamespace(
+ 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):
if pmod.proto_handled(proto):
return pmod
return None
+
+
+def parse_message(proto: str, packet: bytes, is_incoming: bool = True) -> Any:
+ pmod = pmod_for_proto(proto)
+ return pmod.parse_message(packet, is_incoming) if pmod else None
+
+
+def exposed_protos() -> List[Tuple[str, bool]]:
+ return [item for pmod in pmods for item in pmod.exposed_protos()]
def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes:
...
+ class DecodeError(Exception):
+ ...
+
@staticmethod
def exposed_protos() -> List[Tuple[str, bool]]:
...
import zmq
from . import common
-from .zx303proto import parse_message, proto_name, WIFI_POSITIONING
-from .zmsg import Bcast, Resp, topic
+from .zmsg import Bcast, Report, Resp, topic
log = getLogger("loctrkd/rectifier")
def runserver(conf: ConfigParser) -> None:
qry = import_module("." + conf.get("rectifier", "lookaside"), __package__)
qry.init(conf)
+ proto_needanswer = dict(common.exposed_protos())
# Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
zctx = zmq.Context() # type: ignore
zsub = zctx.socket(zmq.SUB) # type: ignore
zsub.connect(conf.get("collector", "publishurl"))
- zsub.setsockopt(zmq.SUBSCRIBE, topic(proto_name(WIFI_POSITIONING)))
+ for proto in proto_needanswer.keys():
+ zsub.setsockopt(zmq.SUBSCRIBE, topic(proto))
zpush = zctx.socket(zmq.PUSH) # type: ignore
zpush.connect(conf.get("collector", "listenurl"))
zpub = zctx.socket(zmq.PUB) # type: ignore
try:
while True:
zmsg = Bcast(zsub.recv())
- msg = parse_message(zmsg.packet)
+ msg = common.parse_message(
+ zmsg.proto, zmsg.packet, is_incoming=zmsg.is_incoming
+ )
log.debug(
"IMEI %s from %s at %s: %s",
zmsg.imei,
datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
msg,
)
- try:
- lat, lon = qry.lookup(
- msg.mcc, msg.mnc, msg.gsm_cells, msg.wifi_aps
- )
- resp = Resp(
- imei=zmsg.imei,
- when=zmsg.when, # not the current time, but the original!
- packet=msg.Out(latitude=lat, longitude=lon).packed,
- )
- log.debug("Response for lat=%s, lon=%s: %s", lat, lon, resp)
- zpush.send(resp.packed)
- except Exception as e:
- log.warning("Lookup for %s resulted in %s", msg, e)
+ rect = msg.rectified()
+ log.debug("rectified: %s", rect)
+ if rect.type == "approximate_location":
+ try:
+ lat, lon = qry.lookup(
+ rect.mcc, rect.mnc, rect.base_stations, rect.wifi_aps
+ )
+ resp = Resp(
+ imei=zmsg.imei,
+ when=zmsg.when, # not the current time, but the original!
+ packet=msg.Out(latitude=lat, longitude=lon).packed,
+ )
+ log.debug(
+ "Response for lat=%s, lon=%s: %s", lat, lon, resp
+ )
+ zpush.send(resp.packed)
+ except Exception as e:
+ log.warning("Lookup for %s resulted in %s", msg, e)
except KeyboardInterrupt:
zsub.close()
self.when = when
self.packet = buffer[24:]
+
+
+class Report(_Zmsg):
+ """Broadcast Zzmq message with "rectified" proto-agnostic json data"""
+
+ KWARGS = (("imei", None), ("payload", ""))
+
+ @property
+ def packed(self) -> bytes:
+ return (
+ pack(
+ "16s",
+ "0000000000000000"
+ if self.imei is None
+ else self.imei.encode(),
+ )
+ + self.payload.encode()
+ )
+
+ def decode(self, buffer: bytes) -> None:
+ imei = buffer[:16]
+ self.imei = (
+ None if imei == b"0000000000000000" else imei.decode().strip("\0")
+ )
+ self.payload = buffer[16:].decode()
from inspect import isclass
from struct import error, pack, unpack
from time import time
+from types import SimpleNamespace
from typing import (
Any,
Callable,
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) -> SimpleNamespace: # JSON-able dict
+ return SimpleNamespace(
+ type="location",
+ devtime=str(self.devtime),
+ speed=self.speed,
+ direction=self.heading,
+ latitude=self.latitude,
+ longitude=self.longitude,
+ )
class GPS_POSITIONING(_GPS_POSITIONING):
]
)
- 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) -> SimpleNamespace: # JSON-able dict
+ return SimpleNamespace(
+ 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):