feat(map): derive station regions from GPS

This commit is contained in:
lingniu
2026-08-12 12:03:09 +08:00
parent f707e6ad68
commit 4c547757a7
8 changed files with 228 additions and 38 deletions
+18 -20
View File
@@ -28,6 +28,7 @@ OPEN_PLATFORM_BASE_URL = os.getenv("OPEN_PLATFORM_BASE_URL", "https://open.d.lno
OPEN_PLATFORM_APP_KEY = os.getenv("OPEN_PLATFORM_APP_KEY", "").strip()
UPSTREAM_TIMEOUT_SECONDS = float(os.getenv("UPSTREAM_TIMEOUT_SECONDS", "15"))
STATION_CACHE_SECONDS = int(os.getenv("STATION_NAVIGATION_CACHE_SECONDS", "120"))
VEHICLE_MAP_INTERNAL_BASE_URL = os.getenv("VEHICLE_MAP_INTERNAL_BASE_URL", "http://127.0.0.1:20800").rstrip("/")
_cache_lock = threading.Lock()
_cache = {}
@@ -91,27 +92,24 @@ def _cached(key, ttl_seconds, loader):
return value
def _public_station(station):
"""Positive allowlist: do not add operational or hydrogen fields here."""
fields = (
"id", "name", "shortName", "province", "city", "district", "address",
"longitude", "latitude", "cooperative",
)
return {field: station.get(field) for field in fields if station.get(field) is not None}
def _load_station_directory():
stations = _post_open_platform("/api/v1/hydrogen-stations/query", {})
directory = [_public_station(station) for station in stations]
return {
"status": "ok",
"asOf": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
"summary": {
"totalStations": len(directory),
"cooperativeStations": sum(1 for station in directory if station.get("cooperative")),
},
"stations": directory,
}
"""Use the operations map's GPS-resolved public station directory.
The map service owns reverse-geocoding and its persistent cache, keeping
the public navigation view and the operations view on identical boundaries.
"""
request = Request(
VEHICLE_MAP_INTERNAL_BASE_URL + "/api/stations",
headers={"Accept": "application/json", "User-Agent": "lingniu-station-navigation/1.0"},
)
try:
with urlopen(request, timeout=UPSTREAM_TIMEOUT_SECONDS) as response:
payload = json.load(response)
except (HTTPError, URLError, ValueError) as exc:
raise RuntimeError("vehicle map station directory unavailable: {}".format(exc)) from exc
if payload.get("status") != "ok" or not isinstance(payload.get("stations"), list):
raise RuntimeError("vehicle map returned an invalid station directory")
return payload
def _prewarm_station_directory_cache():