From 9c7b69e3952b7d06b5318c71dec67853b8f3b702 Mon Sep 17 00:00:00 2001 From: kianiadee Date: Fri, 29 May 2026 00:13:55 +0300 Subject: [PATCH] P1-b: don't let LBS/WIFI fixes overwrite a fresh GPS fix in live view Co-Authored-By: Claude Opus 4.8 --- app/projectors/live_positions.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/projectors/live_positions.py b/app/projectors/live_positions.py index ebcce82..6f7ee4a 100644 --- a/app/projectors/live_positions.py +++ b/app/projectors/live_positions.py @@ -24,6 +24,14 @@ log = structlog.get_logger("projector.live_positions") DRAIN_BATCH = 500 PROJECTED_FLAG_KEY = "live_positions_projected_at" +# Low-accuracy positioning modes (cell-tower / wifi) reported in Tracksolid's +# `posType`. These can be kilometres off a real GPS fix, so we don't let one +# overwrite a *fresh* GPS fix in the live view (it would teleport the marker). +# A GPS fix older than this window does yield to LBS/WIFI, so a genuinely +# GPS-dark vehicle still updates rather than freezing forever. +LOW_ACCURACY_POS_TYPES = ("LBS", "WIFI") +GPS_PREFERENCE_WINDOW_SQL = "interval '10 minutes'" + _PLATE_FROM_DEVICE_NAME = re.compile(r"^.* - (.+?)(?:_cam|_CAM)?$") _CAMERA_NAME_RE = re.compile(r"_cam$", re.IGNORECASE) @@ -190,6 +198,13 @@ async def _project_one( device_name = EXCLUDED.device_name, pos_type = EXCLUDED.pos_type WHERE EXCLUDED.occurred_at > state.live_positions.occurred_at + AND NOT COALESCE( + EXCLUDED.pos_type = ANY (%s) + AND state.live_positions.pos_type = 'GPS' + AND state.live_positions.occurred_at + > now() - """ + GPS_PREFERENCE_WINDOW_SQL + """, + false + ) """, ( imei, vehicle_id, occurred_at, geom_wkt, @@ -204,6 +219,7 @@ async def _project_one( payload.get("satellites"), payload.get("device_name"), payload.get("pos_type"), + list(LOW_ACCURACY_POS_TYPES), ), )