Spruce it up

This commit is contained in:
firestarsdog
2026-08-31 03:45:32 -04:00
parent 764417845f
commit 5d32cccaf1
8 changed files with 406 additions and 123 deletions
+21 -64
View File
@@ -3,7 +3,6 @@ from __future__ import annotations
import shutil
import threading
from dataclasses import dataclass, replace
from pathlib import Path
from cereal import log, messaging
import pyray as rl
@@ -44,12 +43,13 @@ from openpilot.starpilot.common.maps_catalog import (
sanitize_selected_locations_csv,
schedule_label,
)
from openpilot.starpilot.common.maps_selection import COUNTRY_PREFIX, STATE_PREFIX
from openpilot.starpilot.common.maps_download_progress import MAPS_STORAGE_CACHE_PARAM, load_maps_storage_cache
from openpilot.starpilot.common.maps_selection import COUNTRY_PREFIX
from openpilot.starpilot.common.starpilot_variables import MAPS_PATH as OFFLINE_MAPS_PATH
NetworkType = log.DeviceState.NetworkType
OFFLINE_MAPS_PATH = Path("/data/media/0/osm/offline")
CANCEL_REQUEST_TIMEOUT = 3.0
PANEL_STYLE = DEFAULT_PANEL_STYLE
MAPS_METRICS = replace(AETHER_LIST_METRICS, header_height=0)
@@ -435,13 +435,10 @@ class StarPilotMapsLayout(_SettingsPage):
self._worker_params = Params()
self._map_sm = messaging.SubMaster(["mapdExtendedOut", "starpilotCarState"])
self._storage_text = "0 MB"
self._storage_text = tr("Calculating...")
self._storage_known = False
self._has_downloaded_data = False
self._storage_updated_at = 0.0
self._storage_refresh_thread: threading.Thread | None = None
self._storage_refresh_pending = False
self._storage_refresh_generation = 0
self._pending_storage_state: tuple[int, str, bool] | None = None
self._download_started_at: float | None = None
self._cancel_requested_at: float | None = None
self._cancel_visual_until = 0.0
@@ -467,24 +464,19 @@ class StarPilotMapsLayout(_SettingsPage):
self._manager_view = MapsManagerView(self)
self._refresh_storage_cache(force=True)
self._refresh_storage_state(force=True)
self._sync_download_state(force=True)
def show_event(self):
super().show_event()
# On panel launch, if no offline map files exist on disk and no download is actively running,
# reset any stale or default MapsSelected parameter so 0 regions show as selected on clean boot!
if not self._has_downloaded_data and not self._download_in_flight():
raw_selected = self._params.get("MapsSelected", encoding="utf-8") or ""
if raw_selected:
self._params.put("MapsSelected", "")
self._cached_selected_tokens = set()
self._refresh_storage_state(force=True)
raw_selected = self._params.get("MapsSelected", encoding="utf-8") or ""
self._cached_selected_tokens = _selected_token_set(raw_selected)
if self._cancel_requested() and self._cancel_requested_at is None:
self._cancel_requested_at = rl.get_time()
if self._cancel_requested() and self._cancel_visual_until <= rl.get_time():
self._cancel_visual_until = rl.get_time() + 2.5
self._refresh_storage_cache(force=True)
self._sync_download_state(force=True)
def hide_event(self):
@@ -497,15 +489,7 @@ class StarPilotMapsLayout(_SettingsPage):
self._params.get("MapsSelected", encoding="utf-8") or ""
)
self._sync_download_state()
if self._pending_storage_state is not None:
generation, storage_text, has_downloaded_data = self._pending_storage_state
self._pending_storage_state = None
if generation == self._storage_refresh_generation:
self._storage_text = storage_text
self._has_downloaded_data = has_downloaded_data
self._storage_updated_at = rl.get_time()
self._storage_refresh_pending = False
self._refresh_storage_cache()
self._refresh_storage_state()
if self._download_state.active:
device.set_override_interactive_timeout(300)
@@ -559,46 +543,17 @@ class StarPilotMapsLayout(_SettingsPage):
progress_text=progress_text,
)
def _refresh_storage_cache(self, force: bool = False):
def _refresh_storage_state(self, force: bool = False):
now = rl.get_time()
if self._storage_refresh_pending:
return
if not force and (now - self._storage_updated_at) < 4.0:
return
generation = self._storage_refresh_generation + 1
self._storage_refresh_generation = generation
def refresh_worker():
result: tuple[str, bool] | None = None
try:
result = self._calculate_storage_state()
finally:
if result is None:
self._storage_refresh_pending = False
else:
self._pending_storage_state = (generation, result[0], result[1])
self._storage_refresh_pending = True
cache = load_maps_storage_cache(self._worker_params.get(MAPS_STORAGE_CACHE_PARAM, encoding="utf-8") or "")
total_size = cache.storage_bytes
self._storage_known = cache.storage_known
self._has_downloaded_data = bool(cache.maps_present)
self._storage_text = _format_mb(total_size) if total_size is not None else tr("Calculating...")
self._storage_updated_at = now
self._storage_refresh_thread = threading.Thread(target=refresh_worker, daemon=True)
self._storage_refresh_thread.start()
def _calculate_storage_state(self) -> tuple[str, bool]:
if not OFFLINE_MAPS_PATH.exists():
return "0 MB", False
total_size = 0
has_files = False
for path in OFFLINE_MAPS_PATH.rglob("*"):
try:
if not path.is_file():
continue
has_files = True
total_size += path.stat().st_size
except OSError:
continue
return _format_mb(total_size), has_files
def _selected_tokens(self) -> set[str]:
return self._cached_selected_tokens
@@ -909,9 +864,9 @@ class StarPilotMapsLayout(_SettingsPage):
if OFFLINE_MAPS_PATH.exists():
shutil.rmtree(OFFLINE_MAPS_PATH, ignore_errors=True)
OFFLINE_MAPS_PATH.mkdir(parents=True, exist_ok=True)
self._storage_refresh_generation += 1
self._pending_storage_state = (self._storage_refresh_generation, "0 MB", False)
self._storage_refresh_pending = False
cache = load_maps_storage_cache(self._worker_params.get(MAPS_STORAGE_CACHE_PARAM, encoding="utf-8") or "")
cache.clear()
self._worker_params.put(MAPS_STORAGE_CACHE_PARAM, cache.to_json())
self._storage_updated_at = 0.0
threading.Thread(target=remove_worker, daemon=True).start()
@@ -932,6 +887,8 @@ class StarPilotMapsLayout(_SettingsPage):
return tr("Starting Download")
if self._has_downloaded_data:
return tr("Offline Maps")
if not self._storage_known:
return tr("Checking Offline Maps")
if self._selected_count() == 0:
return tr("Select Map Data")
return tr("Download Readiness")